OpenGL ES (7): 绘制三角形

1.初始化三角形Triangle


我们先前已经定义了一个三角形Triangle

public class MyGLRenderer implements GLSurfaceView.Renderer {

    private Triangle mTriangle;

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        ...

        mTriangle = new Triangle();
    }

    ...
}

2.进行绘制前的准备


至少需要3个东西:

  • Vertex Shader - 用于渲染形状的顶点的OpenGLES 图形代码。
  • Fragment Shader - 用于渲染形状的外观(颜色或纹理)的OpenGLES 代码。
  • Program - 一个OpenGLES对象,包含了你想要用来绘制一个或多个形状的shader。

你至少需要一个vertexshader来绘制一个形状和一个fragmentshader来为形状上色。然后被添加到一个 program中,program之后被用来绘制形状。接下来就讲如何得到这3个东西。

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

    ...
}

上面的GLSL代码,必须在使用前编译,为了生成上面对应的Shader,在你的Renderer类中创建一个工具类方法:

public class MyGLRenderer implements GLSurfaceView.Renderer {

    ...

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

}

上面准备工作都做好了:使用流程如下

  1. 编译Shader
  2. 把两个Shader添加到Program
  3. 链接这个Program并使用
public Triangle() {
        ... //float[] → FloatBuffer

        int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
                vertexShaderCode);
        int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
                fragmentShaderCode);
        // create empty OpenGL ES Program
        mProgram = GLES20.glCreateProgram();
        // add the vertex shader to program
        GLES20.glAttachShader(mProgram, vertexShader);
        // add the fragment shader to program
        GLES20.glAttachShader(mProgram, fragmentShader);
        // creates OpenGL ES program executables
        GLES20.glLinkProgram(mProgram);
    }

 

3.真正的绘制


public class MyGLRenderer implements GLSurfaceView.Renderer {

    private Triangle mTriangle;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        mTriangle = new Triangle();
    }

    ...
    @Override
    public void onDrawFrame(GL10 gl) {
//绘制
        mTriangle.draw();

    }

    ...

}
private int mPositionHandle;
private int mColorHandle;

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

public void draw() {
//使用Porgram
    GLES20.glUseProgram(mProgram);
    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
    GLES20.glEnableVertexAttribArray(mPositionHandle);
    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);
}

运行程序可以看到一个直角三角形。

 

 

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页