7 #include <GLFW\glfw3.h>
8 #include "GLFWUtil.hpp"
11 #include "Texture.hpp"
12 //#include "Camera.hpp"
14 //#include "GameObject.hpp"
15 //#include "TextFont.hpp"
16 //#include "Console.hpp"
17 #include "FrameBuffer.hpp"
19 #include "interpolation.h"
21 #include <glm\glm.hpp>
22 #include <glm\gtc\matrix_transform.hpp>
23 #include <glm\gtc\type_ptr.hpp>
25 #define STBI_MSC_SECURE_CRT
26 #define STB_IMAGE_WRITE_IMPLEMENTATION
27 #include "stb_image_write.h"
29 void render_to_png(int x
, int y
, const char* filepath
){
30 void* data
= malloc(4 * x
* y
);
32 glReadPixels(0, 0, x
, y
, GL_RGBA
, GL_UNSIGNED_BYTE
, data
);
35 stbi_flip_vertically_on_write(true);
36 stbi_write_png(filepath
, x
, y
, 4, data
, x
* 4);
40 int main(int argc
, char* argv
[]) {
41 std::cout
<< "Loading VMF\n";
42 vmf::vmf
vmf_main("de_tavr_test.vmf");
44 std::cout
<< "Initializing OpenGL\n";
46 #pragma region init_opengl
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
56 GLFWwindow
* window
= glfwCreateWindow(1, 1, "If you are seeing this window, something is broken", NULL
, NULL
);
58 //Check if window open
60 std::cout
<< "Failed to create GLFW window" << std::endl
;
61 glfwTerminate(); return -1;
63 glfwMakeContextCurrent(window
);
66 if (!gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
)){
67 std::cout
<< "Failed to initialize GLAD" << std::endl
; return -1;
70 glEnable(GL_DEPTH_TEST
);
72 glViewport(0, 0, 1024, 1024);
74 glClearColor(0.00f
, 0.00f
, 0.00f
, 0.00f
);
76 std::cout
<< "Creating render buffers\n";
78 FrameBuffer fb_tex_playspace
= FrameBuffer(1024, 1024);
79 FrameBuffer fb_tex_objectives
= FrameBuffer(1024, 1024);
80 FrameBuffer fb_comp
= FrameBuffer(1024, 1024);
83 std::cout
<< "Creating screenspace mesh\n";
85 std::vector
<float> __meshData
= {
94 Mesh
* mesh_screen_quad
= new Mesh(__meshData
, MeshMode::SCREEN_SPACE_UV
);
98 std::cout
<< "Generating Meshes\n";
100 vmf_main
.ComputeGLMeshes();
101 vmf_main
.ComputeDisplacements();
102 std::vector
<vmf::Solid
*> tavr_solids
= vmf_main
.getAllBrushesInVisGroup("tavr_layout");
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");
107 #pragma region bounds
108 std::cout
<< "Calculating bounds... ";
110 vmf::BoundingBox limits
= vmf::getSolidListBounds(tavr_solids
);
111 float z_render_min
= limits
.SEL
.y
;
112 float z_render_max
= limits
.NWU
.y
;
114 float padding
= 128.0f
;
116 float x_bounds_min
= -limits
.NWU
.x
-padding
; //inflate distances slightly
117 float x_bounds_max
= -limits
.SEL
.x
+padding
;
119 float y_bounds_min
= limits
.SEL
.z
-padding
;
120 float y_bounds_max
= limits
.NWU
.z
+padding
;
122 float dist_x
= x_bounds_max
- x_bounds_min
;
123 float dist_y
= y_bounds_max
- y_bounds_min
;
125 float mx_dist
= glm::max(dist_x
, dist_y
);
127 float justify_x
= (mx_dist
- dist_x
) * 0.5f
;
128 float justify_y
= (mx_dist
- dist_y
) * 0.5f
;
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
);
133 std::cout
<< "done\n";
136 #pragma region shader_compilation
138 std::cout
<< "Compiling Shaders\n";
141 Shader
shader_depth("shaders/depth.vs", "shaders/depth.fs");
142 Shader
shader_unlit("shaders/unlit.vs", "shaders/unlit.fs");
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
149 std::cout
<< "Loading textures\n";
151 Texture tex_background
= Texture("textures/grid.png");
152 Texture tex_gradient
= Texture("textures/gradients/gradientmap_6.png", true);
156 #pragma region render_playable_space
157 std::cout
<< "Rendering playable space...";
159 fb_comp
.Bind(); //Bind framebuffer
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);
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)));
170 glm::mat4 model
= glm::mat4();
171 shader_depth
.setMatrix("model", model
);
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
);
177 for (auto && s_solid
: tavr_solids
) {
178 if(!s_solid
->containsDisplacements
)
179 s_solid
->mesh
->Draw();
181 for (auto && f
: s_solid
->faces
) {
182 if (f
.displacement
!= NULL
) {
183 f
.displacement
->glMesh
->Draw();
190 fb_tex_playspace
.Bind();
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
);
197 shader_precomp_playspace
.use();
199 fb_comp
.BindRTtoTexSlot(0);
200 shader_precomp_playspace
.setInt("tex_in", 0);
202 mesh_screen_quad
->Draw();
204 glEnable(GL_DEPTH_TEST
);
206 render_to_png(1024, 1024, "playable-space.png");
208 std::cout
<< "done!\n";
211 #pragma region render_objectives
212 std::cout
<< "Rendering bombsites & buyzones space...";
216 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
217 glPolygonMode(GL_FRONT
, GL_FILL
);
218 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
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
);
225 shader_unlit
.setVec3("color", 0.0f
, 1.0f
, 0.0f
);
227 for (auto && s_solid
: tavr_buyzones
) {
228 s_solid
->mesh
->Draw();
231 shader_unlit
.setVec3("color", 1.0f
, 0.0f
, 0.0f
);
233 for (auto && s_solid
: tavr_bombtargets
) {
234 s_solid
->mesh
->Draw();
238 fb_tex_objectives
.Bind();
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
);
245 shader_precomp_objectives
.use();
247 fb_comp
.BindRTtoTexSlot(0);
248 shader_precomp_objectives
.setInt("tex_in", 0);
250 mesh_screen_quad
->Draw();
252 render_to_png(1024, 1024, "buyzone-bombtargets.png");
254 glEnable(GL_DEPTH_TEST
);
255 std::cout
<< "done!\n";
258 #pragma region generate_radar_txt
260 kv::DataBlock node_radar
= kv::DataBlock();
261 node_radar
.name
= "de_tavr_test";
262 node_radar
.Values
.insert({ "material", "overviews/de_tavr_test" });
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
) });
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
);
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
) });
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
) });
281 std::ofstream
out("de_tavr_test.txt");
282 out
<< "// TAVR - AUTO RADAR. v 2.0.0\n";
283 node_radar
.Serialize(out
);
288 #pragma region compositing
289 std::cout
<< "Compositing... \n";
293 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
294 glPolygonMode(GL_FRONT
, GL_FILL
);
296 shader_comp_main
.use();
298 tex_background
.bindOnSlot(0);
299 shader_comp_main
.setInt("tex_background", 0);
301 fb_tex_playspace
.BindRTtoTexSlot(1);
302 shader_comp_main
.setInt("tex_playspace", 1);
304 fb_tex_objectives
.BindRTtoTexSlot(2);
305 shader_comp_main
.setInt("tex_objectives", 2);
307 tex_gradient
.bindOnSlot(4);
308 shader_comp_main
.setInt("tex_gradient", 4);
310 mesh_screen_quad
->Draw();
312 render_to_png(1024, 1024, "1whammy.png");
314 std::cout
<< "Done\n";
318 #pragma region auto_export_game