gl.glColor3f() problem

Hi there,

I have another problem about gl.glColor3f(). In my game, I draw a floor, walls and some text.

private void renderScene()
{
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();

drawFloor();
drawText();
drawWall();
}

in each “draw” method, I define a color for it.

void drawFloor()
{
gl.glColor3f(1.0f, 1.0f, 1.0f);
}

void drawWall()
{
gl.glColor3f(0.2f, 0.2f, 0.2f);
}

void drawText()
{
gl.glColor3f(0.1f, 0.6f, 0.5f);
}

but when I run the program, all the floor, wall and text are in the same color. I don’t know why. How can I use different color for different things in JOGL?

Thanks a lot !

Hi anyegongjue,

Do you have lighting disabled or enabled when specifying the colors? Because if you want lighting, you need to specify the color using glMaterial* calls and use normals. If you don’t want lighting yet you can simply call gl.glDisable(GL.GL_LIGHTING); before you draw the floor, text and wall.

Normal OpenGL behavior is that you first use glColor3f, then you draw some vertices, and then repeat the process.

Hope this helps,

Jark

Hi Jark,

Thanks for helping me. But the problem is still there. I use disable and enable method in each of the drawXXX() method. I just use drawFloor() as example. but I still cannot use gl.glColor3f(0.8f, 0.4f, 0.3f); to define the color of the wall and other things. So how can I do now? I just cannot find a way out of this.

Thank you very much for help!

private void drawFloor()
{
gl.glDisable(GL.GL_LIGHTING);

if (floorTex != null)
{
floorTex.enable();
gl.glEnable(GL.GL_TEXTURE_2D);
floorTex.bind();
gl.glTexEnvi(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);

  TextureCoords tc = floorTex.getImageTexCoords();
  float left = tc.left()*40;
  float right = tc.right()*40;
  float bottom = tc.bottom()*40;
  float top = tc.top()*40;
    
  gl.glBegin(GL.GL_QUADS);         // draw floor
  gl.glTexCoord2f(left, bottom);gl.glVertex3f(0.0f, 0.0f, 0.0f);
  gl.glTexCoord2f(right, bottom);gl.glVertex3f(400.0f, 0.0f, 0.0f);
  gl.glTexCoord2f(right, top);gl.glVertex3f(400.0f, 0.0f, -200.0f);
  gl.glTexCoord2f(left, top);gl.glVertex3f(0.0f, 0.0f, -200.0f);
  gl.glEnd();

}
gl.glEnable(GL.GL_LIGHTING);
} // end of CheckerFloor()

You’re binding a texture and using GL_REPLACE, which (although I’m not certain off the top of my head) replaces the previous fragment color by the computed texture color. Try using GL_MODULATE or not using the texture at all. If you’re using textures, why do you want to set the color? If you want color, why aren’t you using lights?

Not to be harsh, but if you’re new to jogl and opengl, I think this project is a little advanced until you’re able to figure out how to control the basics (especially considering that you want a skybox). I would google the redbook and go through the first few chapters if you haven’t already.

i agree with the previous poster. i would develop and work on all the subsystems independently, and build them up one at a time. first i developed a mesh, then i created a scenegraph, etc. each subsystem takes a lot of work to design and test. the good thing is that even if you end up scrapping it and using an existing engine, or portions of one, you will have a much better understanding of how it all works under the hood.

with opengl, you’re creating a drawing system. but then you need a system that uses the drawing system. then you need to take that system and add your content and rules. quite a few steps, and each one is not easy. just work on one thing at a time. first get it to work, then generalize.

good luck