我们有一个以正交模式运行的大部分 2D 游戏,但其中一部分显示了在其他 2D 对象之间渲染的 3D 模型。如何切换到透视模式,渲染该模型,然后切换回以正交模式渲染其他对象?
如果你能展示它是如何在 OpenGL ES 中完成的,那就太棒了。
我们有一个以正交模式运行的大部分 2D 游戏,但其中一部分显示了在其他 2D 对象之间渲染的 3D 模型。如何切换到透视模式,渲染该模型,然后切换回以正交模式渲染其他对象?
如果你能展示它是如何在 OpenGL ES 中完成的,那就太棒了。
我认为这不是完全指定的问题。你想要更多的观点吗?或者你想要 2D 背景、3D 游戏对象、2D gui。如果你想要这个,那么:
或者你想要别的东西?
编辑:
这是小代码:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,near,far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(pos.x,...);
DrawQuads();
//if you want to keep your previus matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPerspective(90,width/(float)height,0.001,1000);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glTranslatef(pos.x,...);
glRotate(-45.0f,1,0,0);
glTranslate(0,0,distance from object);
glRotate(90.0f,1,0,0);
// or use gluLookAt
// 0,0,1 - if you want have z as up in view
// 0,1,0 - for y
//gluLookAt(pos.x,pos.y,pos.z,cam.x,cam.y,cam.z,0,0,1);
glScale(object.width/model.width,...);
DrawModel();
// Restore old ortho
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
好吧,“做吧”
...这可以再次进行
如果您像看起来那样知道对象的顺序,您还可以在每次渲染之间清除 z 缓冲区。
我同意以前的帖子,我认为更一般的情况是 3D 对象和 2D gui。只是为了再次强调。:)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0f, (GLfloat)s_width/(GLfloat)s_height, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// render 3D object
glUseProgram(modelProgram);
glSetUniformMat(glGetUniformLocation(model.mvp, "mvp"), mvpMat);
glBindVertexArray(model.vao);
glDrawArrays(GL_TRIANGLES, 0, model.size);
glUseProgram(0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// draw GUI
renderGUI();