Homework 9

For this assignment, we integrated our lighting with our heirarchical modeling. We also used scanline rendering instead of the z-buffer, so we have cool transparencies. This table is lit with a point white light in the upper left and a directional yellow light in the lower right.


Here's the code that created it (just the top level):
#include "GraphicsIncludes.h"

int main(){
  GC *gc;
  Module *cube, *cube2, *cube3, *tabletop, *leg, *table, *scene;
  int i;
  char filename[80];
  vector4 VPN = {0.0,0.0,1.0,1.0};
  vector4 VUP = {0.0,1.0,0.0,1.0};
  vector4 VRP = {0,.5,-2,1.0}; 

  Lighting *source1, *source2, *source3;

  source1 = (Lighting *)malloc(sizeof(Lighting));

  source1->type = POINT;
  source1->c = makeColor(255,255,255);
  source1->p[0] = .5;
  source1->p[1] = 1;
  source1->p[2] = -1;
  source1->p[3] = 1;

  source2 = (Lighting *)malloc(sizeof(Lighting));

  source2->type = DIRECTIONAL;
  source2->c = makeColor(128,128,32);
  source2->d[0] = -1;
  source2->d[1] = 0;
  source2->d[2] = -1;
  source2->d[3] = 0;

  source3 = (Lighting *)malloc(sizeof(Lighting));

  source3->type = AMBIENT;
  source3->c = makeColor(32,32,32);

  VPN[0] = -VRP[0];
  VPN[1] = -VRP[1];
  VPN[2] = -VRP[2];
  

  gc = GCConstructor(300,300);

  cube = NewModule();
  cube2 = NewModule();
  cube3 = NewModule();
  tabletop = NewModule();
  leg = NewModule();
  table = NewModule();
  scene = NewModule();

  for(i=0; i<60; i++){
    printf("Doing image %d\n", i);
    ClearModule(cube);
    ClearModule(cube2);
    ClearModule(cube3);
    ClearModule(table);
    ClearModule(tabletop);
    ClearModule(leg);
    ClearModule(scene);
    FillImage(makeColor(255,255,255), gc);

    InitGlobalTransform();
  


    OpenModule(cube);
    translate3D(0,0,.5, NULL);
    drawSquare(makeColor(128,32,128), 255, NULL);
    matrix4x4Identity(NULL);
    rotateX(-1, 0, NULL);
    translate3D(0,0,-.5, NULL);
    drawSquare(makeColor(128,32,128), 255, NULL);
    matrix4x4Identity(NULL);
    rotateX(0,1,NULL);
    translate3D(0,-.5, 0, NULL);
    drawSquare(makeColor(128,32,128), 255, NULL);
    matrix4x4Identity(NULL);
    rotateX(0,-1,NULL);
    translate3D(0,.5,0,NULL);
    drawSquare(makeColor(128,32,128), 255, NULL);
    matrix4x4Identity(NULL);
    rotateY(0,1,NULL);
    translate3D(.5,0,0,NULL);
    drawSquare(makeColor(128,32,128), 255, NULL);
    matrix4x4Identity(NULL);
    rotateY(0,-1,NULL);
    translate3D(-.5,0,0,NULL);
    drawSquare(makeColor(128,32,128), 255, NULL);
    CloseModule();
    
    OpenModule(cube2);
    translate3D(0,0,.5, NULL);
    drawSquare(makeColor(32,64,32), 128, NULL);
    matrix4x4Identity(NULL);
    rotateX(-1, 0, NULL);
    translate3D(0,0,-.5, NULL);
    drawSquare(makeColor(32,196,128), 128, NULL);
    matrix4x4Identity(NULL);
    rotateX(0,1,NULL);
    translate3D(0,-.5, 0, NULL);
    drawSquare(makeColor(32,196,128), 128, NULL);
    matrix4x4Identity(NULL);
    rotateX(0,-1,NULL);
    translate3D(0,.5,0,NULL);
    drawSquare(makeColor(32,196,128), 128, NULL);
    matrix4x4Identity(NULL);
    rotateY(0,1,NULL);
    translate3D(.5,0,0,NULL);
    drawSquare(makeColor(32,196,128), 128, NULL);
    matrix4x4Identity(NULL);
    rotateY(0,-1,NULL);
    translate3D(-.5,0,0,NULL);
    drawSquare(makeColor(32,196,128), 128, NULL);
    CloseModule();

    OpenModule(cube3);
    translate3D(0,0,.5, NULL);
    drawSquare(makeColor(64,32,8), 255, NULL);
    matrix4x4Identity(NULL);
    rotateX(-1, 0, NULL);
    translate3D(0,0,-.5, NULL);
    drawSquare(makeColor(64,32,8), 255, NULL);
    matrix4x4Identity(NULL);
    rotateX(0,1,NULL);
    translate3D(0,-.5, 0, NULL);
    drawSquare(makeColor(64,32,8), 255, NULL);
    matrix4x4Identity(NULL);
    rotateX(0,-1,NULL);
    translate3D(0,.5,0,NULL);
    drawSquare(makeColor(64,32,8), 255, NULL);
    matrix4x4Identity(NULL);
    rotateY(0,1,NULL);
    translate3D(.5,0,0,NULL);
    drawSquare(makeColor(64,32,8), 255, NULL);
    matrix4x4Identity(NULL);
    rotateY(0,-1,NULL);
    translate3D(-.5,0,0,NULL);
    drawSquare(makeColor(64,32,8), 255, NULL);
    CloseModule();

    OpenModule(tabletop);
    scale3D(2,2,.2,NULL);
    DrawModule(cube2, NULL);
    CloseModule();

    OpenModule(leg);
    scale3D(.15,.15,1.5, NULL);
    DrawModule(cube3, NULL);
    CloseModule();

    OpenModule(table);
    translate3D(0, 0, .1, NULL);
    DrawModule(cube, NULL);
    matrix4x4Identity(NULL);
    translate3D(0,0,-.6, NULL);
    DrawModule(tabletop, NULL);
    matrix4x4Identity(NULL);
    translate3D(.9,.9,-1.4, NULL);
    DrawModule(leg, NULL);
    matrix4x4Identity(NULL);
    translate3D(-.9,-.9,-1.4, NULL);
    DrawModule(leg, NULL);
    matrix4x4Identity(NULL);
    translate3D(-.9,.9,-1.4, NULL);
    DrawModule(leg, NULL);
    matrix4x4Identity(NULL);
    translate3D(.9,-.9,-1.4, NULL);
    DrawModule(leg, NULL);
    CloseModule();

    OpenModule(scene);
    InsertLight(source1, NULL);
    InsertLight(source2, NULL);
    InsertLight(source3, NULL);
    Set3DTransform(NewViewStruct(VRP, VPN, VUP, 2, 2, 2, 0, 6, 300, 300));
    rotateZ(cos(i*2*3.14159/60), sin(i*2*3.14159/60), NULL);
    rotateX(cos(7*3.14159/4), sin(7*3.14159/4), NULL);
    DrawModule(table, NULL);
    CloseModule();

    DrawModule(scene, gc);
    
    DrawTriangles(gc);
    
    //printLightSources();

    sprintf(filename, "table%d.ppm", i);

    WriteImage(filename, gc);
    
    FreeTriangles();
    
    clearLights();

  }

  Dump(gc);
  return 0;
}