Log*_*man 5 opengl camera interpolation glut glulookat
我正在OpenGL中的天空盒内制作一个过山车,并且没有太多关于它的功能或计算机图形的背景,它被证明是非常困难的.我使用Catmull-Rom样条插值绘制了一个过山车,并用glVertex3f绘制了每个点.现在我想update()每50ms 调用一次功能来将摄像机移动到轨道上.gluLookAt()产生奇怪的结果,要么从屏幕上删除轨道,产生黑屏等等.我想我需要移动一些矩阵函数,但我不知道在哪里放置每个.到目前为止,这是我的代码:
int main(int argc, char** argc)
{
// ... load track, etc ...
// Init currpos, nextpos, iter, up
currpos = Vec3f(0, 0, 0);
nextpos = currpos;
iter = 0;
up = Vec3f(0, 1, 0);
deque<Vec3f> points;
Vec3f newpt;
// Loop through the points and interpolate
for (pointVectorIter pv = g_Track.points().begin(); pv != g_Track.points().end(); pv++)
{
Vec3f curr(*pv); // Initialize the current point and a new point (to be drawn)
points.push_back(curr); // Push the current point onto the stack
allpoints.push_back(curr); // Add current point to the total stack
if (points.size() == 4) // Check if there are 4 points in the stack, if so interpolate
{
for (float u = 0.0f; u < 1.0f; u += 0.01f)
{
newpt = interpolate(points[0], points[1], points[2], points[3], u);
glColor3f(1, 1, 1);
glVertex3f(newpt.x(), newpt.y(), newpt.z());
allpoints.push_back(newpt);
}
points.pop_front();
}
}
// glutInit, InitGL(), etc...
}
void InitGL(GLvoid)
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(100.0, (GLfloat)WINDOW_WIDTH / (GLfloat)WINDOW_HEIGHT, .0001, 999999);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
}
void display (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(currpos.x(), currpos.y(), currpos.z(), nextpos.x(), nextpos.y(), nextpos.z(), up.x(), up.y(), up.z());
glPushMatrix();
glEnable(GL_TEXTURE_2D); // Enable texturing from now on
/* draw skybox, this was from previous assignment and renders correctly */
glPopMatrix();
// now draw rollercoaster ...
glPushMatrix();
glBegin(GL_LINE_STRIP);
deque<Vec3f> points;
Vec3f newpt;
for each (Vec3f pt in allpoints)
{
glColor3f(1, 1, 1);
glVertex3f(pt.x(), pt.y(), pt.z());
}
glutTimerFunc(50, update, 1);
glEnd();
glPopMatrix();
// Swap buffers, so one we just drew is displayed
glutSwapBuffers();
}
void update(int a)
{
if (iter < allpoints.size())
{
currpos = allpoints[iter];
nextpos = allpoints[iter + 1];
gaze = nextpos - currpos;
gaze.Normalize();
Vec3f::Cross3(binorm, gaze, up);
binorm.Normalize();
Vec3f::Cross3(up, binorm, gaze);
up.Normalize();
glutPostRedisplay();
}
iter++;
}
Run Code Online (Sandbox Code Playgroud)
我的想法是保持一个allpoints包含样条控制点和插值点的全局双端队列.一旦完成,我update()每50ms 调用一次,并沿着每个点移动相机allpoints.在该项目的先前版本中,我可以看到正在正确绘制过山车.这gluLookAt()似乎并不像我想要的那样.使用上面的代码,程序开始时相机用过山车的一部分观看天空盒的一侧,然后当update()调用时,过山车消失但相机不移动.我一直在搞乱我放置OpenGL矩阵函数的位置,并且根据它们有时update()会导致空白屏幕.
除了缺少glPopMatrix(用户971377已经发现)之外,您可以调用glLoadIdentity绘图例程,这当然会覆盖您在update方法(使用gluLookAt)中对模型视图矩阵所做的任何更改.
永远记住:gluLookAt,glOrtho,gluPerspective,glTranslate,glRotate,和所有其他矩阵和转换功能始终是头等元素(通过改变上运行glPush/PopMatrix当前选定的矩阵堆栈(通过改变)glMatrixMode).它们总是将当前矩阵相乘,而不是替换它.所以喜欢gluPerspective,你应该在打电话glLoadIdentity前打电话gluLookAt.并且整个摄像机的更改应该在渲染例程中完成,而不是更新例程.
update您应该更改摄像机所依赖的变量,并在方法中设置摄像机(gluLookAt在模型视图矩阵上),而不是进行任何GL变换display.为了演示这些函数的标准用法,您的代码应该类似于:
void display()
{
<general state setup (glClear, ...)>
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLookAt(camera); //view transformation (camera)
//object 1
glPushMatrix(); //save modelview
glTranslate/glRotate/glScale; //local model transformations
<draw object 1>
glPopMatrix();
...
//object n
glPushMatrix(); //save modelview
glTranslate/glRotate/glScale; //local model transformations
<draw object n>
glPopMatrix();
gluSwapBuffers();
}
void update()
{
camera = ...;
}
Run Code Online (Sandbox Code Playgroud)
}