颜色和纹理如何协同工作?
我一定是在问一个非常基本的问题。我刚刚学会了如何应用纹理。
基本上,我有一个场景(一个平面)和一个立方体。我将纹理应用到立方体的一个面。我尝试应用纹理的立方体的表面是红色的,但我希望纹理颜色覆盖它,但它们以某种方式混合在一起,尽管我没有启用混合,纹理图像也不是透明!
这是我的纹理(.png)。
这是渲染:
这是我的代码的一些相关部分(我知道我做错了很多事情,例如使用三角形带而不是四边形,不使用 HLSL 等 - 我是新手:)
初始化部分:
//glInit
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND); //<-- blending is disabled!
纹理部分:
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);
绘图部分:
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.
那么,你能帮我让纹理的颜色保持不变而不是变红吗?如果需要我的代码的任何其他部分来了解我做错了什么,只需说,我会编辑。预先非常感谢您。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您打算绘制立方体的一侧有纹理,而另一侧没有纹理,那么您需要为有纹理的一侧启用
glEnable(GL_TEXTURE_2D)
,并为有纹理的一侧启用glDisable(GL_TEXTURE_2D)
对于没有它的侧面。这不是你的问题,但仍然需要完成。如果您尝试仅使用纹理进行渲染,则可以传递 (1, 1, 1) 作为颜色。然而,这只是掩盖了问题。
如果您真的要坚持使用固定功能 OpenGL,那么您需要使用
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 andglDisable(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 isGL_MODULATE
, which multiplies the color with the texture. You wantGL_REPLACE
.我刚刚快速查看了您的 .png,但在其中找不到 Alpha 通道。如果没有,并且您接受 GL_RGBA 并假设每个像素 4 个字节。这可能会导致您出现明显的颜色混合。
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.您可以对要应用纹理贴图的面使用白色,在这种情况下,颜色不会混乱。
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.