Merge branch 'master' of github.com:sippeangelo/Escape-the-Dawn

This commit is contained in:
ViktorLjung
2014-03-14 01:54:28 +01:00
31 changed files with 880 additions and 10 deletions
-1
View File
@@ -10,7 +10,6 @@ public:
glm::vec3 Forward();
glm::vec3 Right();
glm::quat Orientation();
float AspectRatio() const { return m_AspectRatio; }
void AspectRatio(float val);
+47
View File
@@ -0,0 +1,47 @@
#include "CubemapTexture.h"
CubemapTexture::CubemapTexture(char* posXFile, char* negXFile, char* posYFile, char* negYFile, char* posZFile, char* negZFile)
{
m_TextureFiles[0] = posXFile;
m_TextureFiles[1] = negXFile;
m_TextureFiles[2] = posYFile;
m_TextureFiles[3] = negYFile;
m_TextureFiles[4] = posZFile;
m_TextureFiles[5] = negZFile;
Load();
}
CubemapTexture::~CubemapTexture()
{
//glDeleteTextures(1, &m_Texture);
}
void CubemapTexture::Load()
{
m_Texture = SOIL_load_OGL_cubemap(
m_TextureFiles[0],
m_TextureFiles[1],
m_TextureFiles[2],
m_TextureFiles[3],
m_TextureFiles[4],
m_TextureFiles[5],
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
0);
if (m_Texture == 0)
LOG_ERROR("SOIL cubemap loading error: %s", SOIL_last_result());
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
void CubemapTexture::Bind(GLenum textureUnit)
{
glActiveTexture(textureUnit);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_Texture);
}
+31
View File
@@ -0,0 +1,31 @@
#ifndef CubemapTexture_h__
#define CubemapTexture_h__
#include "OpenGL.h"
#include "glerror.h"
#include <SOIL.h>
#include "logging.h"
class CubemapTexture
{
public:
CubemapTexture() { }
CubemapTexture(
char* posXFile,
char* negXFile,
char* posYFile,
char* negYFile,
char* posZFile,
char* negZFile);
~CubemapTexture();
void Load();
void Bind(GLenum textureSlot);
private:
char* m_TextureFiles[6];
GLuint m_Texture;
};
#endif // CubemapTexture_h__
+6
View File
@@ -98,6 +98,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Camera.cpp" />
<ClCompile Include="CubemapTexture.cpp" />
<ClCompile Include="Engine.h" />
<ClCompile Include="Frame.cpp" />
<ClCompile Include="GameFrame.cpp" />
@@ -109,6 +110,7 @@
<ClCompile Include="OBJ.cpp" />
<ClCompile Include="Renderer.cpp" />
<ClCompile Include="ShaderProgram.cpp" />
<ClCompile Include="Skybox.cpp" />
<ClCompile Include="Systems\CollisionSystem.cpp" />
<ClCompile Include="Systems\TransformSystem.cpp" />
<ClCompile Include="Texture.cpp" />
@@ -126,6 +128,7 @@
<ClInclude Include="Color.h" />
<ClInclude Include="Component.h" />
<ClInclude Include="Components\Camera.h" />
<ClInclude Include="CubemapTexture.h" />
<ClInclude Include="Factory.h" />
<ClInclude Include="Components\Bounds.h" />
<ClInclude Include="Components\Collision.h" />
@@ -155,6 +158,7 @@
<ClInclude Include="OpenGL.h" />
<ClInclude Include="Renderer.h" />
<ClInclude Include="ShaderProgram.h" />
<ClInclude Include="Skybox.h" />
<ClInclude Include="Systems\InputSystem.h" />
<ClInclude Include="System.h" />
<ClInclude Include="Systems\CollisionSystem.h" />
@@ -174,6 +178,8 @@
<None Include="Shaders\Fragment.glsl" />
<None Include="Shaders\Normals.frag.glsl" />
<None Include="Shaders\Normals.geo.glsl" />
<None Include="Shaders\Skybox.frag.glsl" />
<None Include="Shaders\Skybox.vert.glsl" />
<None Include="Shaders\VisualizeDepth.frag.glsl" />
<None Include="Shaders\VisualizeDepth.vert.glsl" />
<None Include="Shaders\ShadowMap.frag.glsl" />
@@ -47,6 +47,8 @@
<ClCompile Include="OBJ.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="GameWorld.cpp" />
<ClCompile Include="Skybox.cpp" />
<ClCompile Include="CubemapTexture.cpp" />
<ClCompile Include="Systems\TransformSystem.cpp">
<Filter>Systems</Filter>
</ClCompile>
@@ -151,6 +153,8 @@
<ClInclude Include="Camera.h" />
<ClInclude Include="OBJ.h" />
<ClInclude Include="OpenGL.h" />
<ClInclude Include="Skybox.h" />
<ClInclude Include="CubemapTexture.h" />
<ClInclude Include="Systems\TransformSystem.h">
<Filter>Systems</Filter>
</ClInclude>
@@ -187,6 +191,12 @@
<None Include="Shaders\AABB.frag.glsl">
<Filter>Shaders</Filter>
</None>
<None Include="Shaders\Skybox.frag.glsl">
<Filter>Shaders</Filter>
</None>
<None Include="Shaders\Skybox.vert.glsl">
<Filter>Shaders</Filter>
</None>
</ItemGroup>
<ItemGroup>
<Filter Include="Systems">
+1
View File
@@ -11,6 +11,7 @@ void GameWorld::Initialize()
std::shared_ptr<Components::Camera> camera;
std::shared_ptr<Components::Bounds> bounds;
std::shared_ptr<Components::Collision> collision;
std::shared_ptr<Components::SoundEmitter> soundEmitter;
EntityID ent;
// Fucking lights
+169
View File
@@ -0,0 +1,169 @@
#include "GameWorld.h"
void GameWorld::Initialize()
{
World::Initialize();
AddSystem("LevelGenerationSystem");
AddSystem("InputSystem");
AddSystem("CollisionSystem");
//AddSystem("ParticleSystem");
AddSystem("PlayerSystem");
AddSystem("SoundSystem");
AddSystem("RenderSystem");
std::shared_ptr<Components::Transform> transform;
std::shared_ptr<Components::Model> model;
std::shared_ptr<Components::PointLight> pointLight;
std::shared_ptr<Components::Camera> camera;
std::shared_ptr<Components::Bounds> bounds;
std::shared_ptr<Components::Collision> collision;
std::shared_ptr<Components::SoundEmitter> soundEmitter;
EntityID ent;
// Fucking lights
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(0.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.0, 0.0, 5.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/sphere.obj";
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(10.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.0, 5.0, 0.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/sphere.obj";
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(-10.f, 7.f, 0.f);
pointLight = AddComponent<Components::PointLight>(ent, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(5.0, 0.0, 0.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/sphere.obj";
//ground
ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(0.f, 0.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/plane.obj";
// Player
m_Player = CreateEntity();
SetProperty(m_Player, "Name", std::string("PlayerShip"));
transform = AddComponent<Components::Transform>(m_Player, "Transform");
transform->Position = glm::vec3(0.f, 2.f, -5.f);
transform->Scale = glm::vec3(1.0f);
model = AddComponent<Components::Model>(m_Player, "Model");
model->ModelFile = "Models/ship.obj";
pointLight = AddComponent<Components::PointLight>(m_Player, "PointLight");
pointLight->Specular = glm::vec3(1.0, 1.0, 1.0);
pointLight->Diffuse = glm::vec3(0.4, 0.4, 1.0);
pointLight->constantAttenuation = 0.f;
pointLight->linearAttenuation = 1.f;
pointLight->quadraticAttenuation = 0.f;
pointLight->spotExponent = 0.0f;
AddComponent<Components::Input>(m_Player, "Input");
collision = AddComponent<Components::Collision>(m_Player, "Collision");
collision->Phantom = false;
collision->Interested = true;
bounds = AddComponent<Components::Bounds>(m_Player, "Bounds");
<<<<<<< HEAD
bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3);
bounds->VolumeVector = glm::vec3(4.f,0.7f,4);
soundEmitter = AddComponent<Components::SoundEmitter>(m_Player, "SoundEmitter");
soundEmitter->Loop = true;
soundEmitter->MaxDistance = FLT_MAX;
soundEmitter->ReferenceDistance = 10;
soundEmitter->Gain = 1;
soundEmitter->Pitch = 1;
soundEmitter->Path = "Sounds/hallelujah.wav";
GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(soundEmitter);
=======
bounds->Origin = glm::vec3(0, 0, 2.f);
bounds->VolumeVector = glm::vec3(4.f, 0.7f, 1);
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
// Camera
entcamera = CreateEntity(m_Player);
SetProperty(entcamera, "Name", std::string("Camera"));
transform = AddComponent<Components::Transform>(entcamera, "Transform");
AddComponent<Components::Input>(entcamera, "Input");
transform->Position = glm::vec3(0.f, 10.f, 14.f);
camera = AddComponent<Components::Camera>(entcamera, "Camera");
camera->FOV = 45.f;
camera->FarClip = 1000.f;
camera->NearClip = 0.01f;
transform->Orientation = glm::angleAxis<float>(glm::radians(15.0f),glm::vec3(1,0,0));
/*ent = CreateEntity();
transform = AddComponent<Components::Transform>(ent, "Transform");
transform->Position = glm::vec3(10.f, 4.f, 0.f);
model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/ship.obj";
collision = AddComponent<Components::Collision>(player2, "Collision");
collision->Phantom = false;
bounds = AddComponent<Components::Bounds>(player2, "Bounds");
bounds->Origin = transform->Position;
bounds->VolumeVector = glm::vec3(2.f,2.f,2.f);*/
}
void GameWorld::Update(double dt)
{
World::Update(dt);
}
void GameWorld::RegisterComponents()
{
m_ComponentFactory.Register("Bounds", []() { return new Components::Bounds(); });
m_ComponentFactory.Register("Camera", []() { return new Components::Camera(); });
m_ComponentFactory.Register("Collision", []() { return new Components::Collision(); });
m_ComponentFactory.Register("DirectionalLight", []() { return new Components::DirectionalLight(); });
m_ComponentFactory.Register("Input", []() { return new Components::Input(); });
m_ComponentFactory.Register("Model", []() { return new Components::Model(); });
m_ComponentFactory.Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); });
m_ComponentFactory.Register("PointLight", []() { return new Components::PointLight(); });
m_ComponentFactory.Register("PowerUp", []() { return new Components::PowerUp(); });
m_ComponentFactory.Register("SoundEmitter", []() { return new Components::SoundEmitter(); });
m_ComponentFactory.Register("Sprite", []() { return new Components::Sprite(); });
m_ComponentFactory.Register("Stat", []() { return new Components::Stat(); });
m_ComponentFactory.Register("Template", []() { return new Components::Template(); });
m_ComponentFactory.Register("Transform", []() { return new Components::Transform(); });
}
void GameWorld::RegisterSystems()
{
m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); });
m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); });
m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); });
//m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); });
m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); });
m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); });
m_SystemFactory.Register("RenderSystem", [this]() { return new Systems::RenderSystem(this, m_Renderer); });
}
+50 -2
View File
@@ -99,6 +99,13 @@ void Renderer::LoadContent()
m_ShaderProgramDebugAABB.Compile();
m_ShaderProgramDebugAABB.Link();
m_ShaderProgramSkybox.AddShader(std::shared_ptr<Shader>(new VertexShader("Shaders/Skybox.vert.glsl")));
m_ShaderProgramSkybox.AddShader(std::shared_ptr<Shader>(new FragmentShader("Shaders/Skybox.frag.glsl")));
m_ShaderProgramSkybox.Compile();
m_ShaderProgramSkybox.Link();
m_Skybox = std::make_shared<Skybox>(CubemapTexture("Textures/right.jpg", "Textures/left.jpg", "Textures/top.jpg", "Textures/bottom.jpg", "Textures/front.jpg", "Textures/back.jpg"));
m_DebugAABB = CreateAABB();
m_ScreenQuad = CreateQuad();
CreateShadowMap(m_ShadowMapRes);
@@ -133,6 +140,7 @@ void Renderer::Draw(double dt)
{
glDisable(GL_BLEND);
DrawSkybox();
DrawShadowMap();
DrawScene();
@@ -167,13 +175,26 @@ void Renderer::Draw(double dt)
glfwSwapBuffers(m_Window);
}
void Renderer::DrawScene()
void Renderer::DrawSkybox()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
m_ShaderProgramSkybox.Bind();
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * glm::toMat4(m_Camera->Orientation());
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramSkybox.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(cameraMatrix));
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
m_Skybox->Draw();
}
void Renderer::DrawScene()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, WIDTH, HEIGHT);
glClear(GL_DEPTH_BUFFER_BIT);
//glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
@@ -466,6 +487,33 @@ GLuint Renderer::CreateAABB()
return vao;
}
GLuint Renderer::CreateSkybox()
{
glm::vec3 skyBoxVertices[] =
{
glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, -1.0f),
glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, 1.0f),
glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, -1.0f),
glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3(-1.0f, -1.0f, 1.0f),
glm::vec3( 1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, -1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3(-1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, 1.0f, 1.0f), glm::vec3( 1.0f, -1.0f, 1.0f),
glm::vec3(-1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, -1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3( 1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, 1.0f, -1.0f), glm::vec3(-1.0f, -1.0f, -1.0f)
};
GLuint vbo, vao;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyBoxVertices), skyBoxVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
return vao;
}
void Renderer::ClearStuff()
{
AABBsToRender.clear();
+8
View File
@@ -15,6 +15,7 @@
#include "ShaderProgram.h"
#include "Model.h"
#include "Components/PointLight.h"
#include "Skybox.h"
class Renderer
{
@@ -66,6 +67,9 @@ public:
void DrawWireframe(bool val) { m_DrawWireframe = val; }
bool DrawBounds() const { return m_DrawBounds; }
void DrawBounds(bool val) { m_DrawBounds = val; }
void DrawSkybox();
private:
GLFWwindow* m_Window;
@@ -76,6 +80,8 @@ private:
bool m_DrawWireframe;
bool m_DrawBounds;
std::shared_ptr<Skybox> m_Skybox;
int m_ShadowMapRes;
glm::vec3 m_SunPosition;
glm::vec3 m_SunTarget;
@@ -93,6 +99,7 @@ private:
ShaderProgram m_ShaderProgramShadows;
ShaderProgram m_ShaderProgramShadowsDrawDepth;
ShaderProgram m_ShaderProgramDebugAABB;
ShaderProgram m_ShaderProgramSkybox;
void ClearStuff();
void DrawScene();
@@ -102,6 +109,7 @@ private:
GLuint CreateQuad();
void DrawDebugShadowMap();
GLuint CreateAABB();
GLuint CreateSkybox(void);
};
+15
View File
@@ -0,0 +1,15 @@
#version 430
uniform samplerCube CubemapTexture;
in VertexData {
vec3 TextureCoord;
} Input;
out vec4 FragColor;
void main()
{
FragColor = texture(CubemapTexture, Input.TextureCoord);
//FragColor = vec4(1.0, 1.0, 1.0, 0.0);
}
+15
View File
@@ -0,0 +1,15 @@
#version 430
uniform mat4 MVP;
layout(location = 0) in vec3 Position;
out VertexData {
vec3 TextureCoord;
} Output;
void main()
{
gl_Position = MVP * vec4(Position, 1.0);
Output.TextureCoord = Position;
}
+79
View File
@@ -0,0 +1,79 @@
#include "Skybox.h"
Skybox::Skybox(CubemapTexture cubemap)
{
m_Cubemap = cubemap;
float cubeVertices[] = {
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
};
//std::copy(cubeVertices, cubeVertices + (3*8 - 1), m_CubeVertices);
unsigned int cubeIndices[] = {
// Back
0, 2, 3,
0, 1, 2,
// Right
1, 6, 2,
1, 5, 6,
// Front
5, 7, 6,
5, 4, 7,
// Left
4, 3, 7,
4, 0, 3,
// Top
3, 6, 7,
3, 2, 6,
// Bottom
4, 1, 0,
4, 5, 1,
};
//std::copy(cubeIndices, cubeIndices + (3*12 - 1), m_CubeIndices);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3 * 8 * sizeof(float), cubeVertices, GL_STATIC_DRAW);
glGenBuffers(1, &ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * 12 * sizeof(int), cubeIndices, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
GLERROR("");
}
Skybox::~Skybox()
{
}
void Skybox::Draw()
{
m_Cubemap.Bind(GL_TEXTURE0);
glBindVertexArray(vao);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDepthMask(GL_FALSE);
glDrawElements(GL_TRIANGLES, 3 * 12, GL_UNSIGNED_INT, 0);
glDepthMask(GL_TRUE);
}
+29
View File
@@ -0,0 +1,29 @@
#ifndef Skybox_h__
#define Skybox_h__
#include <algorithm>
#include "OpenGL.h"
#include "logging.h"
#include "glerror.h"
#include "CubemapTexture.h"
class Skybox
{
public:
Skybox(CubemapTexture cubemap);
~Skybox();
void Draw();
private:
CubemapTexture m_Cubemap;
GLuint ibo;
GLuint vao;
float m_CubeVertices[3 * 8];
int m_CubeIndices[1];
};
#endif // Skybox_h__
@@ -0,0 +1,134 @@
#include "CollisionSystem.h"
#include "World.h"
void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
/*if (parent != 0)
{
auto transform = m_World->GetComponent<Components::Transform>(entity, "Transform");
auto parentTransform = m_World->GetComponent<Components::Transform>(parent, "Transform");
transform->Position[0] += parentTransform->Position[0];
transform->Position[1] += parentTransform->Position[1];
transform->Position[2] += parentTransform->Position[2];
<<<<<<< HEAD
}
=======
}*/
auto collisionComponent = m_World->GetComponent<Components::Collision>(entity, "Collision");
if (!collisionComponent)
return;
// Clear old collisions
collisionComponent->CollidingEntities.clear();
// Quit out if we're not interested in collision events
if (!collisionComponent->Interested)
return;
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
auto entities = m_World->GetEntities();
for (auto pair : *entities) {
EntityID entity2 = pair.first;
EntityID parent2 = pair.second;
// Check if entity2 is a child of entity
/*auto currentParent = parent;
bool childOfEntity2 = false;
while (currentParent != 0) {
if (currentParent == entity2) {
childOfEntity2 = true;
break;
}
currentParent = entities[currentParent];
}
if (childOfEntity2)
continue;*/
// Check if we're the parent to entity2
//pair.first, pair.second;
if(entity == entity2)
continue;
Intersects(entity, entity2);
}
}
void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity)
{
auto aTransform = m_World->GetComponent<Components::Transform>(aEntity, "Transform");
if(aTransform == nullptr)
return;
auto bTransform = m_World->GetComponent<Components::Transform>(bEntity, "Transform");
if(bTransform == nullptr)
return;
auto aCollisionComponent = m_World->GetComponent<Components::Collision>(aEntity, "Collision");
if(aCollisionComponent == nullptr)
return;
auto bCollisionComponent = m_World->GetComponent<Components::Collision>(bEntity, "Collision");
if(bCollisionComponent == nullptr)
return;
auto aBounds = m_World->GetComponent<Components::Bounds>(aEntity, "Bounds");
auto bBounds = m_World->GetComponent<Components::Bounds>(bEntity, "Bounds");
glm::vec3 aPos = aTransform->Position + (aBounds->Origin * aTransform->Scale);
glm::vec3 bPos = bTransform->Position + (bBounds->Origin * bTransform->Scale);
glm::vec3 aMax = aPos + aBounds->VolumeVector * aTransform->Scale;
glm::vec3 aMin = aPos - aBounds->VolumeVector * aTransform->Scale;
glm::vec3 bMax = bPos + bBounds->VolumeVector * bTransform->Scale;
glm::vec3 bMin = bPos - bBounds->VolumeVector * bTransform->Scale;
if (aMin.x <= bMax.x && bMin.x <= aMax.x) {
if (aMin.y <= bMax.y && bMin.y <= aMax.y) {
if (aMin.z <= bMax.z && bMin.z <= aMax.z) {
aCollisionComponent->CollidingEntities.push_back(aEntity);
bCollisionComponent->CollidingEntities.push_back(bEntity);
return;
}
}
}
}
void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr<Components::Bounds> bounds)
{
float width = abs(bounds->VolumeVector.x);
float height = abs(bounds->VolumeVector.y);
float depth = abs(bounds->VolumeVector.z);
GLfloat vertices[] = {
bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0,
bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0,
};
GLuint vbo_vertices;
glGenBuffers(1, &vbo_vertices);
glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLushort elements[] = {
0, 1, 2, 3,
4, 5, 6, 7,
0, 4, 1, 5,
2, 6, 3, 7
};
GLuint ibo_elements;
glGenBuffers(1, &ibo_elements);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
@@ -32,7 +32,7 @@ void Systems::LevelGenerationSystem::SpawnObstacle()
sound->Loop = true;
sound->Gain = 1.f;
sound->ReferenceDistance = 15.f;
sound->ReferenceDistance = 4.f;
m_World->GetSystem<Systems::SoundSystem>("SoundSystem")->PlaySound(sound, "Sounds/hum.wav");
if(typeRandom >= 0 && typeRandom < 1) // Mountain stuff :D
+3 -1
View File
@@ -20,7 +20,9 @@ public:
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string path); // Use if you want to play a temporary .wav file not from component
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter); // Use if you want to play .wav file from component // imon no hate plx T.T
void StopSound(std::shared_ptr<Components::SoundEmitter> emitter);
private:
ALuint LoadFile(std::string fileName);
@@ -0,0 +1,51 @@
#ifndef SoundEmitter_h__
#define SoundEmitter_h__
#include "System.h"
#include "Components/Transform.h"
#include "Components/SoundEmitter.h"
#include <AL/al.h>
#include <AL/alc.h>
#include <vector>
#include <glm/gtx/quaternion.hpp>
namespace Systems
{
class SoundSystem : public System
{
public:
SoundSystem(World* world);
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
<<<<<<< HEAD
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string path); // Use if you want to play a temporary .wav file not from component
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter); // Use if you want to play .wav file from component // imon no hate plx T.T
void StopSound(std::shared_ptr<Components::SoundEmitter> emitter);
=======
void OnComponentRemoved(std::string type, Component* component) override;
void PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string fileName);
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
private:
ALuint LoadFile(std::string fileName);
ALuint CreateSource();
//File-info
char type[4];
unsigned long size, chunkSize;
short formatType, channels;
unsigned long sampleRate, avgBytesPerSec;
short bytesPerSample, bitsPerSample;
unsigned long dataSize;
std::map<Component*, ALuint> m_Sources;
std::map<std::string, ALuint> m_BufferCache; // string = fileName
};
}
#endif // !SoundEmitter_h__
+21 -5
View File
@@ -86,6 +86,19 @@ void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> e
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter)
{
ALuint buffer = LoadFile(emitter->Path);
ALuint source = m_Sources[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::StopSound(std::shared_ptr<Components::SoundEmitter> emitter)
{
alSourceStop(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == "SoundEmitter") {
@@ -104,13 +117,13 @@ void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* compo
}
}
ALuint Systems::SoundSystem::LoadFile(std::string fileName)
ALuint Systems::SoundSystem::LoadFile(std::string path)
{
if (m_BufferCache.find(fileName) != m_BufferCache.end())
return m_BufferCache[fileName];
if (m_BufferCache.find(path) != m_BufferCache.end())
return m_BufferCache[path];
FILE *fp = NULL;
fp = fopen(fileName.c_str(), "rb");
fp = fopen(path.c_str(), "rb");
//CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp);
@@ -176,7 +189,7 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName)
alBufferData(buffer, format, buf, dataSize, sampleRate);
delete[] buf;
m_BufferCache[fileName] = buffer;
m_BufferCache[path] = buffer;
return buffer;
}
@@ -185,5 +198,8 @@ ALuint Systems::SoundSystem::CreateSource()
ALuint source;
alGenSources((ALuint)1, &source);
alDopplerFactor(1); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect
alDopplerVelocity(350.f); // Defines the velocity of the sound
return source;
}
@@ -0,0 +1,210 @@
#include "SoundSystem.h"
#include "World.h"
Systems::SoundSystem::SoundSystem(World* world)
: System(world)
{
//initialize OpenAL
ALCdevice* Device = alcOpenDevice(NULL);
ALCcontext* context;
if(Device)
{
context = alcCreateContext(Device, NULL);
alcMakeContextCurrent(context);
}
else
{
LOG_ERROR("OMG OPEN AL FAIL");
}
alGetError();
alSpeedOfSound(340.29f); // Speed of sound
alDistanceModel(AL_INVERSE_DISTANCE);
}
void Systems::SoundSystem::Update(double dt)
{
}
void Systems::SoundSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
if (transformComponent == nullptr)
return;
auto entityName = m_World->GetProperty<std::string>(entity, "Name");
if (entityName == "Camera")
{
glm::vec3 playerPos = transformComponent->Position;
ALfloat listenerPos[3] = { playerPos.x, playerPos.y, -playerPos.z };
glm::vec3 playerVel = transformComponent->Velocity;
ALfloat listenerVel[3] = { playerVel.x, playerVel.y, -playerVel.z };
glm::fquat playerQuatOri = transformComponent->Orientation;
glm::vec3 playerOriFW = glm::rotate(playerQuatOri, glm::vec3(0, 0, -1));
glm::vec3 playerOriUP = glm::rotate(playerQuatOri, glm::vec3(0, 1, 0));
ALfloat listenerOri[6] = { playerOriFW.x, playerOriFW.y, playerOriFW.z, playerOriUP.x, playerOriUP.y, playerOriUP.z };
//Listener
alListenerfv(AL_POSITION, listenerPos);
alListenerfv(AL_VELOCITY, listenerVel);
alListenerfv(AL_ORIENTATION, listenerOri);
}
auto soundEmitter = m_World->GetComponent<Components::SoundEmitter>(entity, "SoundEmitter");
if(soundEmitter != nullptr)
{
ALuint source = m_Sources[soundEmitter];
alSourcef(source, AL_GAIN, soundEmitter->Gain);
//alSourcef(source, AL_MAX_DISTANCE, soundEmitter->MaxDistance);
alSourcef(source, AL_REFERENCE_DISTANCE, soundEmitter->ReferenceDistance);
alSourcef(source, AL_PITCH, soundEmitter->Pitch);
alSourcei(source, AL_LOOPING, soundEmitter->Loop);
glm::vec3 emitterPos = transformComponent->Position;
ALfloat sourcePos[3] = { emitterPos.x, emitterPos.y, -emitterPos.z };
glm::vec3 emitterVel= transformComponent->Velocity;
ALfloat sourceVel[3] = { emitterVel.x, emitterVel.y, -emitterVel.z };
alSourcefv(source, AL_POSITION, sourcePos);
alSourcefv(source, AL_VELOCITY, sourceVel);
}
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter, std::string path)
{
<<<<<<< HEAD
ALuint buffer = LoadFile(path);
ALuint source = m_Source[emitter.get()];
=======
if (m_Sources.find(emitter.get()) == m_Sources.end())
return;
ALuint buffer = LoadFile(fileName);
ALuint source = m_Sources[emitter.get()];
>>>>>>> f76c0b336f87a259a7eabf3c532fc885f155a605
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Sources[emitter.get()]);
}
void Systems::SoundSystem::PlaySound(std::shared_ptr<Components::SoundEmitter> emitter)
{
ALuint buffer = LoadFile(emitter->Path);
ALuint source = m_Source[emitter.get()];
alSourcei(source, AL_BUFFER, buffer);
alSourcePlay(m_Source[emitter.get()]);
}
void Systems::SoundSystem::StopSound(std::shared_ptr<Components::SoundEmitter> emitter)
{
alSourceStop(m_Source[emitter.get()]);
}
void Systems::SoundSystem::OnComponentCreated(std::string type, std::shared_ptr<Component> component)
{
if(type == "SoundEmitter") {
ALuint source = CreateSource();
m_Sources[component.get()] = source;
}
}
void Systems::SoundSystem::OnComponentRemoved(std::string type, Component* component)
{
if(type == "SoundEmitter") {
if (m_Sources.find(component) != m_Sources.end()) {
ALuint source = m_Sources[component];
alDeleteSources(1, &source);
}
}
}
ALuint Systems::SoundSystem::LoadFile(std::string path)
{
if (m_BufferCache.find(path) != m_BufferCache.end())
return m_BufferCache[path];
FILE *fp = NULL;
fp = fopen(path.c_str(), "rb");
//CHECK FOR VALID WAVE-FILE
fread(type, sizeof(char), 4, fp);
if(type[0]!='R' || type[1]!='I' || type[2]!='F' || type[3]!='F') {
LOG_ERROR("ERROR: No RIFF in WAVE-file");
return 0;
}
fread(&size, sizeof(unsigned long), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') {
LOG_ERROR("ERROR: Not WAVE-file");
return 0;
}
fread(type, sizeof(char), 4, fp);
if(type[0]!='f' || type[1]!='m' || type[2]!='t' || type[3]!=' ') {
LOG_ERROR("ERROR: No fmt in WAVE-file");
return 0;
}
//READ THE DATA FROM WAVE-FILE
fread(&chunkSize, sizeof(unsigned long), 1, fp);
fread(&formatType, sizeof(short), 1, fp);
fread(&channels, sizeof(short), 1, fp);
fread(&sampleRate, sizeof(unsigned long), 1, fp);
fread(&avgBytesPerSec, sizeof(unsigned long), 1, fp);
fread(&bytesPerSample, sizeof(short), 1, fp);
fread(&bitsPerSample, sizeof(short), 1, fp);
fread(type, sizeof(char), 4, fp);
if(type[0]!='d' || type[1]!='a' || type[2]!='t' || type[3]!='a')
{
LOG_ERROR("ERROR: WAVE-file Missing data");
return 0;
}
fread(&dataSize, sizeof(unsigned long), 1, fp);
unsigned char* buf = new unsigned char[dataSize];
fread(buf, sizeof(unsigned char), dataSize, fp);
fclose(fp);
// Create buffer
ALuint format = 0;
if(bitsPerSample == 8)
{
if(channels == 1)
format = AL_FORMAT_MONO8;
else if(channels == 2)
format = AL_FORMAT_STEREO8;
}
if(bitsPerSample == 16)
{
if (channels == 1)
format = AL_FORMAT_MONO16;
else if (channels == 2)
format = AL_FORMAT_STEREO16;
}
ALuint buffer;
alGenBuffers(1, &buffer);
alBufferData(buffer, format, buf, dataSize, sampleRate);
delete[] buf;
m_BufferCache[path] = buffer;
return buffer;
}
ALuint Systems::SoundSystem::CreateSource()
{
ALuint source;
alGenSources((ALuint)1, &source);
alDopplerFactor(2.f); // Numbers greater than 1 will increase Doppler effect, numbers lower than 1 will decrease the Doppler effect
alDopplerVelocity(350.f); // Defines the velocity of the sound
return source;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 361 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 157 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 364 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 363 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 489 KiB