这很尴尬,
我试图按照google的简单指南来使用openGL进行绘制。现在我稍微修改了那里的代码,看看它是如何以及发生了什么,并立即出现了#34;错误#34;包在我身上。代码确实可以正常工作并且显示效果很好 - 但不是我希望的显示效果。
我试图用以下坐标(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);
}
}
答案 0 :(得分:0)
根据您的Matrix代码,您看起来正在构建一个查看< -3,0,0>的View Matrix。从< 0,0,0>开始,鉴于你的三角形平放在x-y平面上,实际上你看到任何东西都是奇迹般的奇迹。因此,您需要修改构建查看矩阵的方式。我还建议你将三角形按顶点着色,这样就可以更容易地判断三角形的顶点在变换后的位置。