-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
335 lines (273 loc) · 7.97 KB
/
client.c
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <arpa/inet.h>
#include <cglm/cglm.h>
#include <ctype.h>
#include <fcntl.h>
#include <netdb.h>
/* #include <ode/ode.h> */
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <unistd.h>
#include "model.h"
#include "shared.h"
#include "chunks.h"
#include "debug.h"
#include "gl_global.h"
struct {
vec3 position;
versor rotation;
} cam;
struct sockaddr_in servaddr;
int sockfd;
int w = 640, h = 480;
double lx = -1, ly = -1;
double ltick;
int updated_cam = 1;
vec3 frontv = { 0, 0, -1.0f };
vec3 upv = { 0, 1.0f, 0 };
mat4 viewm = {
{ 1.0f, 0, 0, 0 },
{ 0, 1.0f, 0, 0 },
{ 0, 0, 1.0f, 0 },
{ 0, 0, 0, 1.0f },
};
mat4 projm = {
{ 1.0f, 0, 0, 0 },
{ 0, 1.0f, 0, 0 },
{ 0, 0, 1.0f, 0 },
{ 0, 0, 0, 1.0f },
};
struct model fox;
unsigned sprog;
unsigned colorLoc;
unsigned viewLoc, projectionLoc;
unsigned lightPosLoc, viewPosLoc, lightColorLoc, objectColorLoc, objectTextureLoc;
static inline void
sock_init() {
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd == -1) {
perror("socket creation failed");
exit(-1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
servaddr.sin_port = htons(1988);
if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == -1) {
perror("make_nonblocking: fcntl");
exit(-1);
}
}
inline static void
text_send(char *str) {
struct pkg pkg;
pkg.type = PT_TEXT;
snprintf(pkg.sp.text, sizeof(pkg.sp.text) - 1, "%s", str);
sendto(sockfd, &pkg, sizeof(pkg), 0, (struct sockaddr *) &servaddr, sizeof(servaddr));
warn("Wrote '%s' to server.\n", str);
}
void
viewport_init(int nw, int nh)
{
vec3 lookat, up;
w = nw;
h = nh;
glm_perspective(glm_rad(45.0f), (float) w / (float) h, 0.1f, 1000.0f, projm);
glUniformMatrix4fv(projectionLoc, 1, 0, &projm[0][0]);
glm_quat_rotatev(cam.rotation, frontv, lookat);
glm_quat_rotatev(cam.rotation, upv, up);
glm_vec3_add(cam.position, lookat, lookat);
glm_lookat(cam.position, lookat, up, viewm);
glUniformMatrix4fv(viewLoc, 1, 0, &viewm[0][0]);
/* warn("cam p(%f,%f,%f) la(%f,%f,%f) u(%f,%f,%f)\n", */
/* cam.position[0], cam.position[1], cam.position[2], */
/* lookat[0], lookat[1], lookat[2], */
/* up[0], up[1], up[2]); */
}
void
cam_rotate(float angle, float axisx, float axisy, float axisz)
{
versor q;
glm_quat(q, angle, axisx, axisy, axisz);
glm_quat_mul(cam.rotation, q, cam.rotation);
updated_cam = 1;
}
void
cam_translate(float dx, float dy, float dz)
{
vec3 a = { dx, dy, dz };
glm_quat_rotatev(cam.rotation, a, a);
glm_vec3_add(cam.position, a, cam.position);
updated_cam = 1;
}
float cam_speed = 6.0;
float cam_aspeed = 0.8;
static unsigned
shader_load(char *fname, int type)
{
unsigned shader;
int success, fd = open(fname, O_RDONLY);
struct stat stat;
char *source;
CBUG(fd < 0);
success = !fstat(fd, &stat);
CBUG(!success);
source = mmap(NULL, stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
CBUG(!source);
shader = glCreateShader(type);
glShaderSource(shader, 1, (const char * const *) &source, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infolog[512];
glGetShaderInfoLog(shader, sizeof(infolog), NULL, infolog);
BUG("Error compiling shader %s: %s", fname, infolog);
}
munmap(source, stat.st_size);
return shader;
}
unsigned
sprog_load() {
unsigned vert = shader_load("test.vert.glsl", GL_VERTEX_SHADER);
unsigned frag = shader_load("test.frag.glsl", GL_FRAGMENT_SHADER);
unsigned prog = glCreateProgram();
int success;
glAttachShader(prog, vert);
glAttachShader(prog, frag);
/* glBindAttribLocation(prog, 0, "position"); */
/* glBindFragDataLocation(prog, 0, "fragColor"); */
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &success);
if (!success) {
char infolog[512];
glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
BUG("Error linking sprog: %s", infolog);
}
positionLoc = glGetAttribLocation(prog, "coord3d");
normalLoc = glGetAttribLocation(prog, "aNormal");
texCoordLoc = glGetAttribLocation(prog, "aTexCoord");
modelLoc = glGetUniformLocation(prog, "model");
viewLoc = glGetUniformLocation(prog, "view");
projectionLoc = glGetUniformLocation(prog, "projection");
lightPosLoc = glGetUniformLocation(prog, "lightPos");
viewPosLoc = glGetUniformLocation(prog, "viewPos");
lightColorLoc = glGetUniformLocation(prog, "lightColor");
objectColorLoc = glGetUniformLocation(prog, "objectColor");
objectTextureLoc = glGetUniformLocation(prog, "objectTexture");
CBUG(glGetError());
CBUG(positionLoc < 0);
glUseProgram(prog);
glDeleteShader(vert);
glDeleteShader(frag);
CBUG(glGetError());
glUniform1i(objectTextureLoc, 0);
return prog;
}
void
cam_init() {
cam.position[0] = 0;
cam.position[1] = 0;
cam.position[2] = 0;
glm_quat(cam.rotation, glm_deg(0.0f), 0.0f, 1.0f, 0.0f);
}
static void
error_callback(int error, const char *description)
{
warn("GLFW: %d %s\n", error, description);
}
int
main(int argc, char *argv[])
{
vec4 light_pos = { 0.0, 400.0, 0.0, 0.0 };
/* sleep(1); */
sock_init();
glg_init(w, h);
sprog = sprog_load();
glm_mat4_identity(modelm);
glUniformMatrix4fv(modelLoc, 1, 0, &modelm[0][0]);
glUniform3fv(lightPosLoc, 1, light_pos);
glUniform3fv(viewPosLoc, 1, cam.position);
vec3 light_color = { 1.0f, 1.0f, 1.0f };
glUniform3fv(lightColorLoc, 1, light_color);
vec3 object_color = { 1.0f, 1.0f, 1.0f };
glUniform3fv(objectColorLoc, 1, object_color);
cam_init();
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
/* glActiveTexture(GL_TEXTURE0); */
CBUG(model_load(&fox, "fox2.glb"));
chunks_init();
warn("OpenGL version: %s\n", glGetString(GL_VERSION));
text_send("auth One=qovmjbl");
ltick = glfwGetTime();
glfwSetInputMode(glfw_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwSetErrorCallback(error_callback);
while (!glfwWindowShouldClose(glfw_window)) {
double tick = glfwGetTime();
double dt = tick - ltick;
int nw, nh;
ltick = tick;
glfwGetFramebufferSize(glfw_window, &nw, &nh);
if (nw != w || nh != h) {
w = nw;
h = nh;
updated_cam = 1;
}
{
double x, y;
glfwGetCursorPos(glfw_window, &x, &y);
if (lx != -1) {
double dx = x - lx;
double dy = y - ly;
cam_rotate(-2.0f * dx / w, 0.0f, 1.0f, 0.0f);
cam_rotate(-2.0f * dy / h, 1.0f, 0.0f, 0.0f);
}
lx = x;
ly = y;
}
if (glfwGetKey(glfw_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS
|| glfwGetKey(glfw_window, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS) {
if (glfwGetKey(glfw_window, GLFW_KEY_W) == GLFW_PRESS)
cam_rotate(cam_aspeed * dt, 1.0f, 0.0f, 0.0f);
if (glfwGetKey(glfw_window, GLFW_KEY_S) == GLFW_PRESS)
cam_rotate(-cam_aspeed * dt, 1.0f, 0.0f, 0.0f);
if (glfwGetKey(glfw_window, GLFW_KEY_A) == GLFW_PRESS)
cam_rotate(cam_aspeed * dt, 0.0f, 1.0f, 0.0f);
if (glfwGetKey(glfw_window, GLFW_KEY_D) == GLFW_PRESS)
cam_rotate(-cam_aspeed * dt, 0.0f, 1.0f, 0.0f);
if (glfwGetKey(glfw_window, GLFW_KEY_Q) == GLFW_PRESS)
cam_rotate(cam_aspeed * dt, 0.0f, 0.0f, 1.0f);
if (glfwGetKey(glfw_window, GLFW_KEY_E) == GLFW_PRESS)
cam_rotate(-cam_aspeed * dt, 0.0f, 0.0f, 1.0f);
} else {
if (glfwGetKey(glfw_window, GLFW_KEY_W) == GLFW_PRESS)
cam_translate(0, 0, -cam_speed * dt);
if (glfwGetKey(glfw_window, GLFW_KEY_S) == GLFW_PRESS)
cam_translate(0, 0, cam_speed * dt);
if (glfwGetKey(glfw_window, GLFW_KEY_A) == GLFW_PRESS)
cam_translate(-cam_speed * dt, 0, 0);
if (glfwGetKey(glfw_window, GLFW_KEY_D) == GLFW_PRESS)
cam_translate(cam_speed * dt, 0, 0);
if (glfwGetKey(glfw_window, GLFW_KEY_Q) == GLFW_PRESS)
cam_translate(0, cam_speed * dt, 0);
if (glfwGetKey(glfw_window, GLFW_KEY_E) == GLFW_PRESS)
cam_translate(0, -cam_speed * dt, 0);
}
if (updated_cam) {
glViewport(0, 0, w, h);
viewport_init(w, h);
updated_cam = 0;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
model_render(&fox);
chunks_render();
glfwSwapBuffers(glfw_window);
glfwPollEvents();
}
glg_destroy();
close(sockfd);
return 0;
}