LMU ☀️ CMSI 3710
COMPUTER GRAPHICS
HOMEWORK #1 PARTIAL ANSWERS
  1. colorwheel.cpp
    // Shows the fully saturated colorwheel in a main window.
    
    #include <cmath>
    #ifdef __APPLE_CC__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
    
    const double pi = std::acos(-1.0);
    
    // Clears the current window and draws the wheel.
    void display() {
      const int N = 600;            // Number of fans
      double delta = 6.0 / N;       // Amount by which to vary color component
      double theta = 0.0;           // Current angle
    
      // Start with red at 0 radians.
      GLfloat red = 1;
      GLfloat green = 0;
      GLfloat blue = 0;
    
      glClear(GL_COLOR_BUFFER_BIT);
      glBegin(GL_TRIANGLE_FAN);
    
      // Start the triangle fan with a white center point
      glColor3f(1, 1, 1);
      glVertex3f(0, 0, 0);
    
      // For N triangles we need N+1 vertices
      for (int i = 0; i <= N; i++) {
        if (i > 5*N/6) blue -= delta;
        else if (i > 4*N/6) red += delta;
        else if (i > 3*N/6) green -= delta;
        else if (i > 2*N/6) blue += delta;
        else if (i > 1*N/6) red -= delta;
        else green += delta;
        glColor3f(red, green, blue);
        glVertex3f(cos(theta), sin(theta), 0);
        theta += 2.0 * pi / N;
      }
    
      glEnd();
      glFlush();
    }
    
    // Run the application.
    int main(int argc, char** argv) {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowPosition(80, 80);
      glutInitWindowSize(500, 500);
      glutCreateWindow("The Color Wheel");
      glutDisplayFunc(display);
      glutMainLoop();
    }
    
  2. psychysquare.cpp
    // A funky, psychedelic square.
    
    #include <cstdlib>
    #include <ctime>
    #ifdef __APPLE_CC__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
    
    const int WIDTH = 10;
    
    static GLfloat colors[WIDTH + 1][WIDTH + 1][3];
    
    // Clears the window and draws the square.
    void display() {
    
      glClear(GL_COLOR_BUFFER_BIT);
    
      for (int strip = 0; strip < WIDTH; strip++) {
        glBegin(GL_QUAD_STRIP);
        for (int v = 0; v < (WIDTH + 1) * 2; v++) {
          int x = v / 2;
          int z = (v & 1) ? strip + 1 : strip;
          glColor3fv(colors[x][z]);
          glVertex3i(x, 0, z);
        }
        glEnd();
      }
      glFlush();
    }
    
    // Sets up global attributes like clear color and drawing color, and sets up
    // the desired projection and modelview matrices.
    void init() {
    
      // Seed the random number generator.
      srand((unsigned)time(0));
    
      // Fill in the color table with random colors.
      for (int i = 0; i <= WIDTH; i++) {
        for (int j = 0; j <= WIDTH; j++) {
          for (int k = 0; k < 3; k++) {
            colors[i][j][k] = (float)rand()/(float)RAND_MAX;
          }
        }
      }
    
      // Black background for coolness.
      glClearColor(0.0, 0.0, 0.0, 1.0);
    
      // Set the camera lens to have a 70 degree (vertical) field of view, an
      // aspect ratio of 4/3, and have everything closer than 1 unit to the
      // camera and greater than 40 units distant clipped away.
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(70.0, 4.0/3.0, 1, 40);
    
      // Position camera at (4, 7, 5) looking at (0, 0, 0) with the vector
      // <0, 1, 0> pointing upward.
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      gluLookAt(4, 7, 5, 0, 0, 0, 0, 1, 0);
    
      // We defined the square from 0-10 in x and 0-10 in z, but we want (0,0,0)
      // in the center, so move it.
      glTranslated(-5, 0, -5);
    }
    
    // Runs the app.
    int main(int argc, char** argv) {
      glutInit(&argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowPosition(80, 80);
      glutInitWindowSize(800, 600);
      glutCreateWindow("An insipid square");
      glutDisplayFunc(display);
      init();
      glutMainLoop();
    }
    
  3. At each iteration the volume is cut in half but the surface area stays the same. We will prove this in class. It is easy. Or, at least you will think it is easy after you see it worked out.