我是新的OpenGL和Android和我的代碼不工作,我不知道爲什麼。我不知道如何使用Netbeans和AVD(android 4.0.3)來調試代碼,所以我請求你的幫助。Android的OpenGL的錯誤
package com.egg.game.test;
import android.opengl.GLES20;
import java.nio.FloatBuffer;
public class glTriangle {
private int mProgram;
private int mPositionHandle;
private int mColorHandle;
private static int COORDS_PER_VERTEX = 3;
private int vertexStride = Float.SIZE * COORDS_PER_VERTEX;
private int vertexCount;
private FloatBuffer vertexBuffer;
private float[] color;
private final String vertexShaderCode =
"attribute vec4 vPosition;"
+ "void main() {"
+ " gl_Position = vPosition;"
+ "}";
private final String fragmentShaderCode =
"precision mediump float;"
+ "uniform vec4 vColor;"
+ "void main() {"
+ " gl_FragColor = vColor;"
+ "}";
public glTriangle() {
color = new float[]{.5f, .2f, 0f, 1.0f};
vertexCount = 3;
vertexBuffer = FloatBuffer.allocate(Float.SIZE * COORDS_PER_VERTEX * vertexCount);
float[] buffer = vertexBuffer.array();
buffer[0] = 0f;
buffer[1] = 0f;
buffer[2] = 0f;
buffer[3] = 1f;
buffer[4] = 0f;
buffer[5] = 0f;
buffer[6] = 0f;
buffer[7] = 1f;
buffer[8] = 0f;
int vertexShader = GameRederer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = GameRederer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL ES Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // creates OpenGL ES program executables
}
public void draw() {
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle,
COORDS_PER_VERTEX,
GLES20.GL_FLOAT,
false,
vertexStride,
vertexBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
我改變了緩衝的代碼如下:
vertexBuffer = ByteBuffer.allocateDirect(Float.SIZE * COORDS_PER_VERTEX * vertexCount).order(ByteOrder.nativeOrder()).asFloatBuffer();
vertexBuffer.put(0f);
vertexBuffer.put(0f);
vertexBuffer.put(0f);
vertexBuffer.put(10f);
vertexBuffer.put(0f);
vertexBuffer.put(0f);
vertexBuffer.put(0f);
vertexBuffer.put(10f);
vertexBuffer.put(0f);
vertexBuffer.position(0);
現在我沒有得到任何錯誤,但!!!!我的三角沒有畫畫!
不要把這個作爲一個粗魯的評論,但是......如果你不知道_how到DEBUG_代碼,也許你應該嘗試找出原因? – 2012-07-18 08:59:54
正如J.Steen所建議的,你應該真的能夠調試你的代碼。如果您不知道如何使用您選擇的IDE調試代碼,則應考慮切換IDE!你爲什麼使用Netbeans?我從來沒有與Netbeans合作過,但對於Android開發,我會推薦Eclipse(尤其是如果你是新手)。有很多使用Eclipse的Android開發(官方)教程,它有一個體面的調試器,使用非常簡單! – kroneml 2012-07-18 13:15:43
我同意kroneml,日食就像一個魅力和調試是非常愉快的與它 – teamalpha5441 2012-07-19 07:14:03