嗯,这是尴尬,
我试图遵循google中的简单指南来使用openGL进行绘制。现在,我对那里的代码进行了一些修改,以查看发生了什么情况以及发生了什么,然后立即就“出错了”。该代码确实可以正常工作并显示良好-但我期望的显示效果并不理想。
我正在尝试绘制一个具有以下坐标(0,0.75,0)(-0.5,0,0)(0,0,0)的三角形。这应该是一个具有垂直长边和短水平边的三角形。对角线应从左下角到右上角。
如前所述:我正在看到一个三角形:但是第二个坐标似乎是围绕y轴“翻转”的:负的x值真的是“向右”吗?
渲染器代码:
public class MyGLRenderer implements GLSurfaceView.Renderer {
private Triangle mTriangle;
// mMVPMatrix is an abbreviation for "Model View Projection Matrix"
private final float[] mMVPMatrix = new float[16];
private final float[] mProjectionMatrix = new float[16];
private final float[] mViewMatrix = new float[16];
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
// Set the background frame color
GLES20.glClearColor(0f, 0f, 0f, 1.0f);
// initialize a triangle
float triangleCoords[] = {
0.0f, 0.75f, 0.0f, // top
-0.5f, 0f, 0.0f, // bottom left
0.0f, 0.0f, 0.0f // bottom right
};
mTriangle = new Triangle(triangleCoords);
}
public void onDrawFrame(GL10 unused) {
// Redraw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
// Draw shape
mTriangle.draw(mMVPMatrix);
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0, 0, width, height);
float ratio = (float) width / height;
// this projection matrix is applied to object coordinates
// in the onDrawFrame() method
Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 2.99f, 7);
}
public static int loadShader(int type, String shaderCode){
// create a vertex shader type (GLES20.GL_VERTEX_SHADER)
// or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)
int shader = GLES20.glCreateShader(type);
// add the source code to the shader and compile it
GLES20.glShaderSource(shader, shaderCode);
GLES20.glCompileShader(shader);
return shader;
}
}
为了完整起见,
Triangle
类:public class Triangle {
private FloatBuffer vertexBuffer;
// number of coordinates per vertex in this array
private static final int COORDS_PER_VERTEX = 3;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
private int vertexCount;
// Set color with red, green, blue and alpha (opacity) values
private static final float colorDefault[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };
public Triangle(float[] sizes) {
this.vertexCount = sizes.length / COORDS_PER_VERTEX;
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(
// (number of coordinate values * 4 bytes per float)
sizes.length * 4);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(sizes);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);
// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);
// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
}
private final int mProgram;
private final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vSizes;" +
"void main() {" +
" gl_Position = uMVPMatrix * vSizes;" +
"}";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
public void draw(float[] mvpMatrix) {
int mSizesHandle;
int mColorHandle;
int mMVPMatrixHandle;
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mSizesHandle = GLES20.glGetAttribLocation(mProgram, "vSizes");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mSizesHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mSizesHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, colorDefault, 0);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
// Pass the projection and view transformation to the shader
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mSizesHandle);
}
}
最佳答案
根据您的矩阵代码,看起来您正在构建一个从到的视图矩阵,并且鉴于您的三角形平放在xy平面上,实际上真神奇,您什么都看不到。因此,您需要修改构建视图矩阵的方式。我还建议您为三角形逐个顶点着色,以便更容易分辨变换后三角形的顶点在哪里结束。