-1

I am trying to render an object in an android app. The geometry is based on an interleaved vertex buffer (position data and normal data) and an index buffer.

The vertex shader expects those two attributes:

VertexShader:

...
attribute vec3 vPosition;
attribute vec3 vNormal;
...

On the Java side:

//after compiling the shader
positionHandle = GLES20.glGetAttribLocation(effect, "vPosition");
normalHandle = GLES20.glGetAttribLocation(effect, "vNormal");

I then draw the geometry with:

GLES20.glUseProgram(effect);
GLES20.glEnableVertexAttribArray(positionHandle);
GLES20.glEnableVertexAttribArray(normalHandle);

final int stride = 24;
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBuffer);
GLES20.glVertexAttribPointer(positionHandle, 3, GLES20.GL_FLOAT, false, stride, 0);
GLES20.glVertexAttribPointer(normalHandle, 3, GLES20.GL_FLOAT, false, stride, 12);

GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexCount, 
    GLES20.GL_UNSIGNED_SHORT, indexBuffer);

However, this results in

A/libc(1010): Fatal signal 11 (SIGSEGV) at 0x00000002 (code=1), thread 1024 (Thread-81)

If I remove the call to glEnableVertexAttribArray(normalHandle), it renders successfully (although black due to missing normals). The normalHandle variable is assigned a value of 1.

I have tested it on the emulator because I don't have a physical device right now.

Is something wrong with the above code? Or can this error be caused by the emulator and the app should run on a physical device?

4

1 回答 1

1

Copied from my previous comment:

The line

GLES20.glDrawElements(GLES20.GL_TRIANGLES, indexCount, GLES20.GL_UNSIGNED_SHORT, indexBuffer);

looks a bit suspicious to me, as the last parameter is supposed to be an byte offset into the currently bound ELEMENT_ARRAY_BUFFER, but your variable names suggest you are passing a buffer handle. If so, this could have any kind of weird effects.

于 2013-05-19T14:03:38.683 回答