私はOpenGLとAndroidを初めて使用しますが、コードが機能せず、理由がわかりません。NetbeansとAVD(android 4.0.3)を使用してコードをデバッグする方法がわからないので、助けを求めます。
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);
エラーは発生しませんが!!!! 私の三角形は描画されませんでした!