Comp Form: Week 1 Assignment

Drawing a portrait, landscape, and grid in C and openGL. . .

Problem 1. Write a comment for every line in the main.cpp file.
/*
* main.cpp
* CompFormApp
*
*/#include <OpenGL/gl.h> //openGL lib
#include <GLUT/glut.h> //glut lib#include <stdio.h> //input output lib
#include <stdlib.h> //standard lib
#include <math.h> //math libfloat mouseX = 0; //new variable, mouseX
float mouseY = 0; //mouseY
int windowW = 800; //window Width
int windowH = 600; //window Height#define PI 3.14159265358979 //variable for pivoid displayFunc ( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //clears screen, color and depth, | binary OR// place your drawing code here
glColor3f( 1,0,0 ); //pen color (r,g,b)
//glColor4f(1,0,0,.5);//pen color + alpha
glRectf( mouseX-5, mouseY-5, mouseX+5, mouseY+5 ); //draw rectangle (only built-in function), corner to cornerglutPostRedisplay(); //repeat, redraw
glutSwapBuffers(); //swaps offscreen buffer, offscreen to onscreen
}void reshapeFunc ( int w, int h )
{
windowW = w; //window width
windowH = h; //window height//camera setup
glViewport( 0, 0, w, h ); //camera view, drawing region (x,y,x,y)
glMatrixMode( GL_PROJECTION );//camera operations matrix
glLoadIdentity();//load projection
gluOrtho2D( 0,w,0,h ); //set camera to ortho, 2dglMatrixMode( GL_MODELVIEW );//drawing space matrix
glLoadIdentity();//load model space
}//mouse functions
void mouseDownFunc ( int button, int state, int x, int y )
{
mouseX = x;
mouseY = windowH - y;//y=0 at bottom of screen
printf(”%d %dn”,x,y);//print mouse x,y loc}void mouseMoveFunc ( int x, int y )//if mouse is moved
{
mouseX = x;
mouseY = windowH - y;
}void mouseDragFunc ( int x, int y )//if mouse is dragged
{
mouseX = x;
mouseY = windowH - y;
}//keyboard functions
void keyboardFunc ( unsigned char key, int x, int y )
{
}void arrowKeyFunc ( int a_keys, int x, int y )
{
}void init ( GLvoid )
{
glShadeModel( GL_SMOOTH ); // flat or smooth shading
glClearColor( 1.0, 1.0, 1.0, 1.0 ); //specify clear values for the color buffers
glEnable ( GL_COLOR_MATERIAL );//enable server side color material tracking
glEnable( GL_BLEND );//enable server side blend of rgb values
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);//specify pixel rgb math
}

//main function at bottom
int main ( int argc, char** argv )
{
glutInit( &argc, argv ); //init GLUT library (& take the address of)

glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); //display mode
glutInitWindowSize( windowW, windowH ); //initialization of window using window sizes
glutCreateWindow( “CompFormApp” ); // create a window with name

//callback functions
glutDisplayFunc( displayFunc ); //draws window 30fps
glutReshapeFunc( reshapeFunc ); //resizing window
glutMouseFunc( mouseDownFunc ); //mousepressed
glutMotionFunc( mouseDragFunc ); //mousedragged
glutPassiveMotionFunc( mouseMoveFunc ); //mouseMoved
glutKeyboardFunc( keyboardFunc ); //keyPressed
glutSpecialFunc( arrowKeyFunc ); //arrow,shift,etc. keyPressed

init();//call void init func

glutMainLoop( );//main event loop, calls custom functions when even occurs

return 0; //returns main=0
}

 

 

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(void){glBegin(GL_TRIANGLE_FAN);//draw triangles from a single starting point
glColor3f( 1,0,0 );
glVertex2f(400,windowH-300);
glVertex2f(371,windowH-518);
glVertex2f(361,windowH-500);
glVertex2f(356,windowH-489);
glVertex2f(350,windowH-480);
glVertex2f(340,windowH-466);
glVertex2f(338,windowH-452);
glVertex2f(337,windowH-442);
glVertex2f(329,windowH-432);
glVertex2f(319,windowH-426);
glVertex2f(300,windowH-428);
glVertex2f(259,windowH-422);
glVertex2f(249,windowH-416);
glVertex2f(244,windowH-410);
glVertex2f(238,windowH-398);
glVertex2f(236,windowH-385);
glVertex2f(241,windowH-371);
glVertex2f(236,windowH-366);
glVertex2f(231,windowH-356);
glVertex2f(233,windowH-347);
glVertex2f(230,windowH-342);
glVertex2f(229,windowH-332);
glVertex2f(230,windowH-321);
glVertex2f(235,windowH-314);
glVertex2f(238,windowH-305);
glVertex2f(232,windowH-300);
glVertex2f(221,windowH-292);
glVertex2f(216,windowH-285);
glVertex2f(220,windowH-272);
glVertex2f(230,windowH-264);
glVertex2f(238,windowH-256);
glVertex2f(250,windowH-248);
glVertex2f(256,windowH-238);
glVertex2f(257,windowH-224);
glVertex2f(255,windowH-207);
glVertex2f(253,windowH-191);
glVertex2f(252,windowH-182);
glVertex2f(259,windowH-174);
glVertex2f(265,windowH-157);
glVertex2f(269,windowH-148);
glVertex2f(278,windowH-134);
glVertex2f(291,windowH-113);
glVertex2f(302,windowH-89);
glVertex2f(320,windowH-77);
glVertex2f(346,windowH-64);
glVertex2f(380,windowH-56);
glVertex2f(440,windowH-53);
glVertex2f(490,windowH-63);
glVertex2f(543,windowH-96);
glVertex2f(577,windowH-147);
glVertex2f(593,windowH-205);
glVertex2f(593,windowH-238);
glVertex2f(589,windowH-270);
glVertex2f(578,windowH-301);
glVertex2f(554,windowH-341);
glVertex2f(546,windowH-358);
glVertex2f(537,windowH-379);
glVertex2f(525,windowH-393);
glVertex2f(525,windowH-404);
glVertex2f(527,windowH-419);
glVertex2f(527,windowH-428);
glVertex2f(530,windowH-443);
glVertex2f(534,windowH-471);
glVertex2f(370,windowH-519);glEnd();//end line strip}
screenshot_1head.png

 

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

void drawEye(){
float scale=120;
int posX=windowW/2-scale*1.5;
int posY=windowH/2;//eyeball
glColor3f( 1,.9,.9 );
glBegin(GL_TRIANGLE_STRIP);//draw triangles from a single starting point
for (float i=0;i<PI;i=i+.1){
glVertex2f(posX+i*scale,posY+sin(i)*scale);//line strip coordinates
glVertex2f(posX+i*scale,posY-sin(i)*scale);//line strip coordinates
}
glEnd();//end line strip//Iris
glColor3f( 0,.4,.35 );
scale=scale*.8;
glBegin(GL_TRIANGLE_FAN);//draw triangles from a single starting point
glVertex2f(windowW/2,windowH/2);//line strip coordinates
for (float i=0;i<=PI*2;i=i+.1){
glVertex2f(windowW/2+scale*sin(i),windowH/2+scale*cos(i));
}
glVertex2f(windowW/2+scale*sin(0),windowH/2+scale*cos(0));
glEnd();//end line strip//pupil
glColor3f( 0,.05,0 );
scale=scale*.5;
glBegin(GL_TRIANGLE_FAN);//draw triangles from a single starting point
glVertex2f(windowW/2,windowH/2);//line strip coordinates
for (float i=0;i<=PI*2;i=i+.1){
glVertex2f(windowW/2+scale*sin(i),windowH/2+scale*cos(i));
}
glVertex2f(windowW/2+scale*sin(0),windowH/2+scale*cos(0));
glEnd();//end line strip}
screenshot_eye.png

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

void drawInitials(){
glLineWidth(3);
glBegin(GL_LINE_STRIP);
glVertex2f(325,windowW/2-170);
glVertex2f(321,windowW/2-182);
glVertex2f(318,windowW/2-198);
glVertex2f(319,windowW/2-217);
glVertex2f(320,windowW/2-223);
glVertex2f(309,windowW/2-225);
glVertex2f(304,windowW/2-211);
glVertex2f(295,windowW/2-190);
glVertex2f(273,windowW/2-181);
glVertex2f(256,windowW/2-177);
glVertex2f(236,windowW/2-175);
glVertex2f(208,windowW/2-182);
glVertex2f(182,windowW/2-201);
glVertex2f(171,windowW/2-212);
glVertex2f(162,windowW/2-228);
glVertex2f(155,windowW/2-244);
glVertex2f(155,windowW/2-250);
glVertex2f(151,windowW/2-270);
glVertex2f(151,windowW/2-280);
glVertex2f(150,windowW/2-292);
glVertex2f(150,windowW/2-304);
glVertex2f(153,windowW/2-315);
glVertex2f(160,windowW/2-330);
glVertex2f(170,windowW/2-352);
glVertex2f(177,windowW/2-360);
glVertex2f(186,windowW/2-368);
glVertex2f(198,windowW/2-378);
glVertex2f(210,windowW/2-382);
glVertex2f(221,windowW/2-386);
glVertex2f(240,windowW/2-390);
glVertex2f(254,windowW/2-389);
glVertex2f(267,windowW/2-388);
glVertex2f(277,windowW/2-385);
glVertex2f(286,windowW/2-381);
glVertex2f(299,windowW/2-374);
glVertex2f(309,windowW/2-365);
glVertex2f(313,windowW/2-356);
glVertex2f(318,windowW/2-349);
glVertex2f(320,windowW/2-343);
glVertex2f(324,windowW/2-346);
glVertex2f(327,windowW/2-350);
glVertex2f(323,windowW/2-360);
glVertex2f(317,windowW/2-374);
glVertex2f(314,windowW/2-385);
glVertex2f(309,windowW/2-393);
glVertex2f(295,windowW/2-399);
glVertex2f(278,windowW/2-402);
glVertex2f(260,windowW/2-404);
glVertex2f(238,windowW/2-404);
glVertex2f(205,windowW/2-401);
glVertex2f(177,windowW/2-391);
glVertex2f(166,windowW/2-386);
glVertex2f(157,windowW/2-380);
glVertex2f(151,windowW/2-373);
glVertex2f(142,windowW/2-365);
glVertex2f(128,windowW/2-346);
glVertex2f(119,windowW/2-327);
glVertex2f(118,windowW/2-315);
glVertex2f(114,windowW/2-288);
glVertex2f(115,windowW/2-271);
glVertex2f(119,windowW/2-245);
glVertex2f(125,windowW/2-228);
glVertex2f(142,windowW/2-203);
glVertex2f(158,windowW/2-191);
glVertex2f(195,windowW/2-173);
glVertex2f(218,windowW/2-168);
glVertex2f(242,windowW/2-161);
glVertex2f(259,windowW/2-162);
glVertex2f(274,windowW/2-162);
glVertex2f(285,windowW/2-165);
glVertex2f(292,windowW/2-167);
glVertex2f(301,windowW/2-170);
glVertex2f(308,windowW/2-172);
glVertex2f(313,windowW/2-169);
glVertex2f(318,windowW/2-167);
glVertex2f(322,windowW/2-167);
glVertex2f(325,windowW/2-169);glEnd();

glBegin(GL_LINE_STRIP);

glVertex2f(424,windowW/2-172);
glVertex2f(425,windowW/2-178);
glVertex2f(420,windowW/2-180);
glVertex2f(412,windowW/2-180);
glVertex2f(407,windowW/2-183);
glVertex2f(406,windowW/2-194);
glVertex2f(420,windowW/2-226);
glVertex2f(470,windowW/2-350);
glVertex2f(474,windowW/2-351);
glVertex2f(476,windowW/2-348);
glVertex2f(520,windowW/2-235);
glVertex2f(522,windowW/2-232);
glVertex2f(521,windowW/2-230);
glVertex2f(506,windowW/2-189);
glVertex2f(501,windowW/2-183);
glVertex2f(498,windowW/2-181);
glVertex2f(493,windowW/2-181);
glVertex2f(488,windowW/2-180);
glVertex2f(485,windowW/2-177);
glVertex2f(485,windowW/2-172);
glVertex2f(486,windowW/2-169);
glVertex2f(554,windowW/2-169);
glVertex2f(557,windowW/2-172);
glVertex2f(558,windowW/2-176);
glVertex2f(554,windowW/2-178);
glVertex2f(547,windowW/2-180);
glVertex2f(542,windowW/2-182);
glVertex2f(539,windowW/2-185);
glVertex2f(539,windowW/2-189);
glVertex2f(598,windowW/2-344);
glVertex2f(598,windowW/2-349);
glVertex2f(603,windowW/2-352);
glVertex2f(604,windowW/2-349);
glVertex2f(655,windowW/2-217);
glVertex2f(659,windowW/2-206);
glVertex2f(662,windowW/2-196);
glVertex2f(663,windowW/2-189);
glVertex2f(660,windowW/2-184);
glVertex2f(651,windowW/2-182);
glVertex2f(646,windowW/2-181);
glVertex2f(642,windowW/2-178);
glVertex2f(643,windowW/2-171);
glVertex2f(645,windowW/2-168);
glVertex2f(655,windowW/2-170);
glVertex2f(693,windowW/2-168);
glVertex2f(702,windowW/2-167);
glVertex2f(707,windowW/2-168);
glVertex2f(708,windowW/2-173);
glVertex2f(708,windowW/2-177);
glVertex2f(699,windowW/2-177);
glVertex2f(689,windowW/2-184);
glVertex2f(688,windowW/2-188);
glVertex2f(612,windowW/2-364);
glVertex2f(609,windowW/2-369);
glVertex2f(603,windowW/2-384);
glVertex2f(597,windowW/2-396);
glVertex2f(593,windowW/2-400);
glVertex2f(589,windowW/2-401);
glVertex2f(585,windowW/2-400);
glVertex2f(583,windowW/2-397);
glVertex2f(530,windowW/2-256);
glVertex2f(469,windowW/2-392);
glVertex2f(468,windowW/2-398);
glVertex2f(459,windowW/2-401);
glVertex2f(456,windowW/2-398);
glVertex2f(452,windowW/2-390);
glVertex2f(377,windowW/2-203);
glVertex2f(375,windowW/2-196);
glVertex2f(369,windowW/2-189);
glVertex2f(367,windowW/2-187);
glVertex2f(366,windowW/2-186);
glVertex2f(361,windowW/2-181);
glVertex2f(356,windowW/2-180);
glVertex2f(349,windowW/2-177);
glVertex2f(347,windowW/2-177);
glVertex2f(351,windowW/2-171);
glVertex2f(356,windowW/2-170);
glVertex2f(424,windowW/2-168);
glVertex2f(425,windowW/2-170);
glEnd();

}

screenshot_1.png

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 drawLandscape(){
glBegin(GL_QUADS);
for (int i=0;i<windowW;i++){
for (int j=0;j<windowH;j++){
if (j>windowH/2){
glColor3f( 1-float(j)/windowH*.7,.4-float(j)/windowH*2,.3+float(j)/windowH/6 ); //pen color (r,g,b)
}
if(j<windowH/2){
glColor3f( .5-float(j)/windowH,.5-float(j)/windowH,.5-float(j)/windowH); //pen color (r,g,b)
}
glVertex2f(i,j);
glVertex2f(i,j+1);
glVertex2f(i+1,j+1);
glVertex2f(i+1,j);}
}
glEnd();}
screenshot_landscape.png

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

 

void drawMountain(){
//mountain range
glColor3f(0,0,0);
glBegin(GL_TRIANGLES);
//+peak
for (int k=0;k<15;k++){
float peakW=90*k;
float peakH=.05;
int peakX=50*k;
for(float i=0;i<peakW/2;i++){
glVertex2f(peakX+i,windowH/2+i*i*mouseY/windowH*peakH);
glVertex2f(peakX+i,windowH/2);
glVertex2f(peakX+i+1,windowH/2);
}
//-peak
for(float i=peakW;i>=peakW/2;i–){
glVertex2f(peakX+i,windowH/2+(peakW-i)*(peakW-i)*mouseY/windowH*peakH);
glVertex2f(peakX+i,windowH/2);
glVertex2f(peakX+i+1,windowH/2);
}
}
glEnd();}
screenshot_mountain.png

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

void drawClouds(){
//clouds
glColor4f(.8,.5,.3,.03*mouseX/windowW);glBegin(GL_TRIANGLE_FAN);
int posX=300;
int posY=500;
glVertex2f(posX,posY);
for (float i=0;i<=windowH/2;i++){
glVertex2f(posX+sin(i)*150,posY+cos(i)*20);
}
glEnd();glBegin(GL_TRIANGLE_FAN);
posX=100;
posY=450;
glVertex2f(posX,posY);
for (float i=0;i<=windowH/2;i++){
glVertex2f(posX+sin(i)*100,posY+cos(i)*10);
}
glEnd();glBegin(GL_TRIANGLE_FAN);
posX=120;
posY=470;
glVertex2f(posX,posY);
for (float i=0;i<=windowH/2;i++){
glVertex2f(posX+sin(i)*500,posY+cos(i)*20);
}
glEnd();}
screenshot_clouds.png

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

void drawRiver(){
glBegin(GL_TRIANGLE_STRIP);for (float i=0;i<=windowH/2;i++){
glColor3f(.6-.6*i/windowH/2,.6-.8*i/windowH,.7-i/windowH);

glVertex2f(100+50*i/windowH+mouseX/i-sin(i*.1)*mouseX/i*10+i,i);
glVertex2f(150-50*i/windowH+mouseX/i+cos(i*.1)*mouseX/i*10+i,i-13);
}
glEnd();
}
screenshot_river.png
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(){
glColor3f(.8,.8,.8);
glBegin(GL_LINES);
for (int i=0;i<windowW;i=i+10){
glVertex2f(i,0);
glVertex2f(i,windowH);glVertex2f(0,i);
glVertex2f(windowW,i);}glEnd();
}
screenshot_grid.png

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

void drawPoints(){
glColor3f(.5,.5,.5);
glPointSize(3.0f);
glBegin(GL_POINTS);
for (int i=0;i<windowW;i=i+10){
for (int j=0;j<windowH;j=j+10){glVertex2f(i,j);}
}
glEnd();
}
screenshot_gridpoints.png