浏览代码

Render into textures, camera room tracking & target UI (all non-working)

Thomas Buck 9 年前
父节点
当前提交
accd3ea4e8

+ 10
- 1
include/Camera.h 查看文件

@@ -39,9 +39,17 @@ class Camera {
39 39
     static void setUpdateViewFrustum(bool u) { updateViewFrustum = u; }
40 40
     static bool getUpdateViewFrustum() { return updateViewFrustum; }
41 41
 
42
+    static void setRoom(int r) { if (room != r) dirty = true; room = r; }
43
+    static int getRoom() { return room; }
44
+
45
+    static void setShowOverlay(bool s) { showOverlay = s; }
46
+    static bool getShowOverlay() { return showOverlay; }
47
+
42 48
     static bool boxInFrustum(BoundingBox b);
43 49
     static void displayFrustum(glm::mat4 MVP);
44 50
 
51
+    static void displayUI();
52
+
45 53
   private:
46 54
     static void calculateFrustumPlanes();
47 55
 
@@ -52,7 +60,8 @@ class Camera {
52 60
     static glm::mat4 projection;
53 61
     static glm::mat4 view;
54 62
     static float rotationDeltaX, rotationDeltaY;
55
-    static bool updateViewFrustum, dirty;
63
+    static bool updateViewFrustum, dirty, showOverlay;
64
+    static int room;
56 65
 };
57 66
 
58 67
 #endif

+ 1
- 1
include/Mesh.h 查看文件

@@ -41,7 +41,7 @@ class Mesh {
41 41
          const std::vector<IndexedColoredRectangle>& coloredRectangles,
42 42
          const std::vector<IndexedColoredRectangle>& coloredTriangles);
43 43
     void prepare();
44
-    void display(glm::mat4 MVP);
44
+    void display(glm::mat4 MVP, ShaderTexture* shaderTexture = nullptr);
45 45
 
46 46
   private:
47 47
     std::vector<unsigned short> indicesBuff;

+ 1
- 1
include/Render.h 查看文件

@@ -43,7 +43,7 @@ class Render {
43 43
     static bool getDisplayViewFrustum() { return displayViewFrustum; }
44 44
 
45 45
   private:
46
-    static void buildRoomList(int room);
46
+    static void buildRoomList(int room = -2);
47 47
 
48 48
     static RenderMode mode;
49 49
     static std::vector<Room*> roomList;

+ 4
- 2
include/SkeletalModel.h 查看文件

@@ -11,10 +11,12 @@
11 11
 
12 12
 #include <vector>
13 13
 
14
+#include "system/Shader.h"
15
+
14 16
 class BoneTag {
15 17
   public:
16 18
     BoneTag(int m, glm::vec3 o, glm::vec3 r, char f) : mesh(m), off(o), rot(r), flag(f) { }
17
-    void display(glm::mat4 MVP);
19
+    void display(glm::mat4 MVP, ShaderTexture* shaderTexture = nullptr);
18 20
 
19 21
     glm::vec3 getOffset() { return off; }
20 22
     glm::vec3 getRotation() { return rot; }
@@ -60,7 +62,7 @@ class SkeletalModel {
60 62
   public:
61 63
     SkeletalModel(int i) : id(i) { }
62 64
     ~SkeletalModel();
63
-    void display(glm::mat4 MVP, int aframe, int bframe);
65
+    void display(glm::mat4 MVP, int aframe, int bframe, ShaderTexture* shaderTexture = nullptr);
64 66
 
65 67
     int getID() { return id; }
66 68
 

+ 4
- 6
include/SoundManager.h 查看文件

@@ -11,11 +11,11 @@
11 11
 #include <vector>
12 12
 
13 13
 struct SoundSource {
14
-    float x, y, z;
14
+    glm::vec3 pos;
15 15
     int id, flags;
16 16
 
17
-    SoundSource(float _x, float _y, float _z, int i, int f)
18
-        : x(_x), y(_y), z(_z), id(i), flags(f) { }
17
+    SoundSource(glm::vec3 p, int i, int f)
18
+        : pos(p), id(i), flags(f) { }
19 19
 };
20 20
 
21 21
 struct SoundDetail {
@@ -30,13 +30,11 @@ class SoundManager {
30 30
     static void clear();
31 31
     static int prepareSources();
32 32
 
33
-    static void addSoundSource(float x, float y, float z, int id, int flags);
33
+    static void addSoundSource(glm::vec3 p, int id, int flags);
34 34
     static void addSoundMapEntry(int id);
35 35
     static void addSoundDetail(int sample, float volume);
36 36
     static void addSampleIndex(int index);
37 37
 
38
-    static int sizeSoundMap();
39
-
40 38
     static int getIndex(int index, float* volume = nullptr);
41 39
 
42 40
     // index --> SoundMap --> SoundDetails --> SampleIndices --> play

+ 2
- 0
include/TextureManager.h 查看文件

@@ -112,6 +112,8 @@ class TextureManager {
112 112
 
113 113
     static void display();
114 114
 
115
+    static unsigned int getTextureID(int n, TextureStorage s);
116
+
115 117
   private:
116 118
     static std::vector<unsigned int>& getIds(TextureStorage s);
117 119
     static std::vector<int>& getUnits(TextureStorage s);

+ 24
- 5
include/system/Shader.h 查看文件

@@ -35,6 +35,21 @@ class ShaderBuffer {
35 35
     int boundSize;
36 36
 };
37 37
 
38
+class ShaderTexture {
39
+  public:
40
+    ShaderTexture(int w = 512, int h = 512);
41
+    ~ShaderTexture();
42
+
43
+    void clear();
44
+    void bind();
45
+    int getTexture() { return texture; }
46
+
47
+  private:
48
+    int width, height;
49
+    unsigned int framebuffer, depth;
50
+    int texture;
51
+};
52
+
38 53
 class Shader {
39 54
   public:
40 55
     Shader() : programID(-1) { }
@@ -58,18 +73,22 @@ class Shader {
58 73
 
59 74
     static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color, unsigned int texture,
60 75
                        TextureStorage store = TextureStorage::SYSTEM, unsigned int mode = GL_TRIANGLES,
61
-                       Shader& shader = textShader);
76
+                       ShaderTexture* target = nullptr, Shader& shader = textShader);
62 77
 
63 78
     static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int texture, glm::mat4 MVP,
64
-                       TextureStorage store = TextureStorage::GAME, Shader& shader = textureShader);
79
+                       TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
80
+                       Shader& shader = textureShader);
65 81
     static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& indices,
66 82
                        unsigned int texture, glm::mat4 MVP,
67
-                       TextureStorage store = TextureStorage::GAME, Shader& shader = textureShader);
83
+                       TextureStorage store = TextureStorage::GAME, ShaderTexture* target = nullptr,
84
+                       Shader& shader = textureShader);
68 85
 
69 86
     static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
70
-                       unsigned int mode = GL_TRIANGLES, Shader& shader = colorShader);
87
+                       unsigned int mode = GL_TRIANGLES, ShaderTexture* target = nullptr,
88
+                       Shader& shader = colorShader);
71 89
     static void drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, ShaderBuffer& indices,
72
-                       glm::mat4 MVP, unsigned int mode = GL_TRIANGLES, Shader& shader = colorShader);
90
+                       glm::mat4 MVP, unsigned int mode = GL_TRIANGLES, ShaderTexture* target = nullptr,
91
+                       Shader& shader = colorShader);
73 92
 
