Objectives

Setup

Projection

typedef std::pair<float, float> Range;

struct Projection
{
  Range windowSize;

  void resize(Range size);
  virtual void render()=0;
};
void Projection::resize(Range size)
{
  windowSize = size;
}
struct Orthographic: public Projection
{
  Range xRange;
  Range yRange;
  Range zRange;

  Orthographic(Range x, Range y, Range z);
  void render();
};
struct Perspective : public Projection
{
  float fovy;
  Range zRange;
  float zDistance;

  Perspective (float fovy, Range zRange, float zDistance);
  void render();
};
Orthographic::Orthographic(Range x, Range y, Range z)
: xRange(x), yRange(y), zRange(z)
{
}

void Orthographic::render()
{
  glLoadIdentity();
  glViewport(0, 0, windowSize.first, windowSize.second);
  glMatrixMode ( GL_PROJECTION);
  glLoadIdentity();
  glOrtho(xRange.first, xRange.second, yRange.first, yRange.second, zRange.first, zRange.second);
  glMatrixMode ( GL_MODELVIEW);
}
Perspective::Perspective (float fovy, Range zRange, float zDistance)
: fovy(fovy), zRange(zRange), zDistance(zDistance)
{
}

void Perspective::render()
{
  glLoadIdentity();
  glViewport(0, 0, windowSize.first, windowSize.second);
  glMatrixMode (GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(fovy, windowSize.first/windowSize.second, zRange.first, zRange.second);
  glMatrixMode (GL_MODELVIEW);
  Vector3(0,0,zDistance).translate();
}

World

class World
{
  public:
    static World&amp; GetInstance();

    void setCmdlineParams(int*argc, char **argv);
    void initialize(int width, int height, std::string name);
    void start();
    void loadModel (std::string modelName);

    void render();
    void keyPress(unsigned char ch);

  private:
    static World* s_World;
    Model theModel;
    int   *argc;
    char **argv;
};
struct World
{
    static World&amp; GetInstance();

    void setCmdlineParams(int*argc, char **argv);
    void initialize(int width, int height, std::string name);
    void start();
    void loadModel (std::string modelName);

    void render();
    void keyPress(unsigned char ch);

    static World* s_World;
    Model         theModel;
    Projection   *currentProjection;
    ProjectionMap projections;

    int   *argc;
    char **argv;
};

Introduce a new member, which will be the current projection in force:

///
    Projection   *currentProjection;
///
void reshape(int w, int h)
{
  theWorld.currentProjection->resize(Range(w,h));
  theWorld.currentProjection->render();
}

Main

int main(int argc, char* argv[])
{
  theWorld.setCmdlineParams(&amp;argc, argv);
  theWorld.initialize(800,600, "First World");

  theWorld.loadModel("model.obj");
  theWorld.currentProjection = new Orthographic (Range(-10,10), Range(-10,10), Range(-10,10));
  theWorld.start();
  return 0;
}
  //theWorld.currentProjection = new Orthographic (Range(-10,10), Range(-10,10), Range(-10,10));
  theWorld.currentProjection = new Perspective(60, Range(1,1000), -10);

Orthographic

struct Orthographic: public Projection
{
  Range xRange;
  Range yRange;
  Range zRange;
  Vector3 axis;
  int angle;

  Orthographic(Range x, Range y, Range z, int angle, Vector3 axis);
  void render();
};
Orthographic::Orthographic(Range x, Range y, Range z, int theAngle, Vector3 theAxis)
: xRange(x), yRange(y), zRange(z), angle(theAngle), axis(theAxis)
{
}

void Orthographic::render()
{
  glLoadIdentity();
  glViewport(0, 0, windowSize.first, windowSize.second);
  glMatrixMode ( GL_PROJECTION);
  glLoadIdentity();
  glOrtho(xRange.first, xRange.second, yRange.first, yRange.second, zRange.first, zRange.second);
  glMatrixMode ( GL_MODELVIEW);

  axis.rotate(angle);
}
  Projection *projection1 = new Orthographic (Range(-10,10), Range(-10,10), Range(-10,10), 90, Vector3::UnitX);
  Projection *projection2 = new Orthographic (Range(-10,10), Range(-10,10), Range(-10,10), 90, Vector3::UnitY);
  Projection *projection3 = new Orthographic (Range(-10,10), Range(-10,10), Range(-10,10), 90, Vector3::UnitZ);

Exercises

Perspective Switching

void reshape(int w, int h)
{
  theWorld.currentProjection->resize(Range(w,h));
  theWorld.currentProjection->render();
}

void World::keyPress(unsigned char ch)
{
  ///
  glutPostRedisplay();
}

Boost Library Install

void Model::render()
{
   for (MeshMapIterator iter = entities.begin(); iter ##= entities.end(); iter++)
    {
      iter->second.render(defaultGroup.vertices);
    }
}

Exercise 3: Camera