2012-04-16 136 views
3

這可能是一個非常新手的問題,但我找不到答案。Android GLES20.glDrawArrays(GLES20.GL_LINE_LOOP ...)

使用從谷歌簡單的教程(http://developer.android.com/resources/tutorials/opengl/opengl-es20.html)

我是想畫一個正方形(ERRM,使用常識?),但獲得有趣的結果。如果有人能告訴我什麼是錯的,我將很感激。

private void initShapes(){  
    float triangleCoords[] = { 
     // X, Y, Z 
     -0.5f, -0.25f, 0, 
     0.5f, -0.25f, 0, 
     0.0f, 0.559016994f, 0, 
     0.0f, 0f, 0 
    }; 

    // initialize vertex Buffer for triangle 
    ByteBuffer vbb = ByteBuffer.allocateDirect(
      // (# of coordinate values * 4 bytes per float) 
      triangleCoords.length * 4); 
    vbb.order(ByteOrder.nativeOrder());// use the device hardware's native byte order 
    triangleVB = vbb.asFloatBuffer(); // create a floating point buffer from the ByteBuffer 
    triangleVB.put(triangleCoords); // add the coordinates to the FloatBuffer 
    triangleVB.position(0);   // set the buffer to read the first coordinate  
} 


public void onDrawFrame(GL10 unused) {  
    // Redraw background color 
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT); 

// Add program to OpenGL environment 
    GLES20.glUseProgram(mProgram); 

    GLES20.glVertexAttribPointer(maPositionHandle, 4, GLES20.GL_FLOAT, false, 12, triangleVB); 
    GLES20.glEnableVertexAttribArray(maPositionHandle); 
    GLES20.glDrawArrays(GLES20.GL_LINE_LOOP, 0, 4); 

} 

其他與教程相同的內容。

private final String vertexShaderCode = 
    "attribute vec4 vPosition; \n" + 
    "void main(){    \n" + 
    " gl_Position = vPosition; \n" + 
    "}       \n"; 

private final String fragmentShaderCode = 
    "precision mediump float; \n" + 
    "void main(){    \n" + 
    " gl_FragColor = vec4 (0.63671875, 0.76953125, 0.22265625, 1.0); \n" + 
    "}       \n"; 

private int mProgram; 
private int maPositionHandle; 

private 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; 
} 

public void onSurfaceCreated(GL10 unused, EGLConfig config) { 
    initShapes(); 

    // Set the background frame color 
    GLES20.glClearColor(0f, 0f, 0f, 1.0f); 

    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode); 
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode); 

    mProgram = GLES20.glCreateProgram();    // create empty OpenGL 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 program executables 

    // get handle to the vertex shader's vPosition member 
    maPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition"); 
} 

謝謝!

+0

對於初學者來說,您的座標不會形成正方形;它們形成類似於星際迷航的符號。你看到了什麼其他有趣的結果? – Marc 2012-04-16 17:24:11

+0

糟糕,忘記了這一點,那只是將一個第四座標添加到三角形。我得到的結果是 – 2012-04-16 18:07:02

+0

http://farm8.staticflickr.com/7213/7084661585_071374e9d0_b.jpg就像這樣。 – 2012-04-16 18:13:49

回答

1

正如我在發佈上述評論:

好像你可能想既然你不使用4D向量(XYZW)的尺寸參數從glVertexAttribPointer呼叫改變從4比3。

乾杯

+0

非常感謝! – 2012-04-16 21:56:25

相關問題