]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Increased texture size for world, made world a little more detailed, and added bounci...
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Tue, 24 Sep 2019 06:29:41 +0000 (02:29 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Tue, 24 Sep 2019 06:29:41 +0000 (02:29 -0400)
Assets/ball.png [new file with mode: 0644]
Assets/ball_normal.png [new file with mode: 0644]
Assets/world.png
Assets/world_normal.png
Scripts/init.lua
Scripts/world.lua
Shaders/world.frag
Shaders/world.vert
src/render.cpp
src/world.cpp

diff --git a/Assets/ball.png b/Assets/ball.png
new file mode 100644 (file)
index 0000000..b8dca47
Binary files /dev/null and b/Assets/ball.png differ
diff --git a/Assets/ball_normal.png b/Assets/ball_normal.png
new file mode 100644 (file)
index 0000000..d238e30
Binary files /dev/null and b/Assets/ball_normal.png differ
index c972c8706bdd0948ddb047419c3c00c5f5be0862..f6c78d93df3233b5a2afe55f5d338e2baf62b433 100644 (file)
Binary files a/Assets/world.png and b/Assets/world.png differ
index 181889588e302d3a45e7a7604820e6a4c35839fd..c691e1e4cf73aa8baede4bafd33a7397196598b7 100644 (file)
Binary files a/Assets/world_normal.png and b/Assets/world_normal.png differ
index 8559f7c46c8f1188ddfa1905cb2495125b633f15..cdd6ccb20d5f76f6a9cd940f50b19eaf91ea0e78 100644 (file)
@@ -55,20 +55,37 @@ player = {
         --end
         --self.visibleTick = self.visibleTick + 1
     end,
-    PhysicsIdle = function(self)
-        if self.Velocity.x < 0 then
-            self.Render.flipx = true
-        elseif self.Velocity.x > 0 then
-            self.Render.flipx = false
+    visibleTick = 0
+}
+
+ball = {
+    Position = {
+        x = 20,
+        y = 100
+    },
+    Velocity = {
+        x = 0.0,
+        y = 0.0,
+    },
+    Physics = 0,
+    Render = {
+        texture = "Assets/ball.png",
+        normal = "Assets/ball_normal.png",
+        visible = true,
+    },
+    Idle = function(self)
+        if self.Physics.standing == true then
+            self.Velocity.y = self.Velocity.y + 15
+            self.Velocity.x = math.random(-1, 1);
         end
     end,
-    visibleTick = 0
 }
 
 -- Create the world
 dofile("Scripts/world.lua")
 
 playerSpawn = game.spawn(player);
+game.spawn(ball);
 
 -------------------
 --  SERIALIZING  --
index 044559ad19205de2729766523f9c31108373a3f1..3b56d9ae9ef86c1f8e1aa3c11d201188eeea8952 100644 (file)
@@ -13,51 +13,63 @@ world = {
             texture = {
                 file = "Assets/world.png",
                 offset = { x = 0, y = 0 },
-                size = { x = 8, y = 8 }
+                size = { x = 64, y = 64 }
             },
             normal = {
                 file = "Assets/world_normal.png",
                 offset = { x = 0, y = 0 },
-                size = { x = 8, y = 8 }
+                size = { x = 64, y = 64 }
             }
         });
         self:registerMaterial("dirt", {
             texture = {
                 file = "Assets/world.png",
-                offset = { x = 8, y = 0 },
-                size = { x = 8, y = 8 }
+                offset = { x = 64, y = 0 },
+                size = { x = 64, y = 64 }
             },
             normal = {
                 file = "Assets/world_normal.png",
-                offset = { x = 8, y = 0 },
-                size = { x = 8, y = 8 }
+                offset = { x = 64, y = 0 },
+                size = { x = 64, y = 64 }
             }
         });
         self:registerMaterial("stone", {
             texture = {
                 file = "Assets/world.png",
-                offset = { x = 16, y = 0 },
-                size = { x = 8, y = 8 }
+                offset = { x = 128, y = 0 },
+                size = { x = 64, y = 64 }
             },
             normal = {
                 file = "Assets/world_normal.png",
-                offset = { x = 16, y = 0 },
-                size = { x = 8, y = 8 }
+                offset = { x = 128, y = 0 },
+                size = { x = 64, y = 64 }
             }
         });
         self:registerMaterial("flower", {
             texture = {
                 file = "Assets/world.png",
-                offset = { x = 24, y = 0 },
-                size = { x = 8, y = 8 }
+                offset = { x = 192, y = 0 },
+                size = { x = 64, y = 64 }
             },
             normal = {
                 file = "Assets/world_normal.png",
-                offset = { x = 24, y = 0 },
-                size = { x = 8, y = 8 }
+                offset = { x = 192, y = 0 },
+                size = { x = 64, y = 64 }
             },
             passable = true
         });
