Week 1 Assignment - Computational Forms

Lucas Longo

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

#include <OpenGL/gl.h>
#include <GLUT/glut.h>

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

Libraries to be used by the program

float mouseX = 0;
float mouseY = 0;
int windowW = 800;
int windowH = 600;

#define PI 3.14159265358979

Variable declarations
void displayFunc ( void ) Function that takes care of definig your drawing. It takes nor returns any parameters.
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); Clears the screen/bitplane - in this case it clears both the color and depth buffer
glColor3f( 1,0,0 ); Defines the color that is going to be used - Red, Green, and Blue
glRectf( mouseX-5, mouseY-5, mouseX+5, mouseY+5 ); Specifies the coordinates of a rectangle x1, y1, x2, y2. In this case it will draw a rectangle 10 pixels wide and tall around the mouse.
glutPostRedisplay(); Marks the window as needing to be redisplayed
glutSwapBuffers(); Swaps the buffers of the current window if double buffered - it will make the back buffer of the layer in use of the current window to become the contents of the front buffer.
void reshapeFunc ( int w, int h ) Function that takes care of the window size and mode
windowW = w;
windowH = h;
Assigns the global variables w and h to the local variables windowW and windowH
glViewport( 0, 0, w, h ); Specifies the size of the window - in this case it starts at 0,0 and ends at 800,600
glMatrixMode( GL_PROJECTION ); Specifies that the target for subsequent matrix operations will be the projection matrix stack.
glLoadIdentity(); Replaces the current matrix with the identity matrix.
gluOrtho2D( 0,w,0,h ); Defines a 2D orthographic projection matrix from 0 to 800 (left to right) and from 0 to 600 (bottom to top)
glMatrixMode( GL_MODELVIEW ); Specifies that the target for subsequent matrix operations will be the modelview matrix stack.
glLoadIdentity(); Replaces the current matrix with the identity matrix.
void mouseDownFunc ( int button, int state, int x, int y ) Function contains what will happen when the mouse is clicked. Receives the variables button, state, x, and y indicating which button was pressed, the state (pressed or released), and its x and y coordinates.
mouseX = x; Assigns the value of x (received by the function mouseDownFunc) to the global variable mouseX.
mouseY = windowH - y; Subtracts the value of y (received by the function mouseDownFunc) from the global variable windowH and assigns it to the global variable mouseY.
void mouseMoveFunc ( int x, int y ) Function contains what will happen when the mouse is moved. Receives the variables x, and y indicating the x and y coordinates of the mouse.
mouseX = x; Assigns the value of x (received by the function mouseDownFunc) to the global variable mouseX.
mouseY = windowH - y; Subtracts the value of y (received by the function mouseDownFunc) from the global variable windowH and assigns it to the global variable mouseY.
void mouseDragFunc ( int x, int y ) Function contains what will happen when the mouse is dragged. Receives the variables x, and y indicating the x and y coordinates of the mouse.
mouseX = x; Assigns the value of x (received by the function mouseDownFunc) to the global variable mouseX.
mouseY = windowH - y; Subtracts the value of y (received by the function mouseDownFunc) from the global variable windowH and assigns it to the global variable mouseY.
void keyboardFunc ( unsigned char key, int x, int y ) Function contains what will happen when the keyboard is used. Receives the variables key, x, and y indicating the what keyboard key was pressed, x and y coordinates of the mouse.
void arrowKeyFunc ( int a_keys, int x, int y ) Function contains what will happen when the arrow keys are used. Receives the variables a_keys, x, and y indicating the what arrow key was pressed, x and y coordinates of the mouse.
void init ( GLvoid ) Function contains what will happen when GL is initialized.
glShadeModel( GL_SMOOTH ); Selects smooth shading technique.
glClearColor( 1.0, 1.0, 1.0, 1.0 ); Specifies the values of red, green, blue, and alpha as 1 when the buffers are cleared.
glEnable ( GL_COLOR_MATERIAL ); Enables one or more material parameters to track the current color.
glEnable( GL_BLEND ); Enables the blending of the incoming RGBA values with the values in the color buffers.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Specifies how the blending factors are computed. For the source, the pixel arithmetic used will be GL_SRC_ALPHA (Ad/ka, Ad/ka, Ad/ka, Ad/ka). For destination, the pixel arithmetic used will be GL_ONE_MINUS_SRC_ALPA (1, 1, 1, 1) - (Ad/ka, Ad/ka, Ad/ka, Ad/ka).
int main ( int argc, char** argv ) Main function that loops when the program is running. Receives argc and argv used to initalize GLUT library and returns an int (0). ??Where does it receive and send these variables to??
glutInit( &argc, argv ); Calls glutInit function passing it argc and argv as parameters to initialize the GLUT library.
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
Calls function glutInitDisplayMode to initialize the display mode as RGBA mode and as a double buffered window.
glutInitWindowSize( windowW, windowH ); Calls function glutInitWindowSize to set the initial window size passing it the global variables windowW and windowH (800 and 600).
glutCreateWindow( "CompFormApp" ); Calls function glutCreateWindow to create a top-level window with the name CompFormApp.
glutDisplayFunc( displayFunc ); Calls function glutDisplayFunc to set that the new display callback function will be displayFunc for the current window.
glutReshapeFunc( reshapeFunc ); Calls function glutReshapeFunc to se that the new reshape callback function will be reshapeFunc for the current window.
glutMouseFunc( mouseDownFunc ); Calls function glutMouseFunc to se that the new mouse click callback function will be mouseDownFunc for the current window.
glutMotionFunc( mouseDragFunc ); Calls function glutMotionFunc to se that the new mouse movent callback function will be mouseDragFunc for the current window.
glutPassiveMotionFunc( mouseMoveFunc ); Calls function glutPassiveMotionFunc to se that the new passive mouse movement callback function will be mouseMoveFunc for the current window.
glutKeyboardFunc( keyboardFunc ); Calls function glutKeyboardFunc to se that the new keyboard callback function will be keyboardFunc for the current window.
glutSpecialFunc( arrowKeyFunc ); Calls function glutSpecialFunc to se that the new special keyboard callback function will be arrowKeyFunc for the current window.
init(); Calls function init.
glutMainLoop( ); Calls function glutMainLoop.
return 0; Returns a 0 to the function calling main.

 

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.

