PDA

View Full Version : Extending Texture2d with opacity?




Alfons
Jul 31, 2008, 09:13 AM
Hi all!

I am trying to extend the opengl Texture2d so it becomes more useful. What I want to achieve is to pass in custom Position, Rotation, Scale and Color in the drawAtPoint function. The position, rotation and scale part was easy, but I can't make the color multiply or blend with the texture. This is how the code looks like now.


- (void) drawAtPoint:(CGPoint)position rotation:(float)rotation scale:(CGPoint)scale
{
CGPoint point = CGPointZero;
GLfloat coordinates[] = { 0, _maxT,
_maxS, _maxT,
0, 0,
_maxS, 0 };
GLfloat width = (GLfloat)_width * _maxS,
height = (GLfloat)_height * _maxT;
GLfloat vertices[] = { -width / 2 + point.x, -height / 2 + point.y, 0.0,
width / 2 + point.x, -height / 2 + point.y, 0.0,
-width / 2 + point.x, height / 2 + point.y, 0.0,
width / 2 + point.x, height / 2 + point.y, 0.0 };


glPushMatrix();
glTranslatef(position.x, position.y, 0);
glRotatef(-rotation, 0, 0, 1);
glScalef(scale.x, scale.y, 1.0f);

glBindTexture(GL_TEXTURE_2D, _name);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glPopMatrix();
}


After doing some research I found that I should enable the GL_BLEND and then use a function glColor4f. Like this, before I draw the texure;


glEnable(GL_BLEND);
//Blend the texture with 25% alpha
glColor4f(1.0f, 1.0f, 1.0f, 0.25f);


But it doesn't work. I initialize my code with the same functions as the CrashLanding sample.

I am glad for any help I can get.
Cheers
Alfons



Alfons
Jul 31, 2008, 02:35 PM
bump!:D

zzajin
Jul 31, 2008, 07:45 PM
You need to define the texture combine state.
glTexEnvf and glTexEnvi are the functions you need learn.

Here is an example. This will color the texture according to the light color.


glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_COMBINE );
glTexEnvf(GL_TEXTURE_ENV,GL_COMBINE_RGB,GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV,GL_SRC0_RGB,GL_PRIMARY_COLOR);
glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND0_RGB,GL_SRC_COLOR);
glTexEnvf(GL_TEXTURE_ENV,GL_SRC1_RGB,GL_TEXTURE);
glTexEnvf(GL_TEXTURE_ENV,GL_OPERAND1_RGB,GL_SRC_COLOR);

Alfons
Aug 1, 2008, 12:04 PM
Thanks it works like a charm. I will read up on these functions and some OpenGL in general.

Cheers