0

Well, I have the following C code on a GLUT Project (I use CodeBlocks). It draw some 2D shapes (something like Robot :p ).

I want to make the whole drawing shapes to be moved with the keyboard arrow keys. I have wrote the following, but for some reason when I push the arrow keys, it seems like it zoom in/out. It does not move.

#include <GL/glut.h>

GLuint head_x1=5, head_y1=30, head_x2=15, head_y2=30, head_x3=15,head_y3=40, head_x4=5,head_y4=40;

// shape
GLuint listID;
void MrRobot(GLsizei displayListID)
{
    glNewList(displayListID,GL_COMPILE);

    //Save current colour state
    glPushAttrib(GL_CURRENT_BIT);

    // body
    glColor3f(0.5,0.5,0.5);
    glBegin(GL_POLYGON);
    glVertex2f(0,10);
    glVertex2f(20,10);
    glVertex2f(20,30);
    glVertex2f(0,30);
    glEnd();

    // head
    glColor3f(0,0,1);
    glBegin(GL_POLYGON);
    glVertex2f(head_x1,head_y1);
    glVertex2f(head_x2,head_y2);
    glVertex2f(head_x3,head_y3);
    glVertex2f(head_x4,head_y4);
    glEnd();

    // legs
    glColor3f(1,0,0);
    glBegin(GL_TRIANGLE_FAN);
    glVertex2f(10,10);
    glVertex2f(20,0);
    glVertex2f(10,-5);
    glVertex2f(0,0);
    glEnd();

    // right hand
    glColor3f(0,1,0);
    glBegin(GL_TRIANGLES);
    glVertex2f(20,30);
    glVertex2f(30,27.5);
    glVertex2f(20,25);
    glEnd();

    // left hand
    glColor3f(0,1,0);
    glBegin(GL_TRIANGLES);
    glVertex2f(-10,27.5);
    glVertex2f(0,30);
    glVertex2f(0,25);
    glEnd();

    //Recall saved colour state
    glPopAttrib();

    glEndList();
}

void display()
{
    glClearColor(0,0,0,0);

    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1,0,0);

    //Defining a modelview transform matrix
    glScalef(0.3,0.3,0.3);

    //Execute the display list (the modelview matrix will be applied)
    glCallList(listID);

    glFlush();
}

void keyboard(unsigned char key,int x, int y)
{
    printf("\nKeyboard event detected. \nCharacter key: %c\nMouse pointer position: x=%d y=%d",key,x,y);

    if (key==GLUT_KEY_UP)
    {
        head_y1++;
        head_y2++;
        head_y3++;
        head_y4++;
    }
    if (key==GLUT_KEY_DOWN)
    {
        head_y1--;
        head_y2--;
        head_y3--;
        head_y4--;
    }
    if (key==GLUT_KEY_LEFT)
    {
        head_x1--;
        head_x2--;
        head_x3--;
        head_x4--;
    }
    if (key==GLUT_KEY_RIGHT)
    {
        head_x1++;
        head_x2++;
        head_x3++;
        head_x4++;
    }

    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc,argv);
    glutInitWindowPosition(50,50);
    glutInitWindowSize(800,600);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutCreateWindow("Mr Robot");

    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-5,35,-5,30);

    listID=glGenLists(1);

    MrRobot(listID);

    glutDisplayFunc(display);
    glutSpecialFunc(keyboard);
    glutMainLoop();

    return 0;
}

Maybe is something with the axis, but have no idea what I should change.

Any help?

user3594130
  • 27
  • 1
  • 7

1 Answers1

0

Well there are a lot of problems. You seem to be writing OpenGL a little bit differently.

Anyways here is the problem.

  • The reason its scaling is because everytime you call display you are calling glScalef(0.3, 0.3, 0.3); without loading the identity matrix.
  • Also when you press a key the increment is happening but you havent even called MrRobot() in the display function. You have only called it once in the main function
  • Also just before you call display you must call glMatrixMode(GL_MODELVIEW); or else all transformations you do will affect the GL_PROJECTION matrix
DollarAkshay
  • 2,063
  • 1
  • 21
  • 39
  • Thank you very much! Sorry for the bad code, I based in some examples that my teacher gives to me. I apply the changes you describe and the move are working. There is only one issue: after some key press, the shapes are moving but from one point and then, the shapes are distorted and take the whole screen. Any idea to fix it? If you need to copy/paste the final code to you, let me know. – user3594130 May 15 '16 at 22:36