Android Opengl GLU.gluLookAt不起作用

时间:2011-07-24 16:00:54

标签: android opengl-es

我是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
}

1 个答案:

答案 0 :(得分:2)

我能看到的两件事。一,因为glulookat被定义为 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轴是你想要的“向上”。

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

第二个问题,如果你在glulookat调用之后调用loadIdentity,我很确定因为它正在加载单位矩阵,你将失去glulookat执行的转换。因此,在放置几何体后尝试添加glulookat。

以下是我在代码中基本上所说的内容:

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;


 }