Преглед изворни кода

New vis check (work in progress)

Thomas Buck пре 9 година
родитељ
комит
4681b6ac10
8 измењених фајлова са 96 додато и 44 уклоњено
  1. 13
    0
      CMakeLists.txt
  2. 3
    1
      include/Render.h
  3. 3
    6
      include/RoomData.h
  4. 4
    0
      src/Camera.cpp
  5. 57
    20
      src/Render.cpp
  6. 10
    0
      src/RoomData.cpp
  7. 1
    3
      src/loader/LoaderTR2.cpp
  8. 5
    14
      src/system/Shader.cpp

+ 13
- 0
CMakeLists.txt Прегледај датотеку

@@ -171,5 +171,18 @@ if (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles")
171 171
         WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
172 172
         COMMENT "Auto-Formatting code..." VERBATIM
173 173
     )
174
+
175
+    #################################################################
176
+
177
+    # Count source code lines
178
+    add_custom_target (count
179
+        COMMAND cloc --exclude-dir=deps,cmake,data,build ${PROJECT_SOURCE_DIR}
180
+        COMMENT "Counting lines of source code..." VERBATIM
181
+    )
182
+
183
+    add_custom_target (countFull
184
+        COMMAND cloc --exclude-dir=build ${PROJECT_SOURCE_DIR}
185
+        COMMENT "Counting lines of source code..." VERBATIM
186
+    )
174 187
 endif (${CMAKE_GENERATOR} STREQUAL "Unix Makefiles")
175 188
 

+ 3
- 1
include/Render.h Прегледај датотеку

