翻译线立方体的问题

时间:2014-11-03 03:01:10

标签: c++ opengl

我正在开发一个程序,当用户按下对应的键时,必须在屏幕上翻译有线多维数据集。当翻译立方体时,会出现主要问题,制作自身的多个副本,从而生成某种路径。这是代码:

#include <iostream>
#include "GL/glut.h"
#include "GL/gl.h"
#include "GL/glu.h"
#include "math.h"

#define LARGURA 600
#define ALTURA 500

using namespace std;

const GLfloat INCREMENT = 2.0f;
const GLfloat CUBE_SIZE = 2.0f;

GLfloat X, Y, Z = 0.0f;

void myReshape(int largura, int altura)
{
    largura = LARGURA;
    altura = ALTURA;

    glViewport(0, 0, largura, altura);
    glMatrixMode(GL_PROJECTION);

    glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
    gluPerspective(25.0, largura / altura, 0.1, 500);

    glMatrixMode(GL_MODELVIEW);
}

void myDisplay()
{
    glClear(GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        gluLookAt(-2.7, 2.0, 5.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);
        glColor3f(0.0, 1.0, 1.0);

        glTranslatef(X, Y, Z);

        glutWireCube(CUBE_SIZE);
        glutPostRedisplay();

    glPopMatrix();
    glFlush();
    glutSwapBuffers();
}

void myKeyboard(int key, int x, int y)
{
    switch (key)
    {
        case GLUT_KEY_RIGHT:
            X += INCREMENT;
            break;

        case GLUT_KEY_LEFT:
            X -= INCREMENT;
            break;

        case GLUT_KEY_UP:
            Y += INCREMENT;
            glutPostRedisplay();
            break;

        case GLUT_KEY_DOWN:
            Y -= INCREMENT;
            glutPostRedisplay();
            break;

        case GLUT_KEY_INSERT:
            Z += INCREMENT;
            glutPostRedisplay();
            break;

        case GLUT_KEY_HOME:
            Z -= INCREMENT;
            glutPostRedisplay();
            break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);

    glutInitWindowSize(LARGURA, ALTURA);

    glutInitWindowPosition(400,50);
    glutCreateWindow("Movendo Cubo");

    glutDisplayFunc(myDisplay);
    glutReshapeFunc(myReshape);
    glutSpecialFunc(myKeyboard);

    glutMainLoop();

    return 0;
}

我已经做了我能做的一切,以便弄清楚发生了什么......你能帮忙吗?

1 个答案:

答案 0 :(得分:0)

每次调用display()时都需要清除整个显示,而不仅仅是深度缓冲区:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);