2011-11-25 71 views
4

我在使用OpenGL启用照明时能够看到创建的对象时遇到问题。我有一个从3D Max导入的对象,即照明可以正常工作,但场景的其余部分不能。我知道我需要指定法线,但这似乎没有帮助。虽然如果我在我的display()函数中创建了一个简单的多边形,但是其他多边形已在类的方法中创建并在display()函数中调用,但没有显示出来OpenGL照明的问题

这是我的照明代码

glewInit(); 

glClearColor(0.0,0.0,0.0,0.0); 
glClear(GL_COLOR_BUFFER_BIT) 

glShadeModel(GL_SMOOTH); 

//light position and colour 
GLfloat light_position[] = { 0.0, 0.0, 20.0,0.0 }; 
GLfloat white_light[] = {0.8,0.8,0.8,0.0}; 
GLfloat diff_light[] = {1.0,1.0,1.0,0.0}; 
GLfloat spec_light[] = {1.0,1.0,1.0,0.0}; 
glLightfv(GL_LIGHT0, GL_AMBIENT, white_light); 
glLightfv(GL_LIGHT0, GL_DIFFUSE, diff_light); 
glLightfv(GL_LIGHT0, GL_SPECULAR, spec_light); 
glLightfv(GL_LIGHT0, GL_POSITION, light_position); 

//ambient light 
GLfloat ambient[] = {0.3,0.3,0.3}; 
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); 

//diffuse material component 
GLfloat diff[] = {0.6,0.6,0.6}; 
glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); 

//specular material component 
GLfloat WhiteSpec[] = {1,1,1}; 
glMaterialfv(GL_FRONT, GL_SPECULAR, WhiteSpec); 

GLfloat shininess = 50; 
glMaterialf(GL_FRONT, GL_SHININESS, shininess); 

//ENABLE LIGHTING AND DEPTH TEST 
glEnable(GL_LIGHTING); 
glEnable(GL_LIGHT0); 

glEnable(GL_DEPTH_TEST); 

这是我的类方法是创建我的海

glColor3f(0,0,1); 
glPushMatrix(); 

//enable texturing 
glEnable(GL_TEXTURE_2D);  
glEnable(GL_BLEND); 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
glBindTexture(GL_TEXTURE_2D, seaTex); 
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

for(int i = 0, k = 0; i < (getWidth()/10); i++){ 
    for(int j = 0; j < (getLength()/10); j++){ 
     if(i >= Sea::waveLoc1 && i <= Sea::waveLoc1+Sea::sinArrayLength){ 

      int nextK = k+1; 
      if(nextK == Sea::sinArrayLength){ 
       nextK = 0; 
      } 

      if(i == Sea::waveLoc1+Sea::sinArrayLength){ 
       //front of wave 
       glBegin(GL_POLYGON);  

       glNormal3f(0.0f, 1.0f, 0.0f); 
        glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::sinVals[nextK], Sea::seaGrid[i][j].z); 
        glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i][j+1].z); 
        glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::seaGrid[i+1][j+1].y , Sea::seaGrid[i+1][j+1].z); 
        glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::seaGrid[i+1][j].y, Sea::seaGrid[i+1][j].z); 
       glEnd(); 
      }else{ 
       //rest of wave 
       glBegin(GL_POLYGON);  

       glNormal3f(0.0f, 1.0f, 0.0f); 
        glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::sinVals[k], Sea::seaGrid[i][j].z); 
        glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::sinVals[k], Sea::seaGrid[i][j+1].z); 
        glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j+1].z); 
        glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j].z); 
       glEnd(); 
      } 

     }else{ 
      //draw flat sea 
      glBegin(GL_POLYGON);  

       glNormal3f(0.0f, 1.0f, 0.0f); 

       glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::seaGrid[i+1][j].y, Sea::seaGrid[i+1][j].z); 
       glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::seaGrid[i+1][j+1].y, Sea::seaGrid[i+1][j+1].z); 
       glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::seaGrid[i][j+1].y, Sea::seaGrid[i][j+1].z); 
       glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::seaGrid[i][j].y, Sea::seaGrid[i][j].z); 
      glEnd(); 
     } 
    } 

    //increment k if i is in the area of the wave 
    if(k < Sea::sinArrayLength-1 && (i >= Sea::waveLoc1 && i <= Sea::waveLoc1+Sea::sinArrayLength)){ 
     k++; 
    }else if(k == Sea::sinArrayLength){ 
     k = 0; 
    } 
} 

if(Sea::waveLoc1 < 100 && Sea::waveInc == Sea::waveSpeedLimiter){ 
    Sea::waveLoc1 +=1; 
}else if(Sea::waveLoc1 >= 100){ 
    Sea::waveLoc1 = 0; 
} 

//limits speed of wave 
if(Sea::waveInc < Sea::waveSpeedLimiter){ 
    Sea::waveInc++; 
}else{ 
    Sea::waveInc = 0; 
} 


//disable texturing 
glDisable(GL_BLEND); 
glDisable(GL_TEXTURE_2D); 
glPopMatrix(); 

这然后在我的显示()函数调用如下

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
gluPerspective(45.0f,(GLfloat)screenWidth/(GLfloat)screenHeight,0.1f,1000.0f); 

glMatrixMode(GL_MODELVIEW); 

glLoadIdentity(); 

camera.updateCameraPosition(mouse_x,mouse_y,screenWidth,screenHeight); 


sea.buildSeaPlane(); 
scene.buildEdges(); 


glPushMatrix(); 
glColor3f(0,1,0); 
glTranslatef(0, 20, 200); 
model.speedDisplayFaceNormals(); 

glPopMatrix(); 

glPushMatrix(); 

plane.updatePlanePosition(); 

glBegin(GL_POLYGON);  

    glNormal3f(0.0f, 1.0f, 0.0f); 

    glVertex3f(0, 25, 10); 
    glVertex3f(2, 25, 10); 
    glVertex3f(2, 25, 20); 
    glVertex3f(0, 25, 20); 
glEnd(); 


glPopMatrix(); 

glFlush(); 

任何想法,为什么我不能看到这些?

UPDATE:

我可以看到我的海我是否禁用纹理。我怎样才能修复它,所以我可以使用纹理和照明?

更新2:

物是人非纹理到GL_MODULATE但我也不得不删除混合,使其工作。我需要启用混合吗?

回答

2
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

说用纹理的结果替换光照计算。如果你想照明来影响纹理,我们GL_MODULATE而不是GL_REPLACE

编辑补充:

你只需要勾兑,如果你希望你的几何体是半透明的(这是勾兑就是通常)。在你的情况下,在代码中有一些问题:

  • 你的光线颜色是完全透明的(全都有0作为第四个组件)。所以你的光线让一切都看不见。将其更改为1.
  • 您的材料没有第四个组件。更糟糕的是,glMaterialfv预计有4个浮标。这甚至可能会导致应用程序崩溃或重新格式化磁盘。添加第四个组件,最好为1.使其完全不透明。
0

尝试为整个场景添加环境光线。喜欢的东西:

GLfloat lightColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; 
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightColor); 
+0

似乎没有帮助:( – Chris

0

在你glMaterialfv来电,颜色参数应该有4个值,而不是3一般情况下,第4个值是1.0的alpha。我不确定是否可以将0.0作为glLightfv的第四个值。

+0

没有区别:( – Chris