GLUT回调方法问题

发布于 2024-12-15 00:24:50 字数 1142 浏览 0 评论 0原文

我正在尝试使用来自以下位置的相机代码实现相机控制的场景: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

邮友 2024-12-22 00:24:50

事实上,风景绘图代码每次被调用时都会重新生成几何图形。 land::draw 中的所有代码(除了drawmesh()、drawsides() 和drawsea() 之外)都应放置在构造函数或初始值设定项中。仅应从绘图函数进行绘图调用。


提供的代码

land.hpp

class land : …
{
// ...
public:
    void generate(void);
// ...
}

land.cpp

void land::generate(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();
   }
}

void land::draw(void)
{
   drawmesh();
   drawsides();
   if (sea) {
       drawsea();
       sea = 0;
   }
}

编辑:修改Main中

glutDisplayFunc(display);
glutReshapeFunc(reshape);

// better just issue a redisplay, allows for smoother input event processing
glutIdleFunc(glutPostRedisplay);

glutPassiveMotionFunc(mouseMovement);

landscape.generate();

// ...

glutMainLoop();

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

class land : …
{
// ...
public:
    void generate(void);
// ...
}

land.cpp

void land::generate(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();
   }
}

void land::draw(void)
{
   drawmesh();
   drawsides();
   if (sea) {
       drawsea();
       sea = 0;
   }
}

In Main

glutDisplayFunc(display);
glutReshapeFunc(reshape);

// better just issue a redisplay, allows for smoother input event processing
glutIdleFunc(glutPostRedisplay);

glutPassiveMotionFunc(mouseMovement);

landscape.generate();

// ...

glutMainLoop();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文