以前,我一直在使用glDrawElements繪製一些3D形狀,但是當涉及到以不同方式着色面部時,事實證明最好擁有唯一的頂點。使用glDrawArrays繪製OpenGL ES的立方體
我一直在試圖重寫我以前工作的多維數據集類,以使用glDrawArrays來代替併爲每個頂點提供顏色(和正常但我沒有使用它)數據,但現在我無法在屏幕上看到任何東西。流程非常多:編譯着色器,創建程序和附加着色器,爲模型數據創建緩衝區,設置程序,傳遞指向模型數據的指針,傳遞MVP矩陣,繪製。
我認爲我已經完成了我爲glDrawElements所做的等價事情,所以我認爲我會責怪我的着色器代碼,但是我真的不清楚什麼是錯的。任何幫助讚賞。
public class Cube {
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 aPosition;" +
"attribute vec4 aColor; \n"
+ "attribute vec3 aNormal; \n"
+ "varying vec4 v_Color; \n"
+ "void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"varying vec4 v_Color;" +
"void main() {" +
" gl_FragColor = v_Color;" +
"}";
// Store our data in float buffer.
private final FloatBuffer mCubePositions;
private final FloatBuffer mCubeColors;
private final FloatBuffer mCubeNormals;
// Bytes per float.
private final int mBytesPerFloat = 4;
/** Size of the position data in elements. */
private final int mPositionDataSize = 3;
/** Size of the color data in elements. */
private final int mColorDataSize = 4;
/** Size of the normal data in elements. */
private final int mNormalDataSize = 3;
private final int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mNormalHandle;
private int mMVPMatrixHandle;
final float[] cubePositionData =
{...
};
final float[] cubeColorData =
{...
};
final float[] cubeNormalData =
{...
};
/**
* Sets up the drawing object data for use in an OpenGL ES context.
*/
public Cube() {
// prepare shaders and OpenGL program
int vertexShader = MyGLRenderer.loadShader(
GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(
GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // create OpenGL program executables
// Initialize the buffers.
mCubePositions = ByteBuffer.allocateDirect(cubePositionData.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mCubePositions.put(cubePositionData);
mCubePositions.position(0);
mCubeColors = ByteBuffer.allocateDirect(cubeColorData.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mCubeColors.put(cubeColorData);
mCubeColors.position(0);
mCubeNormals = ByteBuffer.allocateDirect(cubeNormalData.length * mBytesPerFloat)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mCubeNormals.put(cubeNormalData);
mCubeNormals.position(0);
}
public void draw(float[] mMVPMatrix) {
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "aPosition");
// get handle to vertex shader's aColor member
mColorHandle = GLES20.glGetAttribLocation(mProgram, "aColor");
mNormalHandle = GLES20.glGetAttribLocation(mProgram, "aNormal");
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
// Pass in the position information
mCubePositions.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, mPositionDataSize,
GLES20.GL_FLOAT, false, 0, mCubePositions);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the color information
mCubeColors.position(0);
GLES20.glVertexAttribPointer(mColorHandle, mColorDataSize,
GLES20.GL_FLOAT, false, 0, mCubeColors);
GLES20.glEnableVertexAttribArray(mColorHandle);
// Pass in the normal information
mCubeNormals.position(0);
GLES20.glVertexAttribPointer(mNormalHandle, mNormalDataSize,
GLES20.GL_FLOAT, false, 0, mCubeNormals);
GLES20.glEnableVertexAttribArray(mNormalHandle);
// Pass in the combined matrix.
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
// Draw the cube.
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 36);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}