【问题标题】:Android GLES20.glDrawArrays(GLES20.GL_LINE_LOOP ...)Android GLES20.glDrawArrays(GLES20.GL_LINE_LOOP ...)
【发布时间】:2012-04-16 17:04:26
【问题描述】:

这可能是一个非常新手的问题,但我找不到答案。

使用来自 google 的简单教程 (http://developer.android.com/resources/tutorials/opengl/opengl-es20.html)

我试图画一个正方形(呃,使用常识?),但得到了有趣的结果。如果有人能告诉我出了什么问题,将不胜感激。

   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");
}

谢谢!

【问题讨论】:

  • 对于初学者来说,你的坐标不是正方形;它们形成类似于星际迷航符号的东西。你还看到了哪些有趣的结果?
  • 糟糕,忘记了,这只是在三角形中添加第四个坐标。我得到的结果是
  • 我没有编译和测试,但似乎您可能希望将 glVertexAttribPointer 调用中的 size 参数从 4 更改为 3,因为您没有使用 4d 向量 (XYZW) .
  • 谢谢你,马克。这已经解决了这个问题。看起来我把尺寸误认为是 VB 尺寸了。

标签: android opengl-es


【解决方案1】:

正如我在上面的 cmets 中发布的:

您似乎想将 glVertexAttribPointer 调用中的 size 参数从 4 更改为 3,因为您没有使用 4d 向量 (XYZW)。

干杯

【讨论】:

    猜你喜欢
    • 2018-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 2017-05-13
    • 2016-06-04
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    相关资源
    最近更新 更多