-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
172 lines (136 loc) · 3.59 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// main.cpp
// RecursivePlay
//
// Created by Dibash Chhetri on 10/29/12.
// Copyright (c) 2012 Dibash Chhetri. All rights reserved.
//
#include <iostream>
#include <OpenGL/OpenGL.h>
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#include <GLUT/GLUT.h>
#include <cmath>
#include <ctime>
#include <string>
#include <array>
#include "utility.h"
#include "vmath.h"
#include "opengl_camera.h"
#include "City.h"
using namespace std;
namespace {
enum DisplayType{TWO_DIM, THREE_DIM};
const DisplayType DISPLAY_MODE = THREE_DIM;
const int WINDOW_WIDTH = 600;
const int WINDOW_HEIGHT = 800;
Camera camera;
City city;
}
void renderScene(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
camera.Render();
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
city.render();
glutSwapBuffers();
}
void changeSize(int w, int h){
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0){h = 1;}
float ratio = 1.0* w / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
if(DISPLAY_MODE == TWO_DIM){
gluOrtho2D(0,WINDOW_WIDTH,0, WINDOW_HEIGHT);
}else{
//3d move
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
}
glTranslatef(0.0f,-1.0f, -10.0f);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
void handleNormalKeyPress(unsigned char key, int x, int y) {
enum {ESCAPE = 27,
KEY_1 = '1', KEY_2 = '2',
KEY_W = 'w', KEY_S = 's', KEY_A = 'a', KEY_D = 'd', KEY_X = 'x', KEY_Y = 'y'};
switch(key){
case ESCAPE:
std::exit(0);
break;
case KEY_X: camera.RotateX(5.0f);
break;
case KEY_Y: camera.RotateX(-5.0f);
break;
case KEY_A: camera.StrafeRight(-0.2f);
break;
case KEY_D: camera.StrafeRight(0.2f);
break;
case KEY_W: camera.MoveUpward(0.2f);
break;
case KEY_S: camera.MoveUpward(-0.2f);
break;
}
glutPostRedisplay();
}
void pressKey(int key, int xx, int yy) {
switch (key) {
case GLUT_KEY_LEFT :
camera.RotateY(1.0);
break;
case GLUT_KEY_RIGHT :
camera.RotateY(-1.0);
break;
case GLUT_KEY_UP :
camera.MoveForward(-0.2f);
break;
case GLUT_KEY_DOWN :
camera.MoveForward(0.2f);
break;
}
glutPostRedisplay();
}
void releaseKey(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
break;
case GLUT_KEY_DOWN :
break;
}
}
void initOpenGL(){
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
camera.Move( Vector3f(0.0, 0.0, 3.0 ));
camera.MoveForward( 1.0 );
}
int main(int argc,char *argv[])
{
srand( (unsigned int)time(0));
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(150,150);
glutInitWindowSize(800,600);
glutCreateWindow("City");
initOpenGL();
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(handleNormalKeyPress);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
// enter GLUT event processing cycle
glutMainLoop();
return 0;
}