Android Open GL不是绘图方块

时间:2014-02-19 04:20:14

标签: java android opengl-es

我一直在关注Android Developer Open GL教程,我只是试图在自己的程序中绘制一个正方形,因为我开始在Open GL中重写我的Android游戏而不是使用Canvas类。我遇到了一个问题,它不会绘制我想要绘制的方块,但它会为背景着色。

我正在尝试绘制从GLEntity类扩展的GLSquareEntity类,下面是代码

GLSquareEntity Class:

public class GLSquareEntity extends GLEntity
{

    public GLSquareEntity() 
    {
        super();

        cords = new float[] {
                -0.5f,  0.5f, 0.0f,   // top left
                -0.5f, -0.5f, 0.0f,   // bottom left
                 0.5f, -0.5f, 0.0f,   // bottom right
                 0.5f,  0.5f, 0.0f }; // top right

        CORDS_PER_VERTEX = 3;

        drawOrder = new short[] { 0, 1, 2, 0 , 2, 3 };

        // Initlise the class
        setup();
    }

}

GLEntity Class:

public class GLEntity 
{
    protected final String TAG = "GLENTITY";

    // Shaders
    private final String vertexShaderCode =
            "uniform mat4 uMVPMatrix;" +
            "attribute vec4 vPosition;" +
            "void main() {" +
            "  gl_Position = uMVPMatrix * vPosition;" +
            "}";

    private final String fragmentShaderCode =
            "precision mediump float;" +
            "uniform vec4 vColor;" +
            "void main() {" +
            "  gl_FragColor = vColor;" +
            "}";

    // Open GL Variables
    protected FloatBuffer vertexBuffer;
    protected ShortBuffer drawListBuffer;
    protected short[] drawOrder;
    protected int CORDS_PER_VERTEX;
    protected int vertexStride;
    protected int  vertexCount;
    protected float[] cords; 

    // Just unill i can get an image to display
    protected float[] color = {0.0f, 0.0f, 1.0f, 1.0f};

    protected int program;
    protected int positionHandle;
    protected int colorHandle;

    public GLEntity()
    {

    }

    protected void setup()
    {
        Log.d(TAG, "GLEntity Setup");

        vertexStride = CORDS_PER_VERTEX * 4;
        vertexCount = cords.length / CORDS_PER_VERTEX;

        // Initilise buffer for vertex cordernats
        ByteBuffer vbuffer = ByteBuffer.allocateDirect(cords.length * 4);
        vbuffer.order(ByteOrder.nativeOrder());
        vertexBuffer = vbuffer.asFloatBuffer();
        vertexBuffer.put(cords);
        vertexBuffer.position(0);

        // Initilise buffer for the draw list
        ByteBuffer dlbuffer = ByteBuffer.allocateDirect(drawOrder.length * 2);
        dlbuffer.order(ByteOrder.nativeOrder());
        drawListBuffer = dlbuffer.asShortBuffer();
        drawListBuffer.put(drawOrder);
        drawListBuffer.position(0);

        // Set up the shaders

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

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

    public void draw()
    {
        //Log.d(TAG, "Drawing GL Entity");
        // Select shader program to use
        GLES20.glUseProgram(program);

        positionHandle = GLES20.glGetAttribLocation(program, "vPosition");

        GLES20.glEnableVertexAttribArray(positionHandle);

        // Prepar the data
        GLES20.glVertexAttribPointer(positionHandle, CORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);

        // Prepare and set the color
        colorHandle = GLES20.glGetUniformLocation(program, "vColor");
        GLES20.glUniform4fv(colorHandle, 1, color, 0);

        //GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertexCount);
        GLES20.glDrawElements(GLES20.GL_TRIANGLES, drawOrder.length, GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

        GLES20.glDisableVertexAttribArray(positionHandle);
    }

}

接下来的类是GLSurfaceView类和GLRenderer类

GLSurfaceView类:

public class OpenGLSurfaceView extends GLSurfaceView 
{

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

        // Set to OpenGL ES 2
        setEGLContextClientVersion(2);

        // Set the renderer for drawing
        //OpenGLRenderer glRenderer = ;
        setRenderer( new OpenGLRenderer() );

        // Set to only rerender when function called
        setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

}

OpenGLRenderer类:

public class OpenGLRenderer implements GLSurfaceView.Renderer 
{
    GLPlayer player;

    @Override
    public void onDrawFrame(GL10 gl) 
    {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        player.draw();
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) 
    {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) 
    {
        GLES20.glClearColor(0.0f, 1.0f, 0.0f, 1.0f);

        player = new GLPlayer();

    }

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

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

        return shader;
    }

}

任何帮助搞清楚问题是什么都会非常感激:) 感谢

0 个答案:

没有答案