现在我有一个截头台,我真的需要有一个正交,但我不能让它工作,所以我尝试了一个截头台。当我改变它的参数时,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
最佳答案
有几件事:
在OnDrawFrame中调用转换方法,因此它们会累积。您应该包含push/popmatrix或glloadidentity。
在绘制每个对象之前调用glloadidentity,撤消投影转换。
将当前矩阵模式保留为“投影”,但应切换到“模型视图”进行转换。
建议将正交函数应用于投影矩阵,将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();
}
}
希望能帮上忙,干杯,阿尔特。