我绘制了一条具有顶点(0.2f,0.2f,0.0f)和&的线。 (-0.2f,-0.2f,0.0f)在OpenGL中。由于缺少线坐标的z分量,它意味着线在XY平面中。现在我尝试使用glRotatef函数围绕z轴旋转直线。理想情况下,线旋转应该采用圆形路径(至少它在旋转1到360时应该看起来像一个圆圈)但不知何故它看起来更像椭圆,更向y轴延伸(我的猜测是,可能因为深度效应)。但由于我的线完全处于XY平面(因为Z坐标在线坐标中缺失)为什么我会得到这样的深度效果?我已经在这个问题上发布了我的代码。请指出正确的方向?
GLSurfaceViewActivity代码文件:
package com.mobi.trials.gldemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class GLSurfaceViewActivity extends Activity {
SimpleGLSurfaceView glView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("GLDemo", "In the activity");
glView = new SimpleGLSurfaceView(this);
setContentView(glView);
}
@Override
protected void onPause() {
super.onPause();
glView.onPause();
}
@Override
protected void onResume() {
super.onResume();
glView.onResume();
}
}
SimpleGLSurfaceView代码文件:
package com.mobi.trials.gldemo;
import android.content.Context;
import android.opengl.GLSurfaceView;
public class SimpleGLSurfaceView extends GLSurfaceView {
public SimpleGLSurfaceView(Context context) {
super(context);
final DrawGLSurfaceCanvasView renderer = new DrawGLSurfaceCanvasView(this);
setFocusable(true);
setFocusableInTouchMode(true);
setRenderer(renderer);
}
}
DrawGLSurfaceCanvasView代码文件:
package com.mobi.trials.gldemo;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView;
public class DrawGLSurfaceCanvasView implements GLSurfaceView.Renderer {
SimpleLine line;
private float rotationAngle = 0.0f;
float lineVertices[] = { 0.2f, 0.2f, 0f,
-0.2f, -0.2f, 0f };
public DrawGLSurfaceCanvasView(GLSurfaceView view) {
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
gl.glRotatef(rotationAngle, 0.0f, 0.0f, 1.0f);
if(rotationAngle <= -360.0f)
{
rotationAngle = 0.0f;
}
rotationAngle -= 1f;
line.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
line = new SimpleLine(lineVertices);
}
}
SimpleLine代码文件:
package com.mobi.trials.gldemo;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class SimpleLine {
float vertices[];
private FloatBuffer vertexBuffer;
public SimpleLine(float[] vertices) {
super();
this.vertices = vertices;
}
public void draw(GL10 gl)
{
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
// Update : Added orthographic projection matrix.
// Update II : Corrected the values of orthgraphic projection matrix.
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glDrawArrays(GL10.GL_LINES, 0, 2);
}
}
答案 0 :(得分:4)
我不认为你所观察到的是深度效应。只是你没有设置任何投影矩阵(导致获取默认值:身份)。因此,如果将模型视图矩阵设置为标识,则最终会以[-1:1]表示窗口的范围。
[ - 。2,。2]最终代表你窗户的20%。我假设你的窗口不是方形的,所以你最终会绘制一个偏心率为宽高比的椭圆。
您需要查看解释投影矩阵通常如何设置的教程(通常涉及窗口大小和/或宽高比)。