Week 1 Assignment (desc)
Younghyun Chung
Problem 1. Write a comment for every line in the main.cpp file.
|
/*
* main.cpp
* CompFormApp
*
* Commented by Younghyun Chung
* Sep 7, 2007
*
* // multiple line block comment
*/
#include <gl/glut.h> // include glut header file
#include <stdio.h> // include standard input and output functions header
#include <stdlib.h> // include standard library header
#include <math.h> // include math header
float mouseX = 0; // set mouseX, mouseY to float type variable and initial value 0
float mouseY = 0; // holds a real number, positive or negative number with a decimal
int windowW = 800; // set windowW, windowH to integer type variable, value 800, 600
int windowH = 600; // holds an integer, positive or negative number with no decimal
#define PI 3.14159265358979 // define PI as a constant value 3.141592...
void displayFunc ( void ) // start of displayFunc() function which is no input, no return
// we can only draw here or in functions called here
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clears the main window
glColor3f( 1,0,0 ); // establishes what color to use for drawing objects, RGB
glRectf( mouseX-5, mouseY-5, mouseX+5, mouseY+5 ); // only function which draws rectangle
// place your drawing code here
glutPostRedisplay(); // for animation. make glutMainLoop() call a registered display callback
glutSwapBuffers(); // draw screen off, then draw screen on. double buffering
} // end of displayFunc()
void reshapeFunc ( int w, int h ) // called when the window is resized or moved
// 1. reestablish the rectangular region that will be the new rendering canvas
// 2. define the coordinate system to which objects will be drawn
// ex. camera
{
windowW = w; // set the first argument w to windowW, new width of the window
windowH = h; // set the second argument h to windowH, new height of the window
// but in this main.cpp example, these two variables are not used
glViewport( 0, 0, w, h ); // adjusts the pixel rectangle for drawing to be the entire new window
// window coordinates, check w/2, h/2.
// next three lines adjust the coordinate system for drawing so that
// the lower left corner is (0, 0) and the upper right corner is (w, h)
glMatrixMode( GL_PROJECTION ); // specifies projection matrix will be modified
glLoadIdentity(); // to clear the currently modifiable matrix for future transformation command
gluOrtho2D( 0,w,0,h ); // puts the origin, (0, 0), in the lowest, leftmost square,
// and makes each square represent one unit
glMatrixMode( GL_MODELVIEW ); // specifies projection matrix will be modified
glLoadIdentity();
}
void mouseDownFunc ( int button, int state, int x, int y ) // called when a mouse button is pressed
{
mouseX = x; // set new mouseX, mouseY to x, y
mouseY = windowH - y; // because the coordinate system is upside down
}
void mouseMoveFunc ( int x, int y ) // called when a mouse is moved while a mouse button is not pressed
{
mouseX = x; // same as above
mouseY = windowH - y; // "
}
void mouseDragFunc ( int x, int y ) // called when a mouse is moved while a mouse button is pressed
{
mouseX = x; // same as above
mouseY = windowH - y; // "
}
void keyboardFunc ( unsigned char key, int x, int y ) // called when key is pressed
{
}
void arrowKeyFunc ( int a_keys, int x, int y ) // called when arrow key is pressed
{
}
void init ( GLvoid ) // minimum of GL initialization
{
glShadeModel( GL_SMOOTH ); // sets the shading model to GL_SMOOTH, drawn with many different colors
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // establishes what color the main window will be cleared to
glEnable( GL_COLOR_MATERIAL ); // turns on a capability which evaluated color values affect the results
// of the lighting equation as if the current color were being modified,
// but no change is made to the tracking lighting parameter of the current color.
glEnable( GL_BLEND ); // turns on a capability which controls blending of RGBA values
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // controls how color values in the fragment being processed
// (source, first argument) are combined with those already
// stored in the framebuffer(dest, 2nd)
// source: (As, As, As), As
// dest: (1, 1, 1) - (Rd, Gd, Bd), 1 - Ad
}
int main ( int argc, char** argv ) // initialize a window and callback functions
{
glutInit( &argc, argv ); // initializes GLUT and processes any command line arguments
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); // specifies whether to use an RGBA or color-index color model
glutInitWindowSize( windowW, windowH ); // specifies the size, in pixels, of the window
glutCreateWindow( "CompFormApp" ); // creates a window with on OpenGL context, returns a unique identifier
// glutInitWindowPosition(int x, int y) // specifies the screen location for the upper-left corner of your window
glutDisplayFunc( displayFunc ); // whenever contents of the window need to be redisplayed, callback function
// registered by glutDisplayFunc() is executed
glutReshapeFunc( reshapeFunc ); // indicates what action should be taken when the window is resized or moved
glutMouseFunc( mouseDownFunc ); // links a mouse button with a routine that's invoked when the mouse button is pressed or released
glutMotionFunc( mouseDragFunc ); // registers a routine to call back when a mouse is moved while a mouse button is pressed
glutPassiveMotionFunc( mouseMoveFunc ); // set the passive motion callbacks respectively for the current window
glutKeyboardFunc( keyboardFunc ); // link a routine with a key is pressed or released
glutSpecialFunc( arrowKeyFunc ); // link a routine with an arrow key
init(); // call init(), minimum of GL initialization
glutMainLoop( ); // all windows are now shown, rendering is now effective, event begins, callback is triggered
return 0; // ISO C requires main to return int
}
|
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. |
void drawPortrait ( void ) // draw face of my head using GL_TRIANGLE_FAN
{
// draw my head in black
glColor3f( 0,0,0 );
glBegin( GL_TRIANGLE_FAN ); // Marks the beginning of a vertext-data list
// type of primitives: Linked fan of triangles
glVertex2f( 278,571 ); glVertex2f( 291,559 ); glVertex2f( 299,542 ); glVertex2f( 303,524 );
glVertex2f( 302,503 ); glVertex2f( 301,490 ); glVertex2f( 299,471 ); glVertex2f( 294,460 );
glVertex2f( 287,453 ); glVertex2f( 283,448 ); glVertex2f( 276,451 ); glVertex2f( 271,441 );
glVertex2f( 268,428 ); glVertex2f( 263,419 ); glVertex2f( 248,409 ); glVertex2f( 234,404 );
glVertex2f( 224,399 ); glVertex2f( 212,395 ); glVertex2f( 200,391 ); glVertex2f( 189,394 );
glVertex2f( 180,399 ); glVertex2f( 173,409 ); glVertex2f( 170,420 ); glVertex2f( 164,431 );
glVertex2f( 159,441 ); glVertex2f( 157,455 ); glVertex2f( 154,467 ); glVertex2f( 154,474 );
glVertex2f( 149,481 ); glVertex2f( 146,490 ); glVertex2f( 152,495 ); glVertex2f( 157,500 );
glVertex2f( 161,507 ); glVertex2f( 164,516 ); glVertex2f( 165,526 ); glVertex2f( 165,539 );
glVertex2f( 164,552 ); glVertex2f( 168,562 ); glVertex2f( 178,575 ); glVertex2f( 187,583 );
glVertex2f( 197,585 ); glVertex2f( 209,589 ); glVertex2f( 218,588 ); glVertex2f( 228,586 );
glVertex2f( 237,584 ); glVertex2f( 249,582 ); glVertex2f( 257,579 ); glVertex2f( 268,577 );
glEnd(); // end of shape of my head
// draw eyes in white
glColor3f ( 255, 255, 255 );
glBegin ( GL_TRIANGLE_STRIP );
glVertex2f( 159,491 ); glVertex2f( 161,489 ); glVertex2f( 167,496 ); glVertex2f( 168,486 );
glVertex2f( 176,495 ); glVertex2f( 177,485 ); glVertex2f( 182,490 );
glEnd(); // end of shape of right eye
glBegin ( GL_TRIANGLE_STRIP );
glVertex2f( 207,488 ); glVertex2f( 212,494 ); glVertex2f( 216,485 ); glVertex2f( 220,495 );
glVertex2f( 225,487 ); glVertex2f( 231,494 ); glVertex2f( 235,490 );
glEnd(); // end of shape of left eye
// write my initial in black
glLineWidth(3); // sets the width, in pixels, for rendered lines. default 0
glColor3f ( 0, 0, 0 );
glBegin ( GL_LINE_STRIP );
glVertex2f( 296,441 ); glVertex2f( 302,426 ); glVertex2f( 311,442 ); glVertex2f( 302,426 );
glVertex2f( 301,409 );
glEnd(); // end of Y
glBegin ( GL_LINE_STRIP );
glVertex2f( 324,442 ); glVertex2f( 323,411 ); glVertex2f( 322,427 ); glVertex2f( 339,428 );
glVertex2f( 340,441 ); glVertex2f( 340,411 );
glEnd(); // end of H
}
|
 : original image
 : get x, y values of points by using a simple processing code for convenience
 : result. outline, eyes, and initials
