AIDE OpenGL es 2.0 三角形无法渲染

AIDE OpenGL es 2.0 triangle will not render

您好,这是我第一次使用这个网站,也是我第一次使用 OpenGL es 2.0。 所以我的问题是我正在使用 AIDE 并试图学习如何使用 OpenGL es 2.0 我现在不知所措并且碰壁了。编译器没有显示任何错误,应用程序本身也没有崩溃。它只是不会渲染一个该死的三角形。我已经尝试了我能想到的一切。看了多个例子。什么都不起作用。 所以这是我的代码。 (完全没有盗用 android 开发者网站)

f0x三角形

public class f0xTriangle
{
    private final int program;
    private final int vertexCount = triangleCoords.length / 
COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4;

    private int positionHandle;
    private int colorHandle;

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

private FloatBuffer vertexBuffer;
static final int COORDS_PER_VERTEX = 3;
static float triangleCoords[] = { //in counter clockwise order
    0.0f, 0.622008459f, 0.0f, //top
    -0.5f, 0.311004243f, 0.0f, //bottom left
    0.5f, 0.311004243f, 0.0f  //bottom right
};

// Set color with red, green, blue and alpha (opacity) values
float color[] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f };

public f0xTriangle(){
    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());
    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
    int vertexShader = f0xGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = f0xGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    program = GLES20.glCreateProgram();
    GLES20.glAttachShader(vertexShader, program);
    GLES20.glAttachShader(fragmentShader, program);
    GLES20.glLinkProgram(program);
}

public void draw(){
    GLES20.glUseProgram(program);
    positionHandle = GLES20.glGetAttribLocation(program, "vPosition");
    GLES20.glEnableVertexAttribArray(positionHandle);
    GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX,
                                GLES20.GL_FLOAT, false,
                                vertexStride, vertexBuffer);
    colorHandle = GLES20.glGetUniformLocation(program, "vColor");
    GLES20.glUniform4fv(colorHandle, 1, color, 0);
    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
    GLES20.glDisableVertexAttribArray(positionHandle);
}
}

f0xGLRenderer

public class f0xGLRenderer implements GLSurfaceView.Renderer
{
    private f0xTriangle triangle;

    public void onSurfaceCreated(GL10 p1, EGLConfig config)
    {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        triangle = new f0xTriangle();
    }
    public void onDrawFrame(GL10 p1)
    {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        triangle.draw();
    }
    public void onSurfaceChanged(GL10 p1, int width, int height)
    {
        GLES20.glViewport(0, 0, width, height);
    }
    public static int loadShader(int type, String shaderCode) {
        int shader = GLES20.glCreateShader(type);
        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);
        return shader;
    }
}

f0xGLSurfaceView

public class f0xGLSurfaceView extends GLSurfaceView
{
    private final f0xGLRenderer f0xrender;

    public f0xGLSurfaceView(Context context) {
        super(context);
        setEGLContextClientVersion(2);
        f0xrender = new f0xGLRenderer();
        setRenderer(f0xrender);
        setRenderMode(f0xGLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }
}

OpenGl2Activity

public class OpenGL2Activity extends Activity
{
    private f0xGLSurfaceView glView;

    @Override
    public void onCreate(Bundle savedInstantStates) {
        super.onCreate(savedInstantStates);
        glView = new f0xGLSurfaceView(this);
        setContentView(glView);
    }
}

显然我不知道发生了什么。任何指示和建议将不胜感激。谢谢你。 另一方面,我使用 AIDE 的原因是因为它是我目前唯一可以用来编码的东西。我没有电脑……唉,希望不是编译器吧。

将着色器对象附加到程序对象时交换了参数。 glAttachShader的第一个参数是程序对象,第二个是着色器对象:

GLES20.glAttachShader(vertexShader, program);   // <--- this parameters have to be swapped
GLES20.glAttachShader(fragmentShader, program); // <--- this parameters too

参见 Android developers documentation - glAttachShader:

glAttachShader

public static void glAttachShader (int program, int shader)

Khronos OpenGL ES 2.0 reference page - glAttachShader:

glAttachShader — attach a shader object to a program object

void glAttachShader( GLuint program, GLuint shader);

Parameters

  • program Specifies the program object to which a shader object will be attached.
  • shader Specifies the shader object that is to be attached.


错误使用参数会导致 GL_INVALID_OPERATION 错误,并且没有着色器对象附加到程序对象。
注意,可以通过 glGetError

检查 OpenGL 错误