Changing opacity of a texture on the fly in opengl? - android

I am new to opengl and have no idea how to deal with opacity. I have two layers that are overlapped i am drawing both the layers on to the screen. I want to fade out the one in the foreground to transtition to the background image. Is there any way to do this?? Here's my draw method.
public void draw(GL10 gl10) {
gl10.glDisable(GL10.GL_BLEND);
gl10.glEnable(GL10.GL_BLEND);
gl10.glBlendFunc(GL10.GL_TRUE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl10.glClearColor(0F, 0F, 0F, 0);
gl10.glBindTexture(GL10.GL_TEXTURE_2D, this.mTextureId);
this.layer1.draw(gl10); // i want to transition from this layer
this.layer2.draw(gl10); // to this layer by changing opacity
}
I dont know even i framed my question correctly. Hope you get it :)

Well it depends on the method you're using for this transition. I have a similar situation with an animation view. About your opacity problem, can't you use
View v;
int i = 0; /*Values from 0 to 1, float cast might be needed for intermediate values*/
v.setAlpha(i);
this.layer2.setAlpha(i);
Or something like that, isn't this applicable in your case?

Related

Android Studio Libgdx Sprite/Texture Transparency

I need to change the transparency of a texture/sprite and I really have no idea how to do it. Any help?
I see two solutions here - one is to change transparency directly in sprite:
Sprite sprite = new Sprite( yourTexture );
Sprite otherSprite = new Sprite( yourOtherTexture );
sprite.setAlpha(0.1f); // 0f is full transparent when 1f is full visible
...
batch.begin();
sprite.draw( batch ); //this one will be almost transparent
otherSprite.draw( batch ); //this one will be normal
batch.end();
you can always manipulate with Sprite color directly:
sprite.setColor( sprite.getColor.r, sprite.getColor.g, sprite.getColor.b, 0.1f);
but it seems to be a silly way for me.
The second one which I recommend is to wrap your Sprite into Scene2d Actor class like for example Image. Then you can still change aplha directly but you are also able to use Scene2d Actions mechanism, manipulating actor's alpha smoothly (like alpha changing animation step by step).
Both Scene2d and Actions are to wide to describe it here but I encourage you to read about it here:
Scene2d
Scene2d Actions
I added this to my GameRenderer.java:
private void drawTapTapChuggy() {
batcher.setColor(1, 1, 1, 0.5f);
batcher.draw(AssetLoader.tapTapChuggy, 28, 89, (83 * 0.9f), (52 * 0.9f));
batcher.setColor(1, 1, 1, 1.0f);
}

Android OpenGL ES Overlaying backgrounds

I am currently trying to have a particle fountain spurt out random particles overlaying ontop of a volcano background (textured quad).
I have the volcano backgound and the particles draw statement inside the onDrawFrame
public void onDrawFrame(GL10 gl)
{
// Set the clear colour to red and clear the screen
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
// Enable the vertex array client state
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
background.draw(gl);
// Draw then update the position of all particles
for (VolcanoParticle p : particleArray)
{
p.draw(gl);
p.update();
}
//background.draw(gl);
// Disable the vertex array client state before leaving
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
}
The "background.draw(gl);" is meant do use the class's in VolcanoBackground.java while the p.draw(gl); is meant to use the VolcanoParticle.java. But for some reason the VolcanoParticle.draw is also affecting the background.draw causing the background to also have the same movements and colorchanges / gravity / movement as the particles.
any ideas on how to fix?
Also with the background.draw it makes the particles very hard to see as if there is a black shroud over the top of them, is there a way to remove this alpha layer or whatever it is or force the background to be behind the particles?
Perhaps you need to reset your model-view matrices between drawing each object by calling glLoadIdentity() on them.
Regarding the dimming problem, try disabling lighting. If that is the problem, then you probably need to move the light closer to the model.

drawing a rectangle with min3d in android

I am using the 3d framework min3d and am trying to draw a rectangle that covers the whole screen. What parameters should I pass to the constructor of the Rectangle class?? It is declared this way:
public Rectangle(float $width, float $height, int $segsW, int $segsH, Color4 color)
I don't see what are segsH and segsW for. I can't see any doc about the constructor.
I got it, it didn't have anything to do with those parameters. By default the rectangle is drawn completely perpendicular to the screen so you can't see it. If you want to see it with the same dimensions you drew it you have to rotate it in y axis. Example:
rect = new Rectangle(1, 1, 2, 2,new Color(255,0,0,255));
rect.rotation().y = 180;
rect.lightingEnabled(false);
scene.addChild(rect);
In case this is usefull for someone else =)
It appears segsW and segsH control how many triangles are used to draw the rectangle.

Android Progressbar: Indeterminate animation without images

