2012-07-18 36 views
-2

我是新的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); 

現在我沒有得到任何錯誤,但!!!!我的三角沒有畫畫!

+1

不要把這個作爲一個粗魯的評論,但是......如果你不知道_how到DEBUG_代碼,也許你應該嘗試找出原因? – 2012-07-18 08:59:54

+1

正如J.Steen所建議的,你應該真的能夠調試你的代碼。如果您不知道如何使用您選擇的IDE調試代碼,則應考慮切換IDE!你爲什麼使用Netbeans?我從來沒有與Netbeans合作過,但對於Android開發,我會推薦Eclipse(尤其是如果你是新手)。有很多使用Eclipse的Android開發(官方)教程,它有一個體面的調試器,使用非常簡單! – kroneml 2012-07-18 13:15:43

+0

我同意kroneml,日食就像一個魅力和調試是非常愉快的與它 – teamalpha5441 2012-07-19 07:14:03

回答

0

你在你的FloatBuffer浮點數分配,並將其轉換爲一個陣列。 然後你填充你的數組,但不是你的FloatBuffer,也許這是錯誤。

float[] buffer = new float[] { 
... 
}; 
vertexBuffer.put(buffer); 
vertexBuffer.position(0); 

我不確定,也許.array()返回一個可更改的引用,也許它克隆數據。 只需嘗試一下!

+0

好吧,我會盡量讓你知道。 – Enriquillo 2012-07-18 18:13:32

+0

我試過了,但沒有運氣! – Enriquillo 2012-07-18 23:55:34

+0

什麼不工作? – teamalpha5441 2012-07-19 07:16:06