我正在OpenGL ES for Android(api 3)中编写一个小程序,在黑屏上显示一个白色三角形。但是当我注意到将glTranslatef的调用更改为glTranslatex(在onDrawFrame()方法中)时,我的惊讶让我的三角形看不见!有人可以解释我发生了什么。
这是我的渲染器代码
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLU;
public class TheRenderer implements Renderer {
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glTranslatef(0, 0, -6); // Here my problem
triangle.draw(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if (height == 0)
height = 1;
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
GLU.gluPerspective(gl, 60.0f, (float) width / (float) height, 1, 10);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
private GLSimple2DTriangle triangle = new GLSimple2DTriangle();
}
这里是Triangle的代码
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
public class GLSimple2DTriangle {
public GLSimple2DTriangle(){
buildVerticesBuffer();
}
public void draw(GL10 gl){
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(COORDS_PER_VERTEX, GL10.GL_SHORT, 0, verticesBuffer);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, VERTICES_NUMBER);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
private void buildVerticesBuffer(){
final short vertices [] = {
+0, +1,
+1, -1,
-1, -1
};
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length * SHORTS_SIZE_BYTES);
byteBuffer.order(ByteOrder.nativeOrder());
verticesBuffer = byteBuffer.asShortBuffer();
verticesBuffer.put(vertices);
verticesBuffer.rewind();
}
private ShortBuffer verticesBuffer;
private static final byte SHORTS_SIZE_BYTES = 2;
private static final byte COORDS_PER_VERTEX = 2;
private static final byte VERTICES_NUMBER = 3;
}
此致
答案 0 :(得分:3)
glTranslatex
是定点版本。 OpenGL-ES使用16.16定点格式,因此整数坐标应向左移16位以获得相同的结果。您的翻译电话将变为glTranslatex(0, 0, -6 << 16)
。现在,没有移位,值-6实际上是-0.00009(-6 / 65536),因此您的对象被近平面剪切。