void drawPortrait()
{
glBegin (GL_TRIANGLE_FAN);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(scale*216, windowH-223*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*363, windowH-242*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*342, windowH-256*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*344, windowH-280*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*337, windowH-288*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*343, windowH-301*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*339, windowH-313*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*343, windowH-327*scale);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(scale*334, windowH-343*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*312, windowH-347*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*264, windowH-345*scale);

glColor4f(0.7,1.0,0.0,2.8);
glVertex2f(scale*261, windowH-371*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*200, windowH-369*scale);

glColor4f(0.6,1.0,0.4,0.8);
glVertex2f(scale*101, windowH-321*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*113, windowH-307*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*73, windowH-306*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*56, windowH-281*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*56, windowH-252*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*87, windowH-210*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*73, windowH-176*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*89, windowH-139*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*124, windowH-78*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*214, windowH-41*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*255, windowH-53*scale);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(scale*307, windowH-62*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*322, windowH-103*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*324, windowH-117*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*336, windowH-149*scale);

glColor4f(0.9,1.0,0.3,0.8);
glVertex2f(scale*344, windowH-163*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*350, windowH-182*scale);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(scale*337, windowH-197*scale);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(scale*363, windowH-242*scale);

glEnd();
}

 

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

void drawEye()
{
glBegin (GL_TRIANGLE_STRIP);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(340, windowH - 300);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(94, windowH - 327);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(176, windowH - 259);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(340, windowH - 300);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(270, windowH - 184);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(406, windowH - 157);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(340, windowH - 300);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(540, windowH - 193);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(520, windowH - 288);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(340, windowH - 300);

glColor4f(0.6,1.0,0.1,0.8);
glVertex2f(438, windowH - 363);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(328, windowH - 413);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(340, windowH - 300);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(213, windowH - 402);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(94, windowH - 327);

glColor4f(0.6,1.0,0.0,0.8);
glVertex2f(340, windowH - 300);

glColor4f(0.2,1.0,0.5,0.8);
glVertex2f(176, windowH - 259);

glEnd();


glBegin (GL_TRIANGLE_STRIP);

glColor4f(0.0,0.0,0.0,0.5);
glVertex2f(340, windowH - 300);
glVertex2f(400, windowH - 300);
glVertex2f(340, windowH - 240);
glVertex2f(340, windowH - 300);
glVertex2f(280, windowH - 300);
glVertex2f(340, windowH - 360);
glVertex2f(340, windowH - 300);
glVertex2f(400, windowH - 300);
glEnd();
}

 

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

void drawInitials(float pointX, float pointY)
{
glColor4f(1,0,0,1);
glBegin (GL_TRIANGLE_STRIP);
glVertex2f(pointX, pointY);
glVertex2f(pointX, pointY + 3);
glVertex2f(pointX + 3, pointY + 3);
glVertex2f(pointX + 3, pointY);
glVertex2f(pointX, pointY);
glEnd();

}
//MY CODE
void drawPortrait()
{
float i = 0;
float pointX = 200;
float pointY = 450;

for (i = 0; i < 200; i++)
{
drawPixel(pointX, pointY - i);
}

pointY = 250;
for (i = 0; i < 150; i++)
{
drawPixel(pointX+i, pointY);
}

pointX = 400;
pointY = 450;

for (i = 0; i < 200; i++)
{
drawPixel(pointX, pointY - i);
}

pointY = 250;

for (i = 0; i < 150; i++)
{
drawPixel(pointX+i, pointY);
}

}

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.

void drawBg()
{
//TOP SQUARE
glBegin (GL_QUADS);
glColor4f(0.0,0.0,1.0,1);
glVertex2f(0,windowH);
glVertex2f(windowW, windowH);
glColor4f(0.1,0.0,0.5,1);
glVertex2f(windowW,windowH/2);
glVertex2f(0,windowH/2);
glEnd();

//BOTTOM SQUARE
glBegin (GL_QUADS);
glColor4f(0.4,0.3,0.05,1);
glVertex2f(0,windowH/2);
glVertex2f(windowW, windowH/2);
glColor4f(0.5,0.4,0.05,1);
glVertex2f(windowW,0);
glVertex2f(0,0);
glEnd();

}

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


void drawMt(int start, float width, float position)
{
if (mtH < 0){
mtH = 0;
}

glBegin (GL_TRIANGLES);
glColor4f(0,0,0,0.8);
glVertex2f(start,windowH/2 - position + mtH);
glVertex2f(start - width, windowH/2 - position);
glVertex2f(start + width, windowH/2 - position);
glEnd();
}

void drawLandscape()
{
//TOP SQUARE
glBegin (GL_QUADS);
glColor4f(0.0,0.0,1.0,1);
glVertex2f(0,windowH);
glVertex2f(windowW, windowH);
glColor4f(0.1,0.0,0.5,1);
glVertex2f(windowW,windowH/2);
glVertex2f(0,windowH/2);
glEnd();

//BOTTOM SQUARE
glBegin (GL_QUADS);
glColor4f(0.4,0.3,0.05,1);
glVertex2f(0,windowH/2);
glVertex2f(windowW, windowH/2);
glColor4f(0.5,0.4,0.05,1);
glVertex2f(windowW,0);
glVertex2f(0,0);
glEnd();

//MOUNTAIN RANGE
mtH = mouseY;

drawMt(10, 30, 40);
drawMt(40, 60, 30);
drawMt(45, 40, 10);
drawMt(60, 30, 30);
drawMt(100, 80, 15);
drawMt(140, 40, 30);
drawMt(180, 100, 50);
drawMt(210, 80, 60);
drawMt(250, 60, 50);
drawMt(300, 100, 50);
drawMt(350, 50, 20);
drawMt(390, 50, 10);
drawMt(420, 80, 30);
drawMt(460, 40, 40);
drawMt(470, 100, 10);
drawMt(500, 80, 20);
drawMt(530, 60, 50);
drawMt(570, 100, 45);
drawMt(590, 50, 30);
drawMt(610, 50, 10);
drawMt(630, 80, 70);
drawMt(670, 40, 60);
drawMt(690, 100, 50);
drawMt(720, 80, 10);
drawMt(750, 60, 20);
drawMt(770, 100, 10);
drawMt(790, 50, 30);

}

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

void drawCloud()
{

cloudAlpha = 1 - x/windowW;

glBegin(GL_TRIANGLE_FAN);
float startX = windowW/2;
float startY = windowH/5 * 4;
float angle;
float radius = 70;

glColor4f(1.0,1.0,1.0,cloudAlpha);
glVertex2f(startX, startY);
for (angle = 0; angle < 360; angle++){
//radius = radius + rand()%100/100 * 200;
//startX = startX + rand()%100/100 * 200;
glVertex2f(startX + 3 * sin(angle) * (radius + rand()%100/20), startY + cos(angle) * (radius + rand()%100/20));
}
glEnd();

}

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

void drawRiver()
{
float width = mouseX/windowW * 200;

glColor4f(0.0,0.0,1.0,0.5);
glBegin(GL_TRIANGLE_STRIP);
glVertex2f(200-width,windowH/2);
glVertex2f(210+width,windowH/2);
glVertex2f(340-width,windowH-345);
glColor4f(0.5,0.0,0.5,0.5);
glVertex2f(455+width,windowH-300);
glVertex2f(490-width,windowH-370);
glVertex2f(560+width,windowH-350);
glVertex2f(520-width,windowH-390);
glColor4f(0.5,0.0,1.0,0.5);
glVertex2f(600+width,windowH-370);
glVertex2f(530-width,windowH-400);
glVertex2f(600+width,windowH-410);
glColor4f(0.0,0.0,1.0,0.5);
glVertex2f(520-width,windowH-420);
glVertex2f(580+width,windowH-460);
glVertex2f(390-width,windowH-470);
glColor4f(0.8,0.0,0.0,0.5);
glVertex2f(510+width,windowH-510);
glVertex2f(90-width,windowH-600);
glVertex2f(390+width,windowH-600);
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.

void drawGrid()
{
glColor4f(1,1,1,0.5);

for(int i = 0; i < windowW; i = i + 10){
glBegin(GL_LINES);
glVertex2f(i,windowH);
glVertex2f(i,0);
glEnd();
}

for(int i = 0; i < windowW; i = i + 10){
glBegin(GL_LINES);
glVertex2f(0,i);
glVertex2f(windowW,i);
glEnd();
}
}

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

void drawDots()
{
glColor4f(1,1,1,0.8);
glPointSize(3);

for(int i = 0; i < windowW; i = i + 10){
for(int x = 0; x < windowH; x = x + 10){
glBegin(GL_POINTS);
glVertex2f(i,x);
glEnd();
}
}
}