gpt4 book ai didi

android - 我在 openGL 中设置相机时遇到问题

转载 作者:太空狗 更新时间:2023-10-29 14:31:18 25 4
gpt4 key购买 nike

现在我有一个截锥体,我真的需要一个正视锥体来代替,但我无法让它工作,所以我尝试了一个截锥体。当我更改它的参数时,gluLookAt 不会更改 View ,现在唯一实际对 View 有影响的是 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,撤消投影转换。
  • 您将当前的矩阵模式保留为投影,但您应该切换到模型 View 以进行转换。
  • 建议将 orthof 应用于投影矩阵,将 glulookat 应用于模型 View (有关解释,请参阅 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。

关于android - 我在 openGL 中设置相机时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6525465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com