Objectives

Setup

#include "libopengl.h"

void renderScene(void)
{
  glClear( GL_COLOR_BUFFER_BIT);

  ///

  glFlush();
}

void setupRC()
{
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

  glColor3f(0.0f, 1.0f, 0.0f);

  glOrtho (-100.0f, 100.0f, -100.0f, 100.0f, -100.0f, 100.0f);
}

int main(int argc, char* argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutCreateWindow("lab04");
  glutInitWindowSize(800,600);
  glutDisplayFunc(renderScene);
  setupRC();
  glutMainLoop();

  return 0;
}

Points

void points()
{
}
void renderScene(void)
{
  glClear( GL_COLOR_BUFFER_BIT);

  points();

  glFlush();
}
  glBegin( GL_POINTS);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(50.0f, 40.0f, 0.0f);
    glVertex3f(50.0f, 50.0f, 50.0f);
  glEnd();

Circle

void circlePoints()
{
}
  glBegin( GL_POINTS);
    float x, y, angle;
    for (angle = 0.0f; angle <= (2.0f * M_PI); angle += 0.01f)
    {
      x = 50.0f * sin(angle);
      y = 50.0f * cos(angle);
      glVertex3f(x, y, 0.0f);
    }
  glEnd();
GLfloat sizes[2];
GLfloat step;
GLfloat curSize;
  glGetFloatv(GL_POINT_SIZE_RANGE,sizes);
  glGetFloatv(GL_POINT_SIZE_GRANULARITY,&amp;step);
  curSize = sizes[0];
  glBegin( GL_POINTS);
    float x, y, angle;
    for (angle = 0.0f; angle <= (2.0f * M_PI); angle += 0.01f)
    {
      x = 50.0f * sin(angle);
      y = 50.0f * cos(angle);
      glPointSize(curSize);
      glVertex3f(x, y, 0.0f);
      curSize+=step;
    }
  glEnd();
void circlePoints()
{
    float x, y, angle;
    for (angle = 0.0f; angle <= (2.0f * M_PI); angle += 0.01f)
    {
      x = 50.0f * sin(angle);
      y = 50.0f * cos(angle);
      glPointSize(curSize);
      glBegin( GL_POINTS);
        glVertex3f(x, y, 0.0f);
      glEnd();
      curSize+=step;
    }
}

Lines

void lineLoop()
{
  glBegin( GL_LINE_LOOP);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(50.0f, 50.0f, 0.0f);
    glVertex3f(50.0f, 100.0f, 0.0f);
  glEnd();
}
void circleLines()
{
  glBegin(GL_LINE_LOOP);
    float x, y, angle;
    for (angle = 0.0f; angle <= (2.0f * M_PI); angle += 0.01f)
    {
      x = 50.0f * sin(angle);
      y = 50.0f * cos(angle);
      glVertex3f(x, y, 0.0f);
    }
  glEnd();
}
void linesWidth()
{
  float y;

  glClear(GL_COLOR_BUFFER_BIT);

  for(y = -90.0f; y < 90.0f; y += 20.0f)
  {
    glLineWidth(curSize);

    glBegin(GL_LINES);
      glVertex2f(-80.0f, y);
      glVertex2f(80.0f, y);
    glEnd();

    curSize += 1.0f;
  }
}
void lineStipple()
{
  float y;
  int factor = 3;
  short pattern = 0x5555;

  glClear(GL_COLOR_BUFFER_BIT);

  for(y = -90.0f; y < 90.0f; y += 20.0f)
  {
    glLineStipple(factor,pattern);

    glBegin(GL_LINES);
      glVertex2f(-80.0f, y);
      glVertex2f(80.0f, y);
    glEnd();
  }
}
  glEnable(GL_LINE_STIPPLE);

Exercises

1. Draw a House

2. Define a vertex file format.

2
6
40 40
40 90
70 120
100 90
100 40
40 40
5
50 100
50 120
60 120
60 110

3. Write a method to load the file defined in 2 and draw the house.

  fstream inStream;
  inStream.open(fileName, ios ::in);  // open the file
  if(inStream.fail())
    return;
  int numlines;
  inStream >> numLines;  
  inStream >> x >> y;  
#include <fstream>
using namespace std;

4.More Models