1

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

    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 实现)

4

1 回答 1

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会记住您设置的最后一个颜色,并将其应用于所有后续顶点,直到您将其更改为另一种颜色,然后才会应用。

于 2012-05-25T09:09:35.447 回答