I'd like to create an indeterminate animation that simply fades from one color to another (a pulse, if you will). I don't think this should require the use of images but despite my best efforts, I'm not sure I understand how to use something like AlphaAnimation with a Shape to accomplish this.
Could someone please provide some insight as to how to accomplish this? I have a feeling I'm missing something pretty straightforward here. (Examples are always appreciated!)
Thanks!
This is a trivial task in 3.0 - you can set up an ObjectAnimator to change the "color" or "backgroundColor" of an object (View, ColorDrawable, whatever has the property) between two values. See the ApiDemo animations/BouncingBalls for an example of this.
But assuming you're using pre-3.0 APIs, there are a couple of approaches. First, you could set up your own handler to give you the timing events you need, then calculate the new color at each point.
It's probably slightly easier (if not entirely intuitive) to use an AlphaAnimation. All you really want from the animation is percentage values, not to fade anything. So you don't set the animation on a view, but just set it up to run internally from a value of 0 to 1, then get the current animated value in your onDraw() method and set the current color appropriately.
For example, this will set up and start the alpha animation to run for one second:
Transformation transform = new Transformation();
AlphaAnimation anim = new AlphaAnimation(0f, 1f);
anim.setDuration(1000);
anim.start();
Then in your drawing loop, you grab the current animated value:
long time = getDrawingTime();
anim.getTransformation(time, transform);
float elapsedFraction = transform.getAlpha();
Once you have the elapsedFraction (a value between 0 and 1), you can calculate the appropriate in-between color value.
The code above may not match your situation exactly, but you should be able to do something similar to get what you want.

Animating and rotating an image in a Surface View

I would like to animate movement on a SurfaceView . Ideally I would like to also be notified once the animation had finished.
For example:
I might have a car facing North. If I wanted to animate it so that it faces South for a duration of 500ms, how could I do that?
I am using a SurfaceView so all animation must be handled manually, I don't think I can use XML or the android Animator classes.
Also, I would like to know the best way to animate something continuously inside a SurfaceView (ie. a walk cycle)
Rotating images manually can be a bit of a pain, but here's how I've done it.
private void animateRotation(int degrees, float durationOfAnimation){
long startTime = SystemClock.elapsedRealtime();
long currentTime;
float elapsedRatio = 0;
Bitmap bufferBitmap = carBitmap;
Matrix matrix = new Matrix();
while (elapsedRatio < 1){
matrix.setRotate(elapsedRatio * degrees);
carBitmap = Bitmap.createBitmap(bufferBitmap, 0, 0, width, height, matrix, true);
//draw your canvas here using whatever method you've defined
currentTime = SystemClock.elapsedRealtime();
elapsedRatio = (currentTime - startTime) / durationOfAnimation;
}
// As elapsed ratio will never exactly equal 1, you have to manually draw the last frame
matrix = new Matrix();
matrix.setRotate(degrees);
carBitmap = Bitmap.createBitmap(bufferBitmap, 0, 0, width, height, matrix, true);
// draw the canvas again here as before
// And you can now set whatever other notification or action you wanted to do at the end of your animation
}
This will rotate your carBitmap to whatever angle you specify in the time specified + the time to draw the last frame. However, there is a catch. This rotates your carBitmap without adjusting its position on screen properly. Depending on how you're drawing your bitmaps, you could end up with your carBitmap rotating while the top-left corner of the bitmap stays in place. As the car rotates, the bitmap will stretch and adjust to fit the new car size, filling the gaps around it with transparent pixels. It's hard to describe how this would look, so here's an example rotating a square:
The grey area represents the full size of the bitmap, and is filled with transparent pixels. To solve this problem, you need to use trigonometry. It's a bit complicated... if this ends up being a problem for you (I don't know how you're drawing your bitmaps to the canvas so it might not be), and you can't work out the solution, let me know and I'll post up how I did it.
(I don't know if this is the most efficient way of doing it, but it works smoothly for me as long as the bitmap is less than 300x300 or so. Perhaps if someone knows of a better way, they could tell us!)
Do you want multiple independent animated object? If so, then you should use a game loop. (One master while loop that incrementally updates all game objects.) Here's a good discussion on various loop implementations. (I'm currently using "FPS dependent on Constant Game Speed" for my Android game project.)
So then your Car will look something like this (lots of code missing):
class Car {
final Matrix transform = new Matrix();
final Bitmap image;
Car(Bitmap sprite) {
image = sprite; // Created by BitmapFactory.decodeResource in SurfaceView
}
void update() {
this.transform.preRotate(turnDegrees, width, height);
}
void display(Canvas canvas) {
canvas.drawBitmap(this.image, this.transform, null);
}
}
You only need to load your bitmap once. So if you have multiple Cars, you may want to give them each the same Bitmap object (cache the Bitmap in your SurfaceView).
I haven't got into walk animations yet, but the simplest solution is to have multiple Bitmaps and just draw a different bitmap each time display is called.
Have a look at lunarlander.LunarView in the Android docs if you haven't already.
If you want to be notified when the animation is complete, you should make a callback.
interface CompletedTurnCallback {
void turnCompleted(Car turningCar);
}
Have your logic class implement the callback and have your Car call it when the turn's complete (in update()). Note that you'll get a ConcurrentModificationException if you are iterating over a list of Cars in update_game() and you try to remove a Car from that list in your callback. (You can solve this with a command queue.)

Categories

Resources