Sign in to follow this  

Colors with textures

This topic is 4397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What i'm looking to do is draw my polygons with textures AND a color. an example would be when i went everything to be more of a red tint. i draw everything as normal, textures and everything, then i specify the color to be something like glColorf(1.0f, 0.3f, 0.3f)

Share this post


Link to post
Share on other sites
nv/md.. i found it

float Diffuse[4];
Diffuse[0] = 1.0f;
Duffuse[1] = 0.3f;
Duffuse[2] = 0.3f;
Duffuse[3] = 1.0f;
glMaterialfv(GL_FRONT, GL_DIFFUSE, Diffuse);
is that correct?

Share this post


Link to post
Share on other sites
Material calculations are only done when lighting is enabled, mind. And about the code, it's fine but the best way is to plug it in and see for yourself [grin]. You might also want to use ambient material.

/edit: I just noticed, you mentioned in your first post that you are setting the color after you draw everything? Set the color, bind the texture, and draw. You will have nice tinted textures (this is when lighting is not enabled of course, if it is use material properties).

Share this post


Link to post
Share on other sites
so should i use diffuse and ambient material? if so, should i set them to the same color value? Say i want the sprite to be partially trasparent. set the color to say 1.0, 1.0f, 1.0f, 0.5f. then set the material's diffuse and ambient to the same? what about lighting? should i just have one directional like source going perpendicular to the faces of the sprites? (this is a 2d environment)

Share this post


Link to post
Share on other sites
would this be correct? i can't test it because i'm redoing my whole engine and i'm doing one part at a time :)

[source lang=c++]
// Position sprite
glLoadIdentity();
glScalef(Pos->ScaleX, Pos->ScaleY, 1.0f);
glRotatef(Pos->Radian, 0.0f, 0.0f, 1.0f);
glTranslatef(PosX, PosY, -PosZ);

// Set the colo
glColor4f(m_fDiffuse[0], m_fDiffuse[1], m_fDiffuse[2], m_fDiffuse[3]);
glMaterialfv(GL_FRONT, GL_DIFFUSE, m_fDiffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, m_fDiffuse);

// Bind the texture if there is one
if (m_pTexture == NULL)
glBindTexture(GL_TEXTURE_2D, NULL);
else
glBindTexture(GL_TEXTURE_2D, m_pTexture->GetTextureData());

// Draw sprite
glBegin(GL_QUADS);
{
// Top-left
glTexCoord2d(m_FrameSrcRect.left * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.top * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * -0.5f, m_lFrameHeight * 0.5f, 0.0f);

// Top-right
glTexCoord2d(m_FrameSrcRect.right * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.top * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * 0.5f, m_lFrameHeight * 0.5f, 0.0f);

// Bottom-right
glTexCoord2d(m_FrameSrcRect.right * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.bottom * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * 0.5f, m_lFrameHeight * -0.5f, 0.0f);

// Bottom-left
glTexCoord2d(m_FrameSrcRect.left * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.bottom * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * -0.5f, m_lFrameHeight * -0.5f, 0.0f);
}
glEnd();


Share this post


Link to post
Share on other sites
Hmm I think it is possible, but blending needs to be turned on.

an example:



glBindTexture(GL_TEXTURE_2D, textureId );
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA ,GL_ONE);

//CHANGE COLOR
glColor4f(particle->color[0],
particle->color[1],
particle->color[2],
particle->color[3]);

glBegin(GL_QUADS);


glTexCoord2f( 1.0f , 0.0f ); glVertex3f(-size, size, 0.0f );
glTexCoord2f( 1.0f , 1.0f ); glVertex3f( size, size, 0.0f );
glTexCoord2f( 0.0f , 1.0f ); glVertex3f( size,-size, 0.0f );
glTexCoord2f( 0.0f , 0.0f ); glVertex3f(-size,-size, 0.0f );


glEnd();

glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);



Most parts are taken straight from my particle engine.

Share this post


Link to post
Share on other sites
I set the blend when OGL is initialized.. but i have a question. when i don't wanna use a texture, should i just disable textures? or bind a NULL texture?

Share this post


Link to post
Share on other sites
disable.
Binding 0 ISNT the same, it binds a proxy texture which may or may not do as you expect.

Side note and tip: Only enable blending for things you need blended, turning it off for objects which dont need to blend with the frame buffer improves your speed as it cuts down on framebuffer traffic.

Share this post


Link to post
Share on other sites
well, the correct way depends on what you are doing, if you are doing alot of texture'd objects then enable it and only disable for non-textured stuff, reverse if otherwise

Share this post


Link to post
Share on other sites
Hi, Evil! I'll comment parts of your code:

Quote:

// Set the color
glColor4f(m_fDiffuse[0], m_fDiffuse[1], m_fDiffuse[2], m_fDiffuse[3]);
glMaterialfv(GL_FRONT, GL_DIFFUSE, m_fDiffuse);
glMaterialfv(GL_FRONT, GL_AMBIENT, m_fDiffuse);

You don't need all this material stuff for now. Please, continue reading...

Quote:

// Bind the texture if there is one
if (m_pTexture == NULL)
glBindTexture(GL_TEXTURE_2D, NULL);
else
glBindTexture(GL_TEXTURE_2D, m_pTexture->GetTextureData());
Don't do that (unexpected results). Prefer:

if ( m_pTexture ) {
// Note that the method's name "GetTextureData" is a bit confusing. Prefer
// something like "GetId()" or "GetName()" (yes, "name", OpenGL ARB uses this
// all the time).
glBindTexture( GL_TEXTURE_2D, m_pTexture->GetName() );

// Draw quads
...
}



Quote:

// Draw sprite
glBegin(GL_QUADS);
{
// Top-left
glTexCoord2d(m_FrameSrcRect.left * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.top * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * -0.5f, m_lFrameHeight * 0.5f, 0.0f);

Have you noted that, if "m_pTexture" doesnt't exist, this piece of code would crash your application? You can't call any method from a NULL pointer... And you don't need a "{" after "glBegin(GL_QUADS)", neither before the "glEnd()" call.


Finally, from what I had understood, what you want is just:

if ( m_pTexture ) {
// Position sprite
glLoadIdentity();
glScalef(Pos->ScaleX, Pos->ScaleY, 1.0f);
glRotatef(Pos->Radian, 0.0f, 0.0f, 1.0f);
glTranslatef(PosX, PosY, -PosZ);

// Set the color (this is enough to get the "red tint" effect you're asking for.
// Just play around with the alpha value "color[3]" for adjustments
glColor4f( color[0], color[1], color[2], color[3]);
glBindTexture( GL_TEXTURE_2D, m_pTexture->GetName() ); // Supposing GL_TEXTURE_2D is already enabled

// Draw sprite
glBegin(GL_QUADS);

// Top-left
glTexCoord2d(m_FrameSrcRect.left * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.top * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * -0.5f, m_lFrameHeight * 0.5f, 0.0f);

// Top-right
glTexCoord2d(m_FrameSrcRect.right * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.top * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * 0.5f, m_lFrameHeight * 0.5f, 0.0f);

// Bottom-right
glTexCoord2d(m_FrameSrcRect.right * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.bottom * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * 0.5f, m_lFrameHeight * -0.5f, 0.0f);

// Bottom-left
glTexCoord2d(m_FrameSrcRect.left * 1.0f / m_pTexture->GetWidth(), m_FrameSrcRect.bottom * 1.0f / m_pTexture->GetHeight());
glVertex3f(m_lFrameWidth * -0.5f, m_lFrameHeight * -0.5f, 0.0f);

glEnd();
}




And remember, everything depends on what you are doing / want to do, so my code could be plainly wrong...

Goodbye and good luck!

Share this post


Link to post
Share on other sites
I use glTexEnv(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
After that you could just use glColor3F.

Share this post


Link to post
Share on other sites
Quote:
Original post by EvilKnuckles666
where would that be used and what does it do?


Right after or right before binding the texture. Not sure if that matters.

It combines the texture and gl color.

Share this post


Link to post
Share on other sites
Sign in to follow this