Auto radar
[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 <glm\glm.hpp>
20 #include <glm\gtc\matrix_transform.hpp>
21 #include <glm\gtc\type_ptr.hpp>
22
23 #define STBI_MSC_SECURE_CRT
24 #define STB_IMAGE_WRITE_IMPLEMENTATION
25 #include "stb_image_write.h"
26
27 void render_to_png(int x, int y, const char* filepath){
28 void* data = malloc(3 * x * y);
29
30 glReadPixels(0, 0, x, y, GL_RGB, GL_UNSIGNED_BYTE, data);
31
32 if (data != 0) {
33 stbi_flip_vertically_on_write(true);
34 stbi_write_png(filepath, x, y, 3, data, x * 3);
35 }
36 }
37
38 int main(int argc, char* argv[]) {
39 std::cout << "Loading VMF\n";
40 vmf::vmf vmf_main("blimey.vmf");
41
42 std::cout << "Initializing OpenGL\n";
43
44 #pragma region init_opengl
45
46 glfwInit();
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
52
53 //Create window
54 GLFWwindow* window = glfwCreateWindow(1, 1, "If you are seeing this window, something is broken", NULL, NULL);
55
56 //Check if window open
57 if (window == NULL){
58 std::cout << "Failed to create GLFW window" << std::endl;
59 glfwTerminate(); return -1;
60 }
61 glfwMakeContextCurrent(window);
62
63 //Settingn up glad
64 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
65 std::cout << "Failed to initialize GLAD" << std::endl; return -1;
66 }
67
68 glEnable(GL_DEPTH_TEST);
69
70 glViewport(0, 0, 1024, 1024);
71
72 glClearColor(0.00f, 0.00f, 0.00f, 1.0f);
73
74 #pragma endregion
75
76 #pragma region init_framebuffer
77
78 unsigned int framebuffer;
79 glGenFramebuffers(1, &framebuffer);
80 glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
81
82 // generate texture
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);
90
91 // attach it to currently bound framebuffer object
92 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
93
94 unsigned int rbo;
95 glGenRenderbuffers(1, &rbo);
96 glBindRenderbuffer(GL_RENDERBUFFER, rbo);
97 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1024, 1024);
98 glBindRenderbuffer(GL_RENDERBUFFER, 0);
99
100 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
101
102 if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
103 std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl;
104 //glBindFramebuffer(GL_FRAMEBUFFER, 0);
105
106 #pragma endregion
107
108 std::cout << "Generating Meshes\n";
109
110 vmf_main.ComputeGLMeshes();
111 vmf_main.ComputeDisplacements();
112 std::vector<vmf::Solid*> tavr_solids = vmf_main.getAllBrushesInVisGroup("tavr_layout");
113
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");
116
117 #pragma region bounds
118 std::cout << "Calculating bounds... ";
119
120 vmf::BoundingBox limits = vmf::getSolidListBounds(tavr_solids);
121 float z_render_min = limits.SEL.y;
122 float z_render_max = limits.NWU.y;
123
124 float padding = 128.0f;
125
126 float x_bounds_min = -limits.NWU.x -padding; //inflate distances slightly
127 float x_bounds_max = -limits.SEL.x +padding;
128
129 float y_bounds_min = limits.SEL.z -padding;
130 float y_bounds_max = limits.NWU.z +padding;
131
132 float dist_x = x_bounds_max - x_bounds_min;
133 float dist_y = y_bounds_max - y_bounds_min;
134
135 float mx_dist = glm::max(dist_x, dist_y);
136
137 float justify_x = (mx_dist - dist_x) * 0.5f;
138 float justify_y = (mx_dist - dist_y) * 0.5f;
139
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);
142
143 std::cout << "done\n";
144 #pragma endregion bounds
145
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");
149
150 #pragma region render_playable_space
151 std::cout << "Rendering playable space...";
152
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
154 glPolygonMode(GL_FRONT, GL_FILL);
155 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
156
157 shader_depth.use();
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)));
160
161 glm::mat4 model = glm::mat4();
162 shader_depth.setMatrix("model", model);
163
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);
167
168 for (auto && s_solid : tavr_solids) {
169 if(!s_solid->containsDisplacements)
170 s_solid->mesh->Draw();
171 else {
172 for (auto && f : s_solid->faces) {
173 if (f.displacement != NULL) {
174 f.displacement->glMesh->Draw();
175 }
176 }
177 }
178 }
179
180 render_to_png(1024, 1024, "playable-space.png");
181
182 std::cout << "done!\n";
183 #pragma endregion render_playable_space
184
185 #pragma region render_buyzone_bombsites
186 std::cout << "Rendering bombsites & buyzones space...";
187
188 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
189 glPolygonMode(GL_FRONT, GL_FILL);
190 //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
191
192 shader_unlit.use();
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);
196
197 shader_unlit.setVec3("color", 0.0f, 1.0f, 0.0f);
198
199 for (auto && s_solid : tavr_buyzones) {
200 s_solid->mesh->Draw();
201 }
202
203 shader_unlit.setVec3("color", 1.0f, 0.0f, 0.0f);
204
205 for (auto && s_solid : tavr_bombtargets) {
206 s_solid->mesh->Draw();
207 }
208
209 render_to_png(1024, 1024, "buyzone-bombtargets.png");
210
211 std::cout << "done!\n";
212 #pragma endregion
213
214 #pragma region generate_radar_txt
215
216 kv::DataBlock node_radar = kv::DataBlock();
217 node_radar.name = "de_tavr_test";
218 node_radar.Values.insert({ "material", "overviews/de_tavr_test" });
219
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) });
223
224 std::ofstream out("de_tavr_test.txt");
225 out << "// TAVR - AUTO RADAR. v 2.0.0\n";
226 node_radar.Serialize(out);
227 out.close();
228
229 #pragma endregion generate_radar_txt
230
231 #pragma region compositing
232
233
234 #pragma endregion compositing
235
236 #pragma region auto_export_game
237
238 #pragma endregion
239
240 system("PAUSE");
241 //Exit safely
242 glfwTerminate();
243
244 return 0;
245 }