Merge remote-tracking branch 'origin/master' into Physics

Conflicts:
	include/Core/Engine.h
	src/game/Physics/PhysicsSystem.cpp
This commit is contained in:
viktorljung
2015-09-14 15:37:38 +01:00
53 changed files with 807 additions and 92 deletions
Regular → Executable
+11 -3
View File
@@ -77,12 +77,20 @@ void dd::Camera::SetOrientation(glm::quat val)
void dd::Camera::UpdateProjectionMatrix()
{
m_ProjectionMatrix = glm::perspective(
m_FOV,
m_AspectRatio,
m_ProjectionMatrix = glm::ortho(
-16.f,
16.f,
-9.f,
9.f,
m_NearClip,
m_FarClip
);
// m_ProjectionMatrix = glm::perspective(
// m_FOV,
// m_AspectRatio,
// m_NearClip,
// m_FarClip
// );
}
void dd::Camera::UpdateViewMatrix()
+11 -5
View File
@@ -111,6 +111,7 @@ void dd::Renderer::LoadShaders()
void dd::Renderer::CreateBuffers()
{
m_ScreenQuad = CreateQuad();
m_UnitQuad = ResourceManager::Load<Model>("Models/Core/UnitQuad.obj");
m_UnitSphere = ResourceManager::Load<Model>("Models/Core/UnitSphere.obj");
glGenRenderbuffers(1, &m_rbDepthBuffer);
@@ -264,7 +265,7 @@ void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights)
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
m_spDeferred3->Bind();
glUniform3fv(glGetUniformLocation(*m_spDeferred3, "La"), 1, glm::value_ptr(glm::vec3(0.3f)));
glUniform3fv(glGetUniformLocation(*m_spDeferred3, "La"), 1, glm::value_ptr(glm::vec3(0.5f)));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_GDiffuse);
glActiveTexture(GL_TEXTURE1);
@@ -306,6 +307,7 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
glUniform1f(glGetUniformLocation(shaderProgramHandle, "MaterialShininess"), modelJob->Shininess);
//TODO: Make sure that a normal/specular is loaded in.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture);
if (modelJob->NormalTexture != 0) {
@@ -328,17 +330,21 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
if (spriteJob)
{
glm::mat4 modelMatrix = spriteJob->ModelMatrix;
modelMatrix = modelMatrix * glm::scale(glm::vec3(0.5f));
MVP = PV * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "M"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, spriteJob->Texture);
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, spriteJob->NormalTexture);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, spriteJob->SpecularTexture);
glBindVertexArray(m_ScreenQuad);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(m_UnitQuad->VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_UnitQuad->ElementBuffer);
glDrawElementsBaseVertex(GL_TRIANGLES, m_UnitQuad->m_Indices.size(), GL_UNSIGNED_INT, 0, 0);
continue;
}
+1 -1
View File
@@ -65,7 +65,7 @@ void main()
+ BoneWeights2[3] * Bones[int(BoneIndices2[3])];
}
gl_Position = MVP * boneTransform * vec4(Position, 1.0);
gl_Position = MVP * vec4(Position, 1.0);
Output.Position = (V * M * boneTransform * vec4(Position, 1.0)).xyz;
Output.Normal = (inverse(transpose(V * M)) * boneTransform * vec4(Normal, 0.0)).xyz;
@@ -38,6 +38,7 @@ void main()
//frag_Diffuse = DiffuseTexel * (vec4(La, 0.0) * (vec4(LightingTexel.rgb, 0.0) + vec4(LightingTexel.a, LightingTexel.a, LightingTexel.a, 0.0)));
//frag_Diffuse = DiffuseTexel * (vec4(La, 0.0) + LightingTexel);
frag_Diffuse = DiffuseTexel;
frag_Diffuse = DiffuseTexel * (vec4(1.f, 1.f, 1.f, 0.0) + LightingTexel);
//frag_Diffuse = DiffuseTexel;
//frag_Diffuse = vec4(LightingTexel.r, LightingTexel.g, LightingTexel.b, 1.0);
}
+2 -2
View File
@@ -84,6 +84,6 @@ void main()
vec4 diffuseTexel = texture(DiffuseTexture, Input.TextureCoord);
//FragmentColor = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a);
FragmentColor = diffuseTexel;
FragmentColor = phong(Input.Position, normalize(normal), specularTexel.rgb, specularTexel.a);
//FragmentColor = diffuseTexel;
}
+1 -1
View File
@@ -24,7 +24,7 @@ dd::Texture::Texture(std::string path)
std::unique_ptr<Image> image = std::make_unique<PNG>(path);
if (image->Width == 0 && image->Height == 0 || image->Format == Image::ImageFormat::Unknown) {
image = std::make_unique<PNG>("Textures/ErrorTexture.png");
image = std::make_unique<PNG>("Textures/Core/ErrorTexture.png");
if (image->Width == 0 && image->Height == 0 || image->Format == Image::ImageFormat::Unknown) {
LOG_ERROR("Couldn't even load the error texture. This is a dark day indeed.");
return;
+145
View File
@@ -0,0 +1,145 @@
//
// Created by Adniklastrator on 2015-09-09.
//
#include "PrecompiledHeader.h"
#include "Game/LevelSystem.h"
void dd::Systems::LevelSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_EContact, LevelSystem::OnContact);
return;
}
void dd::Systems::LevelSystem::Update(double dt) {
if (!m_Initialized) {
CreateBasicLevel(tRows, tLines, tSpaceBetweenBricks, tSpaceToEdge);
m_Initialized = true;
}
}
void dd::Systems::LevelSystem::CreateBasicLevel(int rows, int lines, glm::vec2 spacesBetweenBricks, int spaceToEdge)
{
int num = 0;
numberOfBricks = rows * lines;
for (int i = 0; i < rows; i++)
{
num++;
for (int j = 0; j < lines; j++)
{
CreateBrick(i, j, spacesBetweenBricks, spaceToEdge, num);
}
if (num == 7)
num = 0;
}
return;
}
void dd::Systems::LevelSystem::CreateBrick(int row, int line, glm::vec2 spacesBetweenBricks, int spaceToEdge, int num)
{
auto brick = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(brick);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(brick);
std::shared_ptr<Components::Brick> cBrick = m_World->AddComponent<Components::Brick>(brick);
std::shared_ptr<Components::RectangleShape> cRec = m_World->AddComponent<Components::RectangleShape>(brick);
std::shared_ptr<Components::Physics> cPhys = m_World->AddComponent<Components::Physics>(brick);
std::string fileName = "Textures/Bricks/";
fileName.append(std::to_string(num));
fileName.append(".png");
sprite->SpriteFile = fileName;
float x = spaceToEdge + line * spacesBetweenBricks.x;
float y = spaceToEdge + row * spacesBetweenBricks.y;
transform->Scale = glm::vec3(1.6, 0.4, 0.);
transform->Position = glm::vec3(x - 7, y + 1, -10.f);
//sprite->Color = glm::vec4(1.f, 1.f, 1.f, 1.f);
m_World->CommitEntity(brick);
return;
}
void dd::Systems::LevelSystem::ProcessCollision()
{
// Like, go into brick component and check what it does upon collision. Maybe not done here?
return;
}
void dd::Systems::LevelSystem::OnEntityRemoved(EntityID entity)
{
auto brick = m_World->GetComponent<Components::Brick>(entity);
if (brick != NULL)
{
numberOfBricks--;
if (numberOfBricks < 1)
{
EndLevel();
}
}
return;
}
bool dd::Systems::LevelSystem::OnContact(const dd::Events::Contact &event)
{
/*event.Entity1 - Vi vet inte vilken som.
event.Entity2 - */
EntityID entityBrick = event.Entity1;
auto brick = m_World->GetComponent<Components::Brick>(entityBrick);
if (brick == NULL)
{
return false;
}
EntityID entityBall = event.Entity2;
//EntityID entity1 = event.Entity1;
//EntityID entity2 = event.Entity2;
//auto brick = m_World->GetComponent<Components::Brick>(entity1);
//auto ball = m_World->GetComponent<Components::Ball>(entity2);
//std::cout << brick << std::endl;
//std::cout << ball << std::endl;
/*if (brick != NULL)
{
std::cout << "Entity1 is a brick!" << std::endl;
entityBrick = entity1;
if (ball != NULL)
{
std::cout << "Entity2 is a ball!" << std::endl;
entityBall = entity2;
}
}
else
{
brick = m_World->GetComponent<Components::Brick>(entity2);
if (brick != NULL)
{
std::cout << "Entity2 is a brick!" << std::endl;
entityBrick = entity2;
}
ball = m_World->GetComponent<Components::Ball>(entity1);
if (ball != NULL)
{
std::cout << "Entity1 is a ball!" << std::endl;
entityBall = entity1;
}
}
if (entityBrick != NULL && entityBall != NULL)
{*/
//std::cout << "We're destroying!" << std::endl;
m_World->RemoveEntity(entityBrick);
return true;
//}
return false;
}
void dd::Systems::LevelSystem::EndLevel()
{
Events::StageCleared e;
EventBroker->Publish(e);
return;
}
+181
View File
@@ -0,0 +1,181 @@
//
// Created by Adniklastrator on 2015-09-10.
//
#include "PrecompiledHeader.h"
#include "Game/PadSystem.h"
#include "Core/World.h"
#include <iostream>
#include <Game/CPad.h>
void dd::Systems::PadSystem::Initialize()
{
Events::BindKey r;
r.KeyCode = GLFW_KEY_RIGHT;
r.Command = "right";
EventBroker->Publish(r);
Events::BindKey l;
l.KeyCode = GLFW_KEY_LEFT;
l.Command = "left";
EventBroker->Publish(l);
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown);
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp);
EVENT_SUBSCRIBE_MEMBER(m_EContact, PadSystem::OnContact);
return;
}
void dd::Systems::PadSystem::Update(double dt)
{
if (ent == 0) {
for (auto it = m_World->GetEntities()->begin(); it != m_World->GetEntities()->end(); it++) {
if (m_World->GetProperty<std::string>(it->first, "Name") == "Pad") {
ent = it->first;
transform = m_World->GetComponent<Components::Transform>(ent);
break;
}
}
}
if (transform->Velocity.x < -400.f)
{
transform->Velocity.x = -400.f;
}
else if (transform->Velocity.x > 400.f)
{
transform->Velocity.x = 400.f;
}
transform->Position += transform->Velocity * (float)dt;
transform->Velocity += acceleration * (float)dt;
transform->Velocity -= transform->Velocity * (0.9f * (float)dt);
if (left)
{
acceleration.x = -20.f;
}
else if (right)
{
acceleration.x = 20.f;
}
else
{
acceleration.x = 0.f;
}
return;
}
bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event)
{
int val = event.KeyCode;
if (val == 265)
{
//std::cout << "Up!" << std::endl;
}
else if (val == 264)
{
//std::cout << "Down!" << std::endl;
}
else if (val == 263)
{
//std::cout << "Left!" << std::endl;
//acceleration.x = -0.01f;
left = true;
}
else if (val = 262)
{
//std::cout << "Right!" << std::endl;
//acceleration.x = 0.01f;
right = true;
}
return true;
}
bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event)
{
int val = event.KeyCode;
if (val == 265)
{
}
else if (val == 264)
{
}
else if (val == 263)
{
left = false;
}
else if (val = 262)
{
right = false;
}
return true;
}
bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
{
EntityID entityBall = event.Entity2;
auto ball = m_World->GetComponent<Components::Ball>(entityBall);
if (ball == NULL)
{
return false;
}
EntityID entityPad = event.Entity1;
auto pad = m_World->GetComponent<Components::Pad>(entityPad);
if (pad == NULL)
{
return false;
}
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
auto transformPad = m_World->GetComponent<Components::Transform>(entityPad);
float movementMultiplier = 2.f;
float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
std::cout << movementX << " " << movementY << std::endl;
//Temporary solution. Add a new ball and delete the old one.
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.1f, -10.f);
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
m_World->RemoveEntity(entityBall);
m_World->CommitEntity(ent);
Events::SetImpulse e;
e.Entity = ent;
e.Impulse = glm::vec2(movementX, movementY);
e.Point = glm::vec2(event.ContactPoint);
EventBroker->Publish(e);
}
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
{
std::string command = event.Command;
std::cout << "Command!" << std::endl;
if (command == "right")
{
std::cout << "Right!" << std::endl;
}
else if (command == "left")
{
std::cout << "Left!" << std::endl;
}
return true;
}
+2 -2
View File
@@ -12,7 +12,7 @@ void dd::Systems::PhysicsSystem::Initialize()
{
m_ContactListener = new ContactListener(this);
m_Gravity = b2Vec2(0.f, -9.82f);
m_Gravity = b2Vec2(0.f, 0.f);
m_PhysicsWorld = new b2World(m_Gravity);
m_TimeStep = 1.f/60.f;
@@ -188,7 +188,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
b2FixtureDef fixtureDef;
fixtureDef.shape = pShape;
fixtureDef.density = 1.f;
fixtureDef.restitution = 0.0f;
fixtureDef.restitution = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
+6
View File
@@ -20,6 +20,12 @@
#include "Transform/TransformSystem.h"
#include "Core/World.h"
void dd::Systems::TransformSystem::Update(double dt)
{
return;
}
glm::vec3 dd::Systems::TransformSystem::AbsolutePosition(EntityID entity)
{
glm::vec3 absPosition;