Home Documentation Download Screenshots Developper

fastDraw

fastDraw

The fastDraw() function keeps interactivity even with large scenes.

This example demonstrates the use of the fastDraw() function, which is called when the camera moves. This function is usefull for displaying very complex scene, while keeping an interactive camera motion.

fastDraw.h

#include "qglviewer.h"

class Viewer : public QGLViewer
{
protected:
  void init();
  void draw();
  void fastDraw();
  void help();
};

fastDraw.cpp

#include "fastDraw.h"

using namespace std;

void Viewer::init()
{
  // Increase the material shininess, so that the difference between
  // the two versions of the spiral is more visible.
  glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0);
  GLfloat specular_color[4] = { 0.8, 0.8, 0.8, 1.0 };
  glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,  specular_color);

  help();
}

static void drawSpiral(const float nbSub)
{
  const float nbSteps = 400;
  float center[3], shift[3];
  for (unsigned short n=0; n<nbSub; ++n)
    {
      glBegin(GL_QUAD_STRIP);
      for (float i=0.0; i<nbSteps; ++i)
	{
	  float ratio = i/nbSteps;
	  float angle = 20.0*ratio;
	  float radius = 1.0 - 0.5*ratio;
	  
	  center[0] = radius*cos(angle);
	  center[1] = ratio-0.5;
	  center[2] = radius*sin(angle);

	  for (unsigned short j=0; j<2; ++j)
	    {
	      float delta = 3.0*(n+j)/nbSub;
	      shift[0]  = cos(angle)*cos(delta);
	      shift[1]  = sin(delta);
	      shift[2]  = sin(angle)*cos(delta);
	  
	      glColor3f(1-ratio, (n+j)/nbSub , ratio);
	      glNormal3f(shift[0], shift[1], shift[2]);
	      glVertex3f(center[0]+.2*shift[0], center[1]+.2*shift[1], center[2]+.2*shift[2]);
	    }
	}
      glEnd();
    }
}

void Viewer::draw()
{
  drawSpiral(30);
}

void Viewer::fastDraw()
{
  drawSpiral(3);
}

void Viewer::help()
{
  cout << endl << "\t\t- -  F a s t D r a w  - -" << endl << endl;
  cout << "The fastDraw() function is called instead of draw() when the camera is manipulated," << endl;
  cout << "thus allowing interactive displacements if you provide a fast drawing function." << endl;
}

main.cpp

#include "fastDraw.h"
#include <qapplication.h>

int main(int argc, char** argv)
{
  // Read command lines arguments.
  QApplication application(argc,argv);

  // Instantiate the viewer.
  Viewer v;

  // Make the viewer window visible on screen.
  v.show();

  // Set the viewer as the application main widget.
  application.setMainWidget(&v);
  
  // Run main loop.
  return application.exec();
}

Back to the main page

Valid XHTML 1.0! Valid CSS!