如何在Android上的OpenGL ES 2.0中画一条线?

时间:2017-12-17 10:27:14

标签: android opengl-es-2.0

我是OpneGL ES 2.0的新手。我已经阅读了一些关于它的文章和成功绘制三角形的android程序。现在我想编写一个简单的应用程序来绘制这一点的浪潮:

{-3.14, -0.00159265}, {-2.826, -0.31038}, {-2.512, -0.588816}, {-2.198, -0.809672}, {-1.884, -0.951351}, {-1.57, -1.}, {-1.256, -0.950859}, {-0.942, -0.808736}, {-0.628, -0.587528}, {-0.314, -0.308866}, {4.44089*10^-16, 4.44089*10^-16}, {0.314, 0.308866}, {0.628, 0.587528}, {0.942, 0.808736}, {1.256,0.950859}, {1.57, 1.}, {1.884, 0.951351}, {2.198, 0.809672}, {2.512,0.588816}, {2.826, 0.31038}, {3.14, 0.00159265} 

我创建了我的主要Activity类:

public class MainActivity extends Activity {

    private GLSurfaceView surface;
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        surface = new GLSurfaceView(this);
        surface.setEGLContextClientVersion(2);
        surface.setRenderer(new Renderer());

        setContentView(surface);
    }

    public void onPause(){
        super.onPause();
        surface.onPause();      
    }

    public void onResume() {
        super.onResume();
        surface.onResume();
    }
}

Android上OpenGL ES 2.0中的渲染器和线类应该是什么? 非常感谢

1 个答案:

答案 0 :(得分:0)

我们必须写另外两个类:第一个着色器类:
公共课Shader

public class Shader {

    private final static String vertcode = "attribute vec4 a_pos;"+
            "void main(){"+
            "gl_Position = a_pos;" +
            "}";

    private final static String fragcode = "precision mediump float;" +
            "uniform vec4 u_color;"+
            "void main(){"+
            "gl_FragColor = u_color;"+
            "}";

    private static int program;
    public static int positionhandle;
    public static int colorhandle;

    public static void makeprogram()
    {
        int vertexShader   = loadShader(GLES20.GL_VERTEX_SHADER, vertcode);
        int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragcode);

        program = GLES20.glCreateProgram();

        GLES20.glAttachShader(program, vertexShader);
        GLES20.glAttachShader(program, fragmentShader);

        GLES20.glLinkProgram(program);

        positionhandle = GLES20.glGetAttribLocation(program,"a_pos");
        colorhandle = GLES20.glGetUniformLocation(program, "u_color");


        GLES20.glUseProgram(program);

    }

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

}

另一个类名为RendererRenderer课程应该包含您的积分坐标。
请不要将您的积分格式化为:

float vertices[] =  {
            0f,0f,0f,
            -3.14f, -0.00159265f, 0f,
            -2.826f, -0.31038f, 0f,
            -2.512f, -0.588816f, 0f,
            -2.198f, -0.809672f, 0f,
            -1.884f, -0.951351f, 0f,
            -1.57f, -1f, 0f,
            -1.256f, -0.950859f, 0f,
            -0.942f, -0.808736f, 0f,
            -0.628f, -0.587528f, 0f,
            -0.314f, -0.308866f, 0f,
            0f, 0f, 0f,
            0.314f, 0.308866f, 0f,
            0.628f, 0.587528f, 0f,
            0.942f, 0.808736f, 0f,
            1.256f, 0.950859f, 0f,
            1.57f, 1f, 0f,
            1.884f, 0.951351f, 0f,
            2.198f, 0.809672f, 0f,
            2.512f, 0.588816f, 0f,
            2.826f, 0.31038f, 0f,
            3.14f, 0.00159265f, 0f
            };