Browse Source

Can also click on Room Sprites

Thomas Buck 9 years ago
parent
commit
b5f7ad321f
10 changed files with 127 additions and 55 deletions
  1. 6
    0
      ChangeLog.md
  2. 4
    1
      include/Room.h
  3. 5
    0
      include/RoomData.h
  4. 4
    0
      include/Sprite.h
  5. 1
    0
      include/global.h
  6. 16
    0
      src/Room.cpp
  7. 18
    0
      src/RoomData.cpp
  8. 50
    52
      src/Selector.cpp
  9. 15
    0
      src/Sprite.cpp
  10. 8
    2
      src/World.cpp

+ 6
- 0
ChangeLog.md View File

@@ -2,6 +2,12 @@
2 2
 
3 3
 ## OpenRaider (0.1.4) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20150811 ]
6
+    * Can click on RoomModels and RoomSprites
7
+    * This will show their BoundingSpheres
8
+    * Fixed Mouse Selector Depth Sorting Issue
9
+    * Improved Room List UI
10
+
5 11
     [ 20150803 ]
6 12
     * Started working on ray casting for mouse object selection
7 13
 

+ 4
- 1
include/Room.h View File

@@ -44,9 +44,12 @@ class Room {
44 44
     int getNumZSectors() { return numZSectors; }
45 45
     int getIndex() { return roomIndex; }
46 46
 
47
-    void addSprite(RoomSprite* s) { sprites.emplace_back(s); }
48 47
     void addSector(Sector* s) { sectors.emplace_back(s); }
49 48
 
49
+    void addSprite(RoomSprite* s) { sprites.emplace_back(s); }
50
+    unsigned long sizeSprites() { return sprites.size(); }
51
+    RoomSprite& getSprite(unsigned long index) { return *sprites.at(index); }
52
+
50 53
     void addModel(StaticModel* s) { models.emplace_back(s); }
51 54
     unsigned long sizeModels() { return models.size(); }
52 55
     StaticModel& getModel(unsigned long index) { return *models.at(index); }

+ 5
- 0
include/RoomData.h View File

@@ -34,6 +34,11 @@ class RoomSprite {
34 34
   public:
35 35
     RoomSprite(glm::vec3 p, int s) : pos(p), sprite(s) { }
36 36
     void display(glm::mat4 VP);
37
+    void displayUI();
38
+
39
+    glm::vec3 getCenter();
40
+    float getRadius();
41
+    void displayBoundingSphere(glm::mat4 VP, glm::vec3 color);
37 42
 
38 43
   private:
39 44
     glm::vec3 pos;

+ 4
- 0
include/Sprite.h View File

@@ -10,6 +10,8 @@
10 10
 
11 11
 #include <vector>
12 12
 
13
+#include "BoundingSphere.h"
14
+
13 15
 class Sprite {
14 16
   public:
15 17
     Sprite(int tile, int x, int y, int width, int height);
@@ -17,12 +19,14 @@ class Sprite {
17 19
 
18 20
     int getTexture() { return texture; }
19 21
     glm::vec4 getUVs() { return uv2D; }
22
+    BoundingSphere& getBoundingSphere() { return boundingSphere; }
20 23
 
21 24
   private:
22 25
     int texture;
23 26
     std::vector<glm::vec3> vertexBuff;
24 27
     std::vector<glm::vec2> uvBuff;
25 28
     glm::vec4 uv2D;
29
+    BoundingSphere boundingSphere;
26 30
 };
27 31
 
28 32
 class SpriteSequence {

+ 1
- 0
include/global.h View File

@@ -18,6 +18,7 @@ typedef enum {
18 18
     spriteObject,
19 19
     meshObject,
20 20
     modelObject,
21
+    entityObject,
21 22
 
22 23
     WorldObjectCount // Should always be at the end
23 24
 } WorldObjects;

+ 16
- 0
src/Room.cpp View File

@@ -6,6 +6,7 @@
6 6
  */
7 7
 
8 8
 #include "global.h"
9
+#include "Camera.h"
9 10
 #include "Log.h"
10 11
 #include "Room.h"
11 12
 
@@ -139,6 +140,21 @@ void Room::displayUI() {
139 140
         ImGui::Text("None");
140 141
     }
141 142
     ImGui::NextColumn();
143
+    if (sprites.size() > 0) {
144
+        if (ImGui::TreeNode("...##sprite")) {
145
+            for (auto& s : sprites) {
146
+                s->displayUI();
147
+            }
148
+            ImGui::TreePop();
149
+        }
150
+    } else {
151
+        ImGui::Text("None");
152
+    }
153
+    ImGui::NextColumn();
154
+    if (ImGui::Button("Warp")) {
155
+        Camera::setPosition(pos);
156
+    }
157
+    ImGui::NextColumn();
142 158
     ImGui::PopID();
143 159
 }
144 160
 

+ 18
- 0
src/RoomData.cpp View File

@@ -60,6 +60,20 @@ void StaticModel::displayUI() {
60 60
 
61 61
 // ----------------------------------------------------------------------------
62 62
 
63
+glm::vec3 RoomSprite::getCenter() {
64
+    glm::vec3 center = World::getSprite(sprite).getBoundingSphere().getPosition();
65
+    glm::vec4 tmp = glm::translate(glm::mat4(1.0f), pos) * glm::vec4(center, 1.0f);
66
+    return glm::vec3(tmp) / tmp.w;
67
+}
68
+
69
+float RoomSprite::getRadius() {
70
+    return World::getSprite(sprite).getBoundingSphere().getRadius();
71
+}
72
+
73
+void RoomSprite::displayBoundingSphere(glm::mat4 VP, glm::vec3 color) {
74
+    World::getSprite(sprite).getBoundingSphere().display(VP * glm::translate(glm::mat4(1.0f), pos), color);
75
+}
76
+
63 77
 void RoomSprite::display(glm::mat4 VP) {
64 78
     glm::mat4 translate = glm::translate(glm::mat4(1.0f), pos);
65 79
 
@@ -70,6 +84,10 @@ void RoomSprite::display(glm::mat4 VP) {
70 84
     World::getSprite(sprite).display(VP * (translate * rotate));
71 85
 }
72 86
 
87
+void RoomSprite::displayUI() {
88
+    ImGui::Text("Sprite %d", sprite);
89
+}
90
+
73 91
 // ----------------------------------------------------------------------------
74 92
 
75 93
 bool Portal::showBoundingBox = false;

+ 50
- 52
src/Selector.cpp View File

@@ -20,7 +20,10 @@
20 20
 
21 21
 bool Selector::visible = false;
22 22
 WorldObjects Selector::lastClickedObject = WorldObjectCount;
23
-std::array<bool, WorldObjectCount> Selector::clickOnObject = {{ false, true, true, true, false, false }};
23
+
24
+// geometryObject, roomSpriteObject, roomModelObject, spriteObject, meshObject, modelObject, entityObject
25
+std::array<bool, WorldObjectCount> Selector::clickOnObject = {{ false, true, true, false, false, false, false }};
26
+
24 27
 glm::i32vec2 Selector::rayScreen(-1, -1);
25 28
 glm::vec3 Selector::rayWorld, Selector::lastIntersectPos, Selector::lastIntersectNorm;
26 29
 unsigned long Selector::lastIndexA, Selector::lastIndexB;
@@ -41,48 +44,48 @@ void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton b
41 44
         bool foundSomething = false;
42 45
         float depth = -1.0f;
43 46
 
44
-        if (clickOnObject[modelObject]) {
45
-
46
-        }
47
-
48
-        if (clickOnObject[meshObject]) {
49
-
50
-        }
51
-
52
-        if (clickOnObject[spriteObject]) {
53
-
54
-        }
55
-
56
-        if (clickOnObject[roomModelObject]) {
47
+        if (clickOnObject[roomModelObject] || clickOnObject[roomSpriteObject]) {
57 48
             for (unsigned long i = 0; i < World::sizeRoom(); i++) {
58
-                Room &r = World::getRoom(i);
59
-                for (unsigned long j = 0; j < r.sizeModels(); j++) {
60
-                    StaticModel &sm = r.getModel(j);
61
-                    glm::vec3 pos, norm;
62
-                    if (glm::intersectRaySphere(Camera::getPosition(), rayWorld, sm.getCenter(), sm.getRadius(),
63
-                                                pos, norm)) {
64
-                        float newDepth = glm::abs(glm::distance(sm.getCenter(), Camera::getPosition()));
65
-                        if ((newDepth < depth) || (depth < 0.0f)) {
66
-                            depth = newDepth;
67
-                            lastIndexA = i;
68
-                            lastIndexB = j;
69
-                            lastIntersectPos = pos;
70
-                            lastIntersectNorm = norm;
71
-                            lastClickedObject = roomModelObject;
72
-                            foundSomething = true;
49
+                Room& r = World::getRoom(i);
50
+                glm::vec3 pos, norm;
51
+
52
+                if (clickOnObject[roomModelObject]) {
53
+                    for (unsigned long j = 0; j < r.sizeModels(); j++) {
54
+                        StaticModel& sm = r.getModel(j);
55
+                        if (glm::intersectRaySphere(Camera::getPosition(), rayWorld, sm.getCenter(), sm.getRadius(),
56
+                                                    pos, norm)) {
57
+                            float newDepth = glm::abs(glm::distance(sm.getCenter(), Camera::getPosition()));
58
+                            if ((newDepth < depth) || (depth < 0.0f)) {
59
+                                depth = newDepth;
60
+                                lastIndexA = i;
61
+                                lastIndexB = j;
62
+                                lastIntersectPos = pos;
63
+                                lastIntersectNorm = norm;
64
+                                lastClickedObject = roomModelObject;
65
+                                foundSomething = true;
66
+                            }
73 67
                         }
74 68
                     }
75 69
                 }
76
-            }
77
-        }
78
-
79
-        if (clickOnObject[roomSpriteObject]) {
80
-
81
-        }
82
-
83
-        if (clickOnObject[geometryObject]) {
84
-
85 70
 
71
+                if (clickOnObject[roomSpriteObject]) {
72
+                    for (unsigned long j = 0; j < r.sizeSprites(); j++) {
73
+                        RoomSprite& rs = r.getSprite(j);
74
+                        if (glm::intersectRaySphere(Camera::getPosition(), rayWorld, rs.getCenter(), rs.getRadius(), pos, norm)) {
75
+                            float newDepth = glm::abs(glm::distance(rs.getCenter(), Camera::getPosition()));
76
+                            if ((newDepth < depth) || (depth < 0.0f)) {
77
+                                depth = newDepth;
78
+                                lastIndexA = i;
79
+                                lastIndexB = j;
80
+                                lastIntersectPos = pos;
81
+                                lastIntersectNorm = norm;
82
+                                lastClickedObject = roomSpriteObject;
83
+                                foundSomething = true;
84
+                            }
85
+                        }
86
+                    }
87
+                }
88
+            }
86 89
         }
87 90
 
88 91
         if (!foundSomething) {
@@ -94,6 +97,10 @@ void Selector::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton b
94 97
 void Selector::displaySelection() {
95 98
     if (lastClickedObject == roomModelObject) {
96 99
         World::getRoom(lastIndexA).getModel(lastIndexB).displayBoundingSphere(Camera::getProjectionMatrix() * Camera::getViewMatrix(), glm::vec3(1.0f, 0.0f, 0.0f));
100
+    } else if (lastClickedObject == roomSpriteObject) {
101
+        World::getRoom(lastIndexA).getSprite(lastIndexB).displayBoundingSphere(Camera::getProjectionMatrix() * Camera::getViewMatrix(), glm::vec3(1.0f, 0.0f, 0.0f));
102
+    } else {
103
+        lastClickedObject = WorldObjectCount;
97 104
     }
98 105
 }
99 106
 
@@ -106,29 +113,15 @@ void Selector::display() {
106 113
         return;
107 114
     }
108 115
 
109
-    ImGui::Checkbox("Geometry", &clickOnObject[geometryObject]);
110
-    ImGui::SameLine();
111 116
     ImGui::Checkbox("RoomModels", &clickOnObject[roomModelObject]);
112 117
     ImGui::SameLine();
113 118
     ImGui::Checkbox("RoomSprites", &clickOnObject[roomSpriteObject]);
114
-    ImGui::Checkbox("Sprites", &clickOnObject[spriteObject]);
115
-    ImGui::SameLine();
116
-    ImGui::Checkbox("Meshes", &clickOnObject[meshObject]);
117
-    ImGui::SameLine();
118
-    ImGui::Checkbox("Models", &clickOnObject[modelObject]);
119 119
     ImGui::SameLine();
120 120
     if (ImGui::Button("Hide Selector")) {
121 121
         visible = false;
122 122
     }
123 123
     ImGui::Separator();
124 124
 
125
-    // Not yet implemented!
126
-    clickOnObject[modelObject] = false;
127
-    clickOnObject[meshObject] = false;
128
-    clickOnObject[spriteObject] = false;
129
-    clickOnObject[roomSpriteObject] = false;
130
-    clickOnObject[geometryObject] = false;
131
-
132 125
     ImGui::Text("Camera: (%.2f %.2f %.2f)", Camera::getPosition().x, Camera::getPosition().y, Camera::getPosition().z);
133 126
     ImGui::Text("Last click: (%d %d)", rayScreen.x, rayScreen.y);
134 127
     if ((rayScreen.x >= 0) && (rayScreen.y >= 0)) {
@@ -143,6 +136,11 @@ void Selector::display() {
143 136
     if (lastClickedObject == roomModelObject) {
144 137
         ImGui::Text("Last Room: %lu", lastIndexA);
145 138
         ImGui::Text("Last RoomModel: %lu", lastIndexB);
139
+    } else if (lastClickedObject == roomSpriteObject) {
140
+        ImGui::Text("Last Room: %lu", lastIndexA);
141
+        ImGui::Text("Last RoomSprite: %lu", lastIndexB);
142
+    } else {
143
+        lastClickedObject = WorldObjectCount;
146 144
     }
147 145
 
148 146
     ImGui::End();

+ 15
- 0
src/Sprite.cpp View File

@@ -38,6 +38,21 @@ Sprite::Sprite(int tile, int x, int y, int width, int height) : texture(tile) {
38 38
     vertexBuff.emplace_back(vertexBuff.at(2));
39 39
 
40 40
     uv2D = glm::vec4(uvBuff.at(0), uvBuff.at(2));
41
+
42
+    glm::vec3 average(0.0f, 0.0f, 0.0f);
43
+    int averageCount = 0;
44
+    for (auto& vert : vertexBuff) {
45
+        average += vert;
46
+        averageCount++;
47
+    }
48
+    glm::vec3 center = average / float(averageCount);
49
+    float radius = 0.0f;
50
+    for (auto& vert : vertexBuff) {
51
+        float dist = glm::distance(center, vert);
52
+        if (dist > radius) radius = dist;
53
+    }
54
+    boundingSphere.setPosition(center);
55
+    boundingSphere.setRadius(radius);
41 56
 }
42 57
 
43 58
 void Sprite::display(glm::mat4 MVP) {

+ 8
- 2
src/World.cpp View File

@@ -123,7 +123,7 @@ void World::displayUI() {
123 123
     // Rooms
124 124
     static bool offsets = false;
125 125
     if (ImGui::CollapsingHeader("Rooms")) {
126
-        ImGui::Columns(6, "rooms");
126
+        ImGui::Columns(8, "rooms");
127 127
         ImGui::Text("No");
128 128
         ImGui::NextColumn();
129 129
         ImGui::Text("Ind.");
@@ -136,13 +136,19 @@ void World::displayUI() {
136 136
         ImGui::NextColumn();
137 137
         ImGui::Text("Portals");
138 138
         ImGui::NextColumn();
139
+        ImGui::Text("Sprites");
140
+        ImGui::NextColumn();
141
+        ImGui::Text("Tools");
142
+        ImGui::NextColumn();
139 143
         ImGui::Separator();
140 144
         if (!offsets) {
141 145
             ImGui::SetColumnOffset(1, 40.0f);
142 146
             ImGui::SetColumnOffset(2, 80.0f);
143 147
             ImGui::SetColumnOffset(3, 120.0f);
144 148
             ImGui::SetColumnOffset(4, 180.0f);
145
-            ImGui::SetColumnOffset(5, 300.0f);
149
+            ImGui::SetColumnOffset(5, 250.0f);
150
+            ImGui::SetColumnOffset(6, 350.0f);
151
+            ImGui::SetColumnOffset(7, 400.0f);
146 152
             offsets = true;
147 153
         }
148 154
         for (int i = 0; i < rooms.size(); i++) {

Loading…
Cancel
Save