4

我们有一个以正交模式运行的大部分 2D 游戏,但其中一部分显示了在其他 2D 对象之间渲染的 3D 模型。如何切换到透视模式,渲染该模型,然后切换回以正交模式渲染其他对象?

如果你能展示它是如何在 OpenGL ES 中完成的,那就太棒了。

4

3 回答 3

4

我认为这不是完全指定的问题。你想要更多的观点吗?或者你想要 2D 背景、3D 游戏对象、2D gui。如果你想要这个,那么:

  • 渲染全屏背景
  • 将视口设置为 position=obj.pos-obj.size/2, size=obj.size, 渲染对象
  • 渲染 2D 图形用户界面

或者你想要别的东西?

编辑:

这是小代码:

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);
于 2010-08-08T22:15:31.667 回答
2

好吧,“做吧”

  • 将投影矩阵设置为正交
  • 为 2D 对象设置模型视图
  • 渲染您的 2D 对象
  • 将投影矩阵设置为投影
  • 为 3D 对象设置模型视图
  • 渲染您的 3D 对象

...这可以再次进行

  • 和交换缓冲区。

如果您像看起来那样知道对象的顺序,您还可以在每次渲染之间清除 z 缓冲区。

于 2010-08-09T08:21:25.783 回答
0

我同意以前的帖子,我认为更一般的情况是 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();
于 2016-10-17T18:28:40.560 回答