+        self:registerMaterial("trunk", {
+            texture = {
+                file = "Assets/world.png",
+                offset = { x = 256, y = 0 },
+                size = { x = 64, y = 64 }
+            },
+            normal = {
+                file = "Assets/world_normal.png",
+                offset = { x = 256, y = 0 },
+                size = { x = 64, y = 64 }
+            }
+        });
     end,
 
     Generate = function(self)
@@ -83,6 +95,8 @@ world = {
                     elseif Y == YGen + 1 then
                         if math.random(0, 100) == 53 then
                             self:setData(X, Y, Z, "flower");
+                        elseif math.random(0, 100) == 45 then
+                            self:setData(X, Y, Z, "trunk");
                         end
                     end
                     --print(X..","..Y..","..Z);
index 18945a836b551007c5a68f32eb8f52471b076c56..79d87aac640da2a8a97b1b13f7c6806d7d0ca507 100644 (file)
@@ -9,6 +9,7 @@ precision mediump float;
 uniform sampler2D textu;
 uniform sampler2D normu;
 
+in float fragTrans;
 in vec2 texCoord;
 in vec4 fragCoord;
 out vec4 FragColor;
@@ -25,6 +26,7 @@ void main()
     vec3 Falloff = vec3(0.4, 0.1, 0.002);
 
     vec4 DiffuseColor = texture2D(textu, texCoord);
+    DiffuseColor *= fragTrans;
 
     if (DiffuseColor.a < 0.1f)
         discard;
index aa183a25b720f24432b55666305681be7f6ed383..28fd30708905073b0308a5abeac670c0882df48e 100644 (file)
@@ -3,16 +3,19 @@
 //layout(location = 0)in vec3 vertex;
 in vec3 vertex;
 in vec2 texc;
+in float trans;
 
 uniform mat4 projection;
 uniform mat4 view;
 uniform mat4 model;
 
+out float fragTrans;
 out vec2 texCoord;
 out vec4 fragCoord;
 
 void main()
 {
+    fragTrans = trans;
     texCoord = texc;
     fragCoord = vec4(vertex, 1.0f);
     gl_Position = projection * view * model * fragCoord;
index c06b7b3706f05ad72ba759fd067b8ba9ffe69d22..9e63a712debb4e60844d05b5622a38f34456d010 100644 (file)
@@ -45,6 +45,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     static GLuint m = worldShader.getUniform("model");
     static GLuint a = worldShader.getAttribute("vertex");
     static GLuint t = worldShader.getAttribute("texc");
+    static GLuint r = worldShader.getAttribute("trans");
 
     static GLuint q = worldShader.getUniform("textu");
     static GLuint n = worldShader.getUniform("normu");
@@ -52,6 +53,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     static GLuint f = worldShader.getUniform("Flipped");
 
     static glm::vec3 rot = glm::vec3(0.0f, 0.0f, -1.0f);
+    camPos.z = 15.0f;
 
     /***********
     *  SETUP  *
@@ -76,15 +78,21 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     float scaleWidth = static_cast<float>(width) / scale;
     float scaleHeight = static_cast<float>(height) / scale;
 
-    glm::mat4 projection = glm::ortho(-(scaleWidth/2),    // Left
-                                       (scaleWidth/2),    // Right
-                                      -(scaleHeight/2),   // Bottom
-                                       (scaleHeight/2),   // Top
-                                       100.0f,               // zFar
-                                      -100.0f                // zNear
-                                     );
+    //glm::mat4 projection = glm::ortho(-(scaleWidth/2),    // Left
+    //                                   (scaleWidth/2),    // Right
+    //                                  -(scaleHeight/2),   // Bottom
+    //                                   (scaleHeight/2),   // Top
+    //                                   100.0f,               // zFar
+    //                                  -100.0f                // zNear
+    //                                 );
+
+    glm::mat4 projection = glm::perspective(45.0f, 
+                                            ((float)width/(float)height), 
+                                            0.01f, 
+                                            2048.0f);
 
     glm::mat4 model = glm::mat4(1.0f);
+    model = glm::scale(model, glm::vec3(1.0f, 1.0f, -1.0f));
 
     glUseProgram(s);
 
@@ -95,6 +103,7 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
 
     glEnableVertexAttribArray(a);
     glEnableVertexAttribArray(t);
+    glEnableVertexAttribArray(r);
 
     // Ambient light, for now this is static
     GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f};
@@ -143,9 +152,9 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
     *************/
 
     entities.each<Render, Position>(
-        [this](entityx::Entity, Render &r, Position &p) {
+        [this](entityx::Entity, Render &rend, Position &p) {
 
-        if (!r.visible)
+        if (!rend.visible)
             return;
 
         // If our component was created via script, call the entity's
@@ -155,28 +164,28 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         //}
 
         float w = 0.5f;
-        float h = (float)r.texture.height/r.texture.width;
+        float h = (float)rend.texture.height/rend.texture.width;
 
         GLuint tri_vbo;
         GLfloat tri_data[] = {
-                (float)p.x-w, (float)p.y  , 00.0f, 0.0f, 1.0f,
-                (float)p.x+w, (float)p.y  , 00.0f, 1.0f, 1.0f,
-                (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f,
-
-                (float)p.x+w, (float)p.y  , 00.0f, 1.0f, 1.0f,
-                (float)p.x+w, (float)p.y+h, 00.0f, 1.0f, 0.0f,
-                (float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f,
+                (float)p.x-w, (float)p.y  , 0.0f, 0.0f, 1.0f, 1.0f,
+                (float)p.x+w, (float)p.y  , 0.0f, 1.0f, 1.0f, 1.0f,
+                (float)p.x-w, (float)p.y+h, 0.0f, 0.0f, 0.0f, 1.0f,
+                                                             
+                (float)p.x+w, (float)p.y  , 0.0f, 1.0f, 1.0f, 1.0f,
+                (float)p.x+w, (float)p.y+h, 0.0f, 1.0f, 0.0f, 1.0f,
+                (float)p.x-w, (float)p.y+h, 0.0f, 0.0f, 0.0f, 1.0f,
         };
 
         bool flipped = false;
 
         // TODO flip nicely (aka model transformations)
-        if (r.flipX) {
-            std::swap(tri_data[3], tri_data[8]);
-            tri_data[13] = tri_data[3];
+        if (rend.flipX) {
+            std::swap(tri_data[3], tri_data[9]);
+            tri_data[15] = tri_data[3];
 
-            std::swap(tri_data[23], tri_data[28]);
-            tri_data[18] = tri_data[23];
+            std::swap(tri_data[27], tri_data[33]);
+            tri_data[21] = tri_data[27];
 
             flipped = true;
         }
@@ -184,11 +193,11 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         glUniform1i(f, flipped ? 1 : 0);
 
         glActiveTexture(GL_TEXTURE0);
-        glBindTexture(GL_TEXTURE_2D, r.texture.tex);
+        glBindTexture(GL_TEXTURE_2D, rend.texture.tex);
         glUniform1i(q, 0);
 
         glActiveTexture(GL_TEXTURE1);
-        glBindTexture(GL_TEXTURE_2D, r.normal.tex);
+        glBindTexture(GL_TEXTURE_2D, rend.normal.tex);
         glUniform1i(n, 1);
 
         glGenBuffers(1, &tri_vbo);
@@ -196,9 +205,11 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         glBufferData(GL_ARRAY_BUFFER, sizeof(tri_data), tri_data, GL_STREAM_DRAW);
 
         glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE,
-                              5*sizeof(float), 0);
-        glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 
-                              5*sizeof(float), (void*)(3*sizeof(float)));
+                              6*sizeof(float), 0);
+        glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE,
+                              6*sizeof(float), (void*)(3*sizeof(float)));
+        glVertexAttribPointer(r, 1, GL_FLOAT, GL_FALSE,
+                              6*sizeof(float), (void*)(5*sizeof(float)));
         glDrawArrays(GL_TRIANGLES, 0, 6);
     });
     glUniform1i(f, 0);
@@ -218,6 +229,8 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
                               6*sizeof(float), 0);
         glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 
                               6*sizeof(float), (void*)(3*sizeof(float)));
+        glVertexAttribPointer(r, 1, GL_FLOAT, GL_FALSE, 
+                              6*sizeof(float), (void*)(5*sizeof(float)));
         glDrawArrays(GL_TRIANGLES, 0, worldVertex);
     }
 
@@ -348,6 +361,7 @@ int RenderSystem::init(void)
 
     worldShader.addAttribute("vertex");
     worldShader.addAttribute("texc");
+    worldShader.addAttribute("trans");
 
     worldShader.addUniform("textu");
     worldShader.addUniform("normu");
@@ -372,10 +386,9 @@ int RenderSystem::init(void)
     glEnableVertexAttribArray(worldShader.getAttribute("vertex"));
     glEnableVertexAttribArray(uiShader.getAttribute("coord2d"));
 
-    // TODO
     //glPolygonOffset(1.0, 1.0);
 
-    glClearColor(0.6, 0.8, 1.0, 0.0);
+    //glClearColor(0.6, 0.8, 1.0, 0.0);
     
     camPos = glm::vec3(0.0f, 0.0f, 5.0f);
 
index feff72891f3c1100d1f4e3aad7db055ef3aa3e37..cd89a220e665196cc75c9f2c91ee91440660a10a 100644 (file)
@@ -137,7 +137,7 @@ void World::generateMesh()
 
     // Preallocate size of vertexes
     mesh = std::basic_string<WorldMeshData>();
-    for (float Z = 0; Z < data.size(); Z++) {
+    for (float Z = data.size() - 1; Z >= 0; Z--) {
         for (float X = 0; X < data.at(Z).size(); X++) {
             for (float Y = 0; Y < data.at(Z).at(X).size(); Y++) {
                 int d = data.at(Z).at(X).at(Y);
@@ -149,13 +149,26 @@ void World::generateMesh()
                 glm::vec2& to = t.offset;
                 glm::vec2& ts = t.size;
 
-                mesh += {X  , Y  , Z, to.x     , to.y+ts.y, 1.0};
-                mesh += {X+1, Y  , Z, to.x+ts.x, to.y+ts.y, 1.0};
-                mesh += {X  , Y+1, Z, to.x     , to.y     , 1.0};
+                float tr = 1.0f;
+
+                // TODO play with this a bit so it only goes trans
+                //  if player is behind the front layer
+                try {
+                    if (Z < data.size() - 1 && Z >= 0) {
+                        if (data.at(Z+1).at(X).at(Y) == -1)
+                            tr = 1.0f;
+                    }
+                } catch (...) {
+                    tr = 1.0f;
+                }
+
+                mesh += {X  , Y  , Z, to.x     , to.y+ts.y, tr};
+                mesh += {X+1, Y  , Z, to.x+ts.x, to.y+ts.y, tr};
+                mesh += {X  , Y+1, Z, to.x     , to.y     , tr};
 
-                mesh += {X+1, Y  , Z, to.x+ts.x, to.y+ts.y, 1.0};
-                mesh += {X+1, Y+1, Z, to.x+ts.x, to.y     , 1.0};
-                mesh += {X  , Y+1, Z, to.x     , to.y     , 1.0};
+                mesh += {X+1, Y  , Z, to.x+ts.x, to.y+ts.y, tr};
+                mesh += {X+1, Y+1, Z, to.x+ts.x, to.y     , tr};
+                mesh += {X  , Y+1, Z, to.x     , to.y     , tr};
             }
         }
     }