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. However, no matter what I do, I seem unable to render a colored cube.
我正在尝试渲染其他具有纹理的立方体后渲染彩色立方体。我有多个符合Drawer接口的“Drawer”对象,我将每个对GL对象的引用传递给每个单独实现类的draw(final GL gl)方法。但是,无论我做什么,我似乎都无法呈现彩色立方体。
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. What's up?
我也试过在每个顶点调用之前抛出glColor3f调用,但这仍然给了我一个白色的立方体。这是怎么回事?
4 个解决方案
#1
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.
如果这些都不能解决您的问题,您可能需要在代码的这一部分之前向我们提供有关您正在进行/设置的更多信息。
#2
If lighting is enabled, color comes from the material, not the glColor vertex colors. 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.
如果启用了光照,则颜色来自材质,而不是glColor顶点颜色。如果您提到的绘图功能是为纹理对象设置材质(并且纹理下的白色材质很常见),那么其余的立方体将是白色的。使用GL_COLOR_MATERIAL设置OpenGL来获取glColor命令并更新材料而不仅仅是顶点颜色,这样才能工作。
So, simply put, if you have lighting enabled, try GL_COLOR_MATERIAL.
所以,简单地说,如果你启用了照明,请尝试GL_COLOR_MATERIAL。
#3
One thing you might want to try is: glBindTexture(GL_TEXTURE_2D, 0);
to bind the texture to nil.
您可能想要尝试的一件事是:glBindTexture(GL_TEXTURE_2D,0);将纹理绑定到nil。
#4
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.
- 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.
- 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.
着色器是否有效?
您改变了其他哪些州?例如GL_COLOR_MATERIAL,混合或光照将改变几何体的外观。
如果先绘制非纹理立方体,它是否有效?如果它确实试图找出它变成白色的点。也有可能立方体只会在第一帧中以正确的颜色显示,然后肯定会涉及GL状态。
将glPushAttrib / glPopAttrib放置在绘图方法的开头/结尾可能会有所帮助,但最好先找出导致问题的原因。
#1
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.
如果这些都不能解决您的问题,您可能需要在代码的这一部分之前向我们提供有关您正在进行/设置的更多信息。
#2
If lighting is enabled, color comes from the material, not the glColor vertex colors. 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.
如果启用了光照,则颜色来自材质,而不是glColor顶点颜色。如果您提到的绘图功能是为纹理对象设置材质(并且纹理下的白色材质很常见),那么其余的立方体将是白色的。使用GL_COLOR_MATERIAL设置OpenGL来获取glColor命令并更新材料而不仅仅是顶点颜色,这样才能工作。
So, simply put, if you have lighting enabled, try GL_COLOR_MATERIAL.
所以,简单地说,如果你启用了照明,请尝试GL_COLOR_MATERIAL。
#3
One thing you might want to try is: glBindTexture(GL_TEXTURE_2D, 0);
to bind the texture to nil.
您可能想要尝试的一件事是:glBindTexture(GL_TEXTURE_2D,0);将纹理绑定到nil。
#4
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.
- 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.
- 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.
着色器是否有效?
您改变了其他哪些州?例如GL_COLOR_MATERIAL,混合或光照将改变几何体的外观。
如果先绘制非纹理立方体,它是否有效?如果它确实试图找出它变成白色的点。也有可能立方体只会在第一帧中以正确的颜色显示,然后肯定会涉及GL状态。
将glPushAttrib / glPopAttrib放置在绘图方法的开头/结尾可能会有所帮助,但最好先找出导致问题的原因。