Console Update
[tar-legacy.git] / MCDV / main.cpp
1 #include <glad\glad.h>
2 #include <GLFW\glfw3.h>
3 #include <iostream>
4 #include <fstream>
5 #include <sstream>
6 #include <vector>
7
8 #include <regex>
9
10 #include "GLFWUtil.hpp"
11
12 #include "vbsp.hpp"
13 #include "nav.hpp"
14 #include "radar.hpp"
15 #include "util.h"
16
17 #include "Shader.hpp"
18 #include "Texture.hpp"
19 #include "Camera.hpp"
20 #include "Mesh.hpp"
21 #include "GameObject.hpp"
22 #include "TextFont.hpp"
23 #include "Console.hpp"
24 #include "FrameBuffer.hpp"
25
26 #include <glm\glm.hpp>
27 #include <glm\gtc\matrix_transform.hpp>
28 #include <glm\gtc\type_ptr.hpp>
29
30 void framebuffer_size_callback(GLFWwindow* window, int width, int height);
31 void processInput(GLFWwindow* window, util_keyHandler keys);
32 void mouse_callback(GLFWwindow* window, double xpos, double ypos);
33 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
34 void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
35
36 int window_width = 1024;
37 int window_height = 1024;
38
39 float deltaTime = 0.0f;
40 float lastFrame = 0.0f;
41
42 bool isClicking = false;
43
44 double mousex = 0.0;
45 double mousey = 0.0;
46
47 Camera camera;
48 Console console;
49
50 int SV_WIREFRAME = 0; //0: off 1: overlay 2: only
51 int SV_RENDERMODE = 0;
52 int SV_PERSPECTIVE = 0;
53
54 int M_HEIGHT_MIN = 0.0f;
55 int M_HEIGHT_MAX = 10.0f * 100.0f;
56
57 int M_CLIP_NEAR = 0.1f * 100.0f;
58 int M_CLIP_FAR = 100.0f * 100.0f;
59
60 int T_RENDER_RESOLUTION_X = 1024;
61 int T_RENDER_RESOLUTION_Y = 1024;
62 bool T_ARM_FOR_FB_RENDER = false;
63
64 bool SV_DRAW_BSP = true;
65 bool SV_DRAW_NAV = false;
66
67 bool V_DO_QUIT = false;
68
69 std::string _filesrc_bsp;
70 std::string _filesrc_nav;
71
72 Radar* _radar;
73
74 int M_ORTHO_SIZE = 20;
75
76 // Runtime UI stuffz
77 void update_globals_ui_text(TextFont* target); //Auto update the ui text
78 TextFont* ui_text_info;
79 TextFont* ui_text_loaded;
80
81 //CCMDS
82 void _ccmd_renderbsp() { SV_RENDERMODE = 0; }
83 void _ccmd_rendernav() { SV_RENDERMODE = 1; }
84 void _ccmd_exit() { V_DO_QUIT = true; }
85 void _ccmd_perspective() { SV_PERSPECTIVE = 0; }
86 void _ccmd_orthographic() { SV_PERSPECTIVE = 1; }
87 void _ccmd_render_image() { T_ARM_FOR_FB_RENDER = true; }
88 void _ccmd_reset_ang() { camera.pitch = -90; camera.yaw = 0; camera.mouseUpdate(0, 0, true); }
89 void _ccmd_help() {
90 console.FeedBack(R"TERRI00(
91
92 Controls:
93 WASD Fly
94 Click and drag Look around
95 ` or ~ key Open console
96
97 Commands:
98 General:
99 QUIT / EXIT Closes (wow!)
100 NAV View nav mesh
101 BSP View bsp file
102 HELP Helps you out
103
104 Camera:
105 PERSPECTIVE / PERSP Switches to perspective view
106 ORTHOGRAPHIC / ORTHO Switches to orthographic view
107 OSIZE / SIZE <int> Changes the orthographic scale
108 LOOKDOWN Sets yaw to 0, pitch to -90
109
110 RENDER Renders the view to render.png
111
112 Variables:
113 RENDERMODE <int> Same as nav/bsp switch
114 PROJMATRIX <int> Same as persp/ortho
115
116 Variables (the actual height stuff):
117 MIN <int> Minimum levels of height (units)
118 MAX <int> Maximum levels of height (units)
119
120 FARZ <int> Far clip plane of the camera
121 NEARZ <int> Near clip plane of the camera
122 )TERRI00", MSG_STATUS::SUCCESS);
123 }
124
125 int main(int argc, char* argv[]) {
126 _filesrc_bsp = "D:\\Users\\Harry\\Source\\Repos\\MCDV\\Debug\\killhouse.bsp";
127 _filesrc_nav = "D:\\Users\\Harry\\Source\\Repos\\MCDV\\Debug\\killhouse.nav";
128
129 for (int i = 1; i < argc; ++i) {
130 char* _arg = argv[i];
131 std::string arg = _arg;
132
133 if (split(arg, '.').back() == "bsp") {
134 _filesrc_bsp = arg;
135 }
136
137 if (split(arg, '.').back() == "nav") {
138 _filesrc_nav = arg;
139 }
140 }
141
142 #pragma region Initialisation
143
144 std::cout << "CS:GO Heightmap generator" << std::endl;
145
146 //Initialize OpenGL
147 glfwInit();
148 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //We are using version 3.3 of openGL
149 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
150 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
151 glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
152 //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
153
154 //Creating the window
155 GLFWwindow* window = glfwCreateWindow(window_width, window_height, "CS:GO Heightmap generator", NULL, NULL);
156
157 //Check if window open
158 if (window == NULL)
159 {
160 std::cout << "Failed to create GLFW window" << std::endl;
161 glfwTerminate(); return -1;
162 }
163 glfwMakeContextCurrent(window);
164
165 //Settingn up glad
166 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
167 {
168 std::cout << "Failed to initialize GLAD" << std::endl; return -1;
169 }
170
171 //Viewport
172 glViewport(0, 0, 1024, 1024);
173 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
174
175 //Mouse
176 glfwSetCursorPosCallback(window, mouse_callback);
177 glfwSetScrollCallback(window, scroll_callback);
178 glfwSetMouseButtonCallback(window, mouse_button_callback);
179
180 #pragma endregion
181
182 // configure global opengl state
183 // -----------------------------
184 glEnable(GL_DEPTH_TEST);
185
186 //The unlit shader thing
187 Shader shader_unlit("shaders/unlit.vs", "shaders/unlit.fs");
188 Shader shader_lit("shaders/depth.vs", "shaders/depth.fs");
189
190 //Mesh handling -----------------------------------------------------------------------------
191
192 Mesh* t200 = NULL;
193 Mesh* t201 = NULL;
194
195 if (_filesrc_bsp != ""){
196 vbsp_level bsp_map(_filesrc_bsp, true);
197 t200 = new Mesh(bsp_map.generate_bigmesh());
198 }
199
200 if (_filesrc_nav != "") {
201 Nav::Mesh bob(_filesrc_nav);
202 t201 = new Mesh(bob.generateGLMesh());
203 }
204
205 //Radar rtest("de_overpass.txt");
206 //_radar = &rtest;
207
208 //VertAlphaMesh t300(vbsp_level::genVertAlpha(t200.vertices, t201.vertices));
209
210 util_keyHandler keys(window);
211 //Create camera (test)
212 camera = Camera(&keys);
213
214 glEnable(GL_CULL_FACE);
215 glCullFace(GL_FRONT);
216
217 TextFont::init(); //Setup textfonts before we use it
218
219 ui_text_info = new TextFont("Hello World!");
220 ui_text_info->size = glm::vec2(1.0f / window_width, 1.0f / window_height) * 2.0f;
221 ui_text_info->alpha = 1.0f;
222 ui_text_info->color = glm::vec3(0.75f, 0.75f, 0.75f);
223 ui_text_info->screenPosition = glm::vec2(0, (1.0f / window_height) * 15.0f);
224
225 ui_text_loaded = new TextFont("Currently Loaded:\n " + std::string(_filesrc_bsp) + "\n " + std::string(_filesrc_nav));
226 ui_text_loaded->size = glm::vec2(1.0f / window_width, 1.0f / window_height) * 2.0f;
227 ui_text_loaded->alpha = 1.0f;
228 ui_text_loaded->color = glm::vec3(0.75f, 0.75f, 0.75f);
229 ui_text_loaded->screenPosition = glm::vec2(0, 1.0f - ((1.0f / window_height) * 45.0f));
230
231 update_globals_ui_text(ui_text_info); //Update globals
232
233 //Generate console
234 console = Console(&keys, &window_width, &window_height);
235 console.RegisterCVar("RENDERMODE", &SV_RENDERMODE);
236
237 //Help
238 console.RegisterCmd("HELP", &_ccmd_help);
239
240 //Quit ccmds
241 console.RegisterCmd("QUIT", &_ccmd_exit);
242 console.RegisterCmd("EXIT", &_ccmd_exit);
243
244 //Render modes
245 console.RegisterCmd("NAV", &_ccmd_rendernav);
246 console.RegisterCmd("BSP", &_ccmd_renderbsp);
247
248 console.RegisterCmd("PERSPECTIVE", &_ccmd_perspective);
249 console.RegisterCmd("PERSP", &_ccmd_perspective);
250 console.RegisterCmd("ORTHOGRAPHIC", &_ccmd_orthographic);
251 console.RegisterCmd("ORTHO", &_ccmd_orthographic);
252 console.RegisterCmd("LOOKDOWN", &_ccmd_reset_ang);
253
254 //console.RegisterCVar("SX", &T_RENDER_RESOLUTION_X);
255 //console.RegisterCVar("SY", &T_RENDER_RESOLUTION_Y);
256 console.RegisterCmd("RENDER", &_ccmd_render_image);
257
258 //Register CVARS
259 console.RegisterCVar("RENDERMODE", &SV_RENDERMODE);
260 console.RegisterCVar("PROJMATRIX", &SV_PERSPECTIVE);
261 console.RegisterCVar("MIN", &M_HEIGHT_MIN);
262 console.RegisterCVar("MAX", &M_HEIGHT_MAX);
263 console.RegisterCVar("OSIZE", &M_ORTHO_SIZE);
264 console.RegisterCVar("SIZE", &M_ORTHO_SIZE);
265
266 //Far/near
267 console.RegisterCVar("FARZ", &M_CLIP_FAR);
268 console.RegisterCVar("NEARZ", &M_CLIP_NEAR);
269
270 //FrameBuffer t_frame_buffer = FrameBuffer();
271
272 //The main loop
273 while (!glfwWindowShouldClose(window))
274 {
275 float currentFrame = glfwGetTime();
276 deltaTime = currentFrame - lastFrame;
277 lastFrame = currentFrame;
278
279 //Input
280 processInput(window, keys);
281 if(!console.isEnabled)
282 camera.handleInput(deltaTime);
283
284 console.handleKeysTick();
285
286 if (T_ARM_FOR_FB_RENDER) {
287 //t_frame_buffer.Bind();
288 glViewport(0, 0, T_RENDER_RESOLUTION_X, T_RENDER_RESOLUTION_Y);
289 }
290
291 //Rendering commands
292 glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
293
294
295 if(T_ARM_FOR_FB_RENDER) glClearColor(0.0f, 1.0f, 0.00f, 1.0f);
296
297 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
298 glPolygonMode(GL_FRONT, GL_FILL);
299
300 glDisable(GL_BLEND);
301 glEnable(GL_DEPTH_TEST);
302 glDepthMask(1);
303
304
305 shader_lit.use();
306 if(SV_PERSPECTIVE == 0)
307 shader_lit.setMatrix("projection", glm::perspective(glm::radians(90.0f / 2), (float)window_width / (float)window_height, (float)M_CLIP_NEAR * 0.01f, (float)M_CLIP_FAR * 0.01f));
308 else
309 shader_lit.setMatrix("projection", glm::ortho(-(float)M_ORTHO_SIZE, (float)M_ORTHO_SIZE, -(float)M_ORTHO_SIZE, (float)M_ORTHO_SIZE, (float)M_CLIP_NEAR * 0.01f, (float)M_CLIP_FAR * 0.01f));
310
311 shader_lit.setMatrix("view", camera.getViewMatrix());
312
313 glm::mat4 model = glm::mat4();
314 shader_lit.setMatrix("model", model);
315
316 shader_lit.setVec3("color", 0.0f, 0.0f, 1.0f);
317 shader_lit.setFloat("HEIGHT_MIN", (float)M_HEIGHT_MIN * 0.01f);
318 shader_lit.setFloat("HEIGHT_MAX", (float)M_HEIGHT_MAX * 0.01f);
319
320
321 if (SV_RENDERMODE == 0) {
322 glEnable(GL_CULL_FACE);
323 if (t200 != NULL)
324 t200->Draw();
325 }
326
327 if (SV_RENDERMODE == 1) {
328 glDisable(GL_CULL_FACE);
329 if (t201 != NULL)
330 t201->Draw();
331 }
332
333
334
335
336 if (!T_ARM_FOR_FB_RENDER) { // UI
337 console.draw();
338
339 //ui_text_info->DrawWithBackground();
340 //ui_text_loaded->DrawWithBackground();
341 }
342
343 //Sort out render buffer stuff
344 if (T_ARM_FOR_FB_RENDER) {
345 std::cout << "Done" << std::endl;
346
347 void* data = malloc(3 * T_RENDER_RESOLUTION_X * T_RENDER_RESOLUTION_Y);
348 glReadPixels(0, 0, T_RENDER_RESOLUTION_X, T_RENDER_RESOLUTION_Y, GL_RGB, GL_UNSIGNED_BYTE, data);
349
350 if (data != 0) {
351 stbi_flip_vertically_on_write(true);
352 stbi_write_png("render.png", T_RENDER_RESOLUTION_X, T_RENDER_RESOLUTION_Y, 3, data, T_RENDER_RESOLUTION_X * 3);
353 console.FeedBack("Done! Saved to: render.png", MSG_STATUS::SUCCESS);
354 }
355 else
356 console.FeedBack("Something went wrong making render", MSG_STATUS::ERR);
357
358 //t_frame_buffer.Unbind();
359 glViewport(0, 0, window_width, window_height);
360 T_ARM_FOR_FB_RENDER = false;
361 }
362
363
364 //Check and call events, swap buffers
365 glfwSwapBuffers(window);
366 glfwPollEvents();
367
368 if (V_DO_QUIT)
369 break;
370 }
371
372 //Exit safely
373 glfwTerminate();
374 return 0;
375 }
376
377 //Automatically readjust to the new size we just received
378 void framebuffer_size_callback(GLFWwindow* window, int width, int height)
379 {
380 glViewport(0, 0, width, height);
381 window_width = width;
382 window_height = height;
383 }
384
385 bool K_CONTROL_MIN = false;
386 bool K_CONTROL_MAX = false;
387 int SV_EDITMODE = 0;
388
389 void setWindowTitle() {
390 std::string title = "BigmanEngine | ";
391
392 }
393
394 void processInput(GLFWwindow* window, util_keyHandler keys)
395 {
396 if (keys.getKeyDown(GLFW_KEY_ESCAPE))
397 glfwSetWindowShouldClose(window, true);
398
399 if (keys.getKeyDown(GLFW_KEY_GRAVE_ACCENT))
400 console.isEnabled = !console.isEnabled;
401
402
403 return;
404 if (keys.getKeyDown(GLFW_KEY_1)) {
405 SV_EDITMODE = 0;
406 update_globals_ui_text(ui_text_info); //Update globals
407 }
408 if (keys.getKeyDown(GLFW_KEY_2)) {
409 SV_EDITMODE = 1;
410 update_globals_ui_text(ui_text_info); //Update globals
411 }
412 if (keys.getKeyDown(GLFW_KEY_3)) {
413 SV_EDITMODE = 2; //glfwSetWindowTitle(window, "Bigman Engine :: de_overpass.bsp - EDITING NEAR");
414 update_globals_ui_text(ui_text_info); //Update globals
415 }
416 if (keys.getKeyDown(GLFW_KEY_4)) {
417 SV_EDITMODE = 3; //glfwSetWindowTitle(window, "Bigman Engine :: de_overpass.bsp - EDITING FAR");
418 update_globals_ui_text(ui_text_info); //Update globals
419 }
420
421 if (keys.getKeyDown(GLFW_KEY_5)) {
422 SV_PERSPECTIVE = 0;
423 update_globals_ui_text(ui_text_info); //Update globals
424 }
425
426 if (keys.getKeyDown(GLFW_KEY_6)) {
427 SV_PERSPECTIVE = 1;
428 update_globals_ui_text(ui_text_info); //Update globals
429 }
430
431
432 if (keys.getKeyDown(GLFW_KEY_7)) {
433 SV_RENDERMODE = 0;
434 update_globals_ui_text(ui_text_info); //Update globals
435
436 }
437
438 if (keys.getKeyDown(GLFW_KEY_8)) {
439 SV_RENDERMODE = 1;
440 update_globals_ui_text(ui_text_info); //Update globals
441 }
442
443 if (keys.getKeyDown(GLFW_KEY_9)) {
444 SV_EDITMODE = 4;
445 //M_ORTHO_SIZE = (_radar->scale / 0.1f) / 2.0f;
446 //camera.cameraPos.x = (-_radar->pos_x ) * 0.01f;
447 //camera.cameraPos.z = (_radar->pos_y - 1024) * 0.01f;
448 update_globals_ui_text(ui_text_info); //Update globals
449
450 }
451
452 if (keys.getKeyDown(GLFW_KEY_0)) {
453 camera.yaw = 0;
454 camera.pitch = -90;
455 camera.mouseUpdate(0, 0, true);
456 //camera.cameraFront = glm::vec3(0, 0, -1);
457 //camera.cameraUp = glm::vec3(0, 1, 0);
458 }
459 }
460
461 std::string _globals_editmode_lookups[] = {
462 "Minimum Z",
463 "Maximum Z",
464 "Far Clip plane",
465 "Near Clip plane",
466 "Orthographic Size"
467 };
468
469 void update_globals_ui_text(TextFont* target) {
470
471 std::ostringstream ss;
472
473 ss << "Perspective: " << (SV_PERSPECTIVE == 0 ? "Perspective" : "Orthographic") << "\n";
474 if (SV_PERSPECTIVE == 1) ss << "Ortho scale: " << M_ORTHO_SIZE << "\n";
475 ss << "Viewing: " << (SV_RENDERMODE == 0 ? "BSP" : "NAV") << "\n";
476 ss << "Editing: " << _globals_editmode_lookups[SV_EDITMODE] << "\n";
477
478
479 target->SetText(ss.str());
480 }
481
482 void mouse_callback(GLFWwindow* window, double xpos, double ypos)
483 {
484 camera.mouseUpdate(xpos, ypos, isClicking);
485 mousex = xpos; mousey = ypos;
486 }
487
488 void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
489 {
490 //camera.fov = glm::clamp(camera.fov + (float)yoffset, 2.0f, 90.0f);
491
492 if(SV_EDITMODE == 0)
493 M_HEIGHT_MIN += (float)yoffset * 0.1f;
494
495 if (SV_EDITMODE == 1)
496 M_HEIGHT_MAX += (float)yoffset * 0.1f;
497
498 if (SV_EDITMODE == 4)
499 M_ORTHO_SIZE += (float)yoffset * 0.1f;
500
501 //if (SV_EDITMODE == 2) M_CLIP_NEAR += (float)yoffset * 0.1f;
502
503 //if (SV_EDITMODE == 3) M_CLIP_FAR += (float)yoffset * 0.1f;
504 }
505
506 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
507 {
508 if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
509 {
510 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
511 isClicking = true;
512 }
513 else
514 {
515 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
516 isClicking = false;
517 }
518 }