2 #include <GLFW\glfw3.h>
10 #include "GLFWUtil.hpp"
18 #include "Texture.hpp"
21 #include "GameObject.hpp"
22 #include "TextFont.hpp"
23 #include "Console.hpp"
24 #include "FrameBuffer.hpp"
26 #include <glm\glm.hpp>
27 #include <glm\gtc\matrix_transform.hpp>
28 #include <glm\gtc\type_ptr.hpp>
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
);
36 int window_width
= 1024;
37 int window_height
= 1024;
39 float deltaTime
= 0.0f
;
40 float lastFrame
= 0.0f
;
42 bool isClicking
= false;
50 int SV_WIREFRAME
= 0; //0: off 1: overlay 2: only
51 int SV_RENDERMODE
= 0;
52 int SV_PERSPECTIVE
= 0;
54 int M_HEIGHT_MIN
= 0.0f
;
55 int M_HEIGHT_MAX
= 10.0f
* 100.0f
;
57 int M_CLIP_NEAR
= 0.1f
* 100.0f
;
58 int M_CLIP_FAR
= 100.0f
* 100.0f
;
60 int T_RENDER_RESOLUTION_X
= 1024;
61 int T_RENDER_RESOLUTION_Y
= 1024;
62 bool T_ARM_FOR_FB_RENDER
= false;
64 bool SV_DRAW_BSP
= true;
65 bool SV_DRAW_NAV
= false;
67 bool V_DO_QUIT
= false;
69 std::string _filesrc_bsp
;
70 std::string _filesrc_nav
;
74 int M_ORTHO_SIZE
= 20;
77 void update_globals_ui_text(TextFont
* target
); //Auto update the ui text
78 TextFont
* ui_text_info
;
79 TextFont
* ui_text_loaded
;
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); }
90 console
.FeedBack(R
"TERRI00(
94 Click and drag Look around
95 ` or ~ key Open console
99 QUIT / EXIT Closes (wow!)
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
110 RENDER Renders the view to render.png
113 RENDERMODE <int> Same as nav/bsp switch
114 PROJMATRIX <int> Same as persp/ortho
116 Variables (the actual height stuff):
117 MIN <int> Minimum levels of height (units)
118 MAX <int> Maximum levels of height (units)
120 FARZ <int> Far clip plane of the camera
121 NEARZ <int> Near clip plane of the camera
122 )TERRI00", MSG_STATUS::SUCCESS
);
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";
129 for (int i
= 1; i
< argc
; ++i
) {
130 char* _arg
= argv
[i
];
131 std::string arg
= _arg
;
133 if (split(arg
, '.').back() == "bsp") {
137 if (split(arg
, '.').back() == "nav") {
142 #pragma region Initialisation
144 std::cout
<< "CS:GO Heightmap generator" << std::endl
;
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);
154 //Creating the window
155 GLFWwindow
* window
= glfwCreateWindow(window_width
, window_height
, "CS:GO Heightmap generator", NULL
, NULL
);
157 //Check if window open
160 std::cout
<< "Failed to create GLFW window" << std::endl
;
161 glfwTerminate(); return -1;
163 glfwMakeContextCurrent(window
);
166 if (!gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
))
168 std::cout
<< "Failed to initialize GLAD" << std::endl
; return -1;
172 glViewport(0, 0, 1024, 1024);
173 glfwSetFramebufferSizeCallback(window
, framebuffer_size_callback
);
176 glfwSetCursorPosCallback(window
, mouse_callback
);
177 glfwSetScrollCallback(window
, scroll_callback
);
178 glfwSetMouseButtonCallback(window
, mouse_button_callback
);
182 // configure global opengl state
183 // -----------------------------
184 glEnable(GL_DEPTH_TEST
);
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");
190 //Mesh handling -----------------------------------------------------------------------------
195 if (_filesrc_bsp
!= ""){
196 vbsp_level
bsp_map(_filesrc_bsp
, true);
197 t200
= new Mesh(bsp_map
.generate_bigmesh());
200 if (_filesrc_nav
!= "") {
201 Nav::Mesh
bob(_filesrc_nav
);
202 t201
= new Mesh(bob
.generateGLMesh());
205 //Radar rtest("de_overpass.txt");
208 //VertAlphaMesh t300(vbsp_level::genVertAlpha(t200.vertices, t201.vertices));
210 util_keyHandler
keys(window
);
211 //Create camera (test)
212 camera
= Camera(&keys
);
214 glEnable(GL_CULL_FACE
);
215 glCullFace(GL_FRONT
);
217 TextFont::init(); //Setup textfonts before we use it
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
);
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
));
231 update_globals_ui_text(ui_text_info
); //Update globals
234 console
= Console(&keys
, &window_width
, &window_height
);
235 console
.RegisterCVar("RENDERMODE", &SV_RENDERMODE
);
238 console
.RegisterCmd("HELP", &_ccmd_help
);
241 console
.RegisterCmd("QUIT", &_ccmd_exit
);
242 console
.RegisterCmd("EXIT", &_ccmd_exit
);
245 console
.RegisterCmd("NAV", &_ccmd_rendernav
);
246 console
.RegisterCmd("BSP", &_ccmd_renderbsp
);
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
);
254 //console.RegisterCVar("SX", &T_RENDER_RESOLUTION_X);
255 //console.RegisterCVar("SY", &T_RENDER_RESOLUTION_Y);
256 console
.RegisterCmd("RENDER", &_ccmd_render_image
);
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
);
267 console
.RegisterCVar("FARZ", &M_CLIP_FAR
);
268 console
.RegisterCVar("NEARZ", &M_CLIP_NEAR
);
270 //FrameBuffer t_frame_buffer = FrameBuffer();
273 while (!glfwWindowShouldClose(window
))
275 float currentFrame
= glfwGetTime();
276 deltaTime
= currentFrame
- lastFrame
;
277 lastFrame
= currentFrame
;
280 processInput(window
, keys
);
281 if(!console
.isEnabled
)
282 camera
.handleInput(deltaTime
);
284 console
.handleKeysTick();
286 if (T_ARM_FOR_FB_RENDER
) {
287 //t_frame_buffer.Bind();
288 glViewport(0, 0, T_RENDER_RESOLUTION_X
, T_RENDER_RESOLUTION_Y
);
292 glClearColor(0.05f
, 0.05f
, 0.05f
, 1.0f
);
295 if(T_ARM_FOR_FB_RENDER
) glClearColor(0.0f
, 1.0f
, 0.00f
, 1.0f
);
297 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
298 glPolygonMode(GL_FRONT
, GL_FILL
);
301 glEnable(GL_DEPTH_TEST
);
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
));
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
));
311 shader_lit
.setMatrix("view", camera
.getViewMatrix());
313 glm::mat4 model
= glm::mat4();
314 shader_lit
.setMatrix("model", model
);
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
);
321 if (SV_RENDERMODE
== 0) {
322 glEnable(GL_CULL_FACE
);
327 if (SV_RENDERMODE
== 1) {
328 glDisable(GL_CULL_FACE
);
336 if (!T_ARM_FOR_FB_RENDER
) { // UI
339 //ui_text_info->DrawWithBackground();
340 //ui_text_loaded->DrawWithBackground();
343 //Sort out render buffer stuff
344 if (T_ARM_FOR_FB_RENDER
) {
345 std::cout
<< "Done" << std::endl
;
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
);
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
);
356 console
.FeedBack("Something went wrong making render", MSG_STATUS::ERR
);
358 //t_frame_buffer.Unbind();
359 glViewport(0, 0, window_width
, window_height
);
360 T_ARM_FOR_FB_RENDER
= false;
364 //Check and call events, swap buffers
365 glfwSwapBuffers(window
);
377 //Automatically readjust to the new size we just received
378 void framebuffer_size_callback(GLFWwindow
* window
, int width
, int height
)
380 glViewport(0, 0, width
, height
);
381 window_width
= width
;
382 window_height
= height
;
385 bool K_CONTROL_MIN
= false;
386 bool K_CONTROL_MAX
= false;
389 void setWindowTitle() {
390 std::string title
= "BigmanEngine | ";
394 void processInput(GLFWwindow
* window
, util_keyHandler keys
)
396 if (keys
.getKeyDown(GLFW_KEY_ESCAPE
))
397 glfwSetWindowShouldClose(window
, true);
399 if (keys
.getKeyDown(GLFW_KEY_GRAVE_ACCENT
))
400 console
.isEnabled
= !console
.isEnabled
;
404 if (keys
.getKeyDown(GLFW_KEY_1
)) {
406 update_globals_ui_text(ui_text_info
); //Update globals
408 if (keys
.getKeyDown(GLFW_KEY_2
)) {
410 update_globals_ui_text(ui_text_info
); //Update globals
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
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
421 if (keys
.getKeyDown(GLFW_KEY_5
)) {
423 update_globals_ui_text(ui_text_info
); //Update globals
426 if (keys
.getKeyDown(GLFW_KEY_6
)) {
428 update_globals_ui_text(ui_text_info
); //Update globals
432 if (keys
.getKeyDown(GLFW_KEY_7
)) {
434 update_globals_ui_text(ui_text_info
); //Update globals
438 if (keys
.getKeyDown(GLFW_KEY_8
)) {
440 update_globals_ui_text(ui_text_info
); //Update globals
443 if (keys
.getKeyDown(GLFW_KEY_9
)) {
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
452 if (keys
.getKeyDown(GLFW_KEY_0
)) {
455 camera
.mouseUpdate(0, 0, true);
456 //camera.cameraFront = glm::vec3(0, 0, -1);
457 //camera.cameraUp = glm::vec3(0, 1, 0);
461 std::string _globals_editmode_lookups
[] = {
469 void update_globals_ui_text(TextFont
* target
) {
471 std::ostringstream ss
;
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";
479 target
->SetText(ss
.str());
482 void mouse_callback(GLFWwindow
* window
, double xpos
, double ypos
)
484 camera
.mouseUpdate(xpos
, ypos
, isClicking
);
485 mousex
= xpos
; mousey
= ypos
;
488 void scroll_callback(GLFWwindow
* window
, double xoffset
, double yoffset
)
490 //camera.fov = glm::clamp(camera.fov + (float)yoffset, 2.0f, 90.0f);
493 M_HEIGHT_MIN
+= (float)yoffset
* 0.1f
;
495 if (SV_EDITMODE
== 1)
496 M_HEIGHT_MAX
+= (float)yoffset
* 0.1f
;
498 if (SV_EDITMODE
== 4)
499 M_ORTHO_SIZE
+= (float)yoffset
* 0.1f
;
501 //if (SV_EDITMODE == 2) M_CLIP_NEAR += (float)yoffset * 0.1f;
503 //if (SV_EDITMODE == 3) M_CLIP_FAR += (float)yoffset * 0.1f;
506 void mouse_button_callback(GLFWwindow
* window
, int button
, int action
, int mods
)
508 if (button
== GLFW_MOUSE_BUTTON_LEFT
&& action
== GLFW_PRESS
)
510 glfwSetInputMode(window
, GLFW_CURSOR
, GLFW_CURSOR_DISABLED
);
515 glfwSetInputMode(window
, GLFW_CURSOR
, GLFW_CURSOR_NORMAL
);