/
Final-Village.cpp
289 lines (234 loc) · 9.23 KB
/
Final-Village.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Final-Village.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Models.h"
#include "myCamera.h"
#include "Controls.h"
#include "Skybox.h"
#include "SphereModel.h"
#include "IndoorModel.h"
// The enumerations to make a clear tree.
enum {
Sky = 0,
Terrain = 1,
Car = 2,
Grass = 3,
Earth = 4,
Dude = 5,
Room = 6,
Bed = 7,
Rose = 8,
Dragon,
ReflectBall,
NumNodes,
};
// Several Global variables
stack<Angel::mat4> modelViewStack; // the Model view stack
Node nodes[NumNodes]; // The tree struct
Angel::mat4 projmat; //projection matrix
GLuint glutPro, arrayPro, skyPro, carPro, cubeMapPro; // Several shader programs
myCamera camera( vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 1.0, 0.0, 0.0), 50 ); // initialize the camera
int switchCam = 2; // Camera state.
bool switcher = true;// the switcher in room.
Model *room, *bed, *rose, *dragon, *reflectBall; // the indoor model.
bool indoorFlag = false; //Use to define the camera in the room.
GLfloat roomY = -30.0;
// Car Related Parameters and Variables.
// I need put the car as global variables so that it can drive with the camera
Model *car; // Car Model
Angel::vec4 carPosition(270.0, 0.0, 440.0,1.0); //the terrain's high is 0.0
Angel::vec4 carForward(0.0,0.0,-1.0,0.0) ; // the minimum step of the car's move
float carTurn = 2.0; // the minimum angle of car's turn
float carRotation = 0.0; // store the car's rotation
// Functions Declaration
void cam(); // Camera function, press 'V' to switch different Viewpoints.
void m_glewInitAndVersion(void) {
fprintf(stdout, "OpenGL Version: %s\n", glGetString(GL_VERSION));
fprintf(stdout, "GLSL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
}
void initNodes(){
// Initialize the skybox
Model *skybox = new Skybox("data/sphere.obj", "data/textures/sky.ppm", 512, 512, // Object, textures
&modelViewStack, // Model view stack
skyPro, (GLfloat)10.0 ,&camera); // shader program, rotate value and camera object
// Initialize the Terrain.
Model *terrain = new Model("data/HOUSTON.obj",
&modelViewStack, arrayPro,
vec4( 0.0, 83.0, 0.0, 0 ), vec3(-90,0,0), vec3(1000.0, 1000.0, 1000.0));
// Initialize the Grass Field.
Model *grass = new Model("data/center.obj",
&modelViewStack, arrayPro,
vec4( 0.0, -3.0, 0.0, 0 ), vec3(0,0,0), vec3(1000.0, 1.0, 1000.0),
true, "data/textures/grass.ppm", 64, 64);
// Initialize the Car.
car = new Model("data/sr5.obj",
&modelViewStack,
carPosition, vec3(-90.0, -180.0 + carRotation, 0.0), vec3(2.0, 2.0, 2.0),
carPro, true, 21);
// Initialize the Dude.
Model *dude = new Model("data/al.obj",
&modelViewStack, carPro,
vec4( 275.0, 0, 350.0, 0 ), vec3(0,-1,0), vec3(1.0, 1.0, 1.0));
//Initialize the Earth.
Model *earth = new SphereModel("data/textures/Earth_Front.ppm", "data/textures/Earth_Back.ppm",
"data/textures/Earth_Left.ppm", "data/textures/Earth_Right.ppm",
"data/textures/Earth_Upper.ppm", "data/textures/Earth_Below.ppm",
512,512,
&modelViewStack, cubeMapPro, 1,
true, false,
vec4( 270.0, 3.0, 330.0, 0.0 ), vec3(0,0,200), 5.0);
//Initialize the Room.
room = new IndoorModel("data/cube.obj",
&modelViewStack, arrayPro,
vec4( 295.0, roomY + 8, 350.0, 0 ), vec3(90,0,0), vec3(15.0, 15.0, 10.0),
true, "data/textures/wall.ppm", 64, 64 );
//Initialize the Bed.
bed = new IndoorModel("data/MIDEVIL.obj",
&modelViewStack, carPro,
vec4( 302.0, roomY + 3, 347.0, 0 ), vec3(-90,0,0), vec3(10.0, 10.0, 10.0),
true, "data/textures/wall.ppm", 64, 64 );
//Initialize the Rose.
rose = new IndoorModel("data/rose+vase.obj",
&modelViewStack, carPro,
vec4( 300.0, roomY + 1, 343.0, 0 ), vec3(0,0,0), vec3(2.0, 2.0, 2.0),
true, "data/textures/wall.ppm", 64, 64 );
//Initialize the Dragon.
dragon = new IndoorModel("data/dragon.obj",
&modelViewStack, carPro,
vec4( 300.0, roomY - 1, 354.0, 0 ), vec3(-90,90,0), vec3(3.0, 3.0, 3.0),
true, "data/textures/wall.ppm", 64, 64 );
//Initialize the Ball.
reflectBall = new SphereModel("data/textures/front.ppm", "data/textures/back.ppm",
"data/textures/left.ppm", "data/textures/right.ppm",
"data/textures/top.ppm", "data/textures/bottom.ppm",
512,512,
&modelViewStack, cubeMapPro, 2,
false, true,
vec4( 284.0, roomY + 1.0, 339.0, 0.0 ), vec3(0,-45,180), 3.0);
// Build the tree.
nodes[Sky] = Node(skybox, &nodes[Terrain], NULL);
nodes[Terrain] = Node(terrain, &nodes[Grass], NULL);
nodes[Grass] = Node(grass, &nodes[Dude], NULL);
nodes[Dude] = Node(dude, &nodes[Earth], NULL);
nodes[Earth] = Node(earth, &nodes[Room], NULL);
nodes[Room] = Node(room, NULL, &nodes[Bed]);
nodes[Bed] = Node(bed, NULL, &nodes[Dragon]);
nodes[Dragon] = Node(dragon, &nodes[Rose], NULL );
nodes[Rose] = Node(rose, NULL, &nodes[ReflectBall]);
nodes[ReflectBall] = Node(reflectBall, &nodes[Car], NULL);
// Always put the car in last position,
// for rendering the transparent object successfully.
nodes[Car] = Node(car, NULL, NULL);
}
void init()
{
glutPro = InitShader( "glutPro.v", "glutPro.f" );
arrayPro = InitShader( "arrayPro.v", "arrayPro.f" );
skyPro = InitShader( "skyPro.v", "skyPro.f" );
carPro = InitShader( "carPro.v", "carPro.f" );
cubeMapPro = InitShader( "cubeMap.v", "cubeMap.f" );
Angel::mat4 modelView = Angel::identity_mat(); //tmp
projmat = Angel::Perspective( 45.0, 1, 1, 500 );
modelViewStack.push( modelView ); //stack
glClearColor(0.5, 0.8, 0.9, 1.0);
glClearDepth( 1.0f );
glShadeModel( GL_FLAT );
glEnable( GL_DEPTH_TEST );
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
initNodes();
}
void
traverse( Node* node )
{
if ( node == NULL ) { return; }
modelViewStack.push( modelViewStack.top() );
node->_model->render();
if ( node->_child ) { traverse( node->_child ); }
modelViewStack.pop();
if ( node->_sibling ) { traverse( node->_sibling ); }
}
void idle() {
room->updateSwitcher(switcher);
bed->updateSwitcher(switcher);
dragon->updateSwitcher(switcher);
rose->updateSwitcher(switcher);
reflectBall->updateSwitcher(switcher);
if(camera.getEye().x > 265 && camera.getEye().x < 275 &&
camera.getEye().y > -2 && camera.getEye().y < 8 &&
camera.getEye().z > 325 && camera.getEye().z < 335){
switchCam = 0;
camera.setEye(vec4( 285.0, roomY, 365.0, 1 ));
indoorFlag = true;
};
if( indoorFlag == true &&
camera.getEye().x > 280 && camera.getEye().x < 310 &&
camera.getEye().y > roomY - 2 && camera.getEye().y < roomY + 18 &&
camera.getEye().z > 335 && camera.getEye().z < 366){
}else if (indoorFlag == true){
camera.setEye(vec4( 270.0, 0, 340.0, 1 ));
indoorFlag = false;
};
glutPostRedisplay();
}
void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDepthMask( GL_TRUE );
glUseProgram( glutPro );
glUniformMatrix4fv(glGetUniformLocation( glutPro, "Projection" ), 1, GL_TRUE, projmat); //pass to shader
glUniformMatrix4fv(glGetUniformLocation( glutPro, "ModelView" ), 1, GL_TRUE, modelViewStack.top());
glUseProgram( arrayPro );
glUniformMatrix4fv(glGetUniformLocation( arrayPro, "Projection" ), 1, GL_TRUE, projmat); //pass to shader
glUniformMatrix4fv(glGetUniformLocation( arrayPro, "ModelView" ), 1, GL_TRUE, modelViewStack.top());
glUseProgram( skyPro );
glUniformMatrix4fv(glGetUniformLocation( skyPro, "Projection" ), 1, GL_TRUE, projmat); //pass to shader
glUniformMatrix4fv(glGetUniformLocation( skyPro, "ModelView" ), 1, GL_TRUE, modelViewStack.top());
glUseProgram( cubeMapPro );
glUniformMatrix4fv(glGetUniformLocation( cubeMapPro, "Projection" ), 1, GL_TRUE, projmat); //pass to shader
glUniformMatrix4fv(glGetUniformLocation( cubeMapPro, "ModelView" ), 1, GL_TRUE, modelViewStack.top());
glUseProgram( carPro );
glUniformMatrix4fv(glGetUniformLocation( carPro, "Projection" ), 1, GL_TRUE, projmat); //pass to shader
glUniformMatrix4fv(glGetUniformLocation( carPro, "ModelView" ), 1, GL_TRUE, modelViewStack.top());
glPolygonMode(GL_FRONT, GL_FILL);
cam();
traverse( &nodes[Sky] ); //begin traverse the tree
glutSwapBuffers();
}
void cam() {
if ( switchCam == 1 ) {
camera.setEye(Angel::vec4(carPosition.x + sin(carRotation/180*PI)* 0.5,
carPosition.y + 0.6, carPosition.z + cos(carRotation/180*PI)* 0.5, 1.0 ));
}
if ( switchCam == 2 ) {
camera.setEye(Angel::vec4(carPosition.x + sin(carRotation/180*PI)*5.0,
carPosition.y + 1.0, carPosition.z + cos(carRotation/180*PI)*5.0, 1.0 ));
}
modelViewStack.top() = camera.getLookAt();
}
void reshape( int width, int height ) {
glViewport( 0, 0, (GLsizei) width, (GLsizei) height );
projmat = Angel::Perspective( 45.0, width/height, 1, 500 );
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize( SCREENWIDTH, SCREENHEIGHT );
glutInitWindowPosition( 300, 0 );
glutCreateWindow( "Village" );
m_glewInitAndVersion();
init();
glutReshapeFunc( reshape );
glutDisplayFunc( display );
glutIdleFunc(idle);
glutKeyboardFunc( keyBoard );
glutSpecialFunc( funcKey );
glutMainLoop();
return 0;
}