74 93
   private:
75 94
     int programID;

+ 2
- 2
include/system/Sound.h 查看文件

@@ -20,8 +20,8 @@ class Sound {
20 20
     static int numSources(bool atListener = false);
21 21
     static int addSource(int buffer, float volume = 1.0f, bool atListener = false, bool loop = false);
22 22
 
23
-    static int sourceAt(int source, float pos[3]);
24
-    static void listenAt(float pos[3], float orientation[6]);
23
+    static int sourceAt(int source, glm::vec3 pos);
24
+    static void listenAt(glm::vec3 pos, glm::vec3 at, glm::vec3 up);
25 25
 
26 26
     static void play(int source, bool atListener = false);
27 27
     static void stopAll();

+ 3
- 3
include/system/SoundAL.h 查看文件

@@ -22,8 +22,8 @@ class SoundAL {
22 22
     static int numSources(bool atListener);
23 23
     static int addSource(int buffer, float volume, bool atListener, bool loop);
24 24
 
25
-    static int sourceAt(int source, float pos[3]);
26
-    static void listenAt(float pos[3], float orientation[6]);
25
+    static int sourceAt(int source, glm::vec3 pos);
26
+    static void listenAt(glm::vec3 pos, glm::vec3 at, glm::vec3 up);
27 27
 
28 28
     static void play(int source, bool atListener);
29 29
     static void stopAll();
@@ -43,7 +43,7 @@ class SoundAL {
43 43
     static std::vector<unsigned int> sources;
44 44
     static std::vector<unsigned int> listenerSources;
45 45
 
46
-    static float lastPosition[3];
46
+    static glm::vec3 lastPosition;
47 47
 };
48 48
 
49 49
 #endif

+ 18
- 0
src/Camera.cpp 查看文件

@@ -8,6 +8,8 @@
8 8
 
9 9
 #include <limits>
10 10
 
11
+#include "imgui/imgui.h"
12
+
11 13
 #include "global.h"
12 14
 #include "RunTime.h"
13 15
 #include "system/Shader.h"
@@ -56,6 +58,8 @@ float Camera::rotationDeltaX = 0.75f;
56 58
 float Camera::rotationDeltaY = 0.75f;
57 59
 bool Camera::updateViewFrustum = true;
58 60
 bool Camera::dirty = true;
61
+bool Camera::showOverlay = false;
62
+int Camera::room = -1;
59 63
 
60 64
 void Camera::reset() {
61 65
     pos = glm::vec3(0.0f, 0.0f, 0.0f);
@@ -65,6 +69,7 @@ void Camera::reset() {
65 69
     dirty = true;
66 70
     projection = glm::mat4(1.0f);
67 71
     view = glm::mat4(1.0f);
72
+    room = -1;
68 73
 
69 74
     setSize(Window::getSize());
70 75
 }
@@ -189,6 +194,19 @@ bool Camera::update() {
189 194
     return updateViewFrustum;
190 195
 }
191 196
 
197
+void Camera::displayUI() {
198
+    if (!showOverlay)
199
+        return;
200
+
201
+    if (ImGui::Begin("Camera Look-At Overlay", &showOverlay, ImVec2(0, 0), -1.0f,
202
+                     ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
203
+                     | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
204
+                     | ImGuiWindowFlags_AlwaysAutoResize)) {
205
+        // TODO
206
+    }
207
+    ImGui::End();
208
+}
209
+
192 210
 // ----------------------------------------------------------------------------
193 211
 
194 212
 class FrustumPlane {

+ 3
- 3
src/Mesh.cpp 查看文件

@@ -125,7 +125,7 @@ void Mesh::prepare() {
125 125
     colorsBuff.clear();
126 126
 }
127 127
 
128
-void Mesh::display(glm::mat4 MVP) {
128
+void Mesh::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
129 129
     if (indicesBuff.size() > 0) {
130 130
         unsigned int indexStart = 0;
131 131
         unsigned int indexPos = 1;
@@ -140,7 +140,7 @@ void Mesh::display(glm::mat4 MVP) {
140 140
             std::vector<unsigned short> ind(indicesBuff.begin() + indexStart,
141 141
                                             indicesBuff.begin() + indexPos);
142 142
             indices.bufferData(ind);
143
-            Shader::drawGL(vertices, uvs, indices, texture, MVP);
143
+            Shader::drawGL(vertices, uvs, indices, texture, MVP, TextureStorage::GAME, shaderTexture);
144 144
 
145 145
             if (indexPos < indicesBuff.size()) {
146 146
                 indexStart = indexPos;
@@ -151,6 +151,6 @@ void Mesh::display(glm::mat4 MVP) {
151 151
     }
152 152
 
153 153
     if (indicesColor.getSize() > 0)
154
-        Shader::drawGL(verticesColor, colors, indicesColor, MVP);
154
+        Shader::drawGL(verticesColor, colors, indicesColor, MVP, GL_TRIANGLES, shaderTexture);
155 155
 }
156 156
 

+ 26
- 9
src/Render.cpp 查看文件

@@ -45,8 +45,13 @@ void Render::display() {
45 45
     }
46 46
 
47 47
     if (Camera::update()) {
48
-        clearRoomList();
49
-        buildRoomList(-2); // TODO cache room
48
+        int r = Camera::getRoom();
49
+        if (r < 0) {
50
+            clearRoomList();
51
+            buildRoomList();
52
+        } else {
53
+            buildRoomList(r);
54
+        }
50 55
     }
51 56
 
52 57
     glm::mat4 projection = Camera::getProjectionMatrix();
@@ -190,7 +195,7 @@ void Render::displayUI() {
190 195
             item = 2;
191 196
         else if (mode == RenderMode::Solid)
192 197
             item = 3;
193
-        if (ImGui::Combo("Render Mode", &item, modeStrings, modeStringCount)) {
198
+        if (ImGui::Combo("Render Mode##render", &item, modeStrings, modeStringCount)) {
194 199
             if (item == 0)
195 200
                 mode = RenderMode::LoadScreen;
196 201
             else if (item == 1)
@@ -209,6 +214,14 @@ void Render::displayUI() {
209 214
         }
210 215
         ImGui::SameLine();
211 216
         ImGui::Checkbox("Show Frustum##render", &displayViewFrustum);
217
+        ImGui::SameLine();
218
+        bool showOverlay = Camera::getShowOverlay();
219
+        if (ImGui::Checkbox("Overlay", &showOverlay)) {
220
+            Camera::setShowOverlay(showOverlay);
221
+        }
222
+        if (ImGui::Button("Reset Room ID")) {
223
+            Camera::setRoom(-1);
224
+        }
212 225
 
213 226
         ImGui::Separator();
214 227
         ImGui::Text("Bounding Boxes:");
@@ -229,33 +242,37 @@ void Render::displayUI() {
229 242
 
230 243
         ImGui::Separator();
231 244
         ImGui::Text("Renderable Objects:");
245
+        ImGui::Text("Room: ");
246
+        ImGui::SameLine();
232 247
         bool showRoomGeometry = Room::getShowRoomGeometry();
233
-        if (ImGui::Checkbox("Room Geometry##render", &showRoomGeometry)) {
248
+        if (ImGui::Checkbox("Geometry##renderroom", &showRoomGeometry)) {
234 249
             Room::setShowRoomGeometry(showRoomGeometry);
235 250
         }
236 251
         ImGui::SameLine();
237 252
         bool showRoomModels = Room::getShowRoomModels();
238
-        if (ImGui::Checkbox("Room Models##render", &showRoomModels)) {
253
+        if (ImGui::Checkbox("Models##renderroom", &showRoomModels)) {
239 254
             Room::setShowRoomModels(showRoomModels);
240 255
         }
241 256
         ImGui::SameLine();
242 257
         bool showRoomSprites = Room::getShowRoomSprites();
243
-        if (ImGui::Checkbox("Room Sprites##render", &showRoomSprites)) {
258
+        if (ImGui::Checkbox("Sprites##renderroom", &showRoomSprites)) {
244 259
             Room::setShowRoomSprites(showRoomSprites);
245 260
         }
246 261
 
262
+        ImGui::Text("Entity: ");
263
+        ImGui::SameLine();
247 264
         bool showEntitySprites = Entity::getShowEntitySprites();
248
-        if (ImGui::Checkbox("Entity Sprites##render", &showEntitySprites)) {
265
+        if (ImGui::Checkbox("Sprites##renderentity", &showEntitySprites)) {
249 266
             Entity::setShowEntitySprites(showEntitySprites);
250 267
         }
251 268
         ImGui::SameLine();
252 269
         bool showEntityMeshes = Entity::getShowEntityMeshes();
253
-        if (ImGui::Checkbox("Entity Meshes##render", &showEntityMeshes)) {
270
+        if (ImGui::Checkbox("Meshes##renderentity", &showEntityMeshes)) {
254 271
             Entity::setShowEntityMeshes(showEntityMeshes);
255 272
         }
256 273
         ImGui::SameLine();
257 274
         bool showEntityModels = Entity::getShowEntityModels();
258
-        if (ImGui::Checkbox("Entity Models##render", &showEntityModels)) {
275
+        if (ImGui::Checkbox("Models##renderentity", &showEntityModels)) {
259 276
             Entity::setShowEntityModels(showEntityModels);
260 277
         }
261 278
 

+ 1
- 1
src/RunTime.cpp 查看文件

@@ -180,7 +180,7 @@ void RunTime::display() {
180 180
         ImGui::Text("Frames per Second: %luFPS", fps);
181 181
         int size = history.size() - 1;
182 182
         if (size > 0) {
183
-            static bool scroll = true;
183
+            static bool scroll = false;
184 184
             if (scroll) {
185 185
                 int start = 0;
186 186
                 if (size > 10)

+ 4
- 4
src/SkeletalModel.cpp 查看文件

@@ -13,8 +13,8 @@
13 13
 
14 14
 #include <glm/gtc/matrix_transform.hpp>
15 15
 
16
-void BoneTag::display(glm::mat4 MVP) {
17
-    getWorld().getMesh(mesh).display(MVP);
16
+void BoneTag::display(glm::mat4 MVP, ShaderTexture* shaderTexture) {
17
+    getWorld().getMesh(mesh).display(MVP, shaderTexture);
18 18
 }
19 19
 
20 20
 // ----------------------------------------------------------------------------
@@ -100,7 +100,7 @@ SkeletalModel::~SkeletalModel() {
100 100
 #include <bitset>
101 101
 #endif
102 102
 
103
-void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe) {
103
+void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe, ShaderTexture* shaderTexture) {
104 104
     assertLessThan(aframe, size());
105 105
     assertLessThan(bframe, get(aframe).size());
106 106
 
@@ -157,7 +157,7 @@ void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe) {
157 157
         glm::mat4 rotate = rotZ * rotX * rotY;
158 158
 
159 159
         glm::mat4 mod = translate * rotate;
160
-        tag.display(stack.get() * mod);
160
+        tag.display(stack.get() * mod, shaderTexture);
161 161
 #ifdef DEBUG_MODELS
162 162
         Log::get(LOG_DEBUG) << "  --> get (" << cnt << ")" << Log::endl;
163 163
 #endif

+ 33
- 33
src/SoundManager.cpp 查看文件

@@ -31,7 +31,7 @@ int SoundManager::prepareSources() {
31 31
         int index = getIndex(soundSources.at(i).id, &vol);
32 32
         int ret = Sound::addSource(index, vol, false, true);
33 33
         assertEqual(ret, i);
34
-        float pos[3] = { soundSources.at(i).x, soundSources.at(i).y, soundSources.at(i).z };
34
+        glm::vec3 pos = soundSources.at(i).pos;
35 35
         ret = Sound::sourceAt(i, pos);
36 36
         assertEqual(ret, 0);
37 37
         Sound::play(i, false);
@@ -49,8 +49,8 @@ int SoundManager::prepareSources() {
49 49
     return 0;
50 50
 }
51 51
 
52
-void SoundManager::addSoundSource(float x, float y, float z, int id, int flags) {
53
-    soundSources.emplace_back(x, y, z, id, flags);
52
+void SoundManager::addSoundSource(glm::vec3 p, int id, int flags) {
53
+    soundSources.emplace_back(p, id, flags);
54 54
 }
55 55
 
56 56
 void SoundManager::addSoundMapEntry(int id) {
@@ -65,10 +65,6 @@ void SoundManager::addSampleIndex(int index) {
65 65
     sampleIndices.push_back(index);
66 66
 }
67 67
 
68
-int SoundManager::sizeSoundMap() {
69
-    return soundMap.size();
70
-}
71
-
72 68
 int SoundManager::getIndex(int index, float* volume) {
73 69
     if (index <= -1)
74 70
         return -1;
@@ -121,40 +117,44 @@ int SoundManager::playSound(int index) {
121 117
 }
122 118
 
123 119
 void SoundManager::display() {
124
-    if (ImGui::CollapsingHeader("Sound Map Player")) {
120
+    if (ImGui::CollapsingHeader("Sound Player")) {
125 121
         if (!Sound::getEnabled()) {
126 122
             ImGui::Text("Please enable Sound first!");
127 123
             if (ImGui::Button("Enable Sound!")) {
128 124
                 Sound::setEnabled(true);
129 125
             }
126
+            return;
130 127
         } else if (Sound::numBuffers() == 0) {
131
-            ImGui::Text("Please load a level!");
132
-        } else {
133
-            static int index = 0;
134
-            ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
135
-            ImGui::SliderInt("##soundslide", &index, 0, sizeSoundMap() - 1);
136
-            ImGui::PopItemWidth();
137
-            ImGui::SameLine();
138
-            if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
139
-                if (index < (sizeSoundMap() - 1))
140
-                    index++;
141
-                else
142
-                    index = 0;
143
-            }
144
-            ImGui::SameLine();
145
-            if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
146
-                if (index > 0)
147
-                    index--;
148
-                else
149
-                    index = sizeSoundMap() - 1;
150
-            }
151
-            ImGui::SameLine();
152
-            if (ImGui::Button("Play##soundplay")) {
153
-                playSound(index);
154
-            }
128
+            ImGui::Text("No Sounds currently loaded!");
129
+            return;
130
+        }
155 131
 
156
-            ImGui::Text("Index: %d", getIndex(index));
132
+        static int index = 0;
133
+        ImGui::Text("Map");
134
+        ImGui::SameLine();
135
+        ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
136
+        ImGui::SliderInt("##soundslide", &index, 0, soundMap.size() - 1);
137
+        ImGui::PopItemWidth();
138
+        ImGui::SameLine();
139
+        if (ImGui::Button("+##soundplus", ImVec2(0, 0), true)) {
140
+            if (index < (soundMap.size() - 1))
141
+                index++;
142
+            else
143
+                index = 0;
144
+        }
145
+        ImGui::SameLine();
146
+        if (ImGui::Button("-##soundminus", ImVec2(0, 0), true)) {
147
+            if (index > 0)
148
+                index--;
149
+            else
150
+                index = soundMap.size() - 1;
151
+        }
152
+        ImGui::SameLine();
153
+        if (ImGui::Button("Play##soundplay")) {
154
+            playSound(index);
157 155
         }
156
+        ImGui::SameLine();
157
+        ImGui::Text("%d", getIndex(index));
158 158
     }
159 159
 }
160 160
 

+ 7
- 4
src/TextureManager.cpp 查看文件

@@ -150,9 +150,6 @@ int TextureManager::loadBufferSlot(unsigned char* image,
150 150
         getIds(s).push_back(id);
151 151
     }
152 152
 
153
-    if (image == nullptr)
154
-        return slot;
155
-
156 153
     unsigned int glcMode;
157 154
     switch (mode) {
158 155
         case ColorMode::BGR:
@@ -164,7 +161,8 @@ int TextureManager::loadBufferSlot(unsigned char* image,
164 161
             break;
165 162
 
166 163
         case ColorMode::ARGB:
167
-            argb2rgba32(image, width, height);
164
+            if (image != nullptr)
165
+                argb2rgba32(image, width, height);
168 166
             glcMode = GL_RGBA;
169 167
             break;
170 168
 
@@ -224,6 +222,11 @@ int TextureManager::bindTexture(unsigned int n, TextureStorage s) {
224 222
     }
225 223
 }
226 224
 
225
+unsigned int TextureManager::getTextureID(int n, TextureStorage s) {
226
+    assertLessThan(n, getIds(s).size());
227
+    return getIds(s).at(n);
228
+}
229
+
227 230
 void TextureManager::addTile(TextureTile* t) {
228 231
     tiles.push_back(t);
229 232
 }

+ 79
- 2
src/UI.cpp 查看文件

@@ -11,15 +11,19 @@
11 11
 #include "Camera.h"
12 12
 #include "Console.h"
13 13
 #include "Game.h"
14
+#include "Log.h"
14 15
 #include "Menu.h"
15 16
 #include "Render.h"
16 17
 #include "RunTime.h"
17 18
 #include "SoundManager.h"
18 19
 #include "TextureManager.h"
20
+#include "World.h"
19 21
 #include "system/Window.h"
20 22
 #include "utils/time.h"
21 23
 #include "UI.h"
22 24
 
25
+#include <glm/gtc/matrix_transform.hpp>
26
+
23 27
 Shader UI::imguiShader;
24 28
 bool UI::visible = false;
25 29
 unsigned int UI::fontTex;
@@ -242,14 +246,14 @@ void UI::eventsFinished() {
242 246
 
243 247
 void UI::display() {
244 248
     if (RunTime::getShowFPS()) {
245
-        if (ImGui::Begin("Debug Overlay", nullptr, ImVec2(0, 0), 0.3f,
249
+        if (ImGui::Begin("Debug Overlay", nullptr, ImVec2(0, 0), -1.0f,
246 250
                          ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize
247 251
                          | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings
248 252
                          | ImGuiWindowFlags_AlwaysAutoResize)) {
249 253
             ImGui::Text("%d FPS", RunTime::getFPS());
250 254
             ImGui::Text("X: %.1f (%.2f)", Camera::getPosition().x, Camera::getRotation().x);
251 255
             ImGui::Text("Y: %.2f (%.2f)", Camera::getPosition().y, Camera::getRotation().y);
252
-            ImGui::Text("Z: %.2f", Camera::getPosition().z);
256
+            ImGui::Text("Z: %.2f (%d)", Camera::getPosition().z, Camera::getRoom());
253 257
 
254 258
             auto window = ImGui::GetWindowSize();
255 259
             auto screen = Window::getSize();
@@ -258,6 +262,9 @@ void UI::display() {
258 262
         ImGui::End();
259 263
     }
260 264
 
265
+    if (Game::isLoaded())
266
+        Camera::displayUI();
267
+
261 268
     Console::display();
262 269
 
263 270
     if (!visible) {
@@ -284,6 +291,76 @@ void UI::display() {
284 291
                 showStyleWindow = !showStyleWindow;
285 292
             }
286 293
         }
294
+
295
+        if (ImGui::CollapsingHeader("ShaderTexture Test")) {
296
+            static ShaderTexture* st = nullptr;
297
+            static int index = 0;
298
+            ImGui::SliderInt("SkeletalModel", &index, 0, getWorld().sizeSkeletalModel() - 1);
299
+
300
+            static glm::mat4 MVP(1.0f);
301
+            static bool dirty = true, redraw = false;
302
+            static float zoom = 1.0f;
303
+            static float pos[3];
304
+            static float rot[3];
305
+
306
+            if (ImGui::SliderFloat3("Position", pos, -100.0f, 100.0f)) {
307
+                dirty = true;
308
+            }
309
+            if (ImGui::SliderFloat3("Rotation", rot, -6.0f, 6.0f)) {
310
+                dirty = true;
311
+            }
312
+            if (ImGui::SliderFloat("Zoom", &zoom, -1.0f, 2.0f)) {
313
+                dirty = true;
314
+            }
315
+
316
+            if (dirty) {
317
+                static glm::mat4 projection = glm::perspective(45.0f, 1.0f, 0.1f, 2000.0f);
318
+
319
+                glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(zoom, zoom, zoom));
320
+                glm::mat4 rotateX = glm::rotate(glm::mat4(1.0f), rot[0], glm::vec3(1.0f, 0.0f, 0.0f));
321
+                glm::mat4 rotateY = glm::rotate(glm::mat4(1.0f), rot[1], glm::vec3(0.0f, 1.0f, 0.0f));
322
+                glm::mat4 rotateZ = glm::rotate(glm::mat4(1.0f), rot[2], glm::vec3(0.0f, 0.0f, 1.0f));
323
+                glm::mat4 rotate = rotateZ * rotateY * rotateX;
324
+                glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos[0], pos[1], pos[2]));
325
+                glm::mat4 view = glm::inverse(scale * rotate * translate);
326
+                MVP = projection * view;
327
+
328
+                redraw = true;
329
+                dirty = false;
330
+            }
331
+
332
+            if ((index >= 0) && (index < getWorld().sizeSkeletalModel())) {
333
+                auto& sm = getWorld().getSkeletalModel(index);
334
+                if ((sm.size() > 0) && (sm.get(0).size() > 0)) {
335
+                    if (ImGui::Button("(Re)Create ShaderTexture...")) {
336
+                        if (st != nullptr) {
337
+                            delete st;
338
+                            st = nullptr;
339
+                        }
340
+                        if (st == nullptr) {
341
+                            st = new ShaderTexture();
342
+                            redraw = true;
343
+                        }
344
+                    }
345
+                    if ((st != nullptr) && redraw) {
346
+                        st->clear();
347
+                        sm.display(MVP, 0, 0, st);
348
+                        redraw = false;
349
+                        Log::get(LOG_DEBUG) << "Rendered new ShaderTexture!" << Log::endl;
350
+                    }
351
+                } else {
352
+                    ImGui::Text("Selected SkeletalModel has no animation/frame!");
353
+                }
354
+            } else {
355
+                ImGui::Text("No SkeletalModels loaded!");
356
+            }
357
+
358
+            if (st != nullptr) {
359
+                auto bm = TextureManager::getBufferManager(st->getTexture(), TextureStorage::SYSTEM);
360
+                ImGui::Image(bm, ImVec2(ImGui::GetColumnWidth() * 2 / 3,
361
+                                        ImGui::GetColumnWidth() * 2 / 3));
362
+            }
363
+        }
287 364
     }
288 365
     ImGui::End();
289 366
 

+ 1
- 1
src/loader/LoaderTR2.cpp 查看文件

@@ -1033,7 +1033,7 @@ void LoaderTR2::loadSoundSources() {
1033 1033
         // Unknown, 0x40, 0x80 or 0xC0
1034 1034
         uint16_t flags = file.readU16();
1035 1035
 
1036
-        SoundManager::addSoundSource(x, y, z, soundID, flags);
1036
+        SoundManager::addSoundSource(glm::vec3(x, y, z), soundID, flags);
1037 1037
     }
1038 1038
 
1039 1039
     if (numSoundSources > 0)

+ 94
- 12
src/system/Shader.cpp 查看文件

@@ -53,6 +53,52 @@ void ShaderBuffer::unbind(int location) {
53 53
 
54 54
 // ----------------------------------------------------------------------------
55 55
 
56
+static int loadBufferSlot(unsigned char* image = nullptr,
57
+                          unsigned int width = 256, unsigned int height = 256,
58
+                          ColorMode mode = ColorMode::RGBA, unsigned int bpp = 32,
59
+                          TextureStorage s = TextureStorage::GAME,
60
+                          int slot = -1, bool filter = true);
61
+
62
+ShaderTexture::ShaderTexture(int w, int h) : width(w), height(h) {
63
+    glGenFramebuffers(1, &framebuffer);
64
+    bind();
65
+
66
+    texture = TextureManager::loadBufferSlot(nullptr, width, height, ColorMode::RGBA,
67
+              32, TextureStorage::SYSTEM, -1, false);
68
+
69
+    glGenRenderbuffers(1, &depth);
70
+    glBindRenderbuffer(GL_RENDERBUFFER, depth);
71
+    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
72
+    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
73
+
74
+    glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
75
+                         TextureManager::getTextureID(texture, TextureStorage::SYSTEM), 0);
76
+
77
+    GLenum drawBuffer = GL_COLOR_ATTACHMENT0;
78
+    glDrawBuffers(1, &drawBuffer);
79
+
80
+    assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
81
+}
82
+
83
+ShaderTexture::~ShaderTexture() {
84
+    glDeleteRenderbuffers(1, &depth);
85
+    glDeleteFramebuffers(1,  &framebuffer);
86
+
87
+    //! \TODO free texture slot
88
+}
89
+
90
+void ShaderTexture::clear() {
91
+    bind();
92
+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
93
+}
94
+
95
+void ShaderTexture::bind() {
96
+    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
97
+    glViewport(0, 0, width, height);
98
+}
99
+
100
+// ----------------------------------------------------------------------------
101
+
56 102
 Shader::~Shader() {
57 103
     if (programID >= 0)
58 104
         glDeleteProgram(programID);
@@ -238,12 +284,19 @@ void Shader::set2DState(bool on, bool depth) {
238 284
 
239 285
 void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color,
240 286
                     unsigned int texture, TextureStorage store, unsigned int mode,
241
-                    Shader& shader) {
287
+                    ShaderTexture* target, Shader& shader) {
242 288
     assert(vertices.getSize() == uvs.getSize());
243 289
     if (mode == GL_TRIANGLES) {
244 290
         assert((vertices.getSize() % 3) == 0);
245 291
     }
246 292
 
293
+    if (target == nullptr) {
294
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
295
+        glViewport(0, 0, Window::getSize().x, Window::getSize().y);
296
+    } else {
297
+        target->bind();
298
+    }
299
+
247 300
     shader.use();
248 301
     shader.loadUniform(0, Window::getSize());
249 302
     shader.loadUniform(1, texture, store);
@@ -260,10 +313,18 @@ void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color,
260 313
 }
261 314
 
262 315
 void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int texture,
263
-                    glm::mat4 MVP, TextureStorage store, Shader& shader) {
316
+                    glm::mat4 MVP, TextureStorage store, ShaderTexture* target,
317
+                    Shader& shader) {
264 318
     assert(vertices.getSize() == uvs.getSize());
265 319
     assert((vertices.getSize() % 3) == 0);
266 320
 
321
+    if (target == nullptr) {
322
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
323
+        glViewport(0, 0, Window::getSize().x, Window::getSize().y);
324
+    } else {
325
+        target->bind();
326
+    }
327
+
267 328
     shader.use();
268 329
     shader.loadUniform(0, MVP);
269 330
     shader.loadUniform(1, texture, store);
@@ -276,10 +337,17 @@ void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, unsigned int text
276 337
 
277 338
 void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& indices,
278 339
                     unsigned int texture, glm::mat4 MVP, TextureStorage store,
279
-                    Shader& shader) {
340
+                    ShaderTexture* target, Shader& shader) {
280 341
     assert(vertices.getSize() == uvs.getSize());
281 342
     assert((indices.getSize() % 3) == 0);
282 343
 
344
+    if (target == nullptr) {
345
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
346
+        glViewport(0, 0, Window::getSize().x, Window::getSize().y);
347
+    } else {
348
+        target->bind();
349
+    }
350
+
283 351
     shader.use();
284 352
     shader.loadUniform(0, MVP);
285 353
     shader.loadUniform(1, texture, store);
@@ -292,12 +360,19 @@ void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, ShaderBuffer& ind
292 360
 }
293 361
 
294 362
 void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
295
-                    unsigned int mode, Shader& shader) {
363
+                    unsigned int mode, ShaderTexture* target, Shader& shader) {
296 364
     assert(vertices.getSize() == colors.getSize());
297 365
     if (mode == GL_TRIANGLES) {
298 366
         assert((vertices.getSize() % 3) == 0);
299 367
     }
300 368
 
369
+    if (target == nullptr) {
370
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
371
+        glViewport(0, 0, Window::getSize().x, Window::getSize().y);
372
+    } else {
373
+        target->bind();
374
+    }
375
+
301 376
     shader.use();
302 377
     shader.loadUniform(0, MVP);
303 378
     vertices.bindBuffer(0, 3);
@@ -308,12 +383,19 @@ void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, glm::mat4 MVP,
308 383
 }
309 384
 
310 385
 void Shader::drawGL(ShaderBuffer& vertices, ShaderBuffer& colors, ShaderBuffer& indices,
311
-                    glm::mat4 MVP, unsigned int mode, Shader& shader) {
386
+                    glm::mat4 MVP, unsigned int mode, ShaderTexture* target, Shader& shader) {
312 387
     assert(vertices.getSize() == colors.getSize());
313 388
     if (mode == GL_TRIANGLES) {
314 389
         assert((indices.getSize() % 3) == 0);
315 390
     }
316 391
 
392
+    if (target == nullptr) {
393
+        glBindFramebuffer(GL_FRAMEBUFFER, 0);
394
+        glViewport(0, 0, Window::getSize().x, Window::getSize().y);
395
+    } else {
396
+        target->bind();
397
+    }
398
+
317 399
     shader.use();
318 400
     shader.loadUniform(0, MVP);
319 401
     vertices.bindBuffer(0, 3);
@@ -351,7 +433,7 @@ const char* Shader::textShaderFragment = R"!?!(
351 433
 
352 434
 in vec2 UV;
353 435
 
354
-out vec4 color;
436
+layout(location = 0) out vec4 color;
355 437
 
356 438
 uniform sampler2D textureSampler;
357 439
 uniform vec4 colorVar;
@@ -388,7 +470,7 @@ const char* Shader::textureShaderFragment = R"!?!(
388 470
 
389 471
 in vec2 UV;
390 472
 
391
-out vec4 color;
473
+layout(location = 0) out vec4 color;
392 474
 
393 475
 uniform sampler2D textureSampler;
394 476
 
@@ -405,7 +487,7 @@ const char* Shader::colorShaderVertex = R"!?!(
405 487
 layout(location = 0) in vec3 vertexPosition_modelspace;
406 488
 layout(location = 1) in vec3 vertexColor;
407 489
 
408
-out vec3 Color;
490
+out vec3 color;
409 491
 
410 492
 uniform mat4 MVP;
411 493
 
@@ -415,19 +497,19 @@ void main() {
415 497
                           vertexPosition_modelspace.z,
416 498
                           1);
417 499
     gl_Position = vec4(-pos.x, pos.yzw);
418
-    Color = vertexColor;
500
+    color = vertexColor;
419 501
 }
420 502
 )!?!";
421 503
 
422 504
 const char* Shader::colorShaderFragment = R"!?!(
423 505
 #version 330 core
424 506
 
425
-in vec3 Color;
507
+in vec3 color;
426 508
 
427
-out vec4 color;
509
+layout(location = 0) out vec4 color_out;
428 510
 
429 511
 void main() {
430
-    color = vec4(Color, 1);
512
+    color_out = vec4(color, 1);
431 513
 }
432 514
 )!?!";
433 515
 

+ 3
- 3
src/system/Sound.cpp 查看文件

@@ -64,7 +64,7 @@ int Sound::addSource(int buffer, float volume, bool atListener, bool loop) {
64 64
 #endif
65 65
 }
66 66
 
67
-int Sound::sourceAt(int source, float pos[3]) {
67
+int Sound::sourceAt(int source, glm::vec3 pos) {
68 68
 #ifdef USING_AL
69 69
     return SoundAL::sourceAt(source, pos);
70 70
 #else
@@ -72,9 +72,9 @@ int Sound::sourceAt(int source, float pos[3]) {
72 72
 #endif
73 73
 }
74 74
 
75
-void Sound::listenAt(float pos[3], float orientation[6]) {
75
+void Sound::listenAt(glm::vec3 pos, glm::vec3 at, glm::vec3 up) {
76 76
 #ifdef USING_AL
77
-    SoundAL::listenAt(pos, orientation);
77
+    SoundAL::listenAt(pos, at, up);
78 78
 #endif
79 79
 }
80 80
 

+ 21
- 16
src/system/SoundAL.cpp 查看文件

@@ -24,7 +24,7 @@ float SoundAL::volume = 1.0f;
24 24
 std::vector<unsigned int> SoundAL::buffers;
25 25
 std::vector<unsigned int> SoundAL::sources;
26 26
 std::vector<unsigned int> SoundAL::listenerSources;
27
-float SoundAL::lastPosition[3] = { 0.0f, 0.0f, 0.0f };
27
+glm::vec3 SoundAL::lastPosition(0.0f, 0.0f, 0.0f);
28 28
 
29 29
 int SoundAL::initialize() {
30 30
     if (init)
@@ -41,6 +41,12 @@ int SoundAL::initialize() {
41 41
 
42 42
     init = true;
43 43
     setVolume(volume);
44
+
45
+    lastPosition = glm::vec3(0.0f, 0.0f, 0.0f);
46
+    glm::vec3 at(0.0f, 0.0f, -1.0f);
47
+    glm::vec3 up(0.0f, 1.0f, 0.0f);
48
+    listenAt(lastPosition, at, up);
49
+
44 50
     return 0;
45 51
 }
46 52
 
@@ -81,11 +87,10 @@ void SoundAL::clear() {
81 87
         Log::get(LOG_ERROR) << "SoundAL: Error while deleting buffers!" << Log::endl;
82 88
     }
83 89
 
84
-    for (int i = 0; i < 3; i++)
85
-        lastPosition[i] = 0.0f;
86
-
87
-    float orientation[6] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
88
-    listenAt(lastPosition, orientation);
90
+    lastPosition = glm::vec3(0.0f, 0.0f, 0.0f);
91
+    glm::vec3 at(0.0f, 0.0f, -1.0f);
92
+    glm::vec3 up(0.0f, 1.0f, 0.0f);
93
+    listenAt(lastPosition, at, up);
89 94
 }
90 95
 
91 96
 int SoundAL::numBuffers() {
@@ -133,7 +138,7 @@ int SoundAL::addSource(int buffer, float volume, bool atListener, bool loop) {
133 138
         alSourcei(id, AL_LOOPING, AL_TRUE);
134 139
 
135 140
     if (atListener) {
136
-        alSourcefv(id, AL_POSITION, lastPosition);
141
+        alSourcefv(id, AL_POSITION, &lastPosition[0]);
137 142
         listenerSources.push_back(id);
138 143
         return listenerSources.size() - 1;
139 144
     } else {
@@ -142,33 +147,33 @@ int SoundAL::addSource(int buffer, float volume, bool atListener, bool loop) {
142 147
     }
143 148
 }
144 149
 
145
-int SoundAL::sourceAt(int source, float pos[3]) {
150
+int SoundAL::sourceAt(int source, glm::vec3 pos) {
146 151
     if (!init)
147 152
         return -1;
148 153
 
149
-    if ((source < 0) || (source >= sources.size()) || (pos == nullptr)) {
154
+    if ((source < 0) || (source >= sources.size())) {
150 155
         Log::get(LOG_ERROR) << "SoundAL: Can't position non-existing source!" << Log::endl;
151 156
         return -2;
152 157
     }
153 158
 
154
-    alSourcefv(sources.at(source), AL_POSITION, pos);
159
+    alSourcefv(sources.at(source), AL_POSITION, &pos[0]);
155 160
 
156 161
     return 0;
157 162
 }
158 163
 
159
-void SoundAL::listenAt(float pos[3], float orientation[6]) {
160
-    if ((!init) || (pos == nullptr) || (orientation == nullptr))
164
+void SoundAL::listenAt(glm::vec3 pos, glm::vec3 at, glm::vec3 up) {
165
+    if (!init)
161 166
         return;
162 167
 
163
-    alListenerfv(AL_POSITION, pos);
168
+    float orientation[6] = { at.x, at.y, at.z, up.x, up.y, up.z };
169
+    alListenerfv(AL_POSITION, &pos[0]);
164 170
     alListenerfv(AL_ORIENTATION, orientation);
165 171
 
166 172
     for (auto& s : listenerSources) {
167
-        alSourcefv(s, AL_POSITION, pos);
173
+        alSourcefv(s, AL_POSITION, &pos[0]);
168 174
     }
169 175
 
170
-    for (int i = 0; i < 3; i++)
171
-        lastPosition[i] = pos[i];
176
+    lastPosition = pos;
172 177
 }
173 178
 
174 179
 void SoundAL::play(int source, bool atListener) {

正在加载...
取消
保存