万事须己运,他得非我贤。这篇文章主要讲述如何在Android上将OpenGL ES 1.0代码转换为OpenGL Es 2.0?相关的知识,希望能为你提供帮助。
【如何在Android上将OpenGL ES 1.0代码转换为OpenGL Es 2.0()】我有一个用OpenGL ES 1.0编写的代码,使用GL_LINE_STRIP
绘制一条20行。我想将代码升级到OpenGL ES 2.0。代码分为3类:
主要活动
MyGLRenderer
形状
我该怎么办?如何设置fragShader
和vertShader
等?请帮忙
以下是代码:
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}
}
和形状=
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);
}}
谢谢
答案有很多方法可以记下这段代码。这是我的建议,因为我之前遇到过这个问题。 你的
mainActivity
不需要太多变化。只需更改此行即可将OpenGL ES版本更改为版本2.0:surface.setEGLContextClientVersion(2);
正如您所提到的,您还需要两个类。 MyGLRenderer
和Shape
,我将他们在这里Renderer
和Shader
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中,因此您需要使用其他方法)
- 转换指针和客户端状态以对应着色器属性
- 在着色器中添加其他参数,例如颜色
推荐阅读
- 在Android上将Canvas移植到OpenGL
- 如何在android上定义glsl的“time”参数
- 使用OpenGL ES 2.0在Android上创建Audio wave
- PixelBuffer对象和Android上的glReadPixel(ARCore)阻塞
- 如何在Android上的GvrView中显示2D图像()
- 如何确定Android OpenGL ES上的最大纹理内存
- 如何在android中实现Drag Distortion Image过滤器()
- 没有引擎的Android 2D游戏开发
- 在OpenGL ES(Android)中使用带纹理的索引缓冲区有什么意义吗()