回答您关于 genpfault 的问题:
glm::mix(以及 GLSL 的那个)基本上只做linear interpolation。在可能意味着类似
的代码中
struct Color
{
float r, g, b;
};
Color lerp(Color a, Color b, float t)
{
Color c;
c.r = (1-t)*a.r + t*b.r;
c.g = (1-t)*a.g + t*b.g;
c.b = (1-t)*a.b + t*b.b;
return c;
}
现在,用于返回一些来回效果的常用函数是cosine function。
余弦为您提供介于 -1 和 1 之间的值,因此您可能希望在 0 和 1 之间缩放它。这可以使用
float t = cos(x) * 0.5 + 0.5; // *0.5 gets to [-0.5, 0.5], +0.5 gets to [0,1]
然后你使用这个t 来计算你的颜色。 x 可以是当前时间乘以某个有助于控制插值速度的值。
编辑:
使用 gpenfault 的代码作为起点,你可以做这样的事情(如果它造成任何问题我删除它):
// g++ main.cpp -lglut -lGL
#include <GL/glut.h>
#include <cmath>
int dstTime = 0; // milliseconds
struct Color
{
float r, g, b;
};
Color makeColor(float r, float g, float b)
{
Color c = { r, g, b };
return c;
};
Color lerp(Color a, Color b, float t)
{
Color c;
c.r = (1-t)*a.r + t*b.r;
c.g = (1-t)*a.g + t*b.g;
c.b = (1-t)*a.b + t*b.b;
return c;
}
void display()
{
const int curTime = glutGet( GLUT_ELAPSED_TIME );
// figure out how far along duration we are, between 0.0 and 1.0
const float t = std::cos(float(curTime) * 0.001) * 0.5 + 0.5;
// interpolate between two colors
Color curColor = lerp(makeColor(0.0, 0.0, 0.0), makeColor(1.0, 1.0, 1.0), t);
glClearColor( curColor.r, curColor.g, curColor.b, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glutSwapBuffers();
}
void timer( int value )
{
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowSize( 400,400 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}