Auto-Rotating shapes by time while moving them in OpenGL with C++

1

I want to make my shapes to keep rotating by time while I can move them (Left-Right , Down-Up , Front-Back) using keyboard I added a function called timer() that should change the angles of the cube and pyramid by time. The problem is the code keeps breaking at glutPostOverlayRedisplay(); function and it says :

Exception thrown at 0x10004813 (glut32.dll) in Opengl.exe: 0xC0000005: Access violation reading location 0x00000020.

how can I fix this problem and why its happening ? this is the code :

#include <GL/glut.h>
int object = 0; 
float xLeftRight[]{ 0, 0 };
float yDownUp[]{ 0, 0 };
float zFrontBack[]{ 0, 0 };
float PyramidAngle = 0.0;
float CupeAngle = 0.0;
int t_refresh = 20;//ms 
float degree = 0;
float xscale = 1, yscale = 1, zscale = 1;

void Keyboard(int buttons, int x, int y)
{
    switch (buttons)
    {
    case GLUT_KEY_F1: object = 0; break; 
    case GLUT_KEY_F2: object = 1; break; 
    case GLUT_KEY_LEFT: xLeftRight[object] -= 0.1; break;
    case GLUT_KEY_RIGHT: xLeftRight[object] += 0.1; break;
    case GLUT_KEY_DOWN:  yDownUp[object] -= 0.1; break;
    case GLUT_KEY_UP: yDownUp[object] += 0.1; break;
    case GLUT_KEY_PAGE_UP: zFrontBack[object] -= 0.1; break;
    case GLUT_KEY_PAGE_DOWN: zFrontBack[object] += 0.1; break;
    }
    glutPostRedisplay();
}
void Settings()
{
    glClearColor(1, 1, 1, 0);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glShadeModel(GL_SMOOTH);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}

void DrawingFunction()//painting
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity(); //reset
    glTranslatef(1.5f, 0.0f, -6.0f); // rotate left and back by time
    glRotatef(PyramidAngle, 1.0f, 1.0f, 0.0f);
    glColor3f(1.0, 0.0, 0.0);
    glScalef(xscale, yscale, zscale);
    glTranslatef(xLeftRight[0], yDownUp[0], zFrontBack[0]); //move the pyramid (if object = 0)

    glBegin(GL_TRIANGLES);
    // front
    glColor3f(1.0f, 0.0f, 0.0f); //red
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f); //green
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glColor3f(0.0f, 0.0f, 1.0f); //blue
    glVertex3f(1.0f, -1.0f, 1.0f);
    // right

    glColor3f(1.0f, 0.0f, 0.0f); //red
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f); //blue
    glVertex3f(1.0f, -1.0f, 1.0f);
    glColor3f(0.0f, 1.0f, 0.0f); //green
    glVertex3f(1.0f, -1.0f, -1.0f);
    // back

    glColor3f(1.0f, 0.0f, 0.0f); //red
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f); //green
    glVertex3f(1.0f, -1.0f, -1.0f);
    glColor3f(0.0f, 0.0f, 1.0f); //blue
    glVertex3f(-1.0f, -1.0f, -1.0f);
    // left

    glColor3f(1.0f, 0.0f, 0.0f); //red
    glVertex3f(0.0f, 1.0f, 0.0f);
    glColor3f(0.0f, 0.0f, 1.0f); //blue
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glColor3f(0.0f, 1.0f, 0.0f); //green
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glEnd();

    glPopMatrix();
    glPushMatrix();
    glLoadIdentity();//(Reset model-view matrix)
    glTranslatef(-2.0f, 0.0f, -7.0f); // rotate the cube right and back by time
    glRotatef(CupeAngle, 1.0f, 0.0f, 0.0f);
    glTranslatef(xLeftRight[1], yDownUp[1], zFrontBack[1]); //move the cube (if object = 1)                                         
    glBegin(GL_QUADS); //cube

    glColor3f(1.0f, 0.0f, 1.0f); 
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glColor3f(1.0f, 0.5f, 0.0f); 
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);

    glColor3f(0.0f, 0.0f, 1.0f); 
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);

    glColor3f(1.0f, 1.0f, 0.0f); 
    glVertex3f(1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);

    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, -1.0f);
    glEnd();
    glutSwapBuffers();
    PyramidAngle += 1.0f; //changing the angle
    CupeAngle -= 0.2f;    //changing the angle
    glPopMatrix();
}
void timer(int value)
{
    glutPostOverlayRedisplay();
    glutTimerFunc(t_refresh, timer, 0);
}

void ViewSetting(int x, int y)
{
    int aspect = x / y;
    glViewport(0, 0, x, y);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, aspect, 0.1, 20);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE);
    glutInitWindowPosition(50, 50);
    glutInitWindowSize(800, 600);
    glutCreateWindow("3d shapes");
    glutDisplayFunc(DrawingFunction);
    glutReshapeFunc(ViewSetting);
    glutSpecialFunc(Keyboard);
    Settings();
    glutTimerFunc(0, timer, 0);
    glutMainLoop();
    return 0;
}
c++
opengl
3d
glut
opengl-compat
asked on Stack Overflow Jun 11, 2020 by MrXQ • edited Jun 14, 2020 by Rabbid76

1 Answer

2

glutPostOverlayRedisplay marks the overlay of the current window as needing to be redisplayed (See Overlay Management).
You have to use glutPostRedisplay:

void timer(int value)
{
    glutPostRedisplay();
    glutTimerFunc(t_refresh, timer, 0);
}
answered on Stack Overflow Jun 11, 2020 by Rabbid76

User contributions licensed under CC BY-SA 3.0