我用 OpenGL 和 C 编写了一个小型平铺游戏引擎,但我似乎无法弄清楚问题所在。我的主循环如下所示:
void main_game_loop()
{
(poll for events and respond to them)
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
draw_block(WALL, 10, 10);
}
绘制块:
void draw_block(block b, int x, int y)
{
(load b's texture from a hash and store it in GLuint tex)
glPushMatrix();
glTranslatef(x, y, 0);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_QUADS);
//BLOCK_DIM is 32, the width and height of the texture
glTexCoord2i(0, 0); glVertex3f(0, 0, 0);
glTexCoord2i(1, 0); glVertex3f(BLOCK_DIM, 0, 0);
glTexCoord2i(1, 1); glVertex3f(BLOCK_DIM, BLOCK_DIM, 0);
glTexCoord2i(0, 1); glVertex3f(0, BLOCK_DIM, 0);
glEnd();
glPopMatrix;
}
初始化函数:(在 main_game_loop 之前调用)
void init_gl()
{
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 0);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
运行时,会显示黑屏。但是,如果我删除 glViewport 调用,它看似显示纹理,但巨大且位于窗口的角落。截屏:
纹理被正确绘制,因为如果我按比例放大,我可以看到整个图像。y 轴似乎也从我在 gluOrtho2D 调用中使用的翻转(通过使事件从图像的 x/y 坐标中添加或减去,从 y 坐标中减去导致图像向下移动来发现)。我开始感到沮丧,因为这是我能想到的最简单的例子。我正在使用 SDL,并将 SDL_OPENGL 传递给 SDL_SetVideoMode。这里发生了什么?