gpt4 book ai didi

c - 在 OpenGL 动画中实现延迟/刷新

转载 作者:太空宇宙 更新时间:2023-11-03 23:33:31 25 4
gpt4 key购买 nike

我正在研究使用 opengl 进行一些非常基本的 3D 渲染,但我的老师使用一台电脑,您可以使用 sleep() 函数来渲染动画的每个帧。

然而,当我尝试在我的 Mac 上做同样的事情时,我只得到最后一帧。如果我使用 sleep() 函数,我的 mac 就会进入休眠模式。

我读过一些关于 NSTimer 的内容,但我不知道如何实现它。

这是我的代码,它在 pc 上工作得很好,我只想用 mac 等效项替换注释的 sleep(),使我能够在该延迟期间看到每一帧

#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 上是强制性的,以告诉合成器您已完成渲染)。

此外,您不应该为了动画而休眠(缓冲区交换无论如何都会这样做),而是测量帧之间的时间。


更新:固定代码

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com