/* * a1p2.cpp * CompFormApp * * Created by Matthew Parker on 9/7/07. * Copyright 2007 __MyCompanyName__. All rights reserved. * */ #include #include #include #include #include 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(); } void displayFunc ( void ) //what to display on screen (only place to draw on screen) { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //clears the screen (clear color and depth) // place your drawing code here drawLandscape(); glutPostRedisplay(); //redraw off screen buffer (allows for animation) glutSwapBuffers(); //switch from off screen buffer to on screen buffer } void reshapeFunc ( int w, int h ) //resize the window { windowW = w; //update global variables windowH = h; glViewport( 0, 0, w, h ); //window coords, drawing region of window glMatrixMode( GL_PROJECTION ); //how the camera operates glLoadIdentity(); // gluOrtho2D( 0,w,0,h ); // glMatrixMode( GL_MODELVIEW ); //defines what you're drawing glLoadIdentity(); } void mouseDownFunc ( int button, int state, int x, int y ) //called when mouse is pressed (button = which button, state = up or down, x/y = position) { mouseX = x; //set our global mouseX to x mouseY = windowH - y; //set our global mouseY to the invert of y if(state == 0){ printf("{%d,%d}, \n", x, windowH - y); //prints out x, y } } void mouseMoveFunc ( int x, int y ) { if(mouseX != x){ mouseMove++; } mouseX = x; mouseY = windowH - y; } void mouseDragFunc ( int x, int y ) { mouseX = x; mouseY = windowH - y; } void keyboardFunc ( unsigned char key, int x, int y ) //called when a keyboard button is pressed { } void arrowKeyFunc ( int a_keys, int x, int y ) //called when special keys are pressed (arrows, shift, etc) { } void init ( GLvoid ) //minimal amount of OpenGL initialization { glShadeModel( GL_SMOOTH ); glClearColor( 1.0, 1.0, 1.0, 1.0 ); glEnable ( GL_COLOR_MATERIAL ); glEnable( GL_BLEND ); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } int main ( int argc, char** argv ) // * = pointer (** double pointer) { glutInit( &argc, argv ); // & = take the address of glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); // double buffered glutInitWindowSize( windowW, windowH ); // take other functions = callback glutCreateWindow( "CompFormApp" ); glutDisplayFunc( displayFunc ); //display screen glutReshapeFunc( reshapeFunc ); //reshape window size glutMouseFunc( mouseDownFunc ); //mouse click glutMotionFunc( mouseDragFunc ); //dragging mouse glutPassiveMotionFunc( mouseMoveFunc ); //moving mouse glutKeyboardFunc( keyboardFunc ); //key down glutSpecialFunc( arrowKeyFunc ); //special case for arrow key presses/shift/etc. init(); glutMainLoop( ); //runs the glut environment, prevents program from reaching return return 0; //place holder return for main }