Browse Source

Room Mesh displayed more or less correctly

Thomas Buck 10 years ago
parent
commit
014962658a
8 changed files with 74 additions and 31 deletions
  1. 1
    1
      ChangeLog.md
  2. 1
    0
      include/Camera.h
  3. 3
    2
      include/TextureManager.h
  4. 16
    14
      src/Camera.cpp
  5. 4
    0
      src/Game.cpp
  6. 20
    4
      src/Mesh.cpp
  7. 22
    8
      src/Render.cpp
  8. 7
    2
      src/system/Window.cpp

+ 1
- 1
ChangeLog.md View File

4
 
4
 
5
     [ 20141216 ]
5
     [ 20141216 ]
6
     * Allow navigation with a free-floating Camera
6
     * Allow navigation with a free-floating Camera
7
-    * Started drawing the Room Mesh structures
7
+    * Room Meshes are displayed more or less correctly
8
 
8
 
9
     [ 20141215 ]
9
     [ 20141215 ]
10
     * Rewrote GL code for the Font implementations, ImGUI and the Main Menu
10
     * Rewrote GL code for the Font implementations, ImGUI and the Main Menu

+ 1
- 0
include/Camera.h View File

22
 
22
 
23
     static float getRadianPitch() { return thetaX; }
23
     static float getRadianPitch() { return thetaX; }
24
     static float getRadianYaw() { return thetaY; }
24
     static float getRadianYaw() { return thetaY; }
25
+    static void setRadianPitch(float x) { thetaX = x; }
25
 
26
 
26
     static void setPosition(glm::vec3 p) { pos = p; }
27
     static void setPosition(glm::vec3 p) { pos = p; }
27
     static glm::vec3 getPosition() { return pos; }
28
     static glm::vec3 getPosition() { return pos; }

+ 3
- 2
include/TextureManager.h View File