@@ -40,7 +40,9 @@ class Render {
40 40
     static bool getDisplayViewFrustum() { return displayViewFrustum; }
41 41
 
42 42
   private:
43
-    static void buildRoomList(int room = -2, int budget = 10);
43
+    static void buildRoomList(glm::mat4 VP, int room = -2,
44
+                              glm::vec2 min = glm::vec2(-1.0f, -1.0f),
45
+                              glm::vec2 max = glm::vec2(1.0f, 1.0f));
44 46
 
45 47
     static RenderMode mode;
46 48
     static std::vector<Room*> roomList;

+ 3
- 6
include/RoomData.h Прегледај датотеку

@@ -76,11 +76,8 @@ class RoomSprite {
76 76
 
77 77
 class Portal {
78 78
   public:
79
-    Portal(int adj, glm::vec3 n, glm::vec3 v1, glm::vec3 v2, glm::vec3 v3,
80
-           glm::vec3 v4) : adjoiningRoom(adj), normal(n), bbox(v1, v3) {
81
-        vert[0] = v1; vert[1] = v2;
82
-        vert[2] = v3; vert[3] = v4;
83
-    }
79
+    Portal(int adj, glm::vec3 n,
80
+           glm::vec3 v1, glm::vec3 v2, glm::vec3 v3, glm::vec3 v4);
84 81
 
85 82
     int getAdjoiningRoom() { return adjoiningRoom; }
86 83
     glm::vec3 getNormal() { return normal; }
@@ -102,7 +99,7 @@ class Portal {
102 99
     int adjoiningRoom;
103 100
     glm::vec3 normal;
104 101
     glm::vec3 vert[4];
105
-    BoundingBox bbox;
102
+    BoundingBox bbox, bboxNormal;
106 103
 
107 104
     static bool showBoundingBox;
108 105
 };

+ 4
- 0
src/Camera.cpp Прегледај датотеку

@@ -278,6 +278,10 @@ static std::vector<glm::vec3> vertexPointBuffer;
278 278
 static std::vector<glm::vec3> colorPointBuffer;
279 279
 
280 280
 void Camera::calculateFrustumPlanes() {
281
+    vertexBuffer.clear();
282
+    vertexPointBuffer.clear();
283
+    colorPointBuffer.clear();
284
+
281 285
     glm::mat4 combo = projection * view;
282 286
 
283 287
     // Calculate frustum corners to display them

+ 57
- 20
src/Render.cpp Прегледај датотеку

@@ -51,19 +51,20 @@ void Render::display() {
51 51
         gl::glPolygonMode(gl::GL_FRONT_AND_BACK, gl::GL_FILL);
52 52
     }
53 53
 
54
-    if (Camera::update()) {
54
+    bool updated = Camera::update();
55
+    glm::mat4 projection = Camera::getProjectionMatrix();
56
+    glm::mat4 view = Camera::getViewMatrix();
57
+    glm::mat4 VP = projection * view;
58
+
59
+    //if (updated) {
55 60
         int r = Camera::getRoom();
56 61
         clearRoomList();
57 62
         if (r < 0) {
58
-            buildRoomList();
63
+            buildRoomList(VP);
59 64
         } else {
60
-            buildRoomList(r);
65
+            buildRoomList(VP, r);
61 66
         }
62
-    }
63
-
64
-    glm::mat4 projection = Camera::getProjectionMatrix();
65
-    glm::mat4 view = Camera::getViewMatrix();
66
-    glm::mat4 VP = projection * view;
67
+    //}
67 68
 
68 69
     for (int r = roomList.size() - 1; r >= 0; r--) {
69 70
         roomList.at(r)->display(VP);
@@ -84,16 +85,16 @@ void Render::display() {
84 85
     }
85 86
 }
86 87
 
87
-void Render::buildRoomList(int room, int budget) {
88
+void Render::buildRoomList(glm::mat4 VP, int room, glm::vec2 min, glm::vec2 max) {
88 89
     if (room < -1) {
89 90
         // Check if the camera currently is in a room...
90 91
         for (int i = 0; i < World::sizeRoom(); i++) {
91 92
             if (World::getRoom(i).getBoundingBox().inBox(Camera::getPosition())) {
92
-                buildRoomList(i, budget);
93
+                buildRoomList(VP, i);
93 94
                 return;
94 95
             }
95 96
         }
96
-        buildRoomList(-1, budget);
97
+        buildRoomList(VP, -1);
97 98
     } else if (room == -1) {
98 99
         // Check visibility for all rooms!
99 100
         for (int i = 0; i < World::sizeRoom(); i++) {
@@ -102,19 +103,57 @@ void Render::buildRoomList(int room, int budget) {
102 103
             }
103 104
         }
104 105
     } else {
105
-        // Check visibility of room and connected rooms, recursively
106
+        // Check if this room is visible
106 107
         if (Camera::boxInFrustum(World::getRoom(room).getBoundingBox())) {
107 108
             roomList.push_back(&World::getRoom(room));
109
+
110
+            // Display the visibility test for the portal to this room
111
+            BoundingBox debugBox(glm::vec3(min, 0.0f), glm::vec3(max, 0.0f));
112
+            debugBox.display(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
113
+
114
+            ImGui::Text("   Min: %.3f %.3f", min.x, min.y);
115
+            ImGui::Text("   Max: %.3f %.3f", max.x, max.y);
116
+
117
+            // Check all portals leading from this room to somewhere else
108 118
             for (int i = 0; i < World::getRoom(room).sizePortals(); i++) {
109 119
                 auto& portal = World::getRoom(room).getPortal(i);
110 120
 
111
-                // Check if portal is visible / can be seen through
112
-                bool visible = Camera::boxInFrustum(portal.getBoundingBox());
113
-                if (!visible) {
121
+                // Calculate the 2D window of this portal
122
+                glm::vec2 newMin, newMax;
123
+                bool inited = false;
124
+                for (int c = 0; c < 4; c++) {
125
+                    glm::vec3 vert = portal.getVertex(c);
126
+                    glm::vec4 result = VP * glm::vec4(vert, 1.0f);
127
+                    vert = glm::vec3(result) / result.w;
128
+
129
+                    ImGui::Text("Test %d: %.3f %.3f", c, vert.x, vert.y);
130
+
131
+                    if (!inited) {
132
+                        newMin = glm::vec2(vert);
133
+                        newMax = glm::vec2(vert);
134
+                        inited = true;
135
+                    } else {
136
+                        if (vert.x < newMin.x)
137
+                            newMin.x = vert.x;
138
+                        if (vert.y < newMin.y)
139
+                            newMin.y = vert.y;
140
+                        if (vert.x > newMax.x)
141
+                            newMax.x = vert.x;
142
+                        if (vert.y > newMax.y)
143
+                            newMax.y = vert.y;
144
+                    }
145
+                }
146
+
147
+                // Check if the portal intersects the portal leading into this room
148
+                if (!((min.x < newMax.x) && (max.x > newMin.x)
149
+                    && (min.y < newMax.y) && (max.y > newMin.y))) {
150
+                    ImGui::Text("Invisible!");
114 151
                     continue;
152
+                } else {
153
+                    ImGui::Text("Visible!");
115 154
                 }
116 155
 
117
-                // Check if already in list...
156
+                // Check if this room is already in the list...
118 157
                 bool found = false;
119 158
                 for (int n = 0; n < roomList.size(); n++) {
120 159
                     if (roomList.at(n) == &World::getRoom(portal.getAdjoiningRoom())) {
@@ -123,11 +162,9 @@ void Render::buildRoomList(int room, int budget) {
123 162
                     }
124 163
                 }
125 164
 
126
-                // ...only render if not
165
+                // ...only render it if it is not
127 166
                 if (!found) {
128
-                    if (budget > 0) {
129
-                        buildRoomList(portal.getAdjoiningRoom(), --budget);
130
-                    }
167
+                    buildRoomList(VP, portal.getAdjoiningRoom(), newMin, newMax);
131 168
                 }
132 169
             }
133 170
         }

+ 10
- 0
src/RoomData.cpp Прегледај датотеку

@@ -96,9 +96,19 @@ void RoomSprite::display(glm::mat4 VP) {
96 96
 
97 97
 bool Portal::showBoundingBox = false;
98 98
 
99
+Portal::Portal(int adj, glm::vec3 n, glm::vec3 v1, glm::vec3 v2, glm::vec3 v3,
100
+               glm::vec3 v4) : adjoiningRoom(adj), normal(n), bbox(v1, v3),
101
+                               bboxNormal(v1 + ((v3 - v1) / 2.0f),
102
+                                          v1 + ((v3 - v1) / 2.0f)
103
+                                             + (normal * 1024.0f)) {
104
+    vert[0] = v1; vert[1] = v2;
105
+    vert[2] = v3; vert[3] = v4;
106
+}
107
+
99 108
 void Portal::display(glm::mat4 VP) {
100 109
     if (showBoundingBox) {
101 110
         bbox.display(VP, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f));
111
+        bboxNormal.display(VP, glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(1.0f, 0.0f, 0.0f));
102 112
     }
103 113
 }
104 114
 

+ 1
- 3
src/loader/LoaderTR2.cpp Прегледај датотеку

@@ -381,10 +381,8 @@ void LoaderTR2::loadRooms() {
381 381
             int16_t yCorner4 = file.read16();
382 382
             int16_t zCorner4 = file.read16();
383 383
 
384
-            // TODO translate vertices by room offset!
385
-
386 384
             portals.push_back(new Portal(adjoiningRoom,
387
-                                         glm::vec3(xNormal, yNormal, zNormal) + pos,
385
+                                         glm::vec3(xNormal, yNormal, zNormal),
388 386
                                          glm::vec3(xCorner1, yCorner1, zCorner1) + pos,
389 387
                                          glm::vec3(xCorner2, yCorner2, zCorner2) + pos,
390 388
                                          glm::vec3(xCorner3, yCorner3, zCorner3) + pos,

+ 5
- 14
src/system/Shader.cpp Прегледај датотеку

@@ -137,7 +137,11 @@ void Shader::loadUniform(int uni, int texture, TextureStorage store) {
137 137
 
138 138
 void Shader::use() {
139 139
     orAssert(programID >= 0);
140
-    gl::glUseProgram(programID);
140
+    static int lastID = -1;
141
+    if (programID != lastID) {
142
+        gl::glUseProgram(programID);
143
+        lastID = programID;
144
+    }
141 145
 }
142 146
 
143 147
 int Shader::compile(const char* vertex, const char* fragment) {
@@ -299,9 +303,6 @@ void Shader::bindProperBuffer(ShaderTexture* target) {
299 303
 void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs,
300 304
                     glm::mat4 MVP, unsigned int texture, TextureStorage store,
301 305
                     gl::GLenum mode, ShaderTexture* target, Shader& shader) {
302
-    orAssert(vertices.size() == uvs.size());
303
-    if (mode == gl::GL_TRIANGLES)
304
-        orAssert((vertices.size() % 3) == 0);
305 306
     bindProperBuffer(target);
306 307
 
307 308
     shader.use();
@@ -329,8 +330,6 @@ void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uv
329 330
 }
330 331
 
331 332
 void Shader::drawGLBuffer(std::vector<glm::vec3>& vertices, std::vector<glm::vec2>& uvs, Shader& shader) {
332
-    orAssert(vertices.size() == uvs.size());
333
-
334 333
     shader.vertexBuffer.bufferData(vertices);
335 334
     shader.otherBuffer.bufferData(uvs);
336 335
 }
@@ -338,8 +337,6 @@ void Shader::drawGLBuffer(std::vector<glm::vec3>& vertices, std::vector<glm::vec
338 337
 void Shader::drawGLOnly(std::vector<unsigned short>& indices, glm::mat4 MVP,
339 338
                         unsigned int texture, TextureStorage store,
340 339
                         gl::GLenum mode, ShaderTexture* target, Shader& shader) {
341
-    if (mode == gl::GL_TRIANGLES)
342
-        orAssert((indices.size() % 3) == 0);
343 340
     bindProperBuffer(target);
344 341
 
345 342
     shader.use();
@@ -360,9 +357,6 @@ void Shader::drawGLOnly(std::vector<unsigned short>& indices, glm::mat4 MVP,
360 357
 
361 358
 void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
362 359
                     glm::mat4 MVP, gl::GLenum mode, ShaderTexture* target, Shader& shader) {
363
-    orAssert(vertices.size() == colors.size());
364
-    if (mode == gl::GL_TRIANGLES)
365
-        orAssert((vertices.size() % 3) == 0);
366 360
     bindProperBuffer(target);
367 361
 
368 362
     shader.use();
@@ -383,9 +377,6 @@ void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& co
383 377
 void Shader::drawGL(std::vector<glm::vec3>& vertices, std::vector<glm::vec3>& colors,
384 378
                     std::vector<unsigned short>& indices, glm::mat4 MVP,
385 379
                     gl::GLenum mode, ShaderTexture* target, Shader& shader) {
386
-    orAssert(vertices.size() == colors.size());
387
-    if (mode == gl::GL_TRIANGLES)
388
-        orAssert((indices.size() % 3) == 0);
389 380
     bindProperBuffer(target);
390 381
 
391 382
     shader.use();

Loading…
Откажи
Сачувај