Merge branch 'Rendering' of github.com:teamfisk/TacticalZ into Rendering

# Conflicts:
#	include/Engine/Rendering/Renderer.h
#	src/Engine/Rendering/Renderer.cpp
This commit is contained in:
viktorljung
2015-12-03 19:06:24 +01:00
24 changed files with 1172 additions and 58 deletions
+3
View File
@@ -7,6 +7,7 @@ find_package(Boost REQUIRED COMPONENTS system filesystem thread chrono)
find_package(assimp REQUIRED)
find_package(ZLIB REQUIRED)
find_package(PNG REQUIRED)
find_package(Xerces REQUIRED)
# Because FindOpenAL is retarded
#set(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "${CMAKE_SOURCE_DIR}/deps/include/AL")
#find_package(OpenAL REQUIRED)
@@ -23,6 +24,7 @@ include_directories(
${Boost_INCLUDE_DIRS}
${assimp_INCLUDE_DIRS}
${PNG_INCLUDE_DIRS}
${Xerces_INCLUDE_DIRS}
${OPENAL_INCLUDE_DIR}
${X11_INCLUDE_DIRS}
)
@@ -78,6 +80,7 @@ set(LIBRARIES
${Boost_LIBRARIES}
${assimp_LIBRARIES}
${PNG_LIBRARIES}
${Xerces_LIBRARIES}
${OPENAL_LIBRARY}
${X11_LIBRARIES}
)
+79
View File
@@ -0,0 +1,79 @@
#include "Core/ComponentPool.h"
ComponentWrapper ComponentPoolForwardIterator::operator*() const
{
char* data = &(*m_MemoryPoolIterator);
ComponentWrapper wrapper(m_ComponentInfo, data);
return wrapper;
}
bool ComponentPoolForwardIterator::operator==(const ComponentPoolForwardIterator& other) const
{
return m_MemoryPoolIterator == other.m_MemoryPoolIterator;
}
bool ComponentPoolForwardIterator::operator!=(const ComponentPoolForwardIterator& other) const
{
return m_MemoryPoolIterator != other.m_MemoryPoolIterator;
}
ComponentPoolForwardIterator& ComponentPoolForwardIterator::operator++(int)
{
ComponentPoolForwardIterator copyIter(*this);
operator++();
return copyIter;
}
ComponentPoolForwardIterator& ComponentPoolForwardIterator::operator++()
{
++m_MemoryPoolIterator;
return *this;
}
//const ::ComponentInfo& ComponentPool::ComponentInfo() const
//{
// return m_ComponentInfo;
//}
ComponentWrapper ComponentPool::Allocate(EntityID entity)
{
char* data = m_Pool.Allocate();
memcpy(data, &entity, sizeof(EntityID));
m_EntityToComponent[entity] = data;
return ComponentWrapper(m_ComponentInfo, data);
}
ComponentWrapper ComponentPool::GetByEntity(EntityID ent)
{
return ComponentWrapper(m_ComponentInfo, m_EntityToComponent.at(ent));
}
void ComponentPool::Delete(ComponentWrapper& wrapper)
{
m_EntityToComponent.erase(wrapper.EntityID);
m_Pool.Free(wrapper.Data);
}
ComponentPool::iterator ComponentPool::begin() const
{
return iterator(m_ComponentInfo, m_Pool.begin(), m_Pool.end());
}
ComponentPool::iterator ComponentPool::end() const
{
return iterator(m_ComponentInfo, m_Pool.end(), m_Pool.end());
}
template <typename InterpretType /*= char*/>
void ComponentPool::Dump() const
{
m_Pool.Dump<InterpretType>();
}
template <typename InterpretType /*= char*/, typename OutStream>
void ComponentPool::Dump(OutStream& out) const
{
m_Pool.Dump<InterpretType>(out);
}
+54
View File
@@ -0,0 +1,54 @@
#include "Core/World.h"
World::~World()
{
for (auto& pool : m_ComponentPools) {
delete pool.second;
}
}
EntityID World::CreateEntity(EntityID parent /*= 0*/)
{
EntityID newEntity = generateEntityID();
m_EntityParents[newEntity] = parent;
if (parent != 0) {
m_EntityChildren.insert(std::make_pair(parent, newEntity));
}
return newEntity;
}
void World::RegisterComponent(ComponentInfo& ci)
{
m_ComponentPools[ci.Name] = new ComponentPool(ci);
}
ComponentWrapper World::AttachComponent(EntityID entity, std::string componentType)
{
ComponentPool* pool = m_ComponentPools.at(componentType);
const ComponentInfo& ci = pool->ComponentInfo();
// Allocate space for the component
ComponentWrapper c = pool->Allocate(entity);
// Write default values
memcpy(c.Data, ci.Defaults.get(), ci.Meta.Stride);
return c;
}
ComponentWrapper World::GetComponent(EntityID entity, std::string componentType)
{
ComponentPool* pool = m_ComponentPools.at(componentType);
return pool->GetByEntity(entity);
}
const ComponentPool& World::GetComponents(std::string componentType)
{
return *m_ComponentPools.at(componentType);
}
EntityID World::generateEntityID()
{
// TODO: Make EntityID generation smarter
return m_CurrentEntityID++;
}
+3 -48
View File
@@ -161,16 +161,15 @@ void Renderer::InputUpdate(double dt)
static double mousePosX, mousePosY;
glfwGetCursorPos(m_Window, &mousePosX, &mousePosY);
if (glfwGetMouseButton(m_Window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) {
glm::vec3 data = GetClickedPixelData(mousePosX, m_Resolution.Height - mousePosY);
glm::vec3 data = ScreenCoords::ToPixelData(mousePosX, m_Resolution.Height - mousePosY, m_PickingBuffer, m_DepthBuffer);
glm::vec2 color = glm::vec2(data);
float depth = data.z;
glm::vec3 viewPos = ScreenCoordsToWorldPos(glm::vec2(mousePosX, m_Resolution.Height - mousePosY), depth);
glm::vec3 viewPos = ScreenCoords::ToWorldPos(mousePosX, m_Resolution.Height - mousePosY, depth, m_Resolution, m_Camera->ProjectionMatrix(), m_Camera->ViewMatrix());
// glm::vec3 worldPos = glm::vec3(glm::inverse(m_Camera->ViewMatrix()) * glm::vec4(viewPos, 1.f));
//printf("R: %f, G: %f, Depth: %f\n", color.r, color.g, depth);
printf("view: x: %f, y: %f z: %f, Length: %f\n", viewPos.x, viewPos.y, viewPos.z, glm::length(viewPos));
//printf("view: x: %f, y: %f z: %f, Length: %f\n\n", viewPos.x, viewPos.y, viewPos.z, glm::length(viewPos));
if (color != glm::vec2(0, 0)) {
const Model* pickModel = m_PickingColorsToModels[color];
@@ -200,33 +199,6 @@ void Renderer::InputUpdate(double dt)
m_Camera->SetPosition(m_Position);
}
// Utility functions should be moved to a better place
glm::vec3 Renderer::ScreenCoordsToWorldPos(glm::vec2 screenCoord, float depth)
{
glm::vec4 pos;
float near = m_Camera->NearClip();
float far = m_Camera->FarClip();
glm::vec3 ndc;
ndc.x = screenCoord.x / m_Resolution.Width;
ndc.y = screenCoord.y / m_Resolution.Height;
glm::vec4 clipSpace = glm::vec4(glm::vec2(ndc.x, ndc.y) * 2.0f - 1.0f, (depth) * 2.0f - 1.0f, 1.0f);
glm::vec4 EyeSpace = glm::inverse(m_Camera->ProjectionMatrix()) * clipSpace;
glm::vec4 WorldSpace = glm::inverse(m_Camera->ViewMatrix()) * EyeSpace;
WorldSpace = glm::vec4(glm::vec3(WorldSpace) / WorldSpace.w, 1.f);
return glm::vec3(WorldSpace);
}
//EntityID Renderer::ScreenCoordsToEntityID(glm::vec2 screenCoord, float depth)
//{
//
//}
void Renderer::Update(double dt)
{
InputUpdate(dt);
@@ -352,23 +324,6 @@ void Renderer::DrawScreenQuad(GLuint textureToDraw)
}
// Will return a vec3 where RG is the PickColor and B is the Depth
glm::vec3 Renderer::GetClickedPixelData(float x, float y)
{
m_PickingBuffer.Bind();
glm::vec2 pixelData;
glReadPixels(x, y, 1, 1, GL_RG, GL_FLOAT, &pixelData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, m_DepthBuffer);
float depthData;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depthData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return glm::vec3(pixelData, depthData);
}
void Renderer::InitializeTextures()
{
m_ErrorTexture=ResourceManager::Load<Texture>("Textures/Core/ErrorTexture.png");
@@ -0,0 +1,50 @@
#include "Rendering/Util/ScreenCoords.h"
glm::vec3 ScreenCoords::ToWorldPos(float x, float y, float depth, float screenWidth, float screenHeight, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
glm::vec3 ndc;
ndc.x = (x / screenWidth)*2.f - 1.f;
ndc.y = (y / screenHeight)*2.f - 1.f;
ndc.z = depth*2.f - 1.f;
glm::vec4 clipSpace = glm::vec4(ndc, 1.0f);
glm::vec4 EyeSpace = glm::inverse(cameraProjectionMat) * clipSpace;
glm::vec4 WorldSpace = glm::inverse(cameraViewMat) * EyeSpace;
WorldSpace = glm::vec4(glm::vec3(WorldSpace) / WorldSpace.w, 1.f);
return glm::vec3(WorldSpace);
}
glm::vec3 ScreenCoords::ToWorldPos(glm::vec2 screenCoord, float depth, Rectangle resolution, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
return ToWorldPos(screenCoord.x, screenCoord.y, depth, resolution.Width, resolution.Height, cameraProjectionMat, cameraViewMat);
}
glm::vec3 ScreenCoords::ToWorldPos(float x, float y, float depth, Rectangle resolution, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
return ToWorldPos(x, y, depth, resolution.Width, resolution.Height, cameraProjectionMat, cameraViewMat);
}
glm::vec3 ScreenCoords::ToWorldPos(glm::vec2 screenCoord, float depth, float screenWidth, float screenHeight, glm::mat4 cameraProjectionMat, glm::mat4 cameraViewMat)
{
return ToWorldPos(screenCoord.x, screenCoord.y, depth, screenWidth, screenHeight, cameraProjectionMat, cameraViewMat);
}
glm::vec3 ScreenCoords::ToPixelData(float x, float y, GLuint PickDataBuffer, GLuint DepthBuffer)
{
glBindFramebuffer(GL_FRAMEBUFFER, PickDataBuffer);
glm::vec2 pixelData;
glReadPixels(x, y, 1, 1, GL_RG, GL_FLOAT, &pixelData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, DepthBuffer);
float depthData;
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depthData);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
return glm::vec3(pixelData, depthData);
}
glm::vec3 ScreenCoords::ToPixelData(glm::vec2 screenCoord, GLuint PickDataBuffer, GLuint DepthBuffer)
{
return ToPixelData(screenCoord.x, screenCoord.y, PickDataBuffer, DepthBuffer);
}
+4
View File
@@ -1,4 +1,5 @@
#include "Game.h"
#include "HardcodedTestWorld.h"
Game::Game(int argc, char* argv[])
{
@@ -32,6 +33,9 @@ Game::Game(int argc, char* argv[])
m_FrameStack->Width = m_Renderer->Resolution().Width;
m_FrameStack->Height = m_Renderer->Resolution().Height;
// Create a TEST WORLD
m_World = new HardcodedTestWorld();
m_LastTime = glfwGetTime();
}
+34
View File
@@ -0,0 +1,34 @@
#include <boost/test/unit_test.hpp>
#include "Core/ComponentPool.h"
BOOST_AUTO_TEST_CASE(ComponentPoolTest)
{
// TODO: Write an updated test for component pool
BOOST_CHECK(false);
//ComponentInfo ci;
//ci.Name = "Test";
//ci.FieldTypes["Field"] = "int";
//ci.FieldOffsets["Field"] = 0;
//ci.Meta.Allocation = 3;
//ci.Meta.Stride = sizeof(EntityID) + sizeof(int);
//std::vector<ComponentWrapper> wrappers;
//ComponentPool pool(ci);
//for (int i = 0; i < 4; i++) {
// ComponentWrapper c = pool.New();
// c.EntityID = i;
// unsigned int offset = c.Info.FieldOffsets.at("Field");
// memcpy(&c.Data[offset], &i, sizeof(int));
// wrappers.push_back(c);
//}
//int i = 0;
//for (auto& c : pool) {
// BOOST_CHECK(c.EntityID == i);
// BOOST_CHECK((int)c["Field"] == i);
// i++;
//}
//pool.Delete(wrappers[1]);
}
+41
View File
@@ -0,0 +1,41 @@
#include <boost/test/unit_test.hpp>
#include "Common.h"
#include "Core/World.h"
BOOST_AUTO_TEST_CASE(WorldTest)
{
ComponentInfo ci;
ci.Name = "Test";
ci.FieldTypes["Field"] = "int";
ci.FieldOffsets["Field"] = 0;
ci.Meta.Stride = sizeof(int);
ci.Meta.Allocation = 3;
ci.Defaults = std::shared_ptr<char>(new char[ci.Meta.Stride]);
int default_Field = 1337;
memcpy(ci.Defaults.get(), &default_Field, ci.Meta.Stride);
World w;
w.RegisterComponent(ci);
std::vector<EntityID> ids;
for (int i = 0; i < 6; i++) {
EntityID e = w.CreateEntity();
ids.push_back(e);
w.AttachComponent(e, "Test");
ComponentWrapper c = w.GetComponent(e, "Test");
BOOST_CHECK(c.EntityID == e);
BOOST_CHECK((int)c["Field"] == 1337);
c.SetProperty("Field", i);
BOOST_CHECK((int)c["Field"] == i);
}
int i = 0;
for (auto& c : w.GetComponents("Test")) {
EntityID e = ids.at(i);
BOOST_CHECK(c.EntityID == e);
BOOST_CHECK((int)c["Field"] == i);
i++;
}
}