29
 class TextureTile {
29
 class TextureTile {
30
   public:
30
   public:
31
     TextureTile(unsigned int a, unsigned int t) : attribute(a), texture(t) { }
31
     TextureTile(unsigned int a, unsigned int t) : attribute(a), texture(t) { }
32
+    void add(TextureTileVertex t) { vertices.push_back(t); }
32
 
33
 
33
     unsigned int getTexture() { return texture; }
34
     unsigned int getTexture() { return texture; }
34
-    glm::vec2 getUV(unsigned int i) { return glm::vec2(vertices.at(i).xPixel, vertices.at(i).yPixel); }
35
-    void add(TextureTileVertex t) { vertices.push_back(t); }
35
+    glm::vec2 getUV(unsigned int i) { return glm::vec2(vertices.at(i).xPixel / 255.0f,
36
+                                                       vertices.at(i).yPixel / 255.0f); }
36
 
37
 
37
   private:
38
   private:
38
     unsigned int attribute;
39
     unsigned int attribute;

+ 16
- 14
src/Camera.cpp View File

12
 #include "Camera.h"
12
 #include "Camera.h"
13
 
13
 
14
 glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
14
 glm::vec3 Camera::pos(0.0f, 0.0f, 0.0f);
15
-float Camera::thetaX = glm::pi<float>();
15
+float Camera::thetaX = 0.0f;
16
 float Camera::thetaY = 0.0f;
16
 float Camera::thetaY = 0.0f;
17
 float Camera::rotationDeltaX = 0.75f;
17
 float Camera::rotationDeltaX = 0.75f;
18
 float Camera::rotationDeltaY = 0.75f;
18
 float Camera::rotationDeltaY = 0.75f;
19
 
19
 
20
 void Camera::reset() {
20
 void Camera::reset() {
21
     pos = glm::vec3(0.0f, 0.0f, 0.0f);
21
     pos = glm::vec3(0.0f, 0.0f, 0.0f);
22
-    thetaX = glm::pi<float>();
22
+    thetaX = 0.0f;
23
     thetaY = 0.0f;
23
     thetaY = 0.0f;
24
 }
24
 }
25
 
25
 
46
     } else if (action == backwardAction) {
46
     } else if (action == backwardAction) {
47
         pos -= dir * step;
47
         pos -= dir * step;
48
     } else if (action == leftAction) {
48
     } else if (action == leftAction) {
49
-        pos -= right * step;
50
-    } else if (action == rightAction) {
51
         pos += right * step;
49
         pos += right * step;
50
+    } else if (action == rightAction) {
51
+        pos -= right * step;
52
     } else if (action == jumpAction) {
52
     } else if (action == jumpAction) {
53
         pos += up * step;
53
         pos += up * step;
54
     } else if (action == crouchAction) {
54
     } else if (action == crouchAction) {
61
 
61
 
62
 void Camera::handleMouseMotion(int x, int y) {
62
 void Camera::handleMouseMotion(int x, int y) {
63
     while (x > 0) {
63
     while (x > 0) {
64
-        if (thetaX < (glm::pi<float>() / 2.0f)) {
65
-            thetaX += rotationDeltaX;
66
-        }
64
+        thetaX += rotationDeltaX;
67
         x--;
65
         x--;
68
     }
66
     }
69
     while (x < 0) {
67
     while (x < 0) {
70
-        if (thetaX > -(glm::pi<float>() / 2.0f)) {
71
-            thetaX -= rotationDeltaX;
72
-        }
68
+        thetaX -= rotationDeltaX;
73
         x++;
69
         x++;
74
     }
70
     }
75
     while (y > 0) {
71
     while (y > 0) {
76
-        if (thetaY < (glm::pi<float>() / 2.0f)) {
77
-            thetaY += rotationDeltaY;
72
+        if (thetaY > -(glm::pi<float>() / 2.0f)) {
73
+            thetaY -= rotationDeltaY;
78
         }
74
         }
79
         y--;
75
         y--;
80
     }
76
     }
81
     while (y < 0) {
77
     while (y < 0) {
82
-        if (thetaY > -(glm::pi<float>() / 2.0f)) {
83
-            thetaY -= rotationDeltaY;
78
+        if (thetaY < (glm::pi<float>() / 2.0f)) {
79
+            thetaY += rotationDeltaY;
84
         }
80
         }
85
         y++;
81
         y++;
86
     }
82
     }
83
+
84
+    while (thetaX > (glm::pi<float>() * 2.0f))
85
+        thetaX -= glm::pi<float>() * 2.0f;
86
+
87
+    while (thetaX < -(glm::pi<float>() * 2.0f))
88
+        thetaX += glm::pi<float>() * 2.0f;
87
 }
89
 }
88
 
90
 
89
 glm::mat4 Camera::getViewMatrix() {
91
 glm::mat4 Camera::getViewMatrix() {

+ 4
- 0
src/Game.cpp View File

105
         } else {
105
         } else {
106
             mLoaded = true;
106
             mLoaded = true;
107
             Render::setMode(RenderMode::Texture);
107
             Render::setMode(RenderMode::Texture);
108
+
109
+            Camera::setPosition(glm::vec3(getLara().getPos(0),
110
+                                          getLara().getPos(1) + 1024.0f,
111
+                                          getLara().getPos(2)));
108
         }
112
         }
109
     } else {
113
     } else {
110
         getLog() << "No suitable loader for this level!" << Log::endl;
114
         getLog() << "No suitable loader for this level!" << Log::endl;

+ 20
- 4
src/Mesh.cpp View File

81
         }
81
         }
82
 
82
 
83
         if (indices.at(i) == 0) {
83
         if (indices.at(i) == 0) {
84
-            ind.push_back(ind.at(ind.size() - 1));
85
-            ind.push_back(ind.at(ind.size() - 3));
84
+            ind.push_back(ind.at(ind.size() - 2));
85
+            ind.push_back(ind.at(ind.size() - 5));
86
         }
86
         }
87
 
87
 
88
         vertIndex += (indices.at(i) == 0) ? 4 : 3;
88
         vertIndex += (indices.at(i) == 0) ? 4 : 3;
91
     indices = ind;
91
     indices = ind;
92
     vertices = vert;
92
     vertices = vert;
93
     textures = tex;
93
     textures = tex;
94
+
95
+    assert((indices.size() % 3) == 0);
94
 }
96
 }
95
 
97
 
96
 void Mesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {
98
 void Mesh::display(glm::mat4 model, glm::mat4 view, glm::mat4 projection) {
97
     glm::mat4 MVP = projection * view * model;
99
     glm::mat4 MVP = projection * view * model;
98
 
100
 
99
-    // TODO handle different textures!
101
+    unsigned int indexStart = 0;
102
+    unsigned int indexPos = 1;
103
+    unsigned int texture = textures.at(indices.at(0));
104
+
105
+    while ((indexStart != indexPos) && (indexPos < indices.size())) {
106
+        while ((indexPos < indices.size()) && (textures.at(indices.at(indexPos)) == texture))
107
+            indexPos++;
108
+
109
+        std::vector<unsigned short> ind(indices.begin() + indexStart, indices.begin() + indexPos);
110
+        Window::drawGL(vertices, uvs, ind, MVP, texture);
100
 
111
 
101
-    Window::drawGL(vertices, uvs, indices, MVP, textures.at(0));
112
+        if (indexPos < indices.size()) {
113
+            indexStart = indexPos;
114
+            indexPos += 1;
115
+            texture = textures.at(indices.at(indexStart));
116
+        }
117
+    }
102
 }
118
 }
103
 
119
 

+ 22
- 8
src/Render.cpp View File

36
             break;
36
             break;
37
         case RenderMode::Solid:
37
         case RenderMode::Solid:
38
         case RenderMode::Wireframe:
38
         case RenderMode::Wireframe:
39
-            glClearColor(PURPLE[0] / 256.0f, PURPLE[1] / 256.0f,
40
-                         PURPLE[2] / 256.0f, PURPLE[3] / 256.0f);
39
+            //glClearColor(PURPLE[0] / 256.0f, PURPLE[1] / 256.0f,
40
+            //             PURPLE[2] / 256.0f, PURPLE[3] / 256.0f);
41
             //glDisable(GL_TEXTURE_2D);
41
             //glDisable(GL_TEXTURE_2D);
42
             break;
42
             break;
43
         default:
43
         default:
44
-            glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f,
45
-                         BLACK[2] / 256.0f, BLACK[3] / 256.0f);
44
+            //glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f,
45
+            //             BLACK[2] / 256.0f, BLACK[3] / 256.0f);
46
             //glEnable(GL_TEXTURE_2D);
46
             //glEnable(GL_TEXTURE_2D);
47
+            break;
47
     }
48
     }
48
 }
49
 }
49
 
50
 
66
     }
67
     }
67
 
68
 
68
     glm::mat4 view = Camera::getViewMatrix();
69
     glm::mat4 view = Camera::getViewMatrix();
69
-    glm::mat4 projection = glm::perspective(45.0f, // Field of View
70
-                                            (float)getWindow().getWidth() / (float)getWindow().getHeight(),
71
-                                            0.1f, // Min Distance
72
-                                            100000.0f); // Max Distance
70
+
71
+    static unsigned int w = getWindow().getWidth();
72
+    static unsigned int h = getWindow().getHeight();
73
+    static glm::mat4 projection = glm::perspective(45.0f, // Field of View
74
+                                                   (float)getWindow().getWidth()
75
+                                                 / (float)getWindow().getHeight(),
76
+                                                   0.1f, // Min Distance
77
+                                                   100000.0f); // Max Distance
78
+
79
+    if ((w != getWindow().getWidth()) || (h != getWindow().getHeight())) {
80
+        w = getWindow().getWidth();
81
+        h = getWindow().getHeight();
82
+        glm::mat4 projection = glm::perspective(45.0f, // Field of View
83
+                                                (float)getWindow().getWidth() / (float)getWindow().getHeight(),
84
+                                                0.1f, // Min Distance
85
+                                                100000.0f); // Max Distance
86
+    }
73
 
87
 
74
     // Just draw all rooms, as a test
88
     // Just draw all rooms, as a test
75
     for (int i = 0; i < getWorld().sizeRoom(); i++)
89
     for (int i = 0; i < getWorld().sizeRoom(); i++)

+ 7
- 2
src/system/Window.cpp View File

48
 	glBindVertexArray(vertexArrayID);
48
 	glBindVertexArray(vertexArrayID);
49
 
49
 
50
     // Set background to black
50
     // Set background to black
51
-    glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f, BLACK[2] / 256.0f, BLACK[3] / 256.0f);
51
+    //glClearColor(BLACK[0] / 256.0f, BLACK[1] / 256.0f, BLACK[2] / 256.0f, BLACK[3] / 256.0f);
52
+    glClearColor(0.0f, 0.0f, 0.4f, 1.0f);
52
 
53
 
53
     // Set up Z buffer
54
     // Set up Z buffer
54
     glEnable(GL_DEPTH_TEST);
55
     glEnable(GL_DEPTH_TEST);
377
 uniform mat4 MVP;
378
 uniform mat4 MVP;
378
 
379
 
379
 void main() {
380
 void main() {
380
-    gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
381
+    vec4 pos = MVP * vec4(vertexPosition_modelspace.x,
382
+                          -vertexPosition_modelspace.y,
383
+                          vertexPosition_modelspace.z,
384
+                          1);
385
+    gl_Position = vec4(-pos.x, pos.yzw);
381
     UV = vertexUV;
386
     UV = vertexUV;
382
 }
387
 }
383
 )!?!";
388
 )!?!";

Loading…
Cancel
Save