Week 1 Assignment

Matt Parker

 

Problem 1. Write a comment for every line in the main.cpp file.


/*
 *  main.cpp
 *  CompFormApp
 *
 */



#include 	//include glut library

#include 		//include standard io
#include 		//include standard lib
#include 		//include math lib

float	mouseX = 0;		//var to track x position of mouse
float	mouseY = 0;		//var to track y position of mouse
int		windowW = 800;	//var for window width
int		windowH = 600;	//var for window height

#define PI 3.14159265358979  //define constant for PI

void displayFunc ( void )	//function called repeated for drawing screen 
{
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	//clears previous render (both color and depth)
	
	glColor3f( 1,0,0 );					//sets color to red (red, green, blue)
	glRectf( mouseX-5, mouseY-5, mouseX+5, mouseY+5 );	//draws a rectangle based on vars mouseX and mouseY
	
	// place your drawing code here
	
	
	glutPostRedisplay();					//marks the window as needing to be redisplayed
	glutSwapBuffers();					//swaps the buffer of the current window if double buffered
}


void reshapeFunc ( int w, int h )	//function called on window resizing
{
	windowW = w;						//reset window width to new width
	windowH = h;						//reset window height to new height

	glViewport( 0, 0, w, h );				//window coords, drawing region of window
	glMatrixMode( GL_PROJECTION );				//define how the camera operates
	glLoadIdentity();					//replaces the current matrix with the identity matrix
	gluOrtho2D( 0,w,0,h );					//sets up a 2D matrix

	glMatrixMode( GL_MODELVIEW );				//define how we view the model
	glLoadIdentity();					//replaces the current matrix with the identity matrix	
}


void mouseDownFunc ( int button, int state, int x, int y )	//function called when the mouse button is clicked
{
	mouseX = x;						//reset global mouseX value to x
	mouseY = windowH - y;					//reset global mouseY valut to inverted y
}


void mouseMoveFunc ( int x, int y )	//function called when the is mouse is moved
{
	mouseX = x;						//reset global mouseX value to x
	mouseY = windowH - y;					//reset global mouseY valut to inverted y
}


void mouseDragFunc ( int x, int y )	//function called when the is mouse is dragged
{
	mouseX = x;						//reset global mouseX value to x
	mouseY = windowH - y;					//reset global mouseY valut to inverted y
}


void keyboardFunc ( unsigned char key, int x, int y )	//function a standard key is pressed
{
}


void arrowKeyFunc ( int a_keys, int x, int y )	//function an arrow key is pressed
{
}


void init ( GLvoid )	//function called for minimal amount of OpenGL initialization
{
	glShadeModel( GL_SMOOTH );				//set smooth shading
	glClearColor( 1.0, 1.0, 1.0, 1.0 );			//set clear color to white and opaque
	glEnable ( GL_COLOR_MATERIAL );				//allows tracking of current color
	glEnable( GL_BLEND );					//blend the current color values with the color in 
								//the color buffer
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	//sets transparency with primitives sorted from 
								//farthest to nearest.
}


int main ( int argc, char** argv ) //function that gets executed when program starts
{
	glutInit( &argc, argv );				//init glut libraries
	
	glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );		//init creates a double buffered window			
	glutInitWindowSize( windowW, windowH );			//set the init size of the window
	glutCreateWindow( "CompFormApp" );			//make the window with the name "CompFormApp"
	glutDisplayFunc( displayFunc );				//tell glut to use our func when glut is asked to perform this task
	glutReshapeFunc( reshapeFunc );				//tell glut to use our func when glut is asked to perform this task
	glutMouseFunc( mouseDownFunc );				//tell glut to use our func when glut is asked to perform this task
	glutMotionFunc( mouseDragFunc );			//tell glut to use our func when glut is asked to perform this task
	glutPassiveMotionFunc( mouseMoveFunc );			//tell glut to use our func when glut is asked to perform this task
	glutKeyboardFunc( keyboardFunc );			//tell glut to use our func when glut is asked to perform this task
	glutSpecialFunc( arrowKeyFunc );			//tell glut to use our func when glut is asked to perform this task
	init();
	
	glutMainLoop( );					//start glut loop, will call above funcs at correct time
  
	return 0;						//required return statement
}

 

Problem 2. A Portrait.

a. Make a function called drawPortrait that is called from displayFunc. Draw the shape of your head in profile. Use GL_TRIANGLE_FAN. The portrait should be as accurate as possible and involve at least 30 points. Carefully place the first point to ensure a proper fill.

b. Draw the shape of an eye from the front. Use GL_TRIANGLE_STRIP.

c. Draw your initials as stroked lines with a thicknes of 3 pixels. Use GL_LINE_STRIP.

Full Source:
a1p2.h
a1p2.cpp

void drawPortrait(void){

    //Draw Face

    glColor3f(0,0,0);                        
   
    glBegin(GL_TRIANGLE_FAN);   
    
    for(int i = 0; i < facePtsLength; i++){
        glVertex2f(facePts[i][0],facePts[i][1]);
    }
    
    glEnd();
   
    //Draw Eye

    glColor3f(1,1,1);                       
   
    glBegin(GL_TRIANGLE_STRIP);   
    
    for(int i = 0; i < eyePtsLength; i++){
        glVertex2f(eyePts[i][0],eyePts[i][1]);
    }
    
    glEnd();
   
    glColor3f(1,0,0);    

    glLineWidth(3);

    //Draw "M"

    glBegin(GL_LINE_STRIP);   
   
    glVertex2f(600, 50);    
    glVertex2f(600,100);
    glVertex2f(625, 75);
    glVertex2f(650,100);
    glVertex2f(650,50);
   
    glEnd();

    //Draw "P"

    glBegin(GL_LINE_STRIP);   
   
    glVertex2f(670, 50);    
    glVertex2f(670,100);
    glVertex2f(700, 90);
    glVertex2f(700, 80);
    glVertex2f(670, 70);
   
    glEnd();
}
				

 

Problem 3. A Landscape.

a. Make a function called drawLanscape that is called from displayFunc. Draw two gradients, one for the sky and one for the ground. The sky and ground together should fill the window. Use GL_QUADS.

b. Draws a small mountain range. The mountain range should grow vertically with the Y position of the mouse. Use GL_TRIANGLES.

c. Draw a cloud. The cloud's transparency should depend on the mouse X position. Use GL_TRIANGLE_FAN.

d. Draw a winding river. The river's width should expand as the mouse moves left and right. Use GL_TRIANGLE_STRIP.

Full Source:
a1p3.h
a1p3.cpp

void drawLandscape(void){
    
   
    //Sky
   
    for(float y = TOP; y >= HORIZON; y--){
        glColor3f(0,0.25,HORIZON/y);

        glBegin(GL_QUADS);   
   
        glVertex2f(0, y);    
        glVertex2f(X_BORDER,y);
        glVertex2f(X_BORDER, y + 1);
        glVertex2f(0, y + 1);
   
        glEnd();
    }
   
    //Ground
   
    for(float y = 0; y < HORIZON; y++){
        glColor3f(1,y/HORIZON/2,0.1);

        glBegin(GL_QUADS);   
   
        glVertex2f(0, y);    
        glVertex2f(X_BORDER,y);
        glVertex2f(X_BORDER, y + 1);
        glVertex2f(0, y + 1);
   
        glEnd();
    }
   
    //Draw mountains
    for(float x = 0; x < X_BORDER; x = x + 200){   
        glBegin(GL_TRIANGLES);   

        glVertex2f(x, HORIZON);    
        glVertex2f(x + 100, HORIZON + 100 + mouseY/2);
        glVertex2f(x + 200, HORIZON);
   
        glEnd();
    }
   
    for(float x = 100; x < X_BORDER; x = x + 320){   
        glBegin(GL_TRIANGLES);   

        glVertex2f(x, HORIZON);    
        glVertex2f(x + 160, HORIZON + 50 + mouseY/3);
        glVertex2f(x + 320, HORIZON);
   
        glEnd();
    }
   
    //Cloud
   
    glColor4f(1,1,1,mouseX/X_BORDER * 2);
    
    glBegin(GL_TRIANGLE_FAN);   
    
    for(int i = 0; i < cloudPtsLength; i = i + 2){
        glVertex2f(cloudPts[i][0],cloudPts[i][1]);
        glVertex2f(cloudPts[i+1][0],cloudPts[i+1][1]);
    }
    
    glEnd();
    
//     River
    glColor4f(0.1,0.5,1,1); 
    
    glBegin(GL_TRIANGLE_STRIP);   

    for(int i = 0; i < riverPtsLength; i = i + 2){
        glVertex2f(riverPts[i][0] - mouseMove,riverPts[i][1]);
        glVertex2f(riverPts[i+1][0] + mouseMove,riverPts[i+1][1]);
    }
    
    glEnd();
}
				

Problem 4. A Grid.

a. Make a function called drawGrid that draws a grid across the entire window with lines that are spaced 10 pixels apart both horizontally and vertically. Make the color of the grid a very light gray. Use GL_LINES.

b. Draw a dark gray 3 pixel point at ever grid intersection. Use GL_POINTS.

Full Source:
a1p4.h
a1p4.cpp

void drawGrid(void){

    glColor3f(0.75,0.75,0.75);
    glLineWidth(1);
	
	for(int x = 0; x < X_BORDER; x = x + 10){
        glBegin(GL_LINES);   
   
        glVertex2f(x, 0);    
        glVertex2f(x, TOP);
   
        glEnd();
		
	}
	
	for(int y = 0; y < TOP; y = y + 10){
        glBegin(GL_LINES);   
   
        glVertex2f(0, y);    
        glVertex2f(X_BORDER, y);
   
        glEnd();
		
	}
	
	glPointSize(3);
    glColor3f(0.25,0.25,0.25);

	for(int x = 0; x < X_BORDER; x = x + 10){
		for(int y = 0; y < TOP; y = y + 10){
		    glLineWidth(3);
	        glBegin(GL_POINTS); 
	        
	        glVertex2f(x, y);
	   
	        glEnd(); 
		}
	}
}