1

So a subset of my code (within a rendering loop of course) is:

    for(int x=0;x<data.length;x++) 
        for(int y=0;y<data[0].length;y++) {

            float r = colorData[x][y][0], g = colorData[x][y][1], b = colorData[x][y][2];
            glGetError();
            glColor3f(r,g,b);
            int xCoord = x*100;
            int yCoord = y*100;
            int height = (int) Math.round(data[y][x]*25);

            glBegin(GL_QUADS);


                //Top
                glVertex3f(xCoord,yCoord,height);
                glVertex3f(xCoord+100,yCoord,height);
                glVertex3f(xCoord+100,yCoord+100,height);
                glVertex3f(xCoord,yCoord+100,height);
                //*/

                //Sides
                glColor3f(r/2,g/2,b/2);          
                glVertex3f(xCoord,yCoord,height);
                glVertex3f(xCoord,yCoord,0);
                glVertex3f(xCoord+100,yCoord,0);
                glVertex3f(xCoord+100,yCoord,height);

                glVertex3f(xCoord,yCoord+100,height);
                glVertex3f(xCoord+100,yCoord+100,height);
                glVertex3f(xCoord+100,yCoord+100,0);
                glVertex3f(xCoord,yCoord+100,0);

                glVertex3f(xCoord,yCoord+100,height);
                glVertex3f(xCoord,yCoord+100,0);
                glVertex3f(xCoord,yCoord,0);
                glVertex3f(xCoord,yCoord,height);

                glVertex3f(xCoord+100,yCoord+100,height);
                glVertex3f(xCoord+100,yCoord,height);
                glVertex3f(xCoord+100,yCoord,0);
                glVertex3f(xCoord+100,yCoord+100,0);

                //Bottom
                glColor3f(r/4,g/4,b/4);
                glVertex3f(xCoord,yCoord,0);
                glVertex3f(xCoord,yCoord+100,0);
                glVertex3f(xCoord+100,yCoord+100,0);
                glVertex3f(xCoord+100,yCoord,0);



            glEnd();
            int err =  glGetError();
            if(err != GL_NO_ERROR)
                 System.out.println("An error has occured: #"+err);
            glColor3f(1,0,0);
            glBegin(GL_LINES);
                //Top
                glVertex3f(xCoord,yCoord,height);
                glVertex3f(xCoord+100,yCoord,height);
                glVertex3f(xCoord+100,yCoord+100,height);
                glVertex3f(xCoord,yCoord+100,height);
            glEnd();                
        }

The code works correctly, drawing a two dimensional grid of boxes, with the sides of the box being slightly darker than the top. (Poor man's lighting)

Just for curiosity I took out the first call to glColor3f, expecting the top of the box to become colorless, and the sides to remain the same; however the entire grid of boxes goes white. Why is this? Is it something in this code here? Or is some other part of my render loop causing this?

(If it makes a difference, I'm using LWJGL's Java implementation of OpenGL1.1)

Retsam
  • 30,909
  • 11
  • 68
  • 90
  • 1
    Are you familiar with finite state machines? OpenGL happens to be a FSM and the behavior you're seeing is because of this. – Mike Bailey May 25 '12 at 05:14
  • I think your understanding is correct, I can't explain that result off top of my head. Can you throw in a glGetError call to make sure there's nothing else going on? That's the first thing I do whenever something's not quite right (usually reveals the answer). – Tim May 25 '12 at 05:14
  • Also can you put up your real code? Obviously what you have shown there is not a functioning code block. It's possible you made a mistake in the parts you are not showing (what did you remove, where are rgb being assigned?) – Tim May 25 '12 at 05:16
  • Interestingly, adding glGetError() before and after changes the output. When I do, I get the behavior I had expected where only the top changes. (But no error is found in this section; so the error is elsewhere, and it's not worth hunting down here) – Retsam May 25 '12 at 05:51
  • Make sure not to call glGetError inside a glBegin/End block, as that itself will generate an error. Can you post actual code please? You're likely just making a simple mistake. – Tim May 25 '12 at 06:04
  • I posted the whole section I was wondering about. I'm pretty sure I found the error elsewhere in the code. The rest of the code isn't worth going through, it's just me experimenting to learn OpenGL. – Retsam May 25 '12 at 06:20

1 Answers1

2

OpenGL immediate mode works a bit like this (pseudocode)

glColor3f(r,g,b){
state.color.{r,g,b} = r,g,b;
}

glNormal(x,y,z){
state.normal.{x,y,z} = x,y,z;
}

/* and so on */

glVertex3f(x,y,z){
    send_vertex_to_rasterizer(state.color, state.normal, ..., {x,y,z})
}

I.e. OpenGL remembers the last color you've set and will it apply to all following vertices until you change it to another color, which then will apply.

datenwolf
  • 159,371
  • 13
  • 185
  • 298