我在使用openGL设置相机时遇到问题

时间:2011-06-29 18:27:59

标签: android opengl-es

现在我有一个视锥体,我实际上需要一个替代,但我无法让它工作,所以我尝试了一个截头锥体。当我更改它的参数时,gluLookAt不会更改视图,现在唯一对视图产生影响的是glViewport。我刚才注意到我正在搞砸它,实际上应该是glViewport(0, 0, width, height);

package Android.StreetBall;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
//import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
import android.util.Log;

public class GameRenderer implements Renderer{
    //Actual game size 19*12
    private float _Width = 0;
    private float _Height = 0;
    private final float _XBlocks = 19.0f;
    private final float _XBPF = 1.0f / _XBlocks;//Blocks per X frame
    private final float _YBlocks = 12.0f;
    private final float _YBPF = 1.0f / _YBlocks;//blocks per Y frame

    private BaseDrawableObject[] _DrawQueue;
    private Object _DrawLock;
    private boolean _DrawQueueChanged;

    public GameRenderer(){
            //_DrawQueueChanged = false;
            _DrawQueueChanged = true;
            _DrawLock = new Object();
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        //float ratio = _Width / _Height;
        //gl.glViewport(0, 0, (int) _Width, (int) _Height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
        gl.glShadeModel(GL10.GL_SMOOTH);
        gl.glClearDepthf(1.0f);
        gl.glEnable(GL10.GL_DEPTH_TEST);
        gl.glDepthFunc(GL10.GL_LEQUAL);
        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        _Width = width;
        _Height = height;
        Log.v("Dimensions", width + ", " + height);
        gl.glViewport(0, 0, width, height);
        //float ratio = width/height;
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        }

    public void onDrawFrame(GL10 gl) {

        gl.glOrthof(0.0f, _Width, _Height, 0.0f, -1.0f, 1.0f);    
        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);


        synchronized(_DrawLock){
            if(!_DrawQueueChanged){
                while(!_DrawQueueChanged){
                    try{
                        Log.d("game","rendering is waiting");
                        _DrawLock.wait();
                    } catch(InterruptedException e){
                        //no biggy
                    }
                }
            }
            _DrawQueueChanged = false;
        }    
        synchronized(this){
            if(_DrawQueue != null && _DrawQueue.length > 0){
                Log.d("game","rendering");
                gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

                for(BaseDrawableObject i: _DrawQueue){
                    if(i != null){
                        gl.glLoadIdentity();
                        gl.glScalef(_XBPF, _YBPF, 1);
                        gl.glTranslatef(i._Position.x,i._Position.y, 0.0f);
                        i.draw(gl);
                    }
                }
            } else if(_DrawQueue == null){
                gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
            }
        }
    }//end method

    /**
     * blocks while onDrawFrame is in progress, used by other threads to determine when drawing
     */


    public synchronized void setDrawQueue(BaseDrawableObject[] queue){
        _DrawQueue = queue;
        synchronized(_DrawLock){
            _DrawQueueChanged = true;
            _DrawLock.notify();
        }
    }

    public synchronized void waitDrawingComplete() {        
    }
}//end class

1 个答案:

答案 0 :(得分:1)

有几件事:

  • 您在onDrawFrame中调用转换方法,因此它们会累积。你应该包括push / popMatrix或glLoadIdentity。
  • 在绘制每个对象之前调用glLoadIdentity,撤消投影变换。
  • 您将当前矩阵模式保留为投影,但您应切换到模型视图以进行转换。
  • 建议将orthof应用于投影矩阵,并将glulookat应用于模型视图(请参阅here获取解释)。

我认为它应该是这样的:

onSurfaceChanged(...) { 
   glViewPort(...)
   glMatrixMode(GL10.GL_PROJECTION);
   glOrthof(...);
   glMatrixMode(GL10.GL_MODELVIEW);
}

onDrawFrame(...) {
   glLoadIdentity();
   GLU.gluLookAt(...);
   for each object {
      glPushMatrix();
      glRotatef(...);
      glTranslatef(...);
      drawObject(...);
      glPopMatrix();
   }
}

希望这有助于,欢呼,Aert。