All pastes #967093 Raw Edit

imaginaryirk

public text v1 · immutable
·published 2008-04-02 05:28 UTC
rendered paste body

#include <GL/glut.h>
#include <stdlib.h>

/*
 * Constants defining the height and width of the screen to draw to
 */
int height = 	500;
int width = 	500;


/*
 * A global counter for updating the screen
 */
int screenCounter = 0;


/*
 * Callback function for mouse interaction
 */

void mouse (int button, int state, int x, int y) {

    /*
     * Update the counter every time the mouse is pressed,
     * as an indicator to change the screen
     */
    if (state == GLUT_UP) {
	screenCounter++;
	glutPostRedisplay();
    }
}

void randomGrass(int horizon) {

    int i;
    for (i = 0; i < 1000; i++) {
    
	int x1 = random()%width;
	int y1 = random()%horizon;
   
	int y2 = y1+10;
	int x2 = x1 + (random()%10 - 5);

    
	glBegin(GL_LINES);
	glColor3f(0.0,0.5,0.0);
	glVertex2i(x1,y1);
	glVertex2i(x2,y2);
	glEnd();
    }
}

void rocket(int x, int y, float ratio) {

    //rocket
    glBegin(GL_POLYGON);
    glColor3f(0.0,0.0,1.0);
    glVertex2i((x+40)*ratio,y*ratio);
    glVertex2i((90+x)*ratio,y*ratio);
    glVertex2i((90+x)*ratio,(y+200)*ratio);
    glVertex2i((65+x)*ratio,(250+y)*ratio);
    glVertex2i((40+x)*ratio,(200+y)*ratio);
    glEnd();

    glBegin(GL_TRIANGLES);
    glColor3f(1.0,0.9,0.3);
    glVertex2i((40+x)*ratio,y*ratio);
    glVertex2i(x*ratio,y*ratio);
    glVertex2i((40+x)*ratio,(70+y)*ratio);
    glVertex2i((90+x)*ratio,y*ratio);
    glVertex2i((130+x)*ratio,y*ratio);
    glVertex2i((90+x)*ratio,(70+y)*ratio);
    glEnd();

    glBegin(GL_QUADS);
    glVertex2i((40+x)*ratio,(150+y)*ratio);
    glVertex2i((40+x)*ratio,(180+y)*ratio);
    glVertex2i((90+x)*ratio,(180+y)*ratio);
    glVertex2i((90+x)*ratio,(150+y)*ratio);
    glEnd();
}


void stand(int x, int y) {

    glBegin(GL_QUAD_STRIP);
    glColor3f(0.5,0.5,0.5);

    glVertex2i(x,y+20);
    glVertex2i(x,y);
    glVertex2i(20+x,y+40);
    glVertex2i(20+x,y);
 
    glVertex2i(180+x,y+40);
    glVertex2i(180+x,y);
    glVertex2i(200+x,y+20);
    glVertex2i(200+x,y);
    glEnd();

}


void star(int x, int y, float ratio) {
    glBegin(GL_LINE_LOOP);
    glColor3f(1.0,0.9,0.3);
    glVertex2i(x*ratio, y*ratio);
    glVertex2i((x+25)*ratio, (y+100)*ratio);
    glVertex2i((x+50)*ratio, y*ratio);
    glVertex2i((x-15)*ratio,(y+60)*ratio);
    glVertex2i((x+65)*ratio,(y+60)*ratio);
    glEnd();
}

/*
 * Screen 1: Rocket rests on ground
 */
void screen1 (void) {

    //sky
    glBegin(GL_POLYGON);
    glColor3f(0.7,0.7,1.0);
    glVertex2i(0,height);
    glVertex2i(width,height);
    glVertex2i(width, height-350);
    glVertex2i(0,height-350);
    glEnd();

    //ground
    glBegin(GL_POLYGON);
    glColor3f(0.2,1.0,0.2);
    glVertex2i(0,0);
    glVertex2i(0,height-350);
    glVertex2i(width,height-350);
    glVertex2i(width,0);
    glEnd();

    //grass
    randomGrass(150);

    // the props
    rocket(180,100,1);
    stand(145,60);
}

/*
 * Screen 2: Rocket begins launch
 */
void screen2 (void) {

    //sky
    glBegin(GL_POLYGON);
    glColor3f(0.7,0.7,1.0);
    glVertex2i(0,height);
    glVertex2i(width,height);
    glVertex2i(width, height-450);
    glVertex2i(0,height-450);
    glEnd();

    //ground
    glBegin(GL_POLYGON);
    glColor3f(0.2,1.0,0.2);
    glVertex2i(0,0);
    glVertex2i(0,height-450);
    glVertex2i(width,height-450);
    glVertex2i(width,0);
    glEnd();

    //grass
    randomGrass(50);

    // the props
    rocket(180,150,1);
    stand(145,-10);



}

/*
 * Screen 3: Rocket getting high
 */
void screen3 (void) {


}

/*
 * Screen 4: Rocket lost in space
 */
void screen4 (void) {


}

/*
 * display function
 */
void display (void) {

    glClearColor(0.0, 0.0, 0.0, 0.0);

    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();


    gluOrtho2D(0, width, 0, height);
    glViewport(0, 0, width, height); 

    if (screenCounter % 4 == 0)
	screen1();
    else if (screenCounter % 4 == 1)
	screen2();
    else if (screenCounter % 4 == 2)
	screen3();
    else if (screenCounter % 4 == 3)
	screen4();

    glFlush();

}


int main (int argc, char** argv) {

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize ( width, height);
    glutInitWindowPosition(5,5);
    glutCreateWindow("Assignment 1");
    glutMouseFunc(mouse);
    glutDisplayFunc(display);

    glutMainLoop();

    return 0;
}