AIDE OpenGL es 2.0三角形不会渲染

时间:2018-06-08 17:41:10

标签: android opengl-es-2.0

你好,这是我第一次使用这个网站,第一次使用OpenGL es 2.0。 所以我的问题是我正在使用AIDE并试图学习如何使用OpenGL es 2.0我现在只是不知所措并且碰到了一堵砖墙。编译器没有显示错误,应用程序本身也不会崩溃。它只是不会呈现一个三角形。我已经尝试了我能想到的一切。看了几个例子。什么都行不通。 所以这是我的代码。 (完全没有扯掉Android开发者网站)

f0xTriangle

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的原因是因为它是我目前唯一能用来编码的东西。没有电脑给我......叹气我希望它不是编译器。

1 个答案:

答案 0 :(得分:2)

将着色器对象附加到程序对象时,您交换了参数。 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 - 将着色器对象附加到程序对象

void glAttachShader( GLuint program, GLuint shader);
  

参数

     
      
  • program指定着色器对象将附加到的程序对象。
  •   
  • shader指定要附加的着色器对象。
  •   


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

检查OpenGL错误