Objectives

Setup

World

void keyboardSpecial(int key, int x, int y);
void mouseMovement(int x, int y);
void mouseMovement(int x, int y)
{
  theWorld.mouseMovement(x,y);
}

void keyboardSpecial(int key, int x, int y)
{
  theWorld.specialKeyPress(key, x, y);
}
  void specialKeyPress(int key, int x, int y);
  void mouseMovement(int x, int y);
void World::specialKeyPress(int key, int x, int y)
{
  //...
}

void World::mouseMovement(int x, int y)
{
  //...
}

Camera

struct Camera
{
  Camera();

  void specialKeyboard (int key, int x, int y);
  void mouseMovement(int x, int y);
  void render();

  Vector3 position;
  float xrot, yrot, cRadius, lastx, lasty;
};
Camera::Camera()
: position(0,0,0), xrot(0), yrot(0), cRadius(5)
{
}

void Camera::render()
{
  glTranslatef(0.0f, 0.0f, -cRadius);
  Vector3::UnitX.rotate(xrot);
  Vector3::UnitY.rotate(yrot);
  glTranslated(-position.X, 0.0f, -position.Z);
}

void Camera::specialKeyboard(int key, int x, int y)
{
  float xrotrad, yrotrad;
  switch (key)
  {
    case 101: yrotrad = (yrot / 180 * GL_PI);
              xrotrad = (xrot / 180 * GL_PI);
              position.X += float(sin(yrotrad));
              position.Z -= float(cos(yrotrad));
              position.Y -= float(sin(xrotrad));
              break;

    case 103: yrotrad = (yrot / 180 * GL_PI);
              xrotrad = (xrot / 180 * GL_PI);
              position.X -= float(sin(yrotrad));
              position.Z += float(cos(yrotrad));
              position.Y += float(sin(xrotrad));
              break;

    case 102: yrotrad = (yrot / 180 * GL_PI);
              position.X += float(cos(yrotrad)) * 0.2;
              position.Z += float(sin(yrotrad)) * 0.2;
              break;

    case 100: yrotrad = (yrot / 180 * GL_PI);
              position.X -= float(cos(yrotrad)) * 0.2;
              position.Z -= float(sin(yrotrad)) * 0.2;
              break;
  }
}

void Camera::mouseMovement(int x, int y)
{
  int diffx = x - lastx;
  int diffy = y - lasty;
  lastx = x;
  lasty = y;
  xrot += (float) diffy;
  yrot += (float) diffx;
}
void World::keyPress(unsigned char ch)
{
  if (ch >= '1' &amp;&amp; ch <= '4')
  {
    projectors.keyPress(ch);
  }
  glutPostRedisplay();
}
void World::specialKeyPress(int key, int x, int y)
{
  if (projectors.isPerspective())
  {
    camera.specialKeyboard(key, x, y);
  }
  glutPostRedisplay();
}

void World::mouseMovement(int x, int y)
{
  camera.mouseMovement(x,y);
}
  glutSpecialFunc(keyboardSpecial);
  glutPassiveMotionFunc(::mouseMovement);

Cube

#pragma once
#include "actor.h"

struct ColourCube: public  Actor
{
  ColourCube(Geometry* g);
  void render();
};
#include "libs.h"
#include "colourcube.h"
#include "Color.h"

ColourCube::ColourCube(Geometry* g) : Actor(g)
{}

void ColourCube::render()
{
  Actor::render();
}
  foreach (GeometryMap::value_type &amp;value, model->entities)
  {
    string name = value.first;
    Actor *actor;
    if (name == "cube")
    {
      actor = new CubeActor(&amp;value.second);
      animateActors[name] = (AnimateActor*) actor;
    }
    else if (name == "cube2")
    {
      actor = new ColourCube(&amp;value.second);
    }
    else
    {
      actor = new Actor(&amp;value.second);
    }
    actors.insert(name, actor);
  }
void ColourCube::render()
{
  glPolygonMode(GL_FRONT,GL_FILL);
  Actor::render();
  glPolygonMode(GL_FRONT,GL_LINE);
}

ColourCube

void ColourCube::render()
{
  glShadeModel(GL_FLAT);
  glPolygonMode(GL_FRONT,GL_FILL);
  foreach (Face &amp;face, geometry->faces)
  {
    face.render(geometry->vertexGroup->vertices);
  }
  glPolygonMode(GL_FRONT,GL_LINE);
}
void ColourCube::render()
{
  glShadeModel(GL_FLAT);
  glPolygonMode(GL_FRONT,GL_FILL);
  foreach (Face &amp;face, geometry->faces)
  {
    glBegin(GL_QUADS);

    foreach (int index, face.vertexIndices)
    {
      glVertex3f( geometry->vertexGroup->vertices[index-1].X,
                  geometry->vertexGroup->vertices[index-1].Y,
                    geometry->vertexGroup->vertices[index-1].Z );
    }
    glEnd();
  }
  glPolygonMode(GL_FRONT,GL_LINE);
}

Color colours[] =
{
  Color::White,   Color::Yellow, Color::Red,     Color::Magenta,
  Color::Cyan,    Color::Green,  Color::Black,   Color::Blue,
  Color::Cyan,    Color::White,  Color::Magenta, Color::Blue,
  Color::Green,   Color::Yellow, Color::Red,     Color::Black,
  Color::White,   Color::Cyan,   Color::Green,   Color::Yellow,
  Color::Magenta, Color::Blue,   Color::Black,   Color::Red
};

void ColourCube::render()
{
  glShadeModel(GL_FLAT);
  glPolygonMode(GL_FRONT,GL_FILL);
  int edge=0;
  foreach (Face &amp;face, geometry->faces)
  {
    glBegin(GL_QUADS);

    foreach (int index, face.vertexIndices)
    {
      colours[edge].render();
      glVertex3f( geometry->vertexGroup->vertices[index-1].X,
                  geometry->vertexGroup->vertices[index-1].Y,
                  geometry->vertexGroup->vertices[index-1].Z );
      edge++;
    }
    glEnd();
  }
  glPolygonMode(GL_FRONT,GL_LINE);
  Color::White.render();
}

Exercises

Exercise 1

Exercise 2

Exercise 3