user985351
user985351

Reputation: 197

GLUT callback methods issue

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;
   }
}

Upvotes: 0

Views: 1263

Answers (1)

datenwolf
datenwolf

Reputation: 162367

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();

Upvotes: 2

Related Questions