--end
--self.visibleTick = self.visibleTick + 1
end,
+ Update = function(self)
+ if self.Velocity.x < 0 then
+ self.Render.flipx = true
+ elseif self.Velocity.x > 0 then
+ self.Render.flipx = false
+ end
+ end,
visibleTick = 0
}
self.Velocity.x = -100 * math.sin(math.rad(self.counter));
self.Velocity.y = 100 * math.cos(math.rad(self.counter));
self.counter = self.counter + 5;
+ end,
+ Update = function(self)
+ if self.Velocity.x < 0 then
+ self.Render.flipx = true
+ elseif self.Velocity.x > 0 then
+ self.Render.flipx = false
+ end
end
}
r = 0.0,
b = 1.0,
g = 0.2,
- strength = 2.0
+ strength = 1.0
},
Idle = function(self)
self.Velocity.x = -200 * math.sin(math.rad(self.counter));
uniform vec3 LightPos[32];
uniform vec4 LightColor[32];
uniform int LightNum;
+uniform vec4 AmbientLight;
void main()
{
+ // Quadratic light falloff
vec3 Falloff = vec3(0.1, 0.2, 0.0);
vec4 DiffuseColor = texture2D(textu, texCoord);
vec3 N = normalize(NormalMap * 2.0 - 1.0);
vec3 L = normalize(LightDir);
- vec3 Diffuse = (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
+ vec3 Diffuse =
+ (LightColor[i].rgb * LightColor[i].a) * max(dot(N, L), 0.0);
- vec3 Ambient = vec3(0.0);
+ vec3 Ambient = AmbientLight.rgb * AmbientLight.a;
float Attenuation =
1.0 / (Falloff.x + (Falloff.y * D) + (Falloff.z * D * D));
FragColor = vec4(SumLight, DiffuseColor.a);
};
-
-/*
- vec4 normalMap = texture2D(normu, texCoord);
- vec3 normal = normalMap.xyz * 2.0 - 1.0;
-
- if (pixTex.a < 0.1f)
- discard;
-
- vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
-
- float dist = length(light.xy - fragCoord.xy);
- if (dist < light.w) {
- float attenuation = clamp(1.0f - dist*dist/(light.w*light.w), 0.0f, 1.0f);
- attenuation *= attenuation;
- shadeColor += vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(normal.xyz, 1);
- //shadeColor = vec4(1.0) + 0.1*normal;
- //shadeColor = vec4(1.0);
- }
-
- //FragColor = pixTex * shadeColor;
- FragColor = vec4(normal.xyz, 1);
-}
-*/
-
-/*
- vec2 texLoc = vec2(texCoord.x, 1-texCoord.y);
- vec4 pixTex = texture2D(texture, texLoc);
- if (pixTex.a < 0.1f)
- discard;
-
- vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f);
- if (lightImpact > 0.0f) {
- for (int i = 0; i < lightSize; i++) {
- vec2 loc = light[i].xy;
- float dist = length(loc - fragCoord.xy);
- if (dist < light[i].w) {
- float attenuation = clamp(1.0f - dist*dist/(light[i].w*light[i].w), 0.0f, 1.0f);
- attenuation *= attenuation;
- shadeColor += (vec4(attenuation, attenuation, attenuation, 0.0f) * vec4(lightColor[i])) * lightImpact;
- }
- }
- }
- shadeColor += ambientLight;
-*/
Texture texture;
Texture normal;
bool visible;
- bool hasNormal = false;
+ bool flipX = false;
Render(std::string _file) :
texture(_file), visible(true) {}
this->visible = tab["visible"];
if (tab["texture"].get_type() == sol::type::string)
this->texture = Texture(static_cast<std::string>(tab["texture"]));
- if (tab["normal"].get_type() == sol::type::string) {
+ if (tab["normal"].get_type() == sol::type::string)
this->normal = Texture(static_cast<std::string>(tab["normal"]));
- hasNormal = true;
- }
+ if (tab["flipx"].get_type() == sol::type::boolean)
+ this->flipX = tab["flipx"];
} else {
throw std::string(
"Render component table formatted incorrectly"
caller["Idle"](caller); // Call idle function and pass itself
// in or to fulfill the 'self' param
}
+
+ void update(void)
+ {
+ if (caller["Update"] == sol::type::function)
+ caller["Update"](caller);
+ }
};
#endif // COMPONENT_SCRIPT_HPP_
});
systems.update<InputSystem>(dt);
+ systems.update<ScriptSystem>(dt);
/*******************
* LOGIC UPDATES *
[[maybe_unused]] entityx::EventManager& events,
[[maybe_unused]] entityx::TimeDelta dt)
{
+ // TODO move these to only happen once to speed up rendering
GLuint s = worldShader.getProgram();
GLuint v = worldShader.getUniform("view");
GLuint p = worldShader.getUniform("projection");
GLuint q = worldShader.getUniform("textu");
GLuint n = worldShader.getUniform("normu");
+ GLuint b = worldShader.getUniform("AmbientLight");
/***********
* SETUP *
glEnableVertexAttribArray(a);
glEnableVertexAttribArray(t);
- /*************
- * DRAWING *
- *************/
+ GLfloat amb[4] = {1.0f, 1.0f, 1.0f, 0.0f};
+
+ glUniform4fv(b, 1, amb);
+
+ /**************
+ * LIGHTING *
+ **************/
+
std::vector<glm::vec3> lightPos;
std::vector<glm::vec4> lightColor;
lightColor.size(),
reinterpret_cast<GLfloat*>(lightColor.data()));
+ /*************
+ * DRAWING *
+ *************/
+
+
entities.each<Render, Position>(
[this, a, q, t, n](entityx::Entity, Render &r, Position &p) {
(float)p.x-w, (float)p.y+h, 00.0f, 0.0f, 0.0f,
};
+ // TODO flip nicely (aka model transformations)
+ if (r.flipX) {
+ std::swap(tri_data[3], tri_data[8]);
+ tri_data[13] = tri_data[3];
+
+ std::swap(tri_data[23], tri_data[28]);
+ tri_data[18] = tri_data[23];
+ }
+
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, r.texture.tex);
glUniform1i(q, 0);
glBindBuffer(GL_ARRAY_BUFFER, tri_vbo);
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)));
+ 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)));
glDrawArrays(GL_TRIANGLES, 0, 6);
});
worldShader.addUniform("LightPos");
worldShader.addUniform("LightColor");
worldShader.addUniform("LightNum");
+ worldShader.addUniform("AmbientLight");
glEnableVertexAttribArray(worldShader.getAttribute("vertex"));
glUseProgram(worldShader.getProgram());
//init();
}
-void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entites,
+#include <components/Script.hpp>
+void ScriptSystem::update([[maybe_unused]] entityx::EntityManager& entities,
[[maybe_unused]] entityx::EventManager& events,
[[maybe_unused]] entityx::TimeDelta dt)
{
+ entities.each<Scripted>([](entityx::Entity, Scripted &s){
+ s.update();
+ });
}
lua.new_usertype<Render>("Render",
sol::constructors<Render(std::string), Render()>(),
"visible", &Render::visible,
- "texture", &Render::texture);
+ "texture", &Render::texture,
+ "flipx", &Render::flipX);
lua.new_usertype<Velocity>("Velocity",
sol::constructors<Velocity(double, double), Velocity()>(),