问题
I am completely new to opengl and need some help in drawing a texture with gradient applied over it.
Now I am drawing a texture with the following code:
glPushMatrix();
glBindTexture(GL_TEXTURE_2D,fcr.texture);
glTranslatef(trans, 0, 0);
glScalef(sc,sc,1.0);
glColor4f(1.0,1.0,1.0,0.4);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
glColor4f(1,1,1,1);
In the above code glColor4f(1.0,1.0,1.0,0.4);
applies transparency to the drawn image. Now instead of a solid transparent color, I would like to apply a transparent gradient to the image so as to simulate the fading out effect.
Found this thread iPhone OpenGL - How to gradient fade out a texture?, which is the same as my question except I want the gradient to be applied to the main image itself instead of reflection. But I couldn't figure out the solution from answers given.
I guess I should use glTexEnvi
, but then how to draw another image with the gradient?
回答1:
Instead of calling glColor4f before drawing the quad, can you set a glColorPointer (or glVertexAttribPointer), such that each of the four vertices of your quad get a different alpha value?
i.e. RGBA values:
(x,x,x,1) (x,x,x,0)
-----------------
| |
| |
| |
| |
| |
| |
-----------------
(x,x,x,1) (x,x,x,0)
This would give you an alpha gradient from right to left.
来源:https://stackoverflow.com/questions/10029479/opengl-es-drawing-a-texture-with-fading-gradient