-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.cpp
271 lines (225 loc) · 5.85 KB
/
client.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
#include "client.h"
#include "shader.h"
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
Client* Client::s_client=NULL;
Client::Client()
:m_model(NULL)
{
}
Client::~Client()
{
}
bool Client::init(int*argc,char ** argv,BaseModel *model)
{
glutInit(argc,argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA );
glutInitWindowPosition( 100, 100 );
glutInitWindowSize(1024, 768 );
glutCreateWindow("glut-opengl");
if(glewInit()!=GLEW_OK)
{
fprintf(stderr,"Fail to initialize GLEW \n");
return false;
}
s_client=this;
/** init GL **/
glClearColor(0.2f,0.2f,0.4f,0.0f);
glEnable(GL_TEXTURE_2D);
/** register glut method **/
registerClient();
/** test method **/
m_model=model;
m_model->load();
#ifdef KEE_SHADER
m_programID=Shader::loadShaders("vertex.txt", "fragment.txt");
m_matrixID=glGetUniformLocation(m_programID,"MVP");
projectViewModel();
#endif
angle_x=0.0f;
angle_y=0.0f;
return true;
}
void Client::start()
{
glutMainLoop();
}
void Client::registerClient()
{
glutDisplayFunc ( frame ); // this is where we render
glutReshapeFunc ( reshape );
glutMouseFunc ( mousebutton );
glutMotionFunc ( mousemove );
glutKeyboardFunc ( keyboard);
glutSpecialFunc (specialKeyHandle);
}
void Client::projectViewModel()
{
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 modelMatrix=glm::mat4(1.0f);
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 projectMatrix = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
// Or, for an ortho camera :
//glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
// Camera matrix
glm::mat4 viewMatrix = glm::lookAt(
glm::vec3(0,1,10), // Camera is at (4,3,3), in World Space
glm::vec3(0,0,0), // and looks at the origin
glm::vec3(0,1,0)) ; // Head is up (set to 0,-1,0 to look upside-down)
// Our ModelViewProjection : multiplication of our 3 matrices
m_mvp = projectMatrix * viewMatrix * modelMatrix; // Remember, matrix multiplication is the other way around
}
void Client::release()
{
s_client->m_model->release();
}
/** ------------------- static register method ------------------------- **/
void Client::frame()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
#ifdef KEE_SHADER
//use shader
glUseProgram(s_client->m_programID);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(s_client->m_matrixID, 1, GL_FALSE, &s_client->m_mvp[0][0]);
#endif
//变换
// Save the matrix state and do the rotations
// glPushMatrix();
s_client->m_camera.render();
glPushMatrix();
glTranslatef(0.0,0.0f,-20.0f);
glRotatef(s_client->angle_x,1.0,0.0,0.0);
glRotatef(s_client->angle_y,0.0,1.0,0.0);
s_client->m_model->draw();
glutSwapBuffers();
glutPostRedisplay();
}
void Client::reshape(int width,int height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLdouble aspect = (GLdouble)width/(GLdouble)height;
GLdouble left = -1.0f;
GLdouble right = 1.0f;
GLdouble bottom = -1.0f;
GLdouble top = 1.0f;
if(aspect < 1.0)
{
bottom /= aspect;
top /= aspect;
}
else
{
left *= aspect;
right *= aspect;
}
// glOrtho(left, right, bottom, top, -1.0, 1.0);
gluPerspective(45,aspect,1.0f,-10000.0f);
// double mat[16];
// glGetDoublev(GL_PROJECTION_MATRIX,mat);
// printMat(mat);
glutPostRedisplay();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/** button 0 left **/
void Client::mousebutton( int button, int state, int x, int y )
{
if(button ==0)
{
s_client->m_mouseLeftPressed=true;
// pos_x=x;
// pos_y=y;
//temp
}
else
{
s_client->m_mouseLeftPressed=false;
}
}
void Client::mousemove( int x, int y )
{
if(s_client->m_mouseLeftPressed)
{
// int deltaX=x-pos_x;
}
}
void Client::keyboard( unsigned char key, int x, int y )
{
switch(key)
{
case 27:
{
s_client->release();
exit(0);
}
break;
case '1':
s_client->angle_x+=10.0f;
break;
case '2':
s_client->angle_y+=10.0f;
break;
case 'l':
s_client->m_model->setWireMode();
break;
case 'a':
s_client->m_camera.rotateY(5.0);
break;
case 'd':
s_client->m_camera.rotateY(-5.0);
break;
case 'w':
s_client->m_camera.moveForwards( -0.1 ) ;
break;
case 's':
s_client->m_camera.moveForwards( 0.1 ) ;
break;
case 'x':
s_client->m_camera.rotateX(5.0);
break;
case 'y':
s_client->m_camera.rotateX(-5.0);
break;
case 'c':
s_client->m_camera.strafeRight(-0.1);
break;
case 'v':
s_client->m_camera.strafeRight(0.1);
break;
case 'f':
s_client->m_camera.moveUpForwards(-0.3);
break;
case 'r':
s_client->m_camera.moveUpForwards(0.3);
break;
case 'm':
s_client->m_camera.rotateZ(-5.0);
break;
case 'n':
s_client->m_camera.rotateZ(5.0);
case ' ':
// s_client->m_camera.reset();
break;
}
glutPostRedisplay();
}
void Client::specialKeyHandle(int key, int x, int y )
{
}
//print mat[4x4]
void Client::printMat(double *mat)
{
for(int i=0;i<16;i++)
{
fprintf(stderr,"%f\t ",mat[i]);
if((i+1)%4 ==0)
{
fprintf(stderr,"\n");
}
}
}