将Lighting应用于gl_Quad中的特定复选框

时间:2012-05-25 05:39:03

标签: c++ opengl

我想在这个棋盘上的白色和黑色盒子上应用不同的灯光。到目前为止我已经完成了。

myDisplay函数中的嵌套循环正在创建框。

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color(0.2, 0.2, 0.2)
GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
//Coming from the direction (-1, 0.5, 0.5)
GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
void myInit(void)
{
glClearColor (1,1,0,0);
glPointSize(10);
glLineWidth(10.0);
glColor3f(1,1,1);
gluOrtho2D(0,640,0,480); 
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluPerspective(45,2,-2,2);
gluLookAt(100.0,200.0,200.0,//eyeposition
    0.0,0.0,0.0,//ccenter
    0.0,1.0,0.0);//up direction 
}
void myDisplay(void)
{
int posx=10;
int posy=10;
int posWidth=60;
int posHeight=60;
bool whiteORBlack=true;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);

glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);


 // glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
 // glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
for(int j=1;j<=8;j++)
{
    for(int i=1;i<=8;i++)
    {
        if(whiteORBlack==true)
        {
            glBegin(GL_QUADS);
            glColor3f(0,0,0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);

            glVertex2i(posx,posy);
            glVertex2i(posx+posWidth,posy);
            glVertex2i(posx+posWidth,posy+posHeight);
            glVertex2i(posx,posy+posHeight);
            whiteORBlack=false;

        }
        else if(whiteORBlack==false)
        {
            glBegin(GL_QUADS);
            glColor3f(1,1,1);

            glVertex2i(posx,posy);
            glVertex2i(posx+posWidth,posy);
            glVertex2i(posx+posWidth,posy+posHeight);
            glVertex2i(posx,posy+posHeight);
            whiteORBlack=true;
        }
            posx=posx+posWidth;
    }

    if(whiteORBlack==false)
    {
        whiteORBlack=true;
    }
    else
    {
        whiteORBlack=false;
    }
    posy=posy+posHeight;

    posx=10;
}


//glPopMatrix();
//glutSwapBuffers();
glEnd();
glFlush();
 }
  void main(int argc,char ** argv)
 {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize (640,480);
glutInitWindowPosition(100,150);
glutCreateWindow("FINAL LAB");
myInit();
glutDisplayFunc(myDisplay);
//glEnable(GL_LIGHTING);
//glEnable(GL_LIGHT0);
//glLightfv(GL_LIGHT0,GL_AMBIENT,light0_ambient);
//glLightfv(GL_LIGHT0,GL_DIFFUSE,ligth0_diffuse);
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_CULL_FACE);
//glEnable(GL_BLEND);
//glEnable(GL_LINE_SMOOTH);
//glLineWidth(2);
//glMatrixMode(GL_PROJECTION);
/*gluPerspective(40,1,1,10);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);*/
glutMainLoop();
 }

1 个答案:

答案 0 :(得分:1)

我认为您必须将所有glLightfv内容设置为lightColor1等。

我不是每次都要交换它,而是将光线设置为黑色,绘制所有黑色光源,然后将光线设置为白色,并绘制所有白色光源。

如果我错了,那么屏幕截图可以帮助您更好地回答。