以下代码试图绘制 GLSurfaceView 的框架和一条对角线,但结果却是一个大黑屏。我尝试设置正交投影,将视口(viewport)大小与 View 大小对齐,使用 glDrawArrays() 绘制框架线,使用 glDrawElements() 绘制对角线。我找不到问题的根源。
public class MapView extends GLSurfaceView implements Renderer {
private int w, h;
private FloatBuffer frameVertices;
private ByteBuffer diagIndices;
public MapView(Context ctx) { this(ctx, null); }
public MapView(Context context, AttributeSet attrs) {
super(context, attrs);
setRenderer(this);
}
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, frameVertices);
gl.glColor4f(0f, 0f, 1f, 0.5f);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 4);
gl.glDrawElements(GL10.GL_LINES, 1, GL10.GL_UNSIGNED_BYTE, diagIndices);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
w = getWidth(); h = getHeight();
TRC.debug("w = " + w + ", h = " + h);
gl.glClearColor(0, 0, 0, 1);
gl.glViewport(0, 0, w, h);
gl.glDepthRangex(1, -1); // TODO remove
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthox(0, w, 0, h, 1, -1);
float[] frame = {
0, 0,
w-1, 0,
w-1, h-1,
0, h-1 };
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(frame.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
frameVertices = byteBuffer.asFloatBuffer();
frameVertices.put(frame);
frameVertices.flip();
frameVertices.position(0);
gl.glLineWidthx(10);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glDisable(GL10.GL_DEPTH_TEST);
diagIndices = ByteBuffer.allocateDirect(2);
diagIndices.order(ByteOrder.nativeOrder());
diagIndices.put(new byte[] {0, 2});
diagIndices.flip();
}
}
可能是什么问题?
最佳答案
我发现了问题。我使用了 glOrthox()
,我认为 x
只是表示整数值。但这不完全是,中间用二进制小数点处理。 16bit.16bit
。我的顶点超出了屏幕范围...我的解决方案是将 glOrthox()
更改为 glOrthof()
。感谢您的帮助!
关于android - 使用 OpenGL ES 1.0 在 Android 上绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8686042/