Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // #pragma comment (lib,"glut32.lib")
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <GL/glut.h>
- // window size (side)
- int w_side = 600;
- // position of camera
- float x = 0, z = 5;
- // look-at perspective to camera position
- float look_x = 0, look_z = -1;
- // angle for rotation
- float angle = 0.0f;
- // ground's size (side)
- float g_side = 30;
- void drawHouse () {
- glColor3f(0.0f, 1.0f, 0.0f); // Green
- glBegin(GL_QUADS);
- // Top face (y = 1.0f)
- glVertex3f( 1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f( 1.0f, 1.0f, 1.0f);
- // Bottom face (y = -1.0f)
- glVertex3f( 1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f( 1.0f, -1.0f, -1.0f);
- // Front face (z = 1.0f)
- glVertex3f( 1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f( 1.0f, -1.0f, 1.0f);
- // Back face (z = -1.0f)
- glVertex3f( 1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f( 1.0f, 1.0f, -1.0f);
- // Left face (x = -1.0f)
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- // Right face (x = 1.0f)
- glVertex3f(1.0f, 1.0f, -1.0f);
- glVertex3f(1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, -1.0f);
- glEnd(); // End of drawing color-cube
- }
- void drawGround () {
- glColor3f (0.9f, 0.9f, 0.9f);
- glBegin (GL_QUADS);
- glVertex3f (-g_side - 10, 0.0f, -g_side);
- glVertex3f (-g_side - 10, 0.0f, g_side);
- glVertex3f ( g_side - 10, 0.0f, g_side);
- glVertex3f ( g_side - 10, 0.0f, -g_side);
- glEnd ();
- }
- void changeSize (int w, int h) {
- h = (h > 0 ? h : 1);
- float ratio = (w * 1.0) / h;
- // Use the Projection Matrix
- glMatrixMode (GL_PROJECTION);
- // Reset Matrix
- glLoadIdentity ();
- // Set the viewport to be the entire window
- glViewport (0, 0, w, h);
- // Set the correct perspective.
- gluPerspective (45.0, ratio, 0.1, 100.0);
- // Get Back to the Modelview
- glMatrixMode (GL_MODELVIEW);
- }
- void render () {
- // Clear Color and Depth Buffers
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Reset transformations
- glLoadIdentity ();
- // Set the camera
- gluLookAt (x, 1.0, z, // camera position
- x + look_x, 1.0, z + look_z, // look at
- 0.0, 1.0, 0.0); // up
- drawGround ();
- for (int i = -3; i < 3; ++i) {
- for (int j = -3; j < 3; ++j) {
- glPushMatrix (); {
- glTranslatef (i * 10.0, 0, j * 10.0);
- drawHouse ();
- } glPopMatrix ();
- }
- }
- glutSwapBuffers ();
- }
- void changeMode (unsigned char key, int _x, int _y) {
- }
- void keyEvent (int key, int _x, int _y) {
- float fraction = 0.1f;
- switch (key) {
- case GLUT_KEY_LEFT :
- angle -= .01;
- look_x = sin (angle);
- look_z = -cos (angle);
- break;
- case GLUT_KEY_RIGHT :
- angle += .01;
- look_x = sin (angle);
- look_z = -cos (angle);
- break;
- case GLUT_KEY_UP :
- x += look_x * fraction;
- z += look_z * fraction;
- break;
- case GLUT_KEY_DOWN :
- x -= look_x * fraction;
- z -= look_z * fraction;
- break;
- }
- }
- int main (int argc, char **argv) {
- glutInit (&argc, argv);
- glutInitDisplayMode (GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition (100, 100);
- glutInitWindowSize (2 * w_side, w_side);
- glutCreateWindow ("Graphics Lab Project");
- // register callbacks
- glutDisplayFunc (render);
- glutReshapeFunc(changeSize);
- glutIdleFunc (render);
- glutKeyboardFunc (changeMode);
- glutSpecialFunc (keyEvent);
- glEnable (GL_DEPTH_TEST);
- glutMainLoop ();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment