Lab 5: C Code to Draw Our Dogcow Scene

/*
 Render an animated sequence of a Dogcow Scene - MOOF!
 Paul Azunre and David Wright
 Based heavily on code from Bruce Maxwell
 
 C Version
*/

 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "lib/graphics_env.h"

int main(int argc, char *argv[]) {
    Image *src;
    Module *DogCow;
    Module *DancingDogCow;
    Module *Formation;
    Module *Formation1;
    Module *Formation2;
    Module *Formation3;
    Module *Scene;
    Matrix VTM;
    Matrix GTM;
    Polygon *p;
    Point vertex[100];
    Line line;
    FILE *fp;
    int cnt;
    int rows = 480;
    int cols = 640;
    float viewdx = 640.0;
    float viewdy = viewdx * rows / cols;
    int i;
    Pixel RealBlack = {0, 0, 0};
    Pixel Black = {10, 10, 10};
    Pixel DkGrey = {85, 90, 95};
    Pixel MedGrey = {150, 154, 157};
    Pixel Grey = {175, 178, 181};
    Pixel LtGrey = {188, 189, 190};
    Pixel Yellow = {240, 220, 80};
    Pixel White = {255, 255, 255};

    // initialize matrices
    Matrix_identity(&GTM);
    Matrix_identity(&VTM);
    
    // set the VTM
    //Matrix_translate2D(&VTM, 50, 25);
    Matrix_scale2D(&VTM, cols / viewdx, -rows / viewdy);
    Matrix_translate2D(&VTM, 0.0, rows);
    
    // print out VTM
    Matrix_print(&VTM, stdout);
    
    // initialize the polygon we will be using
    p = Polygon_create();
    
    // create the shot module (used later)
    DogCow = Module_create();
    Module_translate2D(DogCow, -156, -50);
    Module_scale2D(DogCow, 1, -1);
    
    // create a polygon for the DogCow
    cnt = 0;
    Point_set2D(&(vertex[cnt++]), 111, 41);
    Point_set2D(&(vertex[cnt++]), 111, 34);
    Point_set2D(&(vertex[cnt++]), 115, 34);
    Point_set2D(&(vertex[cnt++]), 121, 28);
    Point_set2D(&(vertex[cnt++]), 121, 21);
    Point_set2D(&(vertex[cnt++]), 123, 20);
    Point_set2D(&(vertex[cnt++]), 127, 24);
    Point_set2D(&(vertex[cnt++]), 131, 24);
    Point_set2D(&(vertex[cnt++]), 135, 20);
    Point_set2D(&(vertex[cnt++]), 139, 19);
    Point_set2D(&(vertex[cnt++]), 139, 24);
    Point_set2D(&(vertex[cnt++]), 137, 26);
    Point_set2D(&(vertex[cnt++]), 137, 32);
    Point_set2D(&(vertex[cnt++]), 144, 40);
    Point_set2D(&(vertex[cnt++]), 177, 40);
    Point_set2D(&(vertex[cnt++]), 190, 26);
    Point_set2D(&(vertex[cnt++]), 191, 38);
    Point_set2D(&(vertex[cnt++]), 185, 46);
    Point_set2D(&(vertex[cnt++]), 185, 58);
    Point_set2D(&(vertex[cnt++]), 187, 60);
    Point_set2D(&(vertex[cnt++]), 187, 74);
    Point_set2D(&(vertex[cnt++]), 183, 80);
    Point_set2D(&(vertex[cnt++]), 179, 82);
    Point_set2D(&(vertex[cnt++]), 174, 82);
    Point_set2D(&(vertex[cnt++]), 172, 80);
    Point_set2D(&(vertex[cnt++]), 177, 74);
    Point_set2D(&(vertex[cnt++]), 176, 70);
    Point_set2D(&(vertex[cnt++]), 168, 62);
    Point_set2D(&(vertex[cnt++]), 143, 61);
    Point_set2D(&(vertex[cnt++]), 141, 63);
    Point_set2D(&(vertex[cnt++]), 141, 74);
    Point_set2D(&(vertex[cnt++]), 133, 82);
    Point_set2D(&(vertex[cnt++]), 126, 82);
    Point_set2D(&(vertex[cnt++]), 131, 74);
    Point_set2D(&(vertex[cnt++]), 130, 50);
    Point_set2D(&(vertex[cnt++]), 126, 42);

    Polygon_set(p, cnt, vertex);
    
    // create dow cow module
    Module_addColor(DogCow, White);
    Module_addPolygon(DogCow, p);
    
    //add eye spots to Clarus
    Module_addColor(DogCow, Black);
    //the eye
    cnt = 0;
    Point_set2D(&(vertex[cnt++]), 126, 31);
    Point_set2D(&(vertex[cnt++]), 126, 29);
    Point_set2D(&(vertex[cnt++]), 128, 29);
    Point_set2D(&(vertex[cnt++]), 128, 31);
    Polygon_set(p, cnt, vertex);
    Module_addPolygon(DogCow, p);
    //back spot
    cnt = 0;
    Point_set2D(&(vertex[cnt++]), 151, 40);
    Point_set2D(&(vertex[cnt++]), 174, 40);
    Point_set2D(&(vertex[cnt++]), 166, 48);
    Point_set2D(&(vertex[cnt++]), 158, 49);
    Polygon_set(p, cnt, vertex);
    Module_addPolygon(DogCow, p);
    //front spot
    cnt = 0;
    Point_set2D(&(vertex[cnt++]), 131, 61);
    Point_set2D(&(vertex[cnt++]), 138, 58);
    Point_set2D(&(vertex[cnt++]), 138, 51);
    Point_set2D(&(vertex[cnt++]), 130, 49);
    Polygon_set(p, cnt, vertex);
    Module_addPolygon(DogCow, p);
    
    int j;
    //animation loop
    for(j=0; j<50; j++) {
        
        //create intermediate danging dogcows
        DancingDogCow = Module_create();
        Module_rotateZ(DancingDogCow, cos(-j*2*M_PI/50), sin(-j*2*M_PI/50));
        Module_addModule(DancingDogCow, DogCow);
        
        //create a triangular formation of three dog cows
        Formation = Module_create();
        Module_translate2D(Formation, 100, 100);
        Module_addModule(Formation, DogCow);
        Module_translate2D(Formation, 0, 100);
        Module_addModule(Formation, DogCow);
        Module_translate2D(Formation, 100, -50);
        Module_addModule(Formation, DogCow);
        
        //create a formation that moves left to right across the top
        //smallish
        Formation1 = Module_create();
        Module_scale2D(Formation1, -0.5, 0.5);
        Module_translate2D(Formation1, -100 + 900*j/50, 350);
        Module_addModule(Formation1, Formation);
        
        //create a formation that moves right to left across the top
        //somewhat bigger
        Formation2 = Module_create();
        Module_scale2D(Formation2, 0.75, 0.75);
        Module_translate2D(Formation2, 600 - 900/50*j, 0);
        Module_addModule(Formation2, Formation);
        
        //create a formation of backflipping dogcows in a line
        //intermediate size
        Formation3 = Module_create();
        Module_scale2D(Formation3, 0.6, 0.6);
        Module_translate2D(Formation3, 0, 10*j - 0.2*j*j);
        Module_translate2D(Formation3, 100, 110);
        Module_translate2D(Formation3, 100, 100);
        Module_addModule(Formation3, DancingDogCow);
        Module_translate2D(Formation3, 100, 0);
        Module_addModule(Formation3, DancingDogCow);
        Module_translate2D(Formation3, 100, 0);
        Module_addModule(Formation3, DancingDogCow);
        
        // create the scene with three formations
        Scene = Module_create();
        Module_addModule(Scene, Formation1);
        Module_addModule(Scene, Formation2);
        Module_addModule(Scene, Formation3);
        
        // initialize the image
        src = Image_init(rows, cols);
        for(i=0;i<rows * cols;i++) {
            Image_set1D(src, LtGrey, i);
        }
        
        // draw the scene
        Module_draw(Scene, &VTM, &GTM, LtGrey, src);
        
        //name files for animation sequence
        char name[255] = "animation/out";
        char num[255];
        char *file;
        file = &name[0];

        itoa(j, &num[0]);
        file = strcat(file, num);
        file = strcat(file, ".PPM");
        
        // write out the image
        Image_writePPM(src, file);
        Image_free(src);
    
    }//end animation loop
    
    return(0);
}