利用OpenGL更换帆布 - 机器人 [英] Using OpenGL to replace Canvas - Android
问题描述
我试图取代基于canvas的渲染系统,我已经但是有更快的OpenGL-ES面,我似乎无法得到一个OpenGL渲染器以这样的方式,以符合其作为2D场的,而不是一个透视图。
I am attempting to replace the Canvas-based rendering system that I already have with the faster opengl-es surface, however, I can't seem to get an openGL renderer to conform in such a way that it acts as 2d field, rather than a perspective view.
我目前的code渲染器看起来如下:
My current code for the renderer looks as follows:
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0.0f, width, 0.0f, height, 0.0f, 1.0f);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glColor4x(0x10000, 0x10000, 0x10000, 0x10000);
gl.glEnable(GL10.GL_TEXTURE_2D);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glDisable(GL10.GL_DITHER);
gl.glDisable(GL10.GL_LIGHTING);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
}
我将如何设置渲染器,以使平移变换将匹配与屏幕上的像素? (所以5翻译向右侧将移动5个像素)
How would I setup the renderer so that the translate transformation would match up with the pixels on the screen? (so translating 5 to the right would move it 5 pixels)
推荐答案
请注意,在OpenGL中的Y坐标反转。否则,一切都是一样的。照片 至于正确的标志,我建议你看看开源的Android游戏副本岛: HTTP: //$c$c.google.com/p/replicaisland/
Please note that in openGL that Y co-ordinate is inverted. Otherwise all is the same.
As for the correct flags, I recommend you check out the open source android game replica island: http://code.google.com/p/replicaisland/
下面是我在我自己的code使用:
Here's what I use in my own code:
public void onSurfaceChanged(GL10 gl, int width, int height)
{
mViewWidth = width;
mViewHeight = height;
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glShadeModel(GL10.GL_FLAT);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glViewport(0, 0, mViewWidth, mViewHeight);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glShadeModel(GL10.GL_FLAT);
gl.glEnable(GL10.GL_TEXTURE_2D);
GLU.gluOrtho2D(gl, 0, mViewWidth, mViewHeight, 0);
}
其中, mViewWidth
&放大器; mViewHeight
是显示器的尺寸。
Where mViewWidth
& mViewHeight
are the size of the display.
这篇关于利用OpenGL更换帆布 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!