Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Alfons

macrumors newbie
Original poster
Mar 15, 2008
10
0
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.


Code:
- (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;

Code:
	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
 

zzajin

macrumors newbie
Jul 10, 2008
2
0
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.

Code:
        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

macrumors newbie
Original poster
Mar 15, 2008
10
0
Thanks it works like a charm. I will read up on these functions and some OpenGL in general.

Cheers
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.