better compositor shader
[tar-legacy.git] / MCDV / main.cpp
1 #include <iostream>
2
3 #include "vmf.hpp"
4
5 // OPENGL
6 #include <glad\glad.h>
7 #include <GLFW\glfw3.h>
8 #include "GLFWUtil.hpp"
9
10 #include "Shader.hpp"
11 #include "Texture.hpp"
12 //#include "Camera.hpp"
13 //#include "Mesh.hpp"
14 //#include "GameObject.hpp"
15 //#include "TextFont.hpp"
16 //#include "Console.hpp"
17 #include "FrameBuffer.hpp"
18
19 #include "interpolation.h"
20
21 #include <glm\glm.hpp>
22 #include <glm\gtc\matrix_transform.hpp>
23 #include <glm\gtc\type_ptr.hpp>
24
25 #define STBI_MSC_SECURE_CRT
26 #define STB_IMAGE_WRITE_IMPLEMENTATION
27 #include "stb_image_write.h"
28
29 void render_to_png(int x, int y, const char* filepath){
30 void* data = malloc(4 * x * y);
31
32 glReadPixels(0, 0, x, y, GL_RGBA, GL_UNSIGNED_BYTE, data);
33
34 if (data != 0) {
35 stbi_flip_vertically_on_write(true);
36 stbi_write_png(filepath, x, y, 4, data, x * 4);
37 }
38 }
39
40 int main(int argc, char* argv[]) {
41 std::cout << "Loading VMF\n";
42 vmf::vmf vmf_main("de_tavr_test.vmf");
43
44 std::cout << "Initializing OpenGL\n";
45
46 #pragma region init_opengl
47
48 glfwInit();
49 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); //We are using version 3.3 of openGL
50 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
51 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
52 glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
53 glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // Window le nope
54
55 //Create window
56 GLFWwindow* window = glfwCreateWindow(1, 1, "If you are seeing this window, something is broken", NULL, NULL);
57
58 //Check if window open
59 if (window == NULL){
60 std::cout << "Failed to create GLFW window" << std::endl;
61 glfwTerminate(); return -1;
62 }
63 glfwMakeContextCurrent(window);
64
65 //Settingn up glad
66 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
67 std::cout << "Failed to initialize GLAD" << std::endl; return -1;
68 }
69
70 glEnable(GL_DEPTH_TEST);
71
72 glViewport(0, 0, 1024, 1024);
73
74 glClearColor(0.00f, 0.00f, 0.00f, 0.00f);
75
76 std::cout << "Creating render buffers\n";
77
78 FrameBuffer fb_tex_playspace = FrameBuffer(1024, 1024);
79 FrameBuffer fb_tex_objectives = FrameBuffer(1024, 1024);
80 FrameBuffer fb_comp = FrameBuffer(1024, 1024);
81
82 // Screenspace quad
83 std::cout << "Creating screenspace mesh\n";
84
85 std::vector<float> __meshData = {
86 -1, -1,
87 -1, 1,
88 1, -1,
89 -1, 1,
90 1, 1,
91 1, -1
92 };
93
94 Mesh* mesh_screen_quad = new Mesh(__meshData, MeshMode::SCREEN_SPACE_UV);
95
96 #pragma endregion
97
98 std::cout << "Generating Meshes\n";
99
100 vmf_main.ComputeGLMeshes();
101 vmf_main.ComputeDisplacements();
102 std::vector<vmf::Solid*> tavr_solids = vmf_main.getAllBrushesInVisGroup("tavr_layout");
103
104 std::vector<vmf::Solid*> tavr_buyzones = vmf_main.getAllBrushesByClassName("func_buyzone");
105 std::vector<vmf::Solid*> tavr_bombtargets = vmf_main.getAllBrushesByClassName("func_bomb_target");
106
107 #pragma region bounds
108 std::cout << "Calculating bounds... ";
109
110 vmf::BoundingBox limits = vmf::getSolidListBounds(tavr_solids);
111 float z_render_min = limits.SEL.y;
112 float z_render_max = limits.NWU.y;
113
114 float padding = 128.0f;
115
116 float x_bounds_min = -limits.NWU.x -padding; //inflate distances slightly
117 float x_bounds_max = -limits.SEL.x +padding;
118
119 float y_bounds_min = limits.SEL.z -padding;
120 float y_bounds_max = limits.NWU.z +padding;
121
122 float dist_x = x_bounds_max - x_bounds_min;
123 float dist_y = y_bounds_max - y_bounds_min;
124
125 float mx_dist = glm::max(dist_x, dist_y);
126
127 float justify_x = (mx_dist - dist_x) * 0.5f;
128 float justify_y = (mx_dist - dist_y) * 0.5f;
129
130 float render_ortho_scale = glm::round((mx_dist / 1024.0f) / 0.01f) * 0.01f * 1024.0f; // Take largest, scale up a tiny bit. Clamp to 1024 min. Do some rounding.
131 glm::vec2 view_origin = glm::vec2(x_bounds_min - justify_x, y_bounds_max + justify_y);
132
133 std::cout << "done\n";
134 #pragma endregion
135
136 #pragma region shader_compilation
137
138 std::cout << "Compiling Shaders\n";
139
140 // Engine shaders
141 Shader shader_depth("shaders/depth.vs", "shaders/depth.fs");
142 Shader shader_unlit("shaders/unlit.vs", "shaders/unlit.fs");
143
144 // Compositing shaders
145 Shader shader_comp_main("shaders/fullscreenbase.vs", "shaders/ss_test.fs"); // le big one
146 Shader shader_precomp_playspace("shaders/fullscreenbase.vs", "shaders/ss_precomp_playspace.fs"); // computes distance map
147 Shader shader_precomp_objectives("shaders/fullscreenbase.vs", "shaders/ss_precomp_objectives.fs"); // computes distance map
148
149 std::cout << "Loading textures\n";
150
151 Texture tex_background = Texture("textures/grid.png");
152 Texture tex_gradient = Texture("textures/gradients/gradientmap_6.png", true);
153
154 #pragma endregion
155
156 #pragma region render_playable_space
157 std::cout << "Rendering playable space...";
158
159 fb_comp.Bind(); //Bind framebuffer
160
161 glClearColor(0.00f, 0.00f, 0.00f, 1.00f);
162 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163 glPolygonMode(GL_FRONT, GL_FILL);
164 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
165
166 shader_depth.use();
167 shader_depth.setMatrix("projection", glm::ortho(view_origin.x, view_origin.x + render_ortho_scale , view_origin.y - render_ortho_scale, view_origin.y, -1024.0f, 1024.0f));
168 shader_depth.setMatrix("view", glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, -1.0f, 0), glm::vec3(0, 0, 1)));
169
170 glm::mat4 model = glm::mat4();
171 shader_depth.setMatrix("model", model);
172
173 shader_depth.setVec3("color", 1.0f, 1.0f, 1.0f);
174 shader_depth.setFloat("HEIGHT_MIN", z_render_min);
175 shader_depth.setFloat("HEIGHT_MAX", z_render_max);
176
177 for (auto && s_solid : tavr_solids) {
178 if(!s_solid->containsDisplacements)
179 s_solid->mesh->Draw();
180 else {
181 for (auto && f : s_solid->faces) {
182 if (f.displacement != NULL) {
183 f.displacement->glMesh->Draw();
184 }
185 }
186 }
187 }
188
189 // Apply diffusion
190 fb_tex_playspace.Bind();
191
192 glClearColor(0.00f, 0.00f, 0.00f, 0.00f);
193 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
194 glDisable(GL_DEPTH_TEST);
195 glPolygonMode(GL_FRONT, GL_FILL);
196
197 shader_precomp_playspace.use();
198
199 fb_comp.BindRTtoTexSlot(0);
200 shader_precomp_playspace.setInt("tex_in", 0);
201
202 mesh_screen_quad->Draw();
203
204 glEnable(GL_DEPTH_TEST);
205
206 render_to_png(1024, 1024, "playable-space.png");
207
208 std::cout << "done!\n";
209 #pragma endregion
210
211 #pragma region render_objectives
212 std::cout << "Rendering bombsites & buyzones space...";
213
214 fb_comp.Bind();
215
216 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217 glPolygonMode(GL_FRONT, GL_FILL);
218 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
219
220 shader_unlit.use();
221 shader_unlit.setMatrix("projection", glm::ortho(view_origin.x, view_origin.x + render_ortho_scale, view_origin.y - render_ortho_scale, view_origin.y, -1024.0f, 1024.0f));
222 shader_unlit.setMatrix("view", glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, -1.0f, 0), glm::vec3(0, 0, 1)));
223 shader_unlit.setMatrix("model", model);
224
225 shader_unlit.setVec3("color", 0.0f, 1.0f, 0.0f);
226
227 for (auto && s_solid : tavr_buyzones) {
228 s_solid->mesh->Draw();
229 }
230
231 shader_unlit.setVec3("color", 1.0f, 0.0f, 0.0f);
232
233 for (auto && s_solid : tavr_bombtargets) {
234 s_solid->mesh->Draw();
235 }
236
237 // Apply diffusion
238 fb_tex_objectives.Bind();
239
240 glClearColor(0.00f, 0.00f, 0.00f, 0.00f);
241 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
242 glDisable(GL_DEPTH_TEST);
243 glPolygonMode(GL_FRONT, GL_FILL);
244
245 shader_precomp_objectives.use();
246
247 fb_comp.BindRTtoTexSlot(0);
248 shader_precomp_objectives.setInt("tex_in", 0);
249
250 mesh_screen_quad->Draw();
251
252 render_to_png(1024, 1024, "buyzone-bombtargets.png");
253
254 glEnable(GL_DEPTH_TEST);
255 std::cout << "done!\n";
256 #pragma endregion
257
258 #pragma region generate_radar_txt
259
260 kv::DataBlock node_radar = kv::DataBlock();
261 node_radar.name = "de_tavr_test";
262 node_radar.Values.insert({ "material", "overviews/de_tavr_test" });
263
264 node_radar.Values.insert({ "pos_x", std::to_string(view_origin.x) });
265 node_radar.Values.insert({ "pos_y", std::to_string(view_origin.y) });
266 node_radar.Values.insert({ "scale", std::to_string(render_ortho_scale / 1024.0f) });
267
268 // Try resolve spawn positions
269 glm::vec3* loc_spawnCT = vmf_main.calculateSpawnLocation(vmf::team::counter_terrorist);
270 glm::vec3* loc_spawnT = vmf_main.calculateSpawnLocation(vmf::team::terrorist);
271
272 if (loc_spawnCT != NULL) {
273 node_radar.Values.insert({ "CTSpawn_x", std::to_string(glm::round(remap(loc_spawnCT->x, view_origin.x, view_origin.x + render_ortho_scale, 0.0f, 1.0f) / 0.01f) * 0.01f) });
274 node_radar.Values.insert({ "CTSpawn_y", std::to_string(glm::round(remap(loc_spawnCT->y, view_origin.y, view_origin.y - render_ortho_scale, 0.0f, 1.0f) / 0.01f) * 0.01f) });
275 }
276 if (loc_spawnT != NULL) {
277 node_radar.Values.insert({ "TSpawn_x", std::to_string(glm::round(remap(loc_spawnT->x, view_origin.x, view_origin.x + render_ortho_scale, 0.0f, 1.0f) / 0.01f) * 0.01f) });
278 node_radar.Values.insert({ "TSpawn_y", std::to_string(glm::round(remap(loc_spawnT->y, view_origin.y, view_origin.y - render_ortho_scale, 0.0f, 1.0f) / 0.01f) * 0.01f) });
279 }
280
281 std::ofstream out("de_tavr_test.txt");
282 out << "// TAVR - AUTO RADAR. v 2.0.0\n";
283 node_radar.Serialize(out);
284 out.close();
285
286 #pragma endregion
287
288 #pragma region compositing
289 std::cout << "Compositing... \n";
290
291 fb_comp.Bind();
292
293 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
294 glPolygonMode(GL_FRONT, GL_FILL);
295
296 shader_comp_main.use();
297
298 tex_background.bindOnSlot(0);
299 shader_comp_main.setInt("tex_background", 0);
300
301 fb_tex_playspace.BindRTtoTexSlot(1);
302 shader_comp_main.setInt("tex_playspace", 1);
303
304 fb_tex_objectives.BindRTtoTexSlot(2);
305 shader_comp_main.setInt("tex_objectives", 2);
306
307 tex_gradient.bindOnSlot(4);
308 shader_comp_main.setInt("tex_gradient", 4);
309
310 mesh_screen_quad->Draw();
311
312 render_to_png(1024, 1024, "1whammy.png");
313
314 std::cout << "Done\n";
315
316 #pragma endregion
317
318 #pragma region auto_export_game
319
320 #pragma endregion
321
322 system("PAUSE");
323 //Exit safely
324 glfwTerminate();
325
326 return 0;
327 }