|
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.
|
// call in displayFunc() as drawLandscape( mouseY, mouseX );
void drawLandscape( float mountainH, float cloudAlpha )
// use mouseY for mountain height, mouseX for cloud alpha value
{
// ground and sky
glBegin ( GL_QUADS );
glColor3f ( 0.5,0.5,0.2 ); // ground
glVertex2f ( 0,0 ); glVertex2f ( 800,0 ); glVertex2f ( 800,200 ); glVertex2f ( 0, 200 );
glColor3f ( 0.4,1,1 ); // sky
glVertex2f ( 0,200 ); glVertex2f ( 800,200 ); glVertex2f ( 800,600 ); glVertex2f ( 0, 600 );
glEnd ();
// mountin
if ( mountainH < 200 ) { // if height is lower than ground level than don't draw
mountainH = 200;
}
glBegin ( GL_TRIANGLES ); // draw four peaks mountain
glColor3f ( 0,1,0 );
glVertex2f ( 0,200 ); glVertex2f ( 150,200 ); glVertex2f ( 75,mountainH );
glVertex2f ( 150,200 ); glVertex2f ( 300,200 ); glVertex2f ( 225,mountainH );
glVertex2f ( 300,200 ); glVertex2f ( 450,200 ); glVertex2f ( 375,mountainH );
glVertex2f ( 450,200 ); glVertex2f ( 600,200 ); glVertex2f ( 525,mountainH );
glEnd();
// cloud
cloudAlpha /= 800; // mouseX: 0~800, cloudAlpha: 0~1
glBegin ( GL_TRIANGLE_FAN );
glColor4f ( 1,1,1,cloudAlpha );
glVertex2f( 612,502 ); glVertex2f( 612,550 ); glVertex2f( 620,557 ); glVertex2f( 636,556 );
glVertex2f( 646,548 ); glVertex2f( 653,541 ); glVertex2f( 664,543 ); glVertex2f( 680,543 );
glVertex2f( 690,538 ); glVertex2f( 700,526 ); glVertex2f( 708,512 ); glVertex2f( 710,499 );
glVertex2f( 706,489 ); glVertex2f( 697,480 ); glVertex2f( 685,472 ); glVertex2f( 667,470 );
glVertex2f( 654,470 ); glVertex2f( 650,467 ); glVertex2f( 648,459 ); glVertex2f( 635,453 );
glVertex2f( 620,450 ); glVertex2f( 605,450 ); glVertex2f( 595,457 ); glVertex2f( 584,471 );
glVertex2f( 576,475 ); glVertex2f( 559,476 ); glVertex2f( 550,482 ); glVertex2f( 543,495 );
glVertex2f( 544,514 ); glVertex2f( 551,527 ); glVertex2f( 564,540 ); glVertex2f( 573,536 );
glVertex2f( 585,537 ); glVertex2f( 592,537 ); glVertex2f( 601,541 ); glVertex2f( 612,550 );
glEnd();
// river
float riverExpand = abs( mouseX - 400 ) / 4; // if mouseX move left or right then check the differece
glBegin ( GL_TRIANGLE_STRIP );
glColor3f ( 0,0,1 );
// draw river. X value of right side of river expands as mouse movement
glVertex2f( 150+riverExpand,200 ); glVertex2f( 94,200 ); glVertex2f( 171+riverExpand,173 );
glVertex2f( 128,138 ); glVertex2f( 203+riverExpand,152 ); glVertex2f( 182,101 );
glVertex2f( 239+riverExpand,148 ); glVertex2f( 234,102 ); glVertex2f( 272+riverExpand,164 );
glVertex2f( 283,130 ); glVertex2f( 301+riverExpand,168 ); glVertex2f( 322,134 );
glVertex2f( 359+riverExpand,167 ); glVertex2f( 352,115 ); glVertex2f( 407+riverExpand,138 );
glVertex2f( 371,92 ); glVertex2f( 428+riverExpand,79 ); glVertex2f( 377,63 );
glVertex2f( 436+riverExpand,53 ); glVertex2f( 362,24 ); glVertex2f( 433+riverExpand,16 );
glVertex2f( 343,0 ); glVertex2f (433+riverExpand,0 );
glEnd();
}
|
 a, b: ground, sky, and mountain range. also used processing code to get the X, Y values of each point
 b: grow vertically with Y position of mouse
 c: transeparant cloud
 c: cloud's alpha value depends on mouse X position
 d: river. put one more mountain for better look
 d: river's width expands as the mouse moves left and right
 apply gradients
