我正在尝试使用 OpenGL ES2 绘制一个 3D 立方体,而我的代码仅绘制立方体的正面(正方形),如图所示。我不知道出了什么问题。我也尝试过根据其他教程实现我的多维数据集,但没有运气。
我的代码有什么问题?..我是否在某处遗漏了重要的代码行?
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TODO Auto-generated method stub
GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glDepthFunc(GLES20.GL_LEQUAL);
GLES20.glFrontFace(GLES20.GL_CCW);
GLES20.glCullFace(GLES20.GL_BACK);
GLES20.glEnable(GLES20.GL_CULL_FACE);
Matrix.setLookAtM(mVMatrix,0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
}
画框法
@Override
public void onDrawFrame(GL10 gl) {
// TODO Auto-generated method stub
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT|GLES20.GL_DEPTH_BUFFER_BIT);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
//Calling draw method of shapes
square.draw(mMVPMatrix);
}
立方体类
// x, y, z
static float squareCoords[] = {-1f, -1f, 1f,//Bottom Left
1f, -1f, 1f, //Bottom Right
1f, 1f, 1f, //Top Right
-1f, 1f, 1f, //Top left
-1f, -1f, -1f,//Bottom Left
1f, -1f, -1f, //Bottom Right
1f, 1f, -1f, //Top Right
-1f, 1f, -1f //Top left
};
private short drawOrder[] = {0,1,2, 0,2,3,//front
0,3,7, 0,7,4,//Left
0,1,5, 0,5,4,//Bottom
6,7,4, 6,4,5,//Back
6,7,3, 6,3,2,//top
6,2,1, 6,1,5//right
}; //Order to draw vertices
private int vertaxCount = 0; //Vertex count is the array divided by the size of the vertex ex. (x,y) or (x,y,z)
private int vertaxStride = 0; //4 are how many bytes in a float
private float [] color = new float[]{
0.1f, 0.5f, 1.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
0.0f, 10.0f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f
};
public Square()
{
//initialising vertex byte buffer for shape coordinates
// (number of coordinate values * 4 bytes per float)
ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
//Initialise byte buffer for the draw list
//(number of draw values * 2 bytes per short )
ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
ByteBuffer colors = ByteBuffer.allocateDirect(color.length * 4);
colors.order(ByteOrder.nativeOrder());
bufferColor = colors.asFloatBuffer();
bufferColor.put(color);
bufferColor.position(0);
int vertexShader = Shaders.loadShader(GLES20.GL_VERTEX_SHADER, Shaders.vertexShaderCode);
int fragmentShader = Shaders.loadShader(GLES20.GL_FRAGMENT_SHADER, Shaders.fragmentShaderCode);
mProg = GLES20.glCreateProgram();
GLES20.glAttachShader(mProg, vertexShader);
GLES20.glAttachShader(mProg, fragmentShader);
GLES20.glLinkProgram(mProg);
vertaxCount = squareCoords.length / COORDS_PER_VERTEX; //Vertex count is the array divided by the size of the vertex ex. (x,y) or (x,y,z)
vertaxStride = COORDS_PER_VERTEX * 4; //4 are how many bytes in a float
}
public void draw(float[] mMVPMatrix)
{
//Add program to OpenGL environment
GLES20.glUseProgram(mProg);
//Get Handle to the vertices
int mPositionHandle = GLES20.glGetAttribLocation(mProg, "vPosition");
//Prepare the shape coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertaxStride, vertexBuffer);
//Enable a handle to the shape vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
int mColorHandle = GLES20.glGetUniformLocation(mProg, "v_color");
GLES20.glVertexAttribPointer(mColorHandle, 3, GLES20.GL_FLOAT, false, 0, bufferColor);
GLES20.glEnableVertexAttribArray(mColorHandle);
float [] tm = new float[16];
float [] rm = new float[16];
Matrix.scaleM(mMVPMatrix, 0, 0.35f, 0.35f, 0);
Matrix.translateM(tm, 0, mMVPMatrix, 0, 0f, 0f, 0);
Matrix.rotateM(rm, 0, tm, 0, 45f, 0, 0, 1);
// get handle to shape's transformation matrix
int mMVPMatrixHandle = GLES20.glGetUniformLocation(mProg, "u_MVPMatrix");
// Apply the projection and view transformation
//GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, rm, 0);
//Draw the shape
//GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertaxCount);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 36, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
//Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}