简体   繁体   English

OpenGL-纹理贴图后无法呈现白色以外的颜色

[英]OpenGL - Unable to render colors other than white after texture mapping

I'm trying to render a colored cube after rendering other cubes that have textures. 我正在尝试渲染具有纹理的其他多维数据集后渲染彩色的多维数据集。 I have multiple "Drawer" objects that conform to the Drawer interface, and I pass each a reference to the GL object to the draw( final GL gl ) method of each individual implementing class. 我有多个符合Drawer接口的“抽屉”对象,并将每个GL对象的引用传递给每个实现类的draw(final GL gl)方法。 However, no matter what I do, I seem unable to render a colored cube. 但是,无论我做什么,我似乎都无法渲染彩色立方体。

Code sample: 代码示例:

gl.glDisable(GL.GL_TEXTURE_2D);

gl.glColor3f( 1f, 0f, 0f );
gl.glBegin(GL.GL_QUADS);
// Front Face
Point3f point = player.getPosition();

gl.glNormal3f(0.0f, 0.0f, 1.0f);
//gl.glTexCoord2f(0.0f, 0.0f);

gl.glVertex3f(-point.x - 1.0f, -1.0f, -point.z + 1.0f);
//gl.glTexCoord2f(1.0f, 0.0f);

gl.glVertex3f(-point.x + 1.0f, -1.0f, -point.z + 1.0f);
//continue rendering rest of cube. ...
gl.glEnd();
gl.glEnable(GL.GL_TEXTURE_2D);

I've also tried throwing the glColor3f calls before each vertex call, but that still gives me a white cube. 我还尝试在每次顶点调用之前抛出glColor3f调用,但这仍然给了我一个白色立方体。 What's up? 这是怎么回事?

There are a few things you need to make sure you do. 您需要确保做一些事情。

First off: 首先:

gl.glEnable(gl.GL_COLOR_MATERIAL);

This will let you apply colors to your vertices. 这将使您可以将颜色应用于顶点。 (Do this before your calls to glColor3f.) (在调用glColor3f之前执行此操作。)

If this still does not resolve the problem, ensure that you are using blending properly (if you're using blending at all.) 如果仍然不能解决问题,请确保正确使用混合(如果完全使用混合)。

For most applications, you'll probably want to use 对于大多数应用程序,您可能需要使用

gl.glEnable(gl.GL_BLEND);
gl.glBlendFunc(gl.GL_SRC_ALPHA,gl.GL_ONE_MINUS_SRC_ALPHA);

If neither of these things solve your problem, you might have to give us some more information about what you're doing/setting up prior to this section of your code. 如果以上两种方法都不能解决您的问题,那么在代码本节之前,您可能必须向我们提供有关您正在做/设置的更多信息。

If lighting is enabled, color comes from the material, not the glColor vertex colors. 如果启用了照明,则颜色来自材质,而不是glColor顶点颜色。 If your draw function that you mentioned is setting a material for the textured objects (and a white material under the texture would be common) then the rest of the cubes would be white. 如果您提到的绘制功能为纹理对象设置了材质(并且该材质下面的白色材质很常见),那么其余的立方体将是白色的。 Using GL_COLOR_MATERIAL sets up OpenGL to take the glColor commands and update the material instead of just the vertex color, so that should work. 使用GL_COLOR_MATERIAL可以将OpenGL设置为使用glColor命令并更新材质,而不只是更新顶点颜色,这样就可以了。

So, simply put, if you have lighting enabled, try GL_COLOR_MATERIAL. 因此,简单地说,如果启用了照明,请尝试GL_COLOR_MATERIAL。

One thing you might want to try is: glBindTexture(GL_TEXTURE_2D, 0); 您可能想尝试的一件事是: glBindTexture(GL_TEXTURE_2D, 0); to bind the texture to nil. 将纹理绑定为nil。

Some things to check: 要检查的一些事情:

  • Is there a shader active? 是否有着色器处于活动状态?
  • Any gl-errors? 有毛病吗?
  • What other states did you change? 您还更改了哪些其他状态? For example GL_COLOR_MATERIAL, blending or lighting will change the appearance of your geometry. 例如GL_COLOR_MATERIAL,混合或照明将更改几何的外观。
  • Does it work if you draw the non-textured cube first? 如果先绘制无纹理的立方体,是否可行? And if it does try to figure out at which point it turns white. 如果确实尝试找出在哪一点变成白色。 It's also possible that the cube will only show up in the correct color in the first frame, then there's definitely a GL state involved. 多维数据集也可能仅在第一帧中以正确的颜色显示,那么肯定涉及到GL状态。
  • Placing glPushAttrib/glPopAttrib at the beginning/end of your drawing methods might help, but it's better to figure out what caused the problem in the first place. 将glPushAttrib / glPopAttrib放在绘图方法的开头/结尾可能会有所帮助,但是最好首先弄清楚是什么引起了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM