Browse Source

Fixed new 2D portal visibility checks.

Thomas Buck 9 years ago
parent
commit
cffa1c7041
8 changed files with 93 additions and 75 deletions
  1. 5
    0
      ChangeLog.md
  2. 2
    3
      include/Camera.h
  3. 1
    0
      include/Render.h
  4. 9
    10
      src/Camera.cpp
  5. 63
    50
      src/Render.cpp
  6. 7
    6
      src/RoomData.cpp
  7. 2
    2
      src/SkeletalModel.cpp
  8. 4
    4
      src/system/Shader.cpp

+ 5
- 0
ChangeLog.md View File

@@ -2,6 +2,11 @@
2 2
 
3 3
 ## OpenRaider (0.1.4) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20150405 ]
6
+    * No longer flipping axis in shaders, now turning camera upside-down.
7
+    * Mostly fixed portal-to-portal 2D visibility checks.
8
+        * Currently also checking behind player.
9
+
5 10
     [ 20150404 ]
6 11
     * World is now static
7 12
     * Can display debug informations about Rooms and StaticMeshes

+ 2
- 3
include/Camera.h View File

@@ -22,9 +22,8 @@ class Camera {
22 22
     static void handleMouseMotion(int x, int y);
23 23
     static void handleControllerAxis(float value, KeyboardButton axis);
24 24
 
25
-    //! \fixme The Y axis seems to be the source of all evil?
26
-    static void setPosition(glm::vec3 p) { pos = glm::vec3(p.x, -p.y, p.z); dirty = true; }
27
-    static glm::vec3 getPosition() { return glm::vec3(pos.x, -pos.y, pos.z); }
25
+    static void setPosition(glm::vec3 p) { pos = p; dirty = true; }
26
+    static glm::vec3 getPosition() { return pos; }
28 27
 
29 28
     static glm::vec2 getRotation() { return rot; }
30 29
     static glm::mat4 getProjectionMatrix() { return projection; }

+ 1
- 0
include/Render.h View File

@@ -48,6 +48,7 @@ class Render {
48 48
     static std::vector<Room*> roomList;
49 49
 
50 50
     static bool displayViewFrustum;
51
+    static bool displayVisibilityCheck;
51 52
 };
52 53
 
53 54
 #endif

+ 9
- 10
src/Camera.cpp View File

@@ -79,7 +79,6 @@ void Camera::reset() {
79 79
 }
80 80
 
81 81
 void Camera::setSize(glm::i32vec2 s) {
82
-    //! \fixme TODO instead of mirroring the Y axis in the shader, scale with -1 here
83 82
     projection = glm::perspective(fov, float(s.x) / float(s.y), nearDist, farDist);
84 83
 }
85 84
 
@@ -93,9 +92,9 @@ void Camera::handleAction(ActionEvents action, bool isFinished) {
93 92
     } else if (action == backwardAction) {
94 93
         posSpeed -= dirUnit * maxSpeed * factor;
95 94
     } else if (action == leftAction) {
96
-        posSpeed += rightUnit * maxSpeed * factor;
97
-    } else if (action == rightAction) {
98 95
         posSpeed -= rightUnit * maxSpeed * factor;
96
+    } else if (action == rightAction) {
97
+        posSpeed += rightUnit * maxSpeed * factor;
99 98
     } else if (action == jumpAction) {
100 99
         posSpeed += upUnit * maxSpeed * factor;
101 100
     } else if (action == crouchAction) {
@@ -114,12 +113,12 @@ void Camera::handleMouseMotion(int x, int y) {
114 113
         dirty = true;
115 114
 
116 115
     while (x > 0) {
117
-        rot.x += rotationDeltaX;
116
+        rot.x -= rotationDeltaX;
118 117
         x--;
119 118
     }
120 119
 
121 120
     while (x < 0) {
122
-        rot.x -= rotationDeltaX;
121
+        rot.x += rotationDeltaX;
123 122
         x++;
124 123
     }
125 124
 
@@ -143,11 +142,11 @@ void Camera::handleControllerAxis(float value, KeyboardButton axis) {
143 142
         value = 0.0f;
144 143
 
145 144
     if (axis == leftXAxis) {
146
-        posSpeed.x = -maxSpeed * value;
145
+        posSpeed.x = maxSpeed * value;
147 146
     } else if (axis == leftYAxis) {
148 147
         posSpeed.z = maxSpeed * value;
149 148
     } else if (axis == rightXAxis) {
150
-        rotSpeed.x = controllerViewFactor * value;
149
+        rotSpeed.x = -controllerViewFactor * value;
151 150
     } else if (axis == rightYAxis) {
152 151
         rotSpeed.y = -controllerViewFactor * value;
153 152
     } else {
@@ -178,9 +177,10 @@ bool Camera::update() {
178 177
     else
179 178
         rotSpeed = glm::vec2(0.0f, 0.0f);
180 179
 
180
+    static glm::quat quatZ = glm::angleAxis(glm::pi<float>(), glm::vec3(0.0f, 0.0f, 1.0f));
181 181
     glm::quat quatY = glm::angleAxis(rot.x, glm::vec3(0.0f, 1.0f, 0.0f));
182 182
     glm::quat quatX = glm::angleAxis(rot.y, glm::vec3(1.0f, 0.0f, 0.0f));
183
-    glm::quat quaternion = quatY * quatX;
183
+    glm::quat quaternion = quatZ * quatY * quatX;
184 184
 
185 185
     glm::vec3 clampedSpeed;
186 186
     if (movingFaster) {
@@ -297,7 +297,6 @@ void Camera::calculateFrustumPlanes() {
297 297
     for (int i = 0; i < 8; i++) {
298 298
         glm::vec4 t = inverse * glm::vec4(frustumVertices[i], 1.0f);
299 299
         frustumVertices[i] = glm::vec3(t) / t.w;
300
-        frustumVertices[i].y *= -1.0f;
301 300
     }
302 301
 
303 302
     // Set planes used for frustum culling
@@ -373,7 +372,7 @@ bool Camera::boxInFrustum(BoundingBox b) {
373 372
     for (int i = 0; i < 6; i++) {
374 373
         int out = 0, in = 0;
375 374
         for (int c = 0; (c < 8) && ((in == 0) || (out == 0)); c++) {
376
-            if (planes[i].distance(b.getCorner(c)) < 0)
375
+            if (planes[i].distance(b.getCorner(c)) > 0)
377 376
                 out++;
378 377
             else
379 378
                 in++;

+ 63
- 50
src/Render.cpp View File

@@ -28,6 +28,7 @@
28 28
 RenderMode Render::mode = RenderMode::LoadScreen;
29 29
 std::vector<Room*> Render::roomList;
30 30
 bool Render::displayViewFrustum = false;
31
+bool Render::displayVisibilityCheck = false;
31 32
 
32 33
 void Render::display() {
33 34
     gl::glClear(gl::GL_COLOR_BUFFER_BIT | gl::GL_DEPTH_BUFFER_BIT);
@@ -56,7 +57,7 @@ void Render::display() {
56 57
     glm::mat4 view = Camera::getViewMatrix();
57 58
     glm::mat4 VP = projection * view;
58 59
 
59
-    //if (updated) {
60
+    if (updated || displayVisibilityCheck) {
60 61
         int r = Camera::getRoom();
61 62
         clearRoomList();
62 63
         if (r < 0) {
@@ -64,7 +65,7 @@ void Render::display() {
64 65
         } else {
65 66
             buildRoomList(VP, r);
66 67
         }
67
-    //}
68
+    }
68 69
 
69 70
     for (int r = roomList.size() - 1; r >= 0; r--) {
70 71
         roomList.at(r)->display(VP);
@@ -103,70 +104,80 @@ void Render::buildRoomList(glm::mat4 VP, int room, glm::vec2 min, glm::vec2 max)
103 104
             }
104 105
         }
105 106
     } else {
106
-        // Check if this room is visible
107
-        if (Camera::boxInFrustum(World::getRoom(room).getBoundingBox())) {
108
-            roomList.push_back(&World::getRoom(room));
107
+        roomList.push_back(&World::getRoom(room));
109 108
 
109
+        if (displayVisibilityCheck) {
110 110
             // Display the visibility test for the portal to this room
111 111
             BoundingBox debugBox(glm::vec3(min, 0.0f), glm::vec3(max, 0.0f));
112 112
             debugBox.display(glm::mat4(1.0f), glm::vec3(1.0f, 1.0f, 1.0f), glm::vec3(0.0f, 1.0f, 0.0f));
113 113
 
114 114
             ImGui::Text("   Min: %.3f %.3f", min.x, min.y);
115 115
             ImGui::Text("   Max: %.3f %.3f", max.x, max.y);
116
+        }
117
+
118
+        // Check all portals leading from this room to somewhere else
119
+        for (int i = 0; i < World::getRoom(room).sizePortals(); i++) {
120
+            auto& portal = World::getRoom(room).getPortal(i);
121
+
122
+            // Calculate the 2D window of this portal
123
+            glm::vec3 newMin, newMax;
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;
116 128
 
117
-            // Check all portals leading from this room to somewhere else
118
-            for (int i = 0; i < World::getRoom(room).sizePortals(); i++) {
119
-                auto& portal = World::getRoom(room).getPortal(i);
120
-
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
-                    }
129
+                if (displayVisibilityCheck) {
130
+                    ImGui::Text("Test %d: %.3f %.3f %.3f", c, vert.x, vert.y, vert.z);
145 131
                 }
146 132
 
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!");
151
-                    continue;
133
+                if (c == 0) {
134
+                    newMin = vert;
135
+                    newMax = vert;
152 136
                 } else {
153
-                    ImGui::Text("Visible!");
137
+                    if (vert.x < newMin.x)
138
+                        newMin.x = vert.x;
139
+                    if (vert.y < newMin.y)
140
+                        newMin.y = vert.y;
141
+                    if (vert.z < newMin.z)
142
+                        newMin.z = vert.z;
143
+                    if (vert.x > newMax.x)
144
+                        newMax.x = vert.x;
145
+                    if (vert.y > newMax.y)
146
+                        newMax.y = vert.y;
147
+                    if (vert.z > newMax.z)
148
+                        newMax.z = vert.z;
154 149
                 }
150
+            }
155 151
 
156
-                // Check if this room is already in the list...
157
-                bool found = false;
158
-                for (int n = 0; n < roomList.size(); n++) {
159
-                    if (roomList.at(n) == &World::getRoom(portal.getAdjoiningRoom())) {
160
-                        found = true;
161
-                        break;
162
-                    }
163
-                }
152
+            if (displayVisibilityCheck) {
153
+                ImGui::Text("NewMin: %.3f %.3f %.3f", newMin.x, newMin.y, newMin.z);
154
+                ImGui::Text("NewMax: %.3f %.3f %.3f", newMax.x, newMax.y, newMax.z);
155
+            }
164 156
 
165
-                // ...only render it if it is not
166
-                if (!found) {
167
-                    buildRoomList(VP, portal.getAdjoiningRoom(), newMin, newMax);
157
+            //! \fixme Currently also checking behind player, because Z is always 1.0f?!
158
+            //if ((newMin.z > 0.0f) || (newMin.z < -1.0f) || (newMax.z > 0.0f) || (newMax.z < -1.0f)) {
159
+            //    continue;
160
+            //}
161
+
162
+            // Check if the portal intersects the portal leading into this room
163
+            if (!((min.x < newMax.x) && (max.x > newMin.x)
164
+                && (min.y < newMax.y) && (max.y > newMin.y))) {
165
+                continue;
166
+            }
167
+
168
+            // Check if this room is already in the list...
169
+            bool found = false;
170
+            for (int n = 0; n < roomList.size(); n++) {
171
+                if (roomList.at(n) == &World::getRoom(portal.getAdjoiningRoom())) {
172
+                    found = true;
173
+                    break;
168 174
                 }
169 175
             }
176
+
177
+            // ...only render it if it is not
178
+            if (!found) {
179
+                buildRoomList(VP, portal.getAdjoiningRoom(), glm::vec2(newMin), glm::vec2(newMax));
180
+            }
170 181
         }
171 182
     }
172 183
 }
@@ -269,6 +280,8 @@ void Render::displayUI() {
269 280
         if (ImGui::Checkbox("Portals##bbox", &showBoundingBox3)) {
270 281
             Portal::setShowBoundingBox(showBoundingBox3);
271 282
         }
283
+        ImGui::SameLine();
284
+        ImGui::Checkbox("VisChecks##bbox", &displayVisibilityCheck);
272 285
 
273 286
         ImGui::Separator();
274 287
         ImGui::Text("Renderable Objects:");

+ 7
- 6
src/RoomData.cpp View File

@@ -59,7 +59,7 @@ void BoundingBox::display(glm::mat4 VP, glm::vec3 colorLine, glm::vec3 colorDot)
59 59
 // ----------------------------------------------------------------------------
60 60
 
61 61
 StaticModel::StaticModel(glm::vec3 pos, float angle, int i) : id(i), cache(-1) {
62
-    glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
62
+    glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
63 63
     glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), angle, glm::vec3(0.0f, 1.0f, 0.0f));
64 64
     model = translate * rotate;
65 65
 }
@@ -84,12 +84,13 @@ void StaticModel::displayUI() {
84 84
 // ----------------------------------------------------------------------------
85 85
 
86 86
 void RoomSprite::display(glm::mat4 VP) {
87
-    glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
88
-    glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), Camera::getRotation().x, glm::vec3(0.0f, 1.0f,
89
-                                   0.0f));
90
-    glm::mat4 model = translate * rotate;
87
+    glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
91 88
 
92
-    World::getSprite(sprite).display(VP * model);
89
+    //! \fixme Calculate angle between camera and sprite
90
+    glm::mat4 rotate = glm::rotate(glm::mat4(1.0f), Camera::getRotation().x,
91
+                                   glm::vec3(0.0f, 1.0f, 0.0f));
92
+
93
+    World::getSprite(sprite).display(VP * (translate * rotate));
93 94
 }
94 95
 
95 96
 // ----------------------------------------------------------------------------

+ 2
- 2
src/SkeletalModel.cpp View File

@@ -108,7 +108,7 @@ void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe, ShaderTexture
108 108
     BoneFrame& boneframe = anim.get(bframe);
109 109
 
110 110
     glm::vec3 pos = boneframe.getPosition();
111
-    glm::mat4 frameTrans = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));
111
+    glm::mat4 frameTrans = glm::translate(glm::mat4(1.0f), pos);
112 112
 
113 113
     MatrixStack stack(MVP * frameTrans);
114 114
 
@@ -147,7 +147,7 @@ void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe, ShaderTexture
147 147
 #endif
148 148
 
149 149
             glm::vec3 off = tag.getOffset();
150
-            translate = glm::translate(glm::mat4(1.0f), glm::vec3(off.x, -off.y, off.z));
150
+            translate = glm::translate(glm::mat4(1.0f), off);
151 151
         }
152 152
 
153 153
         glm::vec3 rot = tag.getRotation();

+ 4
- 4
src/system/Shader.cpp View File

@@ -411,10 +411,10 @@ uniform mat4 MVP;
411 411
 
412 412
 void main() {
413 413
     vec4 pos = MVP * vec4(vertexPosition_modelspace.x,
414
-                          -vertexPosition_modelspace.y,
414
+                          vertexPosition_modelspace.y,
415 415
                           vertexPosition_modelspace.z,
416 416
                           1);
417
-    gl_Position = vec4(-pos.x, pos.yzw);
417
+    gl_Position = pos;
418 418
     UV = vertexUV;
419 419
 }
420 420
 )!?!";
@@ -447,10 +447,10 @@ uniform mat4 MVP;
447 447
 
448 448
 void main() {
449 449
     vec4 pos = MVP * vec4(vertexPosition_modelspace.x,
450
-                          -vertexPosition_modelspace.y,
450
+                          vertexPosition_modelspace.y,
451 451
                           vertexPosition_modelspace.z,
452 452
                           1);
453
-    gl_Position = vec4(-pos.x, pos.yzw);
453
+    gl_Position = pos;
454 454
     color = vertexColor;
455 455
 }
456 456
 )!?!";

Loading…
Cancel
Save