Android OpenGl 2.0。三角形不绘图

时间:2016-07-16 15:17:56

标签: java android opengl-es

我已经尝试了一切,看着每个人都尝试过每一个解决方案都没有用。我需要绘制一个三角形,我的Surface工作正常。使用的灰色,三角形构造中使用的日志,三角形绘制和所有渲染方法。

MainActivity

import android.content.Context;

import android.opengl.GLES20;

import android.opengl.GLSurfaceView;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

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

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

public class MainActivity extends AppCompatActivity {

    private GLEngine glEngine;
    public static final String LOG = "testing";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glEngine = new GLEngine(this);

        setContentView(glEngine);
    }

    class GLEngine extends GLSurfaceView {

        private final GLRenderer mGLRenderer;


        public GLEngine(Context context) {
            super(context);
            setEGLContextClientVersion(2);
            mGLRenderer = new GLRenderer();

            setRenderer(mGLRenderer);
        }
    }
}

三角

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

    final int COORDS_PER_POINT = 3;
    float triangleCoords[] = {
            0.0f,0.66f,0.0f
            -0.5f,-0.33f,0.0f,
            0.5f,-0.33f,0.0f
    };
    float color[] = {0.63f,0.76f,0.22f,1.0f};

    private final int mProgram;
    private FloatBuffer vertexBuffer;
    private int mPositionHandle;
    private int mColorHandle;
    private final int vertexCount ;
    private final int vertexStride;


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

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

        int vertexShader = GLRenderer.loadShader(
                GLES20.GL_VERTEX_SHADER,
                vertexShaderCode
        );
        int fragmentShader = GLRenderer.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.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(mPositionHandle,
                COORDS_PER_POINT,
                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);

    }

}

GLRenderer

class GLRenderer implements GLSurfaceView.Renderer{
    private Triangle mTriangle;


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


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

        Log.i(MainActivity.LOG,"OnSurfaceCreated");

    }
    public void onDrawFrame(GL10 unused){

        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT| GL10.GL_DEPTH_BUFFER_BIT);
        mTriangle.draw();
        Log.i(MainActivity.LOG,"OnDrawFrame");


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

        Log.i(MainActivity.LOG,"OnSurfaceChanged");
    }
}

0 个答案:

没有答案