Omberg
Omberg

Reputation: 1

Object movement in OpenGL

Here the coordinate plane and the cube are drawn, it is necessary to ensure the movement of the cube. But in this situation, both the cube and the coordinate plane move. What do I need to add to ensure that only the cube moves. As I understand it, I need to zero the matrix, but how can I implement this?

#include <glut.h>

void Reshape(int w, int h);
void Display();
void ProcessNormalKeys(unsigned char key, int x, int y);
void ProcessSpecialKeys(int key, int x, int y);

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowSize(800, 600);
    glutReshapeFunc(Reshape);
    glutDisplayFunc(Display);
    glutKeyboardFunc(ProcessNormalKeys);
    glutSpecialFunc(ProcessSpecialKeys);
    glutMainLoop();
    return 0;
}
void Reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1000, 1000, -1000, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void Display()
{
    glClearColor(1, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_QUADS);
    glColor3f(1.0, 1.0, 1.0);
    glVertex2i(350, 550);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2i(350, 150);
    glColor3f(0.0, 1.0, 0.0);
    glVertex2i(650, 150);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2i(650, 550);
    glEnd();
    
    glBegin(GL_LINES);
    glColor3f(0, 0, 0);
    glVertex2i(-10000, 0);
    glVertex2i(10000, 0);
    glVertex2i(0, -10000);
    glVertex2i(0, 10000);
    glEnd();
    glutSwapBuffers();
}
void ProcessNormalKeys(unsigned char key, int x, int y)
{
    if (key == 27)
    {
        exit(0);
    }
    if (key == 65)
    {
        glMatrixMode(GL_MODELVIEW);
        glTranslated(80, 80, 0);
        Display();
    }
}
void ProcessSpecialKeys(int key, int x, int y)
{
    switch (key)
    {
    case GLUT_KEY_UP:
        glMatrixMode(GL_MODELVIEW);
        glTranslated(0, 80, 0);
        Display();
        break;
    case GLUT_KEY_DOWN:
        glMatrixMode(GL_MODELVIEW);
        glTranslated(0, -80, 0);
        Display();
        break;
    case GLUT_KEY_LEFT:
        glMatrixMode(GL_MODELVIEW);
        glTranslated(-80, 0, 0);
        Display();
        break;
    case GLUT_KEY_RIGHT:
        glMatrixMode(GL_MODELVIEW);
        glTranslated(80, 0, 0);
        Display();
        break;
    }
}

Upvotes: 0

Views: 73

Answers (1)

genpfault
genpfault

Reputation: 52165

Store off the cube position in a global/per-window variable and use glPushMatrix()/glPopMatrix() around the glTranslated() + cube draw to ensure the transform doesn't effect later geometry:

// gcc main.c -lglut -lGL -lGLU
#include <GL/glut.h>

float cubePos[3] = {0, 0, 0};

void ProcessNormalKeys(unsigned char key, int x, int y)
{
    if (key == 27)
    {
        exit(0);
    }
    if (key == 65)
    {
        cubePos[0] = 80;
        cubePos[1] = 80;
        cubePos[2] = 0;
        glutPostRedisplay();
    }
}

void ProcessSpecialKeys(int key, int x, int y)
{
    switch (key)
    {
    case GLUT_KEY_UP:
        cubePos[0] += 0;
        cubePos[1] += 80;
        cubePos[2] += 0;
        glutPostRedisplay();
        break;
    case GLUT_KEY_DOWN:
        cubePos[0] += 0;
        cubePos[1] += -80;
        cubePos[2] += 0;
        glutPostRedisplay();
        break;
    case GLUT_KEY_LEFT:
        cubePos[0] += -80;
        cubePos[1] += 0;
        cubePos[2] += 0;
        glutPostRedisplay();
        break;
    case GLUT_KEY_RIGHT:
        cubePos[0] += 80;
        cubePos[1] += 0;
        cubePos[2] += 0;
        glutPostRedisplay();
        break;
    }
}

void Display()
{
    glClearColor(1, 1, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1000, 1000, -1000, 1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();
    glTranslated(cubePos[0], cubePos[1], cubePos[2]);
    glBegin(GL_QUADS);
    glColor3f(1.0, 1.0, 1.0);
    glVertex2i(350, 550);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2i(350, 150);
    glColor3f(0.0, 1.0, 0.0);
    glVertex2i(650, 150);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2i(650, 550);
    glEnd();
    glPopMatrix();

    glBegin(GL_LINES);
    glColor3f(0, 0, 0);
    glVertex2i(-10000, 0);
    glVertex2i(10000, 0);
    glVertex2i(0, -10000);
    glVertex2i(0, 10000);
    glEnd();

    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(800, 600);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
    glutCreateWindow("GLUT");
    glutDisplayFunc(Display);
    glutKeyboardFunc(ProcessNormalKeys);
    glutSpecialFunc(ProcessSpecialKeys);
    glutMainLoop();
    return 0;
}

Upvotes: 1

Related Questions