Objectives

Setup

#define GL_PI 3.1415

void triangleCircle()
{
  glBegin(GL_TRIANGLE_FAN);
    glVertex3f(0.0f, 0.0f, 0.0f);
    float x, y, angle;
    for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
    {
      x = 50.0f*sin(angle);
      y = 50.0f*cos(angle);

      glVertex2f(x, y);
    }
  glEnd();
}

void drawCone(float x, float y, float z, float radius)
{
  glBegin(GL_TRIANGLE_FAN);

  glVertex3f(x, y, z);
  float angle;
  for(angle = 0.0f; angle < (2.0f*GL_PI); angle += (GL_PI/8.0f))
  {
    x = radius*sin(angle);
    y = radius*cos(angle);

    glVertex2f(x, y);
  }
  glEnd();
}
  drawCone(0,0,0, 50);

SpecialKeys & Rotate

void specialKeys(int key, int x, int y)
{
}
  glutSpecialFunc(specialKeys);
  int xRot=0,yRot=0;

  xRot = (key == GLUT_KEY_UP)?    -1 : xRot;
  xRot = (key == GLUT_KEY_DOWN)?   1 : xRot;
  yRot = (key == GLUT_KEY_LEFT)?  -1 : yRot;
  yRot = (key == GLUT_KEY_RIGHT)?  1 : yRot;

  glRotatef(xRot, 1.0f, 0.0f, 0.0f);
  glRotatef(yRot, 0.0f, 1.0f, 0.0f);

  glutPostRedisplay();

  drawCone(0,0,75, 50);

Colour

  int step = 0;
    glColor3f(step % 2 == 0, step % 2, 0.0f);
    step++;

  glPolygonMode(GL_FRONT,GL_FILL);
  glPolygonMode(GL_BACK,GL_FILL);

  glShadeModel(GL_FLAT);

  drawCone(0,0,0, 50);

Depth Testing

  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Culling

  //glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
  glFrontFace(GL_CW);
  drawCone(0,0,75, 50);
  glFrontFace(GL_CCW);
  drawCone(0,0,0, 50);

Exercises

1. Four Cones

2. Animated Cones

3. Coloured Cones