2012-05-25 29 views
0

我想在此棋盤上的白色和黑色框上應用不同的光照。到目前爲止我所做的一切。將光照應用於gl_Quad中的特定複選框

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

我認爲你必須所有的東西glLightfv設置lightColor1

而是圍繞這個每次交換的,我可能會設置照明黑色,繪製所有的黑色,然後將燈光設置爲白色,並畫出所有白色燈光。

如果我錯了,那麼截圖可能會幫助你更好地回答你。