将世界保持在视口范围内

时间:2014-03-13 13:22:10

标签: c++ opengl glut

我目前正在尝试创建一个2D侧卷轴,我目前有我的“世界”绘图(暂时是一个大白框),但我无法弄清楚世界地图边缘与边缘之间的任何关系视口以确保视口始终完全被地图覆盖。

我的基本世界绘图代码是:

void drawTiles(void)
{
    for (int i = 0; i < 50; i++)
    {
        for (int j = 0; j < 500; j++) 
        {           
            glPushMatrix();
            glTranslatef(j, -i, 0);

            glBegin (GL_QUADS);
            glTexCoord2d(0.0, 0.0);
            glVertex3f(0.0, 0.0, 0.0);

            glTexCoord2d(1.0, 0.0);
            glVertex3f(1.0, 0.0, 0.0);

            glTexCoord2d(1.0, 1.0);
            glVertex3f(1.0, 1.0, 0.0);

            glTexCoord2d(0.0, 1.0);
            glVertex3f(0.0, 1.0, 0.0);
            glEnd();
            glPopMatrix();
        }
    }
}

void display(void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity(); 
    glEnable(GL_TEXTURE_2D);

    glPushMatrix();
    glTranslatef(camX, camY, -20); //translate back a bit to view the map correctly (our camera)

    drawTiles(); //draw our tiles

    glPopMatrix();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

void keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {
    case 'w':
        camY -= 0.25;
    break;
    case 's':
        camY += 0.25;
    break;
    case 'a':
        camX += 0.25;
    break;
    case 'd':
        camX -= 0.25;
    break;
    }
}

我如何确保当我使用WASD并在视口大小调整时,我不会超出地图范围(目前为500x50平铺)?

1 个答案:

答案 0 :(得分:1)