在 OpenGL 中绑定创建背景
Tying to create a background in OpenGL
我正在尝试在 OpenGL 游戏中创建背景。这是基于 freeglut 和 OpenGL 即时模式(由于要求)。我的问题是我无法正确显示背景图像,它显示为随相机旋转的细条。这让我相信这是一个定位问题,但我已经尝试更改对象的所有 Z 位置。下面是我的渲染函数,我想绘制背景然后是播放器 "mSpaceShip"
对于某些上下文
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 1200
void GameInstance::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(-SCREEN_WIDTH / 2, SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 100.0f);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(SCREEN_WIDTH, 0);
glVertex2i(SCREEN_WIDTH, SCREEN_HEIGHT);
glVertex2i(0, SCREEN_HEIGHT);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
mSpaceShip->Render();
//for (int i = 0; i < 500; i++) {
// cubeField[i]->Draw();
//}
glFlush();
glutSwapBuffers();
}
尽量不要让自己的事情过于复杂。
void GameInstance::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// the default matricies set the origin to be at the center of the screen, with
// (-1, -1) being at the bottom-left corner, and
// ( 1, 1) being at the top-right.
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// render a full-screen quad without writing to the depth buffer
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glVertex2i(-1, -1);
glVertex2i( 1, -1);
glVertex2i( 1, 1);
glVertex2i(-1, 1);
glEnd();
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
mSpaceShip->Render();
glFlush();
glutSwapBuffers();
}
在您对 gluOrtho2D
的调用中,您将原点设置在屏幕的中心,但只在右上象限内渲染四边形(但是当您更改 GL_MODELVIEW
在 Render()
之外,这只是一个猜测)。
使用 gluOrtho2D
相当于调用 glOrtho
并将 z 裁剪范围设置为 [-1 .. 1]
,因此我怀疑您对 glTranslatef
的调用需要小得多z 值,例如 0.5f
而不是 100.0f
.
我正在尝试在 OpenGL 游戏中创建背景。这是基于 freeglut 和 OpenGL 即时模式(由于要求)。我的问题是我无法正确显示背景图像,它显示为随相机旋转的细条。这让我相信这是一个定位问题,但我已经尝试更改对象的所有 Z 位置。下面是我的渲染函数,我想绘制背景然后是播放器 "mSpaceShip"
对于某些上下文 SCREEN_WIDTH = 1200 SCREEN_HEIGHT = 1200
void GameInstance::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(-SCREEN_WIDTH / 2, SCREEN_WIDTH / 2, -SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(0.0f, 0.0f, 100.0f);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(SCREEN_WIDTH, 0);
glVertex2i(SCREEN_WIDTH, SCREEN_HEIGHT);
glVertex2i(0, SCREEN_HEIGHT);
glEnd();
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
mSpaceShip->Render();
//for (int i = 0; i < 500; i++) {
// cubeField[i]->Draw();
//}
glFlush();
glutSwapBuffers();
}
尽量不要让自己的事情过于复杂。
void GameInstance::Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// the default matricies set the origin to be at the center of the screen, with
// (-1, -1) being at the bottom-left corner, and
// ( 1, 1) being at the top-right.
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// render a full-screen quad without writing to the depth buffer
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glVertex2i(-1, -1);
glVertex2i( 1, -1);
glVertex2i( 1, 1);
glVertex2i(-1, 1);
glEnd();
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
mSpaceShip->Render();
glFlush();
glutSwapBuffers();
}
在您对 gluOrtho2D
的调用中,您将原点设置在屏幕的中心,但只在右上象限内渲染四边形(但是当您更改 GL_MODELVIEW
在 Render()
之外,这只是一个猜测)。
使用 gluOrtho2D
相当于调用 glOrtho
并将 z 裁剪范围设置为 [-1 .. 1]
,因此我怀疑您对 glTranslatef
的调用需要小得多z 值,例如 0.5f
而不是 100.0f
.