C:每x毫秒显示一次(OpenGL) [英] C : display every x milliseconds (OpenGL)

查看:73
本文介绍了C:每x毫秒显示一次(OpenGL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C/C ++(只有主文件是.cpp,所以我可以使用OpenGL)程序,我在其中使用OpenGL(GLUT,GLUI).它已经显示了一些东西,但我希望它每x ms移动一次.我绘制了一些圆(已知的速度和坐标),并且已经有了知道刷新率的计算其下一个位置的函数.

I've got a C / C++ (only main file is .cpp so I can use OpenGL) program, I use OpenGL (GLUT, GLUI) in it. It already displays something but I want it to move every x ms. I render some circles (known speed and coordinates) and I have already made the function that computes it's next position knowing the refresh rate.

我试图将显示回调放入计时器回调中,但程序只是冻结了.

I've tried to put my display callback in a timer callback but the program just freezed.

我该怎么做才能每隔x ms运行一次显示回调?

What can I do in order to run the display callback every x ms ?

推荐答案

我试图将显示回调放入计时器回调中,但程序只是冻结了.

I've tried to put my display callback in a timer callback but the program just freezed.

请确保重新设置计时器在您的计时器回调中,否则只会触发一次:

Make sure to re-arm the timer in your timer callback otherwise it will only fire once:

#include <GL/glut.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-10, 10, -10, 10, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    static float angle = 0;
    angle += 1.0f;
    glRotatef(angle, 0, 0, 1);

    glColor3ub(255,0,0);
    glBegin(GL_TRIANGLES);
    glVertex2f(0,0);
    glVertex2f(10,0);
    glVertex2f(10,10);
    glEnd();

    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

void timer(int extra)
{
    glutPostRedisplay();
    glutTimerFunc(30, timer, 0);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutCreateWindow("Timer");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(0, timer, 0);
    glutMainLoop();
    return 0;
}

这篇关于C:每x毫秒显示一次(OpenGL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