OpenGL/GLUT 使骑行顺畅的简单问题

Posted

技术标签:

【中文标题】OpenGL/GLUT 使骑行顺畅的简单问题【英文标题】:OpenGL/GLUT Simple issue in making ride to be smooth 【发布时间】:2014-11-07 05:02:52 【问题描述】:

我几乎完成了我被要求执行的程序,但我陷入了一个非常简单的逻辑问题,那就是:

通过鼠标左键单击来提高骑行速度,直到达到最大速度或鼠标右键单击被击中。

右击鼠标平稳降低骑行速度,直到达到零速度或鼠标左击被击中。

我正在使用信号和速度int 变量来实现我的目标,但我有两个问题:

    如果我使用Sleep() 函数来减慢有限的while 循环,程序只会冻结,并且没有任何效果。如果我不使用Sleep() 函数,运动就会变成瞬间跳跃,根本感觉不到任何东西在运动。 另外鼠标的左右点击只能用2次,之后就不能用了。

Screenshot of the program

任何提示都会很棒。

我的鼠标切换语句:

 switch (button) 
    case GLUT_LEFT_BUTTON:
        signal = 0;
        smothIncrease();
        break;
    case GLUT_MIDDLE_BUTTON:
    case GLUT_RIGHT_BUTTON:
        signal = 1;
        smothDecrease();
        break;
    default:
        break;
    

辅助函数:

void smothIncrease()
    while (true)
        if (signal == 0)
            if (speed == 15)
                break;
            angle++;
            speed++;
            Sleep(15);
            glutPostRedisplay();
        
        else if (signal == 1)
            break;
    



void smothDecrease()
    while (true)
        if (signal == 1)
            if (speed == 0)
                break;
            angle--;
            speed--;
            Sleep(15);
            glutPostRedisplay();
        
        else if (signal == 0)
            break;
    

这里是完整的源代码:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#define PI 3.14159265

static GLfloat lpos[] =  0.0, 5.0, 4.0, 1.0 ;
static GLfloat black[] =  0.0, 0.0, 0.0, 1.0 ;
static GLfloat white[] =  1.0, 1.0, 1.0, 1.0 ;
static GLfloat red[] =  1.0, 0.0, 0.0, 1.0 ;
static GLfloat lightgreen[] =  0.5, 1.0, 0.5, 1.0 ;
static float alpha = 0.0;
static float beta = PI / 6.0;
static float zoom = 25.0;
static bool lightSource = true;

float numberOfTriangles = 1;
static GLdouble cpos[3];

static double fenceHeight = -0.5;
static int angle = 0;
static int angle__IN_RANGE = 0.0;
static double radian__IN_RANGE = 0.0;
static int arrayOfAnglesInRange[181];
static int id = 0;

static int speed = 0;
static int signal = 0;

void writemessage()



void processAngle()
    angle__IN_RANGE = arrayOfAnglesInRange[abs(angle) % 181];


void setRadian_IN_RANGE()
    radian__IN_RANGE = ((float)angle__IN_RANGE / 180) * PI;


void fillArray()
    int j = -45;
    for (int i = 0; i < 181; i++)
    
        if (i < 90)
            arrayOfAnglesInRange[i] = j++;
        else
            arrayOfAnglesInRange[i] = j--;
    

    //for (int i = 0; i < 182; i++)
    //
    //  printf("%d\n", arrayOfAnglesInRange[i]);
    //


void keepTrackOfID()
    int tempAngle = angle;

    //if (id % 4 == 0)
    //  angle += 0;
    //else if (id % 4 == 1)
    //  angle += 60;
    //else if (id % 4 == 2)
    //  angle += 120;
    //else if (id % 4 == 3)
    //  angle += 180;

    //if (id % 4 == 0)
    //  angle += 0;
    //else if (id % 4 == 1)
    //  angle += 45;
    //else if (id % 4 == 2)
    //  angle += 90;
    //else if (id % 4 == 3)
    //  angle += 135;

    if (id % 4 == 0)
        angle += 0;
    else if (id % 4 == 1)
        angle += 30;
    else if (id % 4 == 2)
        angle += 60;
    else if (id % 4 == 3)
        angle += 90;

    processAngle();
    setRadian_IN_RANGE();
    angle = tempAngle;


void smothIncrease()
    while (true)
        if (signal == 0)
            if (speed == 15)
                break;
            angle++;
            speed++;
            Sleep(15);
            glutPostRedisplay();
        
        else if (signal == 1)
            break;
    



void smothDecrease()
    while (true)
        if (signal == 1)
            if (speed == 0)
                break;
            angle--;
            speed--;
            Sleep(15);
            glutPostRedisplay();
        
        else if (signal == 0)
            break;
    


void reshape(int w, int h)

    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 0.01, 50.0);
    glMatrixMode(GL_MODELVIEW);


void DrawSticksArroundYard()
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
    glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, black);
    GLUquadricObj *quadObj;

    // Right-Line
    glPushMatrix();
    glTranslatef(6.8, 1.0 + fenceHeight, -7.0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 14.0, 10, 10);
    glPopMatrix();

    // Left-Line
    glPushMatrix();
    glTranslatef(-6.8, 1.0 + fenceHeight, -7.0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 14.0, 10, 10);
    glPopMatrix();

    // Back-Line
    glPushMatrix();
    glTranslatef(-6.8, 1.0 + fenceHeight, -7.0);
    glRotatef(90, 0, 1, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 13.7, 10, 10);
    glRotatef(-90, 0, 1, 0);
    glPopMatrix();

    // Front-Line
    glPushMatrix();
    glTranslatef(6.8, 1.0 + fenceHeight, 7.0);
    glRotatef(-90, 0, 1, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.1, 0.1, 13.7, 10, 10);
    glRotatef(90, 0, 1, 0);
    glPopMatrix();

    // Pin-Front-Right
    glPushMatrix();
    glTranslatef(6.8, 0, 7.0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Front-Left
    glPushMatrix();
    glTranslatef(-6.8, 0, 7.0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Back-Left
    glPushMatrix();
    glTranslatef(-6.8, 0, -7.0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Back-Right
    glPushMatrix();
    glTranslatef(6.8, 0, -7.0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Back-Center
    glPushMatrix();
    glTranslatef(0, 0, -7.0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Front-Center
    glPushMatrix();
    glTranslatef(0, 0, 7.0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Right-Center
    glPushMatrix();
    glTranslatef(6.8, 0, 0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();

    // Pin-Left-Center
    glPushMatrix();
    glTranslatef(-6.8, 0, 0);
    glRotatef(-90, 1, 0, 0);
    quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.1, 1.3 + fenceHeight, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();


void DrawYardFloor()
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, lightgreen);
    glMaterialfv(GL_BACK, GL_AMBIENT_AND_DIFFUSE, lightgreen);
    glBegin(GL_POLYGON);
    glNormal3f(0, 1, 0);
    glVertex3f(-7.3, -0.005, -7.3);
    glVertex3f(-7.3, -0.005, 7.3);
    glVertex3f(7.3, -0.005, 7.3);
    glVertex3f(7.3, -0.005, -7.3);
    glEnd();


void DrawCenterPin()
    glRotatef(-90, 1, 0, 0);
    GLUquadricObj *quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.2, 7, 10, 10);
    glRotatef(90, 1, 0, 0);


void DrawBase()
    glRotatef(-90, 1, 0, 0);
    GLUquadricObj *quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.5, 0.1, 2, 10, 10);
    glRotatef(90, 1, 0, 0);



void DrawTop()
    glPushMatrix();
    glTranslatef(0, 7, 0);
    glRotatef(-90, 1, 0, 0);
    GLUquadricObj *quadObj = gluNewQuadric();
    gluCylinder(quadObj, 0.2, 0.0, 0.5, 10, 10);
    glRotatef(90, 1, 0, 0);
    glPopMatrix();



void DrawHorizontalStick()
    glLineWidth(15);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f(0.0, 7.0, 0.0);
    glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE), 0.0);
    glEnd();


void DrawVerticalStick()
    glLineWidth(5);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
    glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE), 0.0);
    glVertex3f(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE) - 1, 0.0);
    glEnd();


void DrawCabin()

    // Back
    glNormal3f(0.0, 0.0, -1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 0, -1);
    glVertex3f(0, 1, -1);
    glVertex3f(2, 1, -1);
    glVertex3f(2, 0, -1);
    glEnd();

    glNormal3f(0.0, 0.0, -1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 1.7, -1);
    glVertex3f(0, 2, -1);
    glVertex3f(2, 2, -1);
    glVertex3f(2, 1.7, -1);
    glEnd();

    glNormal3f(0.0, 0.0, -1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 1, -1);
    glVertex3f(0, 1.7, -1);
    glVertex3f(0.2, 1.7, -1);
    glVertex3f(0.2, 1, -1);
    glEnd();

    glNormal3f(0.0, 0.0, -1.0);
    glBegin(GL_POLYGON);
    glVertex3f(1.8, 1, -1);
    glVertex3f(1.8, 1.7, -1);
    glVertex3f(2, 1.7, -1);
    glVertex3f(2, 1, -1);
    glEnd();


    // Front
    glNormal3f(0.0, 0.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 0, 1);
    glVertex3f(2, 1, 1);
    glVertex3f(0, 1, 1);
    glVertex3f(0, 0, 1);
    glEnd();

    glNormal3f(0.0, 0.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 1.7, 1);
    glVertex3f(2, 2, 1);
    glVertex3f(0, 2, 1);
    glVertex3f(0, 1.7, 1);
    glEnd();

    glNormal3f(0.0, 0.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f(0.2, 1, 1);
    glVertex3f(0.2, 1.7, 1);
    glVertex3f(0, 1.7, 1);
    glVertex3f(0, 1, 1);
    glEnd();

    glNormal3f(0.0, 0.0, 1.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 1, 1);
    glVertex3f(2, 1.7, 1);
    glVertex3f(1.8, 1.7, 1);
    glVertex3f(1.8, 1, 1);
    glEnd();


    // Floor
    glNormal3f(0.0, -1.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 0, -1);
    glVertex3f(2, 0, 1);
    glVertex3f(0, 0, 1);
    glVertex3f(0, 0, -1);
    glEnd();


    // Top
    glNormal3f(0.0, 1.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 2, 1);
    glVertex3f(2, 2, -1);
    glVertex3f(0, 2, -1);
    glVertex3f(0, 2, 1);
    glEnd();

    // Right
    glNormal3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 0, -1);
    glVertex3f(2, 1, -1);
    glVertex3f(2, 1, 1);
    glVertex3f(2, 0, 1);
    glEnd();

    glNormal3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 1.7, -1);
    glVertex3f(2, 2, -1);
    glVertex3f(2, 2, 1);
    glVertex3f(2, 1.7, 1);
    glEnd();

    glNormal3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 1, -1);
    glVertex3f(2, 1.7, -1);
    glVertex3f(2, 1.7, -0.8);
    glVertex3f(2, 1, -0.8);
    glEnd();

    glNormal3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(2, 1, 0.8);
    glVertex3f(2, 1.7, 0.8);
    glVertex3f(2, 1.7, 1);
    glVertex3f(2, 1, 1);
    glEnd();

    // Left
    glNormal3f(-1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 0, -1);
    glVertex3f(0, 0, 1);
    glVertex3f(0, 1, 1);
    glVertex3f(0, 1, -1);
    glEnd();

    glNormal3f(-1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 1.7, -1);
    glVertex3f(0, 1.7, 1);
    glVertex3f(0, 2, 1);
    glVertex3f(0, 2, -1);
    glEnd();

    glNormal3f(-1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 1, -1);
    glVertex3f(0, 1, -0.8);
    glVertex3f(0, 1.7, -0.8);
    glVertex3f(0, 1.7, -1);
    glEnd();

    glNormal3f(-1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    glVertex3f(0, 1, 0.8);
    glVertex3f(0, 1, 1);
    glVertex3f(0, 1.7, 1);
    glVertex3f(0, 1.7, 0.8);
    glEnd();


void darwCabin__FINAL()
    glPushMatrix();
    glTranslatef(4.0 * cos(radian__IN_RANGE), 7.0 + 3.0 * sin(radian__IN_RANGE) - 3, 0.0);
    glRotatef(angle, 0, 1, 0);
    glPushMatrix();
    glTranslatef(-1, 0, 0);
    DrawCabin();
    glPopMatrix();
    glRotatef(-angle, 0, 1, 0);
    glPopMatrix();


void display(void)

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64);
    cpos[0] = zoom * cos(beta) * sin(alpha);
    cpos[1] = zoom * sin(beta);
    cpos[2] = zoom * cos(beta) * cos(alpha);
    gluLookAt(cpos[0], cpos[1], cpos[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    if (lightSource == true)
        glLightfv(GL_LIGHT0, GL_POSITION, lpos);
        glMaterialfv(GL_FRONT, GL_EMISSION, white);
        glPushMatrix();
        glTranslatef(lpos[0], lpos[1], lpos[2]);
        glutSolidSphere(0.1, 10, 8);
        glPopMatrix();
        glMaterialfv(GL_FRONT, GL_EMISSION, black);
    

    DrawYardFloor();
    DrawSticksArroundYard();
    DrawCenterPin();
    DrawBase();
    DrawTop();

    glRotatef(angle, 0, 1, 0);
    for (int i = 0; i < 4; i++)
        glPushMatrix();
        glRotatef(i * 360 / 4, 0, 1, 0);
        keepTrackOfID();

        DrawHorizontalStick();
        DrawVerticalStick();
        darwCabin__FINAL();

        id++;
        glPopMatrix();
    
    glRotatef(-angle, 0, 1, 0);

    glutSwapBuffers();
    glFlush();



void keyboard(unsigned char key, int x, int y)

    static int polygonmode[2];

    switch (key) 
    case 27:
        exit(0);
        break;
    case 'x':
        if (lightSource == true)
            lpos[0] = lpos[0] + 0.2;
        glutPostRedisplay();
        break;
    case 'X':
        if (lightSource == true)
            lpos[0] = lpos[0] - 0.2;
        glutPostRedisplay();
        break;
    case 'y':
        if (lightSource == true)
            lpos[1] = lpos[1] + 0.2;
        glutPostRedisplay();
        break;
    case 'Y':
        if (lightSource == true)
            lpos[1] = lpos[1] - 0.2;
        glutPostRedisplay();
        break;
    case 'z':
        if (lightSource == true)
            lpos[2] = lpos[2] + 0.2;
        glutPostRedisplay();
        break;
    case 'Z':
        if (lightSource == true)
            lpos[2] = lpos[2] - 0.2;
        glutPostRedisplay();
        break;

    case '+':
        if (zoom != 1.5)zoom = zoom - 0.5;
        glutPostRedisplay();
        break;
    case '-':
        if (zoom != 30)zoom = zoom + 0.5;
        glutPostRedisplay();
        break;
    case '0':
        if (lightSource == true)
            glDisable(GL_LIGHT0);
            lightSource = false;
        
        else
            glEnable(GL_LIGHT0);
            lightSource = true;
        
        glutPostRedisplay();
        break;

    case 'e':
        if (fenceHeight < 2)
            fenceHeight += 0.5;
        glutPostRedisplay();
        break;
    case 'd':
        if (fenceHeight > -0.5)
            fenceHeight -= 0.5;
        glutPostRedisplay();
        break;

    case 'w':
        glGetIntegerv(GL_POLYGON_MODE, polygonmode);
        if (polygonmode[0] == GL_FILL)
            glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
        glutPostRedisplay();
        break;
    case 'n':
        angle++;
        processAngle();
        setRadian_IN_RANGE();
        glutPostRedisplay();
        break;
    case 'm':
        angle--;
        processAngle();
        setRadian_IN_RANGE();
        glutPostRedisplay();
        break;
    default:
        break;
    


void mouse(int button, int state, int x, int y)

    switch (button) 
    case GLUT_LEFT_BUTTON:
        signal = 0;
        smothIncrease();
        break;
    case GLUT_MIDDLE_BUTTON:
    case GLUT_RIGHT_BUTTON:
        signal = 1;
        smothDecrease();
        break;
    default:
        break;
    


void specialkey(GLint key, int x, int y)

    switch (key) 
    case GLUT_KEY_RIGHT:
        alpha = alpha + PI / 180;
        if (alpha > 2 * PI) alpha = alpha - 2 * PI;
        glutPostRedisplay();
        break;
    case GLUT_KEY_LEFT:
        alpha = alpha - PI / 180;
        if (alpha < 0) alpha = alpha + 2 * PI;
        glutPostRedisplay();
        break;
    case GLUT_KEY_UP:
        if (beta < 0.45*PI) beta = beta + PI / 180;
        glutPostRedisplay();
        break;
    case GLUT_KEY_DOWN:
        if (beta > -0.05*PI) beta = beta - PI / 180;
        glutPostRedisplay();
        break;
    default:
        break;
    



int main(int argc, char** argv)

    writemessage();
    fillArray();
    processAngle();
    setRadian_IN_RANGE();
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(1200, 800);
    glutInitWindowPosition(0, 0);
    glutCreateWindow(argv[0]);

    glClearColor(0.0, 0.0, 0.0, 0.0);
    glEnable(GL_DEPTH_TEST);
    glShadeModel(GL_SMOOTH);

    /* initially GL_FILL mode (default), later GL_LINE to show wireframe */
    glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

    glEnable(GL_LIGHTING);
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
    glEnable(GL_LIGHT0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.0, 5.0, 10.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    glutSpecialFunc(specialkey);
    glutMainLoop();
    return 0;

【问题讨论】:

GLUTPostRedisplay 不显示您的框架。它所做的只是通知 GLUT 在下一个 GLUT-mainloop 中显示您的帧。摆脱 smoothXXX 函数中的循环,每次将速度更改一。 【参考方案1】:

您的代码有几个问题,我会在发现它们时进行解释。

第一件事是您对所有变量(例如速度和角度)使用int 数据类型,并且使用整数不会得到任何平滑,因此您应该将所有这些整数更改为float .

glutPostRedisplay 只会标记你的帧被再次渲染。换句话说,该函数将立即返回,并且在下一个渲染帧事件之前不会发生任何事情。 Sleep 调用使您的进程冻结(它会中断您程序中的任何处理,包括 glut 渲染循环)。无需在此函数上放置 while 循环,因为您无法以这种方式控制渲染循环。

你应该有一个新的变量来控制速度的变化,并且在你的显示函数中你应该改变运动的速度。您的新 smothIncrease 可能如下所示:

float speedChange = 0.0f;
void smothIncrease()
    speedChange += 1.0f; // you may want to have other increment factor
    glutPostRedisplay();


在您的 display 函数中,您应该将速度更改为您刚刚增加的因子:

if (speed < 15.0f)
    speed += speedChange;

但是这样我们如何在没有事件发生(鼠标点击,按键)时重绘框架?我知道的最好的方法是设置一个空闲函数,然后在这个空闲函数上调用glutPostRedisplay。这将保证您的窗口始终处于更新状态。

看看那些链接

glutPostRedisplay reference glutIdleFunc reference

【讨论】:

+1 以获得普遍的好答案。我仍然想指出,使用int 变量是没有问题的,只要它们的精度足以解决手头的问题。特别是在从 0 到 15 的离散范围内使用 intfloat 几乎没有区别。 我认为他只是从 0 到 15 作为起点,最终会调整这些数字以达到他想要的速度。所以最好删除这些整数,因为它会在将来导致错误。例如:int speed += 0.5 将导致 C++ 将数字四舍五入到最接近的整数,从长远来看可能不会发生任何事情。过去这发生在我身上,即使过程中没有明确的浮点数。感谢您的意见。

以上是关于OpenGL/GLUT 使骑行顺畅的简单问题的主要内容,如果未能解决你的问题,请参考以下文章

天府绿道骑行

天府绿道骑行

天府绿道骑行

携程攻略千岛湖骑行-其他

我如何使用 CYCHealthKitHandler 将我的活动定义为“室内骑行”

我为什么要骑行?