OpenGL lines meet in the middle of the screen instead of being straight
I tried to draw a line with OpenGL which goes horizontal from one edge of the screen to the other (on an Android phone). It should move up and down by reading the accelerometer sensor. My problem is, that the line moves up and down only at the end points. In the middle it stays at 0,0 (middle of the screen). So the line has the form of the letter V.
Here is my code:
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glClearColor(0f, 0f, 0f, 0f);
float vertices[] = {
0-width/2,-accel,0,
width/2,-accel,0
};
ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
vbb.order(ByteOrder.nativeOrder());
FloatBuffer vertexBuffer = vbb.asFloatBuffer();
vertexBuffer.put(vertices);
vertexBuffer.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
short[] indices = { 0, 1, 2, 0, 2, 3 };
ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
ibb.order(ByteOrder.nativeOrder());
ShortBuffer indexBuffer = ibb.asShortBuffer();
indexBuffer.put(indices);
indexBuffer.position(0);
gl.glFrontFace(GL10.GL_CCW);
gl.glEnable(GL10.GL_CULL_FACE);
gl.gl开发者_运维技巧CullFace(GL10.GL_BACK);
gl.glDrawElements(GL10.GL_LINE_LOOP, indices.length,
GL10.GL_UNSIGNED_SHORT, ibb);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
Are you sure you indices are correct? If I understand it correctly you only have 2 vertices and yet your indices are using 4 vertices. I looked at the example at http://www.songho.ca/opengl/gl_vertexarray.html to come up with that.
精彩评论