用C++在OpenGL中移动图形时,按时间自动旋转图形。

问题描述 投票:0回答:1

我想让我的形状保持旋转,同时我可以用键盘移动它们(左-右,下-上,前-后),我添加了一个函数,叫做 timer() 应该按时间来改变立方体和金字塔的角度。glutPostOverlayRedisplay(); 函数,它说:

在 Opengl.exe 中 0x10004813 (glut32.dll) 抛出的异常:0xC0000005: 在Opengl.exe中,在0x10004813 (glut32.dll)处发生异常:0xC0000005:读取位置0x00000020的访问违规。

我怎么能解决这个问题,为什么会发生这种情况呢?

#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
1个回答
2
投票

glutPostOverlayRedisplay 标记当前窗口的叠加需要重新显示(请看下面的例子) 叠加管理). 你必须使用 glutPostRedisplay:

void timer(int value)
{
    glutPostRedisplay();
    glutTimerFunc(t_refresh, timer, 0);
}
© www.soinside.com 2019 - 2024. All rights reserved.