2012-10-08 62 views
0

我想在屏幕中間畫一個旋轉的立方體,並且我希望它可以通過它上方的燈光點亮(我希望它看起來像立方體是從固定屏幕位置點亮)。我的問題是我不知道如何防止光線與立方體一起旋轉。openGL - 如何在不旋轉光線的情況下旋轉對象

下面的代碼:

(總結:initGL,paintGL和resizeGl是,你總是必須實現的功能在paintGL我使用makeCube()在makeCube()我使用在glBegin(GL_QUADS)至使用calcNormals()來計算立方體的法線)

------------- initGL -------------- ------------

angle=0.0; 
glEnable (GL_DEPTH_TEST); 
glEnable (GL_LIGHTING); 
GLfloat LightDiffuse[]=  { 1.0f, 1.0f, 1.0f, 1.0f }; 
GLfloat LightPosition[]= { 0.0f, 1.5f,1.5f, 1.0f }; 
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse); 
glLightfv(GL_LIGHT0, GL_POSITION,LightPosition); 
glEnable (GL_LIGHT0); 

-------------- paintGL ---------------- -

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

glLoadIdentity(); 
glTranslatef(0.0, 0.0, -13.0); 
glRotatef(angle,0.0f,1.0f,0.0f); 

makeCube(); 

angle+=0.3; 

--------------空隙makeCube()-------------------

float P[8][3]={ {-1,-1, 1},{1,-1, 1},{1,1, 1},{-1,1, 1}, 
       {-1,-1,-1},{1,-1,-1},{1,1,-1},{-1,1,-1}}; 

float * planes[6][4] ={ {P[0],P[1],P[2],P[3]}, 
         {P[1],P[5],P[6],P[2]}, 
         {P[4],P[7],P[6],P[5]}, 
         {P[0],P[3],P[7],P[4]}, 
         {P[3],P[2],P[6],P[7]}, 
         {P[0],P[4],P[5],P[1]}}; 
int i; 
for(i=0;i<6;i++){ 
    float *normal; 
    normal = calcNormal(planes[i][0],planes[i][1],planes[i][2]); 
    glBegin(GL_QUADS); 
     glNormal3f(normal[0], normal[1], normal[2]); 
     glVertex3f(planes[i][0][0],planes[i][0][1],planes[i][0][2]); 
     glVertex3f(planes[i][1][0],planes[i][1][1],planes[i][1][2]); 
     glVertex3f(planes[i][2][0],planes[i][2][1],planes[i][2][2]); 
     glVertex3f(planes[i][3][0],planes[i][3][1],planes[i][3][2]); 
    glEnd(); 
} 

- --------------- float * calcNormal()----------------------

float vec1[3] = {P2[0]-P1[0],P2[1]-P1[1],P2[2]-P1[2]}; 
float vec2[3] = {P3[0]-P2[0],P3[1]-P2[1],P3[2]-P2[2]}; 
float cross[3] = {vec1[1]*vec2[2]-vec2[1]*vec1[2], 
        vec1[2]*vec2[0]-vec2[2]*vec1[0], 
        vec1[0]*vec2[1]-vec2[0]*vec1[1]}; 
float modCross = sqrt(cross[0]*cross[0]+cross[1]*cross[1]+cross[2]*cross[2]); 
cross[0]/=modCross; 
cross[1]/=modCross; 
cross[2]/=modCross; 

return cross; 

- ------------ resizeGL --------------------------

glViewport(0, 0, width, height); 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
GLfloat x = GLfloat(width)/height; 
glFrustum(-x, +x, -1.0, +1.0, 4.0, 15.0); 
glMatrixMode(GL_MODELVIEW); 

回答

0

我解決了這個問題繪圖與頂點數組,而不是直接方式對立方,似乎旋轉或燈光影響對象不同每種方法都很方便,這很奇怪

0

看來你'正在改變你的paintGL秒光的位置灰。

縱觀舊代碼,我在我的代碼目錄中發現了一個應用程序,用於加載和旋轉.OBJ網格,同時允許光線移動。

我認爲解決的辦法是設置每一幀光的位置。 (不記得它已經超過18個月,因爲我感動項目)

void idleFunc() 
{ 
    light();  /// *** I think you need to replicate this functionality **** 
    glPushMatrix(); 
    myGluLookAt(0.0, -.50, -6.0, /* eye is at (0,0,5) */ 
       0.0, 0.0, 0.0,  /* center is at (0,0,0) */ 
       0.0, 1.0, 0.);  /* up is in positive Y direction */ 
    transformFunc(); 
    displayFunc(); 
    glPopMatrix(); 
} 


void displayFunc() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    if (useDrawList) 
     glCallList(DLid); 
    else 
     drawObj(loadedObj); 
    drawLight0();    // *** just displays an unlit sphere at the position of the light ** 
    glutSwapBuffers(); 
    frameCount++; 
} 


/* set the poition of each of the lights */ 
void light() 
{ 
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos1); 
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos2); 
}