问题
I must be asking a very basic question. I've just learnt how to apply textures.
Basically, I have a scene (a plane) and a cube on it. I apply a texture to one of the faces of the cube. The face of the cube I am trying to apply the texture to is red, but I want the texture color to override it, but they somehow blend together, although I have not enabled blending, nor is the texture image transparent!
Here's my texture(.png).
And here's the rendering:
Here are some relevant parts of my code ( I know I am doing many things wrong, like using triangle strips instead of quads, not using HLSL etc - I'm a newbie :)
Initing part:
//glInit
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND); //<-- blending is disabled!
Texture part:
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
Drawing part:
glBegin(GL_TRIANGLE_STRIP);
glColor3f(1, 0, 0); //red front face
glTexCoord2d(0, 0);
glVertex3d(-a, -a, -a);
glTexCoord2d(0, 1);
glVertex3d(-a, -a, a);
glTexCoord2d(1, 0);
glVertex3d(a, -a, -a);
glTexCoord2d(1, 1);
glVertex3d(a, -a, a);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0, 1, 0); //green back face
glVertex3d(-a, a, -a);
glVertex3d(-a, a, a);
glVertex3d(a, a, -a);
glVertex3d(a, a, a);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glColor3f(0, 0, 1); //blue right face
glVertex3d(a, -a, -a);
glVertex3d(a, -a, a);
glVertex3d(a, a, -a);
glVertex3d(a, a, a);
glEnd();
//etc.
So, can you please help me to make the color of my texture preserve and not become reddish? If any other part of my code is needed to understand what I am doing wrong, just say, I'll edit. Thank you very much in advance.
回答1:
If you intend to draw one side of the cube with a texture, and the other sides without it, then you need to glEnable(GL_TEXTURE_2D)
for the side with the texture and glDisable(GL_TEXTURE_2D)
for the sides without it. That's not your problem, but it still needs to be done.
If you're trying to render with just a texture, you could pass (1, 1, 1) for the color. However, that just paints over the issue.
If you're really going to stick with fixed function OpenGL, then you need to use the texture environment. Each texture unit (glActiveTexture(GL_TEXTURE0 + texture_unit)
) has a "texture environment", which is essentially a mathematical operation that combines a particular value fetched from the texture with the per-vertex interpolated color or the previous texture environment command. They are executed in texture unit order. The default is GL_MODULATE
, which multiplies the color with the texture. You want GL_REPLACE
.
回答2:
I have just taken a quick look at your .png and I can't find an alpha channel in there. If there isnt one and your are accepting GL_RGBA
and assuming 4 bytes per pixel. This could be causing your apparent colour blending.
回答3:
you can use white color for the facet that you want to apply texture map to, in that case, the color won't mess up.
来源:https://stackoverflow.com/questions/7134353/how-do-color-and-textures-work-together