// use two colors for gradients
glBegin ( GL_QUADS );
glColor3f ( 0.6,0.6,0.2 ); // ground
glVertex2f ( 0,0 ); glVertex2f ( 800,0 );
glColor3f ( 0.2,0.2,0.1 );
glVertex2f ( 800,200 ); glVertex2f ( 0, 200 );
glColor3f ( 0.4,0.7,1 ); // sky
glVertex2f ( 0,200 ); glVertex2f ( 800,200 );
glColor3f ( 0.0,0.4,0.4 );
glVertex2f ( 800,600 ); glVertex2f ( 0, 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.
b. Draw a dark gray 3 pixel point at ever grid intersection. Use GL_POINTS.
|
void drawGrid ( void )
{
// a. lines
glBegin ( GL_LINES );
glColor3f ( 0.9,0.9,0.9 ); // very light gray
for(int i=10;i<windowW;i+=10) // vertical lines
{
glVertex2f( i,0 );
glVertex2f( i,windowH );
}
for(int j=10;j<windowH;j+=10) // horizontal lines
{
glVertex2f( 0,j );
glVertex2f( windowW,j );
}
glEnd();
// b. points
glPointSize(3.0); // 3 pixel point. why it only works outside of glBegin-glEnd.
glBegin ( GL_POINTS );
glColor3f ( 0.3,0.3,0.3 ); // dark gray
for(int i=10;i<windowW;i+=10) // draw point at every grid intersection
{
for(int j=10;j<windowH;j+=10)
{
glVertex2f( i,j );
}
}
glEnd();
}
|

lines and points
|
|