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 <glm\glm.hpp>
20 #include <glm\gtc\matrix_transform.hpp>
21 #include <glm\gtc\type_ptr.hpp>
23 #define STBI_MSC_SECURE_CRT
24 #define STB_IMAGE_WRITE_IMPLEMENTATION
25 #include "stb_image_write.h"
27 void render_to_png(int x
, int y
, const char* filepath
){
28 void* data
= malloc(3 * x
* y
);
30 glReadPixels(0, 0, x
, y
, GL_RGB
, GL_UNSIGNED_BYTE
, data
);
33 stbi_flip_vertically_on_write(true);
34 stbi_write_png(filepath
, x
, y
, 3, data
, x
* 3);
38 int main(int argc
, char* argv
[]) {
39 std::cout
<< "Loading VMF\n";
40 vmf::vmf
vmf_main("blimey.vmf");
42 std::cout
<< "Initializing OpenGL\n";
44 #pragma region init_opengl
47 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR
, 3); //We are using version 3.3 of openGL
48 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR
, 3);
49 glfwWindowHint(GLFW_OPENGL_PROFILE
, GLFW_OPENGL_CORE_PROFILE
);
50 glfwWindowHint(GLFW_RESIZABLE
, GL_FALSE
);
51 glfwWindowHint(GLFW_VISIBLE
, GL_FALSE
); // Window le nope
54 GLFWwindow
* window
= glfwCreateWindow(1, 1, "If you are seeing this window, something is broken", NULL
, NULL
);
56 //Check if window open
58 std::cout
<< "Failed to create GLFW window" << std::endl
;
59 glfwTerminate(); return -1;
61 glfwMakeContextCurrent(window
);
64 if (!gladLoadGLLoader((GLADloadproc
)glfwGetProcAddress
)){
65 std::cout
<< "Failed to initialize GLAD" << std::endl
; return -1;
68 glEnable(GL_DEPTH_TEST
);
70 glViewport(0, 0, 1024, 1024);
72 glClearColor(0.00f
, 0.00f
, 0.00f
, 1.0f
);
76 #pragma region init_framebuffer
78 unsigned int framebuffer
;
79 glGenFramebuffers(1, &framebuffer
);
80 glBindFramebuffer(GL_FRAMEBUFFER
, framebuffer
);
83 unsigned int texColorBuffer
;
84 glGenTextures(1, &texColorBuffer
);
85 glBindTexture(GL_TEXTURE_2D
, texColorBuffer
);
86 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
, 1024, 1024, 0, GL_RGB
, GL_UNSIGNED_BYTE
, NULL
);
87 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
88 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
89 glBindTexture(GL_TEXTURE_2D
, 0);
91 // attach it to currently bound framebuffer object
92 glFramebufferTexture2D(GL_FRAMEBUFFER
, GL_COLOR_ATTACHMENT0
, GL_TEXTURE_2D
, texColorBuffer
, 0);
95 glGenRenderbuffers(1, &rbo
);
96 glBindRenderbuffer(GL_RENDERBUFFER
, rbo
);
97 glRenderbufferStorage(GL_RENDERBUFFER
, GL_DEPTH24_STENCIL8
, 1024, 1024);
98 glBindRenderbuffer(GL_RENDERBUFFER
, 0);
100 glFramebufferRenderbuffer(GL_FRAMEBUFFER
, GL_DEPTH_STENCIL_ATTACHMENT
, GL_RENDERBUFFER
, rbo
);
102 if (glCheckFramebufferStatus(GL_FRAMEBUFFER
) != GL_FRAMEBUFFER_COMPLETE
)
103 std::cout
<< "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl
;
104 //glBindFramebuffer(GL_FRAMEBUFFER, 0);
108 std::cout
<< "Generating Meshes\n";
110 vmf_main
.ComputeGLMeshes();
111 vmf_main
.ComputeDisplacements();
112 std::vector
<vmf::Solid
*> tavr_solids
= vmf_main
.getAllBrushesInVisGroup("tavr_layout");
114 std::vector
<vmf::Solid
*> tavr_buyzones
= vmf_main
.getAllBrushesByClassName("func_buyzone");
115 std::vector
<vmf::Solid
*> tavr_bombtargets
= vmf_main
.getAllBrushesByClassName("func_bomb_target");
117 #pragma region bounds
118 std::cout
<< "Calculating bounds... ";
120 vmf::BoundingBox limits
= vmf::getSolidListBounds(tavr_solids
);
121 float z_render_min
= limits
.SEL
.y
;
122 float z_render_max
= limits
.NWU
.y
;
124 float padding
= 128.0f
;
126 float x_bounds_min
= -limits
.NWU
.x
-padding
; //inflate distances slightly
127 float x_bounds_max
= -limits
.SEL
.x
+padding
;
129 float y_bounds_min
= limits
.SEL
.z
-padding
;
130 float y_bounds_max
= limits
.NWU
.z
+padding
;
132 float dist_x
= x_bounds_max
- x_bounds_min
;
133 float dist_y
= y_bounds_max
- y_bounds_min
;
135 float mx_dist
= glm::max(dist_x
, dist_y
);
137 float justify_x
= (mx_dist
- dist_x
) * 0.5f
;
138 float justify_y
= (mx_dist
- dist_y
) * 0.5f
;
140 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.
141 glm::vec2 view_origin
= glm::vec2(x_bounds_min
- justify_x
, y_bounds_max
+ justify_y
);
143 std::cout
<< "done\n";
144 #pragma endregion bounds
146 std::cout
<< "Compiling Shaders\n";
147 Shader
shader_depth("shaders/depth.vs", "shaders/depth.fs");
148 Shader
shader_unlit("shaders/unlit.vs", "shaders/unlit.fs");
150 #pragma region render_playable_space
151 std::cout
<< "Rendering playable space...";
153 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
154 glPolygonMode(GL_FRONT
, GL_FILL
);
155 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
158 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
));
159 shader_depth
.setMatrix("view", glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, -1.0f
, 0), glm::vec3(0, 0, 1)));
161 glm::mat4 model
= glm::mat4();
162 shader_depth
.setMatrix("model", model
);
164 shader_depth
.setVec3("color", 1.0f
, 1.0f
, 1.0f
);
165 shader_depth
.setFloat("HEIGHT_MIN", z_render_min
);
166 shader_depth
.setFloat("HEIGHT_MAX", z_render_max
);
168 for (auto && s_solid
: tavr_solids
) {
169 if(!s_solid
->containsDisplacements
)
170 s_solid
->mesh
->Draw();
172 for (auto && f
: s_solid
->faces
) {
173 if (f
.displacement
!= NULL
) {
174 f
.displacement
->glMesh
->Draw();
180 render_to_png(1024, 1024, "playable-space.png");
182 std::cout
<< "done!\n";
183 #pragma endregion render_playable_space
185 #pragma region render_buyzone_bombsites
186 std::cout
<< "Rendering bombsites & buyzones space...";
188 glClear(GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
189 glPolygonMode(GL_FRONT
, GL_FILL
);
190 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
193 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
));
194 shader_unlit
.setMatrix("view", glm::lookAt(glm::vec3(0, 0, 0), glm::vec3(0, -1.0f
, 0), glm::vec3(0, 0, 1)));
195 shader_unlit
.setMatrix("model", model
);
197 shader_unlit
.setVec3("color", 0.0f
, 1.0f
, 0.0f
);
199 for (auto && s_solid
: tavr_buyzones
) {
200 s_solid
->mesh
->Draw();
203 shader_unlit
.setVec3("color", 1.0f
, 0.0f
, 0.0f
);
205 for (auto && s_solid
: tavr_bombtargets
) {
206 s_solid
->mesh
->Draw();
209 render_to_png(1024, 1024, "buyzone-bombtargets.png");
211 std::cout
<< "done!\n";
214 #pragma region generate_radar_txt
216 kv::DataBlock node_radar
= kv::DataBlock();
217 node_radar
.name
= "de_tavr_test";
218 node_radar
.Values
.insert({ "material", "overviews/de_tavr_test" });
220 node_radar
.Values
.insert({ "pos_x", std::to_string(view_origin
.x
) });
221 node_radar
.Values
.insert({ "pos_y", std::to_string(view_origin
.y
) });
222 node_radar
.Values
.insert({ "scale", std::to_string(render_ortho_scale
/ 1024.0f
) });
224 std::ofstream
out("de_tavr_test.txt");
225 out
<< "// TAVR - AUTO RADAR. v 2.0.0\n";
226 node_radar
.Serialize(out
);
229 #pragma endregion generate_radar_txt
231 #pragma region compositing
234 #pragma endregion compositing
236 #pragma region auto_export_game