OpenGL - Can't display non-white colors after texture mapping

I am trying to render a colored cube after rendering other textured cubes. I have several Drawer objects that conform to the Drawer interface, and I pass each GL object reference to the draw (final GL gl) method of each individual implementation class. However, no matter what I do, I do not seem to be able to render the colored cube.

Sample code:

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 also tried to call glColor3f before each vertex call, but that still gives me a white cube. What?

+1


source to share


4 answers


There are several things you need to do to make sure this is true.

First of all:

gl.glEnable(gl.GL_COLOR_MATERIAL);

      

This will allow you to apply colors to your vertices. (Do this before glColor3f calls.)



If that still doesn't solve the problem, make sure you are using blending correctly (if you are using blending at all).

For most applications, you probably want to use

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

      

If none of these problems solve your problem, you may need to provide us with more information on what you are doing / configuring prior to this section of your code.

+3


source


When lighting is on, the color comes from the material, not the glColor vertex colors. If your paint function you talked about sets the material for the textured objects (and the white material under the texture will be spread), the rest of the cubes will be white. Using GL_COLOR_MATERIAL sets OpenGL to accept glColor commands and update material, not just the vertex color, so it should work.



So just put, if lighting is on, try GL_COLOR_MATERIAL.

+3


source


One thing you can try: glBindTexture(GL_TEXTURE_2D, 0);

bind the texture to zero.

0


source


Some things to check:

  • Is there an active shader?
  • Any gl-bugs?
  • What other states have you changed? For example GL_COLOR_MATERIAL, blending or lighting will change the appearance of your geometry.
  • Does this work if you draw an untextured cube first? And if he tries to figure out at what point he turns white. It is also possible that the cube will only display in the correct color in the first frame, then the GL state will definitely be involved.
  • Placing glPushAttrib / glPopAttrib at the beginning / end of your draw methods might help, but it's better to understand what caused the problem in the first place.
0


source







All Articles