-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
160 lines (130 loc) · 4.05 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
#include "glutMaster/glutMaster.h"
#include "GLSL_helper.h"
#include "cameraWindow.h"
#include "Shader.h"
#include "Scene.h"
#include <cstdlib>
#include <cstdio>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp" //perspective, trans etc
using namespace std;
/* intialize ground data */
static Model *initGround(float groundSize, float groundY) {
Model *groundModel = new Model();
Mesh ground("bunny.m");
groundModel->meshes.push_back(ground);
Instance gInst;
glm::mat4 Trans = glm::translate(glm::mat4(1.0f), glm::vec3(0.0,groundY,0) );
gInst.transform = glm::scale(Trans, glm::vec3(groundSize,groundSize,groundSize));
groundModel->instances.push_back(gInst);
return groundModel;
}
float rf() {
return (float)rand()/(float)RAND_MAX;
}
void InitGeom(Scene &s) {
Model *ground = initGround(100.0, -10);
s.models.push_back(*ground);
s.lights.push_back(Light(0,20,1));
}
class MyAction : public KeyAction {
public:
virtual void operator()(Scene scene, double deltaT) {
cout << "MyAction: " << deltaT << endl;
};
};
float pxf(Camera &cam, int deltaT) {
return cos(3.14159*cam.rot/180) * (float)deltaT/1000 * cam.moveSensitivity;
}
float pyf(Camera &cam, int deltaT) {
return sin(3.14159*cam.rot/180) * (float)deltaT/1000 * cam.moveSensitivity;
}
void addKeyResponders(CameraWindow &cw) {
int moveSensitivity = 10;
// Up is pressed
struct UpAction : public KeyAction {
Camera &cam;
Scene &s;
void operator()(int deltaT) {
float px = pxf(cam,deltaT), py = pyf(cam,deltaT);
cam.trans += glm::vec3(-py,0,px);
s.tileFrustum(cam);
}
UpAction(Camera &cam, Scene &scene) : cam(cam), s(scene) { }
};
KeyAction *up = new UpAction(cw.camera,*cw.scene);
cw.addKeyHandler('w',up);
cw.addKeyHandler(',',up);
// Left is pressed
struct LeftAction : public KeyAction {
Camera &cam;
Scene &s;
void operator()(int deltaT) {
float px = pxf(cam,deltaT), py = pyf(cam,deltaT);
cam.trans += glm::vec3(px,0,py);
s.tileFrustum(cam);
}
LeftAction(Camera &cam, Scene &scene) : cam(cam), s(scene) { }
};
KeyAction *left = new LeftAction(cw.camera,*cw.scene);
cw.addKeyHandler('a',left);
// Down is pressed
struct DownAction : public KeyAction {
Camera &cam;
Scene &s;
void operator()(int deltaT) {
float px = pxf(cam,deltaT), py = pyf(cam,deltaT);
cam.trans += glm::vec3(py,0,-px);
s.tileFrustum(cam);
}
DownAction(Camera &cam, Scene &scene) : cam(cam), s(scene) { }
};
KeyAction *down = new DownAction(cw.camera,*cw.scene);
cw.addKeyHandler('s',down);
cw.addKeyHandler('o',down);
// Right is pressed
struct RightAction : public KeyAction {
Camera &cam;
Scene &s;
void operator()(int deltaT) {
float px = pxf(cam,deltaT), py = pyf(cam,deltaT);
cam.trans += glm::vec3(-px,0,-py);
s.tileFrustum(cam);
}
RightAction(Camera &cam, Scene &scene) : cam(cam), s(scene) { }
};
KeyAction *right = new RightAction(cw.camera,*cw.scene);
cw.addKeyHandler('d',right);
cw.addKeyHandler('e',right);
}
void removeKeyResponders(CameraWindow &cw) {
delete cw.removeKeyHandler('w');
delete cw.removeKeyHandler('a');
delete cw.removeKeyHandler('s');
delete cw.removeKeyHandler('d');
cw.removeKeyHandler(',');
cw.removeKeyHandler('o');
cw.removeKeyHandler('e');
}
int main( int argc, char *argv[] ) {
GlutMaster glutMaster;
CameraWindow window(glutMaster, 900, 900, 100, 100, "Grass Demo");
window.camera.trans -= glm::vec3(0.0f,1.5f,0.0f);
#ifndef __APPLE__
glewInit();
#endif
Scene sc;
InitGeom(sc);
window.scene = ≻
sc.tileFrustum(window.camera);
addKeyResponders(window);
//test the openGL version
getGLversion();
window.grassShader = new GrassShader();
window.phongShader = new PhongShader();
window.StartSpinning(glutMaster);
//glutMaster.CallGlutMainLoop();
glutMainLoop();
removeKeyResponders(window);
return 0;
}