GLUT回调方法问题
我正在尝试使用来自以下位置的相机代码实现相机控制的场景: http://www. swiftless.com/tutorials/opengl/camera2.html
以及书中的景观代码,它随机生成地形。通过使用如下所示的代码,鼠标控制和景观工作,但景观始终保持随机生成。我怀疑这与 IdleFunc 有关,但如果我从 main() 中取出该行,鼠标就会停止工作。
如何将相机和土地分开独立工作?
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera();
landscape.draw();
glutSwapBuffers();
}
在主要:
// callback methods
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutPassiveMotionFunc(mouseMovement);
land.cpp中:
void land::draw(void)
{
int i;
size = 1;
for (i=1;i<=numlevels;i++) size = size * 2;
for (i=0;i<=size;i++) cd[i] = (GLfloat)i/(GLfloat)size;
twotopowerh = exp(log(2.0)*hvalue);
cvalue = 1.0*sqrt(1.0 - twotopowerh*twotopowerh/4.0);
if (!sea) {
calcheights();
makesmooth();
}
drawmesh();
drawsides();
if (sea) {
drawsea();
sea = 0;
}
}
I am trying to implement a camera controlled scene using the camera code from: http://www.swiftless.com/tutorials/opengl/camera2.html
And the landscape code from a book, which randomly generates terrain. By using the code as shown below, the mouse controls and the landscape work BUT the landscape keeps randomly generating ALL the time. I suspect this is to do with the IdleFunc, but if I take that line from main() out, the mouse stops working.
How can I separate the camera and the land to work independently?
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera();
landscape.draw();
glutSwapBuffers();
}
in Main:
// callback methods
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutPassiveMotionFunc(mouseMovement);
land.cpp:
void land::draw(void)
{
int i;
size = 1;
for (i=1;i<=numlevels;i++) size = size * 2;
for (i=0;i<=size;i++) cd[i] = (GLfloat)i/(GLfloat)size;
twotopowerh = exp(log(2.0)*hvalue);
cvalue = 1.0*sqrt(1.0 - twotopowerh*twotopowerh/4.0);
if (!sea) {
calcheights();
makesmooth();
}
drawmesh();
drawsides();
if (sea) {
drawsea();
sea = 0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,风景绘图代码每次被调用时都会重新生成几何图形。 land::draw 中的所有代码(除了drawmesh()、drawsides() 和drawsea() 之外)都应放置在构造函数或初始值设定项中。仅应从绘图函数进行绘图调用。
提供的代码
land.hpp
land.cpp
编辑:修改Main中
Indeed that landscape drawing code regenerates the geometry each time it's called. All that's code in land::draw, except drawmesh(), drawsides() and drawsea() should be placed in the constructor or a initializer. Only the drawing calls should be made from a drawing function.
Edit: Modification of provided code
land.hpp
land.cpp
In Main