颜色和纹理如何协同工作? [英] How do color and textures work together?
问题描述
我必须问一个非常基本的问题.我刚刚学习了如何应用纹理.
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!
这是我的纹理(.png).
Here's my texture(.png).
这是渲染图:
这是我代码中的一些相关部分(我知道我做错了很多事情,例如使用三角带代替四边形,不使用HLSL等-我是新手:)
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 :)
启动部分:
//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.
那么,能否请您帮助我保持纹理的颜色而不发红?如果需要代码的任何其他部分来了解我在做什么错,请说,我将进行编辑.提前非常感谢您.
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.
推荐答案
如果打算用纹理绘制立方体的一侧,而没有纹理的另一侧,则需要使用glEnable(GL_TEXTURE_2D)
绘制具有纹理的那一侧.纹理和glDisable(GL_TEXTURE_2D)
表示没有纹理的侧面.那不是您的问题,但是仍然需要完成.
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.
如果您尝试仅使用纹理进行渲染,则可以传递(1、1、1、1)颜色.但是,这只能解决问题.
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.
如果您真的要坚持使用固定功能的OpenGL,则需要使用纹理环境.每个纹理单元(glActiveTexture(GL_TEXTURE0 + texture_unit)
)都有一个纹理环境",它本质上是一种数学运算,它将从纹理中获取的特定值与每个顶点插值的颜色或先前的纹理环境命令结合在一起.它们以纹理单元顺序执行.默认值为GL_MODULATE
,它将颜色与纹理相乘.您要GL_REPLACE
.
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
.
这篇关于颜色和纹理如何协同工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!