aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-24 02:29:41 -0400
committerAndy Belle-Isle <drumsetmonkey@gmail.com>2019-09-24 02:29:41 -0400
commitbe9d14233e9fc7b7c5e4fc3711125132e3cee151 (patch)
tree0b3fefe33ef1ab566df17bfd32217a039d2c2334
parentf6a0e340bc82cb5fb96f836686bd59aaffd5db97 (diff)
Increased texture size for world, made world a little more detailed, and added bouncing ball
-rw-r--r--Assets/ball.pngbin0 -> 5146 bytes
-rw-r--r--Assets/ball_normal.pngbin0 -> 5885 bytes
-rw-r--r--Assets/world.pngbin236 -> 11118 bytes
-rw-r--r--Assets/world_normal.pngbin1877 -> 10591 bytes
-rw-r--r--Scripts/init.lua29
-rw-r--r--Scripts/world.lua42
-rw-r--r--Shaders/world.frag2
-rw-r--r--Shaders/world.vert3
-rw-r--r--src/render.cpp71
-rw-r--r--src/world.cpp27
10 files changed, 118 insertions, 56 deletions
diff --git a/Assets/ball.png b/Assets/ball.png
new file mode 100644
index 0000000..b8dca47
--- /dev/null
+++ b/Assets/ball.png
Binary files differ
diff --git a/Assets/ball_normal.png b/Assets/ball_normal.png
new file mode 100644
index 0000000..d238e30
--- /dev/null
+++ b/Assets/ball_normal.png
Binary files differ
diff --git a/Assets/world.png b/Assets/world.png
index c972c87..f6c78d9 100644
--- a/Assets/world.png
+++ b/Assets/world.png
Binary files differ
diff --git a/Assets/world_normal.png b/Assets/world_normal.png
index 1818895..c691e1e 100644
--- a/Assets/world_normal.png
+++ b/Assets/world_normal.png
Binary files differ
diff --git a/Scripts/init.lua b/Scripts/init.lua
index 8559f7c..cdd6ccb 100644
--- a/Scripts/init.lua
+++ b/Scripts/init.lua
@@ -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 --
diff --git a/Scripts/world.lua b/Scripts/world.lua
index 044559a..3b56d9a 100644
--- a/Scripts/world.lua
+++ b/Scripts/world.lua
@@ -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);
diff --git a/Shaders/world.frag b/Shaders/world.frag
index 18945a8..79d87aa 100644
--- a/Shaders/world.frag
+++ b/Shaders/world.frag
@@ -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;
diff --git a/Shaders/world.vert b/Shaders/world.vert
index aa183a2..28fd307 100644
--- a/Shaders/world.vert
+++ b/Shaders/world.vert
@@ -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;
diff --git a/src/render.cpp b/src/render.cpp
index c06b7b3..9e63a71 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -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);
diff --git a/src/world.cpp b/src/world.cpp
index feff728..cd89a22 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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};
}
}
}