首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游戏开发: OpenGlContext错误问题

游戏开发: OpenGlContext错误问题
EN

Stack Overflow用户
提问于 2011-07-06 14:51:17
回答 1查看 492关注 0票数 1

我正在开发安卓游戏,得到了OpenGlContext error,有什么建议来克服同样的问题吗?下面是我的代码:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2011-07-06 15:02:20

使用Android的GLSurfaceView,而不是自己制作包装器。然而,如果你真的想做你自己的GL包装器,你应该做一个正确的实现,你可以找到here

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6592555

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档