为什么删除对glColor3f的一次调用会影响整个渲染?

时间:2012-05-25 05:02:46

标签: opengl lwjgl

所以我的代码的一个子集(当然是在渲染循环中)是:

    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();                
        }

代码正常工作,绘制一个二维网格框,框的两侧比顶部稍暗。 (穷人的照明)

为了好奇,我第一次打电话给glColor3f,期待盒子的顶部变成无色,两边保持不变;然而,整个盒子网格变白了。为什么是这样?这是这里的代码吗?或者是我的渲染循环的其他部分导致了这个?

(如果它有所不同,我正在使用LWJGL的OpenGL1.1的Java实现)

1 个答案:

答案 0 :(得分:2)

OpenGL立即模式有点像这样(伪代码)

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})
}

即。 OpenGL会记住您设置的最后一种颜色,它将应用于所有后续顶点,直到您将其更改为另一种颜色,然后将应用该颜色。