将“光照”应用于 gl_Quad 中的特定复选框

Apply Lighting to specific checkboxes in gl_Quad

本文关键字:复选框 Quad 光照 应用于 gl      更新时间:2023-10-16

我想在这个棋盘格的白色和黑色盒子上应用不同的照明。到目前为止,我做了什么。

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

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

与其每次都交换这个,我可能会将照明设置为黑色,绘制所有黑色,然后将照明设置为白色,并绘制所有白色。

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