我正在使用 opengl 学习一些非常基本的 3D 渲染,但我的老师使用一台电脑,您可以在其中使用 sleep() 函数来渲染每个动画帧。
但是,当我尝试在 Mac 上做同样的事情时,我只得到最后一帧。如果我使用 sleep() 函数,我的 Mac 就会进入休眠模式。
我读过一些有关 NSTimer 的内容,但我不知道如何实现它。
这是我的代码,它在 PC 上运行得很好,我只想将注释的 sleep() 替换为 Mac 等效代码,使我能够在延迟期间看到每一帧
#include<OpenGL/gl.h>
#include<OpenGL/glu.h>
#include<GLUT/glut.h>
#include <stdlib.h>
void init (void)
{
glClearColor(0.5,0.5,0.5,0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0,4,4,0,0,0,0,1,0);
glMatrixMode(GL_PROJECTION);
gluPerspective(90,1,1,12);
}
void Cubes(void)
{
int i=0;
glMatrixMode(GL_MODELVIEW);
glutInitDisplayMode(GL_DEPTH);
for (i=0; i<360;i++)
{
glRotated(1,1,0,0);
glClear(GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,0,0);
glutSolidSphere(1,20,20);
glColor3f(0.8,0.8,0);
glutWireSphere(1,20,20);
glColor3f(0,0,1);
glutSolidTorus(1,2,40,40);
glFlush();
glDisable(GL_DEPTH_TEST);
//Sleep(20);
}
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(600,600);
glutCreateWindow("Depth Buffer");
init();
glutDisplayFunc(Cubes);
glutMainLoop();
}
最佳答案
请告诉你的老师他做错了!要使用 GLUT 制作动画,不要将动画循环放入显示函数中,而是注册一个空闲函数,该函数会重新发出显示。此外,此代码还缺少缓冲区交换(如果不在全屏模式下运行,则在 MacOS X 上这是强制的,以告诉合成器您已完成渲染)。
此外,您不应该为了动画而 sleep (缓冲区交换无论如何都会这样做),而应该测量帧之间的时间。
更新:修复代码
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#include <stdlib.h>
#include <sys/time.h>
#include <math.h>
#include <stdio.h>
double getftime(
void )
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec*1e-6;
}
static double lasttime;
void display(
void )
{
int width, height;
double finishtime, delta_t;
static float angle = 0;
width = glutGet(GLUT_WINDOW_WIDTH);
height = glutGet(GLUT_WINDOW_HEIGHT);
glClearColor( 0.5, 0.5, 0.5, 1.0 );
/* combine the clearing flags for more efficient operation */
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
glViewport(0, 0, width, height);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 90, (float)width/(float)height, 1, 12 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt( 0, 4, 4, 0, 0, 0, 0, 1, 0 );
glEnable( GL_DEPTH_TEST );
glRotated( angle, 1, 0, 0 );
glColor3f( 1, 0, 0 );
glutSolidSphere( 1, 20, 20 );
glColor3f( 0.8, 0.8, 0 );
glutWireSphere( 1, 20, 20 );
glColor3f( 0, 0, 1 );
glutSolidTorus( 1, 2, 40, 40 );
glDisable( GL_DEPTH_TEST );
glutSwapBuffers();
finishtime = getftime();
delta_t = finishtime - lasttime;
angle = fmodf(angle + 10*delta_t, 360);
lasttime = finishtime;
}
int main(
int argc,
char **argv )
{
glutInit( &argc, argv );
glutInitWindowSize( 600, 600 );
/* glutInitDisplayMode must be called before glutCreateWindow, flags are prefixed with GLUT_ not GL */
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutCreateWindow( "Depth Buffer" );
glutDisplayFunc( display );
/* register glutPostRedisplay for continuous animation */
glutIdleFunc(glutPostRedisplay);
lasttime = getftime();
glutMainLoop( );
}
关于c - 在 OpenGL 动画中实现延迟/刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9929416/