如何将OpenGL ES 1.0代码转换为Android上的OpenGL ES 2.0



我使用openGL ES 1.0编写的代码使用GL_LINE_STRIP绘制20分。我想将代码升级到OpenGL ES 2.0。代码分为三个类:
MADACTIVITY
myglrenderer
shape

我该怎么办?如何设置fragShadervertShader等?请帮助
这是代码:
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
    }
}


和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

有很多方法可以写下此代码。这是我以前面对这个问题时的建议。
您的mainActivity不需要太多更改。只要您必须通过更改此行来将OpenGL ES版本更改为2.0版:surface.setEGLContextClientVersion(2);

如您所提到的,您需要另外两个类。MyGLRendererShape我将在此处RendererShader Clasees。您的Shader类应具有vertcode字符串和fragcode字符串。您的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;
    }
}

现在,您必须设置Renderer类,包括您的点。公共

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

es1.x和es2.x截然不同。如果您正在绘制一条线路,而ES1足以适合您,那么我的建议是"不要转换"。

如果不可能,那么我建议您宁愿在ES2中启动一个新的项目/模块,并使用一些可以帮助您整个过程的教程。根据您发布的内容,应该足够快。

然后简单地修改教程项目以执行您需要的事情,或者开始从教程将逻辑复制到您的案例进行转换。

从您的代码中,一些方法可能已经改变,但总的来说,您需要做的是:

  • 添加着色器(代码,编译和使用)
  • 将矩阵方法移动到着色器(矩阵不超出GL,因此您需要使用其他方法)
  • 将指针和客户状态转换为对应着色器属性
  • 在着色器中添加其他参数,例如颜色

相关内容

  • 没有找到相关文章

最新更新