我有一些关于opengles 2.0的麻烦。目前我试图显示3D茶壶模型没有成功。方法GLES20.glDrawElements生成数字1282的opengl错误,屏幕上不显示任何内容。
我的绘制方法代码:
public void draw(float[] tfMVPMatrix) {
GLES20.glUseProgram(miProgramID);
miVertexPositionHandle = GLES20.glGetAttribLocation(miProgramID, VertexShader.Variables.VERTEX_POSITION.getName());
miVertexTexCoordsHandle = GLES20.glGetAttribLocation(miProgramID, VertexShader.Variables.VERTEX_TEXTURE_CORDS.getName());
miVertexNormalsHandle = GLES20.glGetAttribLocation(miProgramID, VertexShader.Variables.VERTEX_NORMALS.getName());
miProjectionMatrixHandle = GLES20.glGetUniformLocation(miProgramID, VertexShader.Variables.MVP_MATRIX.getName());
miColorHandle = GLES20.glGetUniformLocation(miProgramID, FragmentShader.Variables.COLOR.getName());
GLES20.glEnableVertexAttribArray(miVertexPositionHandle);
GLES20.glVertexAttribPointer(miProgramID, 3, GLES20.GL_FLOAT, false, 3 * Float.SIZE, moVertBuff);
GLES20.glEnableVertexAttribArray(miVertexTexCoordsHandle);
GLES20.glVertexAttribPointer(miProgramID, 2, GLES20.GL_FLOAT, false, 2 * Float.SIZE, moTexCoordBuff);
GLES20.glEnableVertexAttribArray(miVertexNormalsHandle);
GLES20.glVertexAttribPointer(miProgramID, 3, GLES20.GL_FLOAT, false, 3 * Float.SIZE, moNormBuff);
GLES20.glUniform4fv(miColorHandle, 1, COLOR, 0);
GLES20.glUniformMatrix4fv(miProjectionMatrixHandle, 1, false, tfMVPMatrix, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, miIndicesNumber, GLES20.GL_UNSIGNED_SHORT, moIndBuff); // opengl error 1282
GLES20.glDisableVertexAttribArray(miVertexPositionHandle);
GLES20.glDisableVertexAttribArray(miVertexTexCoordsHandle);
GLES20.glDisableVertexAttribArray(miVertexNormalsHandle);
}
顶点着色器代码:
attribute vec4 vPosition;
attribute vec4 vNormal;
attribute vec2 vTexture;
varying vec2 texCoord;
varying vec4 normal;
uniform mat4 uMVPMatrix;
void main() {
gl_Position = uMVPMatrix * vPosition;
normal = vNormal;
texCoord = vTexture;
}
片段着色器代码:
precision mediump float;
varying vec4 normal;
uniform vec4 vColor;
void main() {
gl_FragColor = vColor;
}
我的渲染器代码:
public class MyRenderer implements GLSurfaceView.Renderer {
private int GLProgramID;
private Demo3DObj oDemoTeapot;
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
private float fRatio;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLProgram oProgram = new GLProgram();
GLProgramID = oProgram.build();
oDemoTeapot = new Demo3DObj(GLProgramID);
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
fRatio = (float)width/height;
Matrix.frustumM(mProjectionMatrix, 0, -fRatio, fRatio, -1, 1, 3, 7);
}
@Override
public void onDrawFrame(GL10 gl) {
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
oDemoTeapot.draw(mMVPMatrix);
}
}
我是opengles世界的新手,我不知道这里有什么不对。任何人都可以帮我解决这个问题吗?我应该在这里更改什么来显示我的模型?
UPDATE!
这是方法,用于创建moIndBuff:
private Buffer fillBufferI(int[] oContainer) {
ByteBuffer bb = ByteBuffer.allocateDirect(2 * oContainer.length);
bb.order(ByteOrder.LITTLE_ENDIAN);
for (int s : oContainer)
bb.putInt(s);
bb.rewind();
return bb;
}