游戏开发:OpenGlContext错误问题 [英] Game development: OpenGlContext error issue

查看:209
本文介绍了游戏开发:OpenGlContext错误问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发Android游戏,并获得 OpenGlContext误差,有什么建​​议克服一样吗?
下面是我的code:

 公共类GLView扩展SurfaceView实现SurfaceHolder.Callback
{
私人OpenGLContext CTX;
私人Tunnel3D隧道;
私人布尔创建的;
私人GL10 GL;
私人诠释瓦;
私人诠释H;
私人BMP位图;
私人诠释TEX;
公共GLView(上下文的背景下)
{
//父...
超级(上下文);
getHolder().addCallback(本);//内部成员..
CTX =新OpenGLContext(OpenGLContext.DEPTH_BUFFER);
GL =(GL10)ctx.getGL();
隧道=新Tunnel3D(10,20);
创建= FALSE;//启用状态...
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);//载入纹理...
BMP = BitmapFactory.de codeResource(context.getResources(),R.drawable.plants03);
TEX = loadTexture(GL,BMP);
}公共布尔surfaceCreated(SurfaceHolder持有人)
{
同步(本)
{
创建= TRUE;
}
返回true;
}公共无效surfaceDestroyed(SurfaceHolder持有人)
{
同步(本)
{
创建= FALSE;
}
}公共无效surfaceChanged(SurfaceHolder架,INT格式,诠释W,INT高)
{
同步(本)
{
this.w = W;
this.h = H;
}
}公共无效渲染()
{
//检查创建的标志......
布尔C = FALSE;
同步(本)
{
C =创造的;
}
如果回报(C!);//开始表面座...
SurfaceHolder SH = getHolder();
帆布G = sh.lockCanvas();//挂钩GL的观点...
ctx.makeCurrent(克,NULL);//设置突起...
流通股比例=(浮点)W / H;
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0,0,W,H);
GLU.gluPerspective(GL,45.0f,((浮点)W)/小时,1F,100F);//设置模型视图...
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();//清除z缓冲区...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);//渲染隧道...
tunnel.render(GL,-1.6f);
tunnel.nextFrame();// OpenGL的光洁度
gl.glFlush();
gl.glFinish();//钩完成
ctx.waitGL();// END表面座...
sh.unlockCanvasAndPost(G);
}私人诠释loadTexture(GL10 GL,BMP位图)
{
ByteBuffer的BB = ByteBuffer.allocateDirect(bmp.height()* bmp.width()* 4);
bb.order(ByteOrder.nativeOrder());
IntBuffer IB = bb.asIntBuffer();对于(INT Y = 0; Y< bmp.height(); Y ++)
为(中间体X = 0; X&下; bmp.width(); X ++){
ib.put(bmp.getPixel(X,Y));
}
ib.position(0);
bb.position(0);INT [] tmp_tex =新INT [1];gl.glGenTextures(1,tmp_tex,0);
INT特克斯= tmp_tex [0];
gl.glBindTexture(GL10.GL_TEXTURE_2D,TEX);
gl.glTexImage2D(GL10.GL_TEXTURE_2D,0,GL10.GL_RGBA,bmp.width(),bmp.height(),0,GL10.GL_RGBA,GL10.GL_UNSIGNED_BYTE,BB);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MIN_FILTER,GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,GL10.GL_TEXTURE_MAG_FILTER,GL10.GL_LINEAR);返回TEX;
}


解决方案

使用Android的 GLSurfaceView ,而不是把自己的包装。但是,如果你真的想使自己的GL的包装,你应该做出正确的实现,你可以找到<一个href=\"http://$c$c.google.com/p/replicaisland/source/browse/trunk/src/com/replica/replicaisland/GLSurfaceView.java\"相对=nofollow>这里。

I am developing game in Android and got OpenGlContext error,any suggestions to overcome the same? Below is my Code:

public class GLView extends SurfaceView implements SurfaceHolder.Callback
{
private OpenGLContext ctx;
private Tunnel3D tunnel;
private boolean created;
private GL10 gl;
private int w;
private int h;
private Bitmap bmp;
private int tex;


public GLView (Context context)
{
// Parent...
super (context);
getHolder ().addCallback (this);

// Internal members..
ctx = new OpenGLContext (OpenGLContext.DEPTH_BUFFER);
gl = (GL10)ctx.getGL ();
tunnel = new Tunnel3D (10, 20);
created = false;

// Enabling the state...
gl.glEnable (GL10.GL_DEPTH_TEST);
gl.glEnable (GL10.GL_TEXTURE_2D);
gl.glEnableClientState (GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState (GL10.GL_COLOR_ARRAY);
gl.glEnableClientState (GL10.GL_TEXTURE_COORD_ARRAY);

// Loading texture...
bmp = BitmapFactory.decodeResource (context.getResources(), R.drawable.plants03);
tex = loadTexture (gl, bmp);
}

public boolean surfaceCreated (SurfaceHolder holder)
{
synchronized (this)
{
created = true;
}
return true;
}

public void surfaceDestroyed (SurfaceHolder holder)
{
synchronized (this)
{
created = false;
}
}

public void surfaceChanged (SurfaceHolder holder, int format, int w, int h)
{
synchronized (this)
{
this.w = w;
this.h = h;
}
}

public void render ()
{
// Check the created flag...
boolean c = false;
synchronized (this)
{
c = created;
}
if (!c) return;

// Start the surface holder...
SurfaceHolder sh = getHolder ();
Canvas g = sh.lockCanvas ();

// Hooking GL with the view...
ctx.makeCurrent (g, null);

// Setting up the projection...
float ratio = (float)w / h;
gl.glMatrixMode (GL10.GL_PROJECTION);
gl.glLoadIdentity ();
gl.glViewport (0, 0, w, h);
GLU.gluPerspective (gl, 45.0f, ((float)w)/h, 1f, 100f);

// Setting up the modelview...
gl.glMatrixMode (GL10.GL_MODELVIEW);
gl.glLoadIdentity ();

// Clear the z-buffer...
gl.glClear (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

// Render the tunnel...
tunnel.render (gl, -1.6f);
tunnel.nextFrame ();

// OpenGL finish
gl.glFlush ();
gl.glFinish ();

// Finish with hook
ctx.waitGL ();

// End the surface holder...
sh.unlockCanvasAndPost (g);
}

private int loadTexture (GL10 gl, Bitmap bmp)
{
ByteBuffer bb = ByteBuffer.allocateDirect(bmp.height()*bmp.width()*4);
bb.order(ByteOrder.nativeOrder());
IntBuffer ib = bb.asIntBuffer();

for (int y=0;y<bmp.height();y++)
for (int x=0;x<bmp.width();x++) {
ib.put(bmp.getPixel(x,y));
}
ib.position(0);
bb.position(0);

int[] tmp_tex = new int[1];

gl.glGenTextures(1, tmp_tex, 0);
int tex = tmp_tex[0];
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_RGBA, bmp.width(), bmp.height(), 0, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

return tex;
}

解决方案

Use Android's GLSurfaceView instead of making your own wrapper. However, if you really want to make your own GL wrapper you should make a correct implementation, which you can find here.

这篇关于游戏开发:OpenGlContext错误问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