Opengl es Android简单三角形没有显示

时间:2014-04-15 18:48:05

标签: android opengl-es

我正在关注"构建一个OpenGLES环境"文档。我只是出现了一个三角形。应用程序运行但三角形不存在。

主要活动

package com.example.testopengl;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {

    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mGLView = new MyGLSurfaceView(this);
        setContentView (mGLView);

}


}

渲染器类

package com.example.testopengl;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.egl.EGLConfig;

import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

public class MyGLRenderer implements GLSurfaceView.Renderer {

    private Triangle mTriangle;


    public void onSurfaceCreated (GL10 unused, EGLConfig config) {
        GLES20.glClearColor(0.4f, 0.4f, 0.4f, 0.4f);
        mTriangle = new Triangle();

    }

    public void onDrawFrame (GL10 unused) {
    GLES20.glClear (GLES20.GL_COLOR_BUFFER_BIT);
    }

    public void onSurfaceChanged (GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        mTriangle.draw();
    }

    public static int loadShader (int type, String shaderCode) {
        int shader = GLES20.glCreateShader(type);

        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }

}

Surface View类

package com.example.testopengl;

import android.content.Context;
import android.opengl.GLSurfaceView;

public class MyGLSurfaceView extends GLSurfaceView {


    public MyGLSurfaceView (Context context) {
        super (context);

        setEGLContextClientVersion(2);

        setRenderer(new MyGLRenderer());

        setRenderMode (GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

}

三角课

package com.example.testopengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES20;

public class Triangle {

    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;
    private final int mProgram;
    private int mPositionHandle;
    private int mColorHandle;

    static final int COORDS_PER_VERTEX = 3;
    static float triangleCoords[] = {
        0.0f, 0.622008459f, 0.0f,
        -0.5f, -0.311004243f, 0.0f,
        0.5f, -0.311004243f, 0.0f
    };

    private final int vertexCount = triangleCoords.length / COORDS_PER_VERTEX;
    private final int vertexStride = COORDS_PER_VERTEX * 4;

    float color [] = { 0.63671875f, 0.76953125f, 0.22265625f, 1.0f};

    public Triangle() {
        ByteBuffer bb = ByteBuffer.allocateDirect (
                triangleCoords.length * 4);
        bb.order(ByteOrder.nativeOrder());

        vertexBuffer = bb.asFloatBuffer();
        vertexBuffer.put(triangleCoords);
        vertexBuffer.position(0);

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

        mProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, vertexShader);
        GLES20.glAttachShader(mProgram, fragmentShader);
        GLES20.glLinkProgram(mProgram);

    }

    public void draw() {
        GLES20.glUseProgram(mProgram);
        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
        mColorHandle = GLES20.glGetUniformLocation(mProgram, "vcolor");
        GLES20.glUniform4fv(mColorHandle, 1, color, 0);
        GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
        GLES20.glDisableVertexAttribArray(mPositionHandle);
    }

}

1 个答案:

答案 0 :(得分:1)

主要问题是渲染器中的onDrawFrame()方法只能清除,但不会渲染任何内容。您的mTriangle.draw()来电应该在onDrawFrame()而非onSurfaceChanged()

我相信你也错过了对glEnableVertexAttribArray()的电话。