Android Opengl GLU.gluLookAt 不起作用



我是OpenGL的新手,我不知道如何使用gluLookAt。以下是我的来源-任何帮助将不胜感激。

   public void onSurfaceCreated(GL10 gl, EGLConfig config)
    {       
       gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbientBuffer);                gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuseBuffer);              gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPositionBuffer);   
 gl.glEnable(GL10.GL_LIGHT0);                                                   
gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f);               
      gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);       
     gl.glDisable(GL10.GL_DITHER);              
     gl.glEnable(GL10.GL_TEXTURE_2D);           
     gl.glShadeModel(GL10.GL_SMOOTH);           
     gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);           
     gl.glClearDepthf(1.0f);                    
     gl.glEnable(GL10.GL_DEPTH_TEST);                 
     gl.glDepthFunc(GL10.GL_LEQUAL);            

    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST); 

    cube.loadGLTexture(gl, this.context);
}

public void onDrawFrame(GL10 gl) {
    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();                    //Reset The Modelview Matrix
    GLU.gluLookAt(gl, xrot, yrot, 0.0f, 0.0f, xrot, yrot, 0.0f, 0.0f, 0.0f);
    //Clear Screen And Depth Buffer
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
    gl.glLoadIdentity();                    //Reset The Current Modelview Matrix
    //Check if the light flag has been set to enable/disable lighting
    if(light) {
        gl.glEnable(GL10.GL_LIGHTING);
    } else {
        gl.glDisable(GL10.GL_LIGHTING);
    }
    //Check if the blend flag has been set to enable/disable blending
    if(blend) {
        gl.glEnable(GL10.GL_BLEND);         //Turn Blending On ( NEW )
        gl.glDisable(GL10.GL_DEPTH_TEST);   //Turn Depth Testing Off ( NEW )
    } else {
        gl.glDisable(GL10.GL_BLEND);        //Turn Blending On ( NEW )
        gl.glEnable(GL10.GL_DEPTH_TEST);    //Turn Depth Testing Off ( NEW )
    }
    //Drawing
    gl.glTranslatef(0.0f, 0.0f, z);         //Move z units into the screen
    gl.glScalef(0.8f, 0.8f, 0.8f);          //Scale the Cube to 80 percent, otherwise it would be too large for the screen
    //Rotate around the axis based on the rotation matrix (rotation, x, y, z)
    gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
    gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y
    cube.draw(gl, filter);                  //Draw the Cube 

    //Change rotation factors
    xrot += xspeed;
    yrot += yspeed;
}

/**
 * If the surface changes, reset the view
 */
public void onSurfaceChanged(GL10 gl, int width, int height) {
    if(height == 0) {                       //Prevent A Divide By Zero By
        height = 1;                         //Making Height Equal One
    }
    gl.glViewport(0, 0, width, height);     //Reset The Current Viewport
    gl.glMatrixMode(GL10.GL_PROJECTION);    //Select The Projection Matrix
    gl.glLoadIdentity();                    //Reset The Projection Matrix
    //Calculate The Aspect Ratio Of The Window
    GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
    gl.glMatrixMode(GL10.GL_MODELVIEW);     //Select The Modelview Matrix
    gl.glLoadIdentity();                    //Reset The Modelview Matrix
}

我可以看到两件事。第一,因为glulook被定义为 gluLookAt ( eyeX , eyeY , eyeZ , centerX , centerY , centerZ , upX , upY , upZ )

您的呼叫应该改为GLU.gluLookAt(gl, xrot, yrot, 0.0f, 0.0f, xrot, yrot, 0.0f, 1.0f, 0.0f); 注意新的向上向量'0.0,1.0,0.0'。基本上说y轴是你希望'up'所在的位置。

此外,您似乎在调用的其余部分使用旋转值。第一个三元组应该是你正在看的位置,第二个矢量应该是参考位置,通常是你的观察者所在的位置。看看http://developer.android.com/reference/android/opengl/GLU.html

第二个问题,如果你在glulook调用后调用loadiidentity,我很确定,因为它正在加载单位矩阵,你将失去glulook执行的转换。所以,在你放置几何图形后,尝试添加glulook。

下面是我在代码中说的:

public void onDrawFrame(GL10 gl) {
//cleaned up the reset code
gl.glMatrixMode(GL10.GL_MODELVIEW);     
gl.glLoadIdentity();                    
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);    
//Check if the light flag has been set to enable/disable lighting
if(light) {
    gl.glEnable(GL10.GL_LIGHTING);
} else {
    gl.glDisable(GL10.GL_LIGHTING);
}
//Check if the blend flag has been set to enable/disable blending
if(blend) {
    gl.glEnable(GL10.GL_BLEND);         //Turn Blending On ( NEW )
    gl.glDisable(GL10.GL_DEPTH_TEST);   //Turn Depth Testing Off ( NEW )
} else {
    gl.glDisable(GL10.GL_BLEND);        //Turn Blending On ( NEW )
    gl.glEnable(GL10.GL_DEPTH_TEST);    //Turn Depth Testing Off ( NEW )
}
//Drawing
gl.glTranslatef(0.0f, 0.0f, z);         //Move z units into the screen
gl.glScalef(0.8f, 0.8f, 0.8f);          //Scale the Cube to 80 percent, otherwise it would be too large for the screen
//Rotate around the axis based on the rotation matrix (rotation, x, y, z)
gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);   //X
gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f);   //Y
//change the perspective matrix to look at the rotating cube (0,0,z), from (0,0,0)
//with (0,1,0) as the up vector
GLU.gluLookAt(gl, 0.0f, 0.0, z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
cube.draw(gl, filter);                  //Draw the Cube 

//Change rotation factors
xrot += xspeed;
yrot += yspeed;

 }

最新更新