2

所以我有一个二维数组,它充当要绘制的图块的地图。

int sMap[12][20] = {
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 2, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1},
  {1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 1},
  {1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1},
  {1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 2, 0, 0, 1, 0, 0, 0, 1},
  {1, 1, 0, 0, 0, 0, 0, 1, 2, 1, 0, 0, 1, 0, 1, 1, 2, 2, 2, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
};

一旦我的瓷砖被加载,我用它function()来放置瓷砖:

for (int y = 0; y < 12; y++){
    for (int x = 0; x < 20; x++){
        if    (sMap[y][x] == 1)
            glBindTexture( GL_TEXTURE_2D, brick1);
        else if (sMap[y][x] == 2)
            glBindTexture( GL_TEXTURE_2D, brick2);
        else
            glBindTexture( GL_TEXTURE_2D, wall );

        glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(float(x + offsetx),     float(MAP_SIZEY - (y + offsety)), 0.0f); 
        glTexCoord2f(1.0f, 0.0f); glVertex3f(float(x + 1 + offsetx), float(MAP_SIZEY - (y + offsety)), 0.0f); 
        glTexCoord2f(1.0f, 1.0f); glVertex3f(float(x + 1 + offsetx), float(MAP_SIZEY - (y + 1 + offsety)), 0.0f); 
        glTexCoord2f(0.0f, 1.0f); glVertex3f(float(x + offsetx),     float(MAP_SIZEY - (y + 1 + offsety)), 0.0f);
        glEnd();
    }
}

我想我可能对瓷砖的坐标系感到困惑,因为当我绘制一个OpenGL充当精灵的基本正方形时,我在运行程序时会出现黑屏。

我不确定这是否意味着精灵对瓷砖的比例是错误的,或者精灵和瓷砖是否在不同的Z平面上......

如果有人能解释坐标系,我将不胜感激,以防我没有像我想象的那样理解它,并建议我如何OpenGL在相同的坐标上画一个正方形。

目前我有这个来绘制我的基本精灵:

struct RECT{float x, y, w, h;};
RECT sprite = {0, 0, 10, 10};

void drawSprite (RECT rect){
    glBegin(GL_QUADS);
    glColor3f(1.0f, 0.0f, 0.0f);    
        glVertex3f(rect.x, rect.y, 0.0);
        glVertex3f(rect.x, rect.y+rect.h, 0.0);
        glVertex3f(rect.x+rect.w, rect.y+rect.h, 0.0);
        glVertex3f(rect.x+rect.w, rect.y, 0.0);
    glEnd();
}

编辑:

调整屏幕大小:

glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,20.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

绘制场景:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(10.0f, 8.0f, 20.0f, 10.0f, 8.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glTranslatef(5.0f,4.0f,0.0f);
draw_tiles();
draw_sprite();
4

1 回答 1

1

在 draw_tiles 函数中,您可能传递了不正确的坐标 - 也许您应该将 x 和 y 值乘以您的图块大小。

还可以尝试关闭深度测试和背面剔除以帮助解决黑屏问题。

glDisable( GL_DEPTH_TEST );
glDisable( GL_CULL_FACE );
于 2013-04-29T17:15:13.980 回答