0

I have a code written in OpenGL ES 1.0 drawing a line by 20 points using GL_LINE_STRIP. I want to upgrade code to OpenGL ES 2.0. The codes are in 3 classes:
mainActivity
MyGLRenderer
Shape

What should I do? How set fragShader and vertShader and others? Please help
Here are the codes:
mainActivity=

public class mainActivuty extends Activity {
    private GLSurfaceView surface;
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        surface = new GLSurfaceView(this);
        surface.setEGLContextClientVersion(1);
        surface.setRenderer(new MyGLRenderer());

        setContentView(surface);
    }

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

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


MyGLRenderer=

public class MyGLRenderer implements Renderer {
    Shape s;

    public MyGLRenderer() {
        s = new Shape();
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        gl.glClearColor(0.0f, 0.0f, 0.5f, 0.5f);
    }

    public void onDrawFrame(GL10 gl) {
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
        gl.glLoadIdentity();
        gl.glTranslatef(0, 0, -8);
        s.draw(gl);
    }

    public void onSurfaceChanged(GL10 gl, int width, int height) {
        gl.glViewport(0, 0, width, height);
        gl.glMatrixMode(GL10.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
                100.0f);
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    @Override
    public void onSurfaceCreated(GL10 arg0, javax.microedition.khronos.egl.EGLConfig arg1) {
        // TODO Auto-generated method stub

    }
}


and Shape=

public class Shape {

    float vertices[] =  {
            -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
            }; 
            private short[] indices = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

            FloatBuffer vertexBuffer;
            ShortBuffer indexBuffer;
            public Shape() {
            ByteBuffer bb = ByteBuffer.allocateDirect(vertices.length * 4);
            bb.order(ByteOrder.nativeOrder());
            vertexBuffer = bb.asFloatBuffer();
            vertexBuffer.put(vertices);
            vertexBuffer.position(0);

            ByteBuffer bb2=ByteBuffer.allocateDirect(indices.length*2);
            bb2.order(ByteOrder.nativeOrder());
            indexBuffer=bb2.asShortBuffer();
            indexBuffer.put(indices);
            indexBuffer.position(0);
            }

            public void draw(GL10 gl) {
            gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
            gl.glDrawElements(GL10.GL_LINE_STRIP,indices.length,GL10.GL_UNSIGNED_SHORT,indexBuffer);
            gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
            }

}

Tahnks

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

2 Answers2

0

The ES1.x and ES2.x are extremely different. If you are drawing a line and ES1 is enough for you then my advice is "don't convert it".

If that is not possible then I suggest you rather start a new project/module in ES2 and use some tutorial that will help you through the process. From what you posted this should be quick enough.

Then simply either modify the tutorial project to do what you need or start copying logic from tutorial to your case to convert it.

From your code a few methods may have changed but in general what you need to do is:

  • Add shader (code, compiling and using)
  • Move matrix methods into shaders (matrices are out of GL so you need to use other methods)
  • Convert pointers and client states to correspond to shader attributes
  • Add other parameters in shader such as colors
Matic Oblak
  • 16,318
  • 3
  • 24
  • 43
  • I have to move on OpenGL ES 2.0. It's more complicated than ES 1. Any way, I searched the net for shader matrix and moving matrix method into shaders but could not find ant thing about drawing line. All about was about drawing triangles. Could you please send some references about `GL_LINE_STRIP` example on OpenGL ES 2.0? thanks alot – Stephen Frosty Dec 19 '17 at 05:14
  • Actually drawing is the same in both versions. You use line strip instead of triangles or whatever is in your example. – Matic Oblak Dec 19 '17 at 07:40
0

There are many ways to write down this code. Here is my suggestion as I faced this problem before.
Your mainActivity does not need much changes. Just you have to change your OpenGL ES version to version 2.0 by changing this line: surface.setEGLContextClientVersion(2);

As you mentioned, you need two more classes. MyGLRenderer and Shape which I will them here Renderer and Shader clasees. Your Shader class should have vertcode string and fragcode string. Your Shader class shuld be like:

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

}

Now you have to set up your Renderer class including you points. public

class Renderer implements GLSurfaceView.Renderer {

    private FloatBuffer vertbuffer;

    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        tut1.Shader.makeprogram();
        GLES20.glEnableVertexAttribArray(tut1.Shader.positionhandle);
        float[] verts = {-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
        }; 
        vertbuffer = makefloatbuffer(verts);
    }

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

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        GLES20.glUniform4f(tut1.Shader.colorhandle, 1.0f, 0.0f, 0.0f, 1.0f);
        GLES20.glVertexAttribPointer(tut1.Shader.positionhandle, 3, GLES20.GL_FLOAT, false, 0, vertbuffer);
        GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, 3);
    }

    public FloatBuffer makefloatbuffer(float[] array) {
        FloatBuffer floatbuff = ByteBuffer.allocateDirect(array.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
        floatbuff.put(array).position(0);
        return floatbuff;
    }

}
MJB
  • 65
  • 13