Starting off with specified rendering.

Almost there. Just an odd linker error so far.
This commit is contained in:
elipzer 2018-10-13 15:26:22 -04:00
parent 6020231c23
commit 2f6c5ee319
15 changed files with 316 additions and 31 deletions

View File

@ -148,6 +148,8 @@ copy "$(ProjectDir)*.h" "$(SolutionDir)include\charcoal-builtin\"</Command>
<ClCompile Include="LitScene.cpp" />
<ClCompile Include="LitShadowedBatch.cpp" />
<ClCompile Include="LitShadowedScene.cpp" />
<ClCompile Include="SpecifiedBatch.cpp" />
<ClCompile Include="SpecifiedScene.cpp" />
<ClCompile Include="TexturedBatch.cpp" />
<ClCompile Include="TexturedScene.cpp" />
<ClCompile Include="TextureGenerator.cpp" />
@ -178,6 +180,11 @@ copy "$(ProjectDir)*.h" "$(SolutionDir)include\charcoal-builtin\"</Command>
<ClInclude Include="MeshGenerator.h" />
<ClInclude Include="Poseable2DBatch.h" />
<ClInclude Include="PoseableBatch.h" />
<ClInclude Include="SpecifiedBatch.h" />
<ClInclude Include="SpecifiedPoseable.h" />
<ClInclude Include="SpecifiedPoseableBatch.h" />
<ClInclude Include="SpecifiedScene.h" />
<ClInclude Include="SpecifiedTypes.h" />
<ClInclude Include="SpriteBatch.h" />
<ClInclude Include="TexturedBatch.h" />
<ClInclude Include="TexturedScene.h" />

View File

@ -55,6 +55,12 @@
<Filter Include="Source Files\Scenes\Textured">
<UniqueIdentifier>{5f931ae6-23b1-43df-a4d8-e7f134d755c8}</UniqueIdentifier>
</Filter>
<Filter Include="Header Files\Scenes\Specified">
<UniqueIdentifier>{d1604903-a209-427a-a879-3278b9fe8088}</UniqueIdentifier>
</Filter>
<Filter Include="Source Files\Scenes\Specified">
<UniqueIdentifier>{eecad623-46fa-4c24-b93d-c8d77002608e}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="GLUtil.cpp">
@ -93,6 +99,12 @@
<ClCompile Include="TexturedScene.cpp">
<Filter>Source Files\Scenes\Textured</Filter>
</ClCompile>
<ClCompile Include="SpecifiedBatch.cpp">
<Filter>Source Files\Scenes\Specified</Filter>
</ClCompile>
<ClCompile Include="SpecifiedScene.cpp">
<Filter>Source Files\Scenes\Specified</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TexturedBatch.h">
@ -191,5 +203,20 @@
<ClInclude Include="LitShaderProgram.h">
<Filter>Header Files\Scenes\Lit</Filter>
</ClInclude>
<ClInclude Include="SpecifiedPoseableBatch.h">
<Filter>Header Files\General</Filter>
</ClInclude>
<ClInclude Include="SpecifiedPoseable.h">
<Filter>Header Files\General</Filter>
</ClInclude>
<ClInclude Include="SpecifiedBatch.h">
<Filter>Header Files\Scenes\Specified</Filter>
</ClInclude>
<ClInclude Include="SpecifiedScene.h">
<Filter>Header Files\Scenes\Specified</Filter>
</ClInclude>
<ClInclude Include="SpecifiedTypes.h">
<Filter>Header Files\Scenes\Specified</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -4,7 +4,7 @@
#include <charcoal/Renderable.h>
#include <charcoal/Poseable2D.h>
#include "BuiltinBatch.h"
#include "PoseableBatch.h"
namespace charcoal
{
@ -13,43 +13,25 @@ namespace charcoal
// This has to be made completely seperately in order to avoid the vtable that gets added if Poseable is abstracted between
// 2D and 3D
template <typename VertexType, typename IndexType, typename RenderableT = RenderableT<VertexType, IndexType> >
class Poseable2DBatch : public builtin::Batch<VertexType, IndexType, 1, RenderableT>
class Poseable2DBatch : public PoseableBatch<VertexType, IndexType, RenderableT>
{
public:
Poseable2DBatch(
RenderableT* renderable,
int element_count
) : Poseable2DBatch(renderable, element_count, element_count)
{}
using PoseableBatch<VertexType, IndexType, RenderableT>::PoseableBatch;
Poseable2DBatch(
RenderableT* renderable,
int element_count,
int element_render_count
) : builtin::Batch<VertexType, IndexType, 1, RenderableT>(renderable, element_render_count), m_pose_elements(element_count)
{}
virtual ~Poseable2DBatch() {}
Poseable2D& get_pose(int index) { return m_pose_elements[index]; }
const Poseable2D& get_pose(int index) const { return m_pose_elements[index]; }
protected:
void setup_element_buffers()
Poseable2D& get_pose(int index)
{
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, RenderableT>::m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
Poseable& pose = PoseableBatch<VertexType, IndexType, RenderableT>::get_pose(index);
Poseable* p_pose = &pose;
Poseable2D* p_pose_2d = reinterpret_cast<Poseable2D*>(p_pose); // Can do this since both are just mat4 wrappers
return *p_pose_2d;
}
void update_element_buffers()
const Poseable2D& get_pose(int index) const
{
// TODO: There are probably better ways to do this. Should check with the old engine to see what I did there.
glBindBuffer(GL_ARRAY_BUFFER, charcoal::Batch<VertexType, IndexType, 1, RenderableT>::m_element_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, m_pose_elements.size() * sizeof(Poseable), NULL, GL_STREAM_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_pose_elements.size() * sizeof(Poseable), m_pose_elements.data());
Poseable& pose = PoseableBatch<VertexType, IndexType, RenderableT>::get_pose(index);
Poseable* p_pose = &pose;
Poseable2D* p_pose_2d = reinterpret_cast<Poseable2D*>(p_pose); // Can do this since both are just mat4 wrappers
return *p_pose_2d;
}
std::vector<Poseable2D> m_pose_elements;
};
}
}

View File

@ -0,0 +1,32 @@
#include "SpecifiedBatch.h"
namespace charcoal
{
namespace builtin
{
namespace specified
{
void Batch::setup_vao()
{
glBindBuffer(GL_ARRAY_BUFFER, m_vertex_vbo);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), NULL);
glBindBuffer(GL_ARRAY_BUFFER, m_element_buffers[0]);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glEnableVertexAttribArray(3);
glEnableVertexAttribArray(4);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(0 * sizeof(vec4)));
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(1 * sizeof(vec4)));
glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(2 * sizeof(vec4)));
glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(Poseable), (void*)(3 * sizeof(vec4)));
glVertexAttribDivisor(0, 0); // Send the mesh data once
glVertexAttribDivisor(1, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(2, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(3, 1); // Send the offset data for each instance drawn
glVertexAttribDivisor(4, 1); // Send the offset data for each instance drawn
}
}
}
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "SpecifiedPoseableBatch.h"
#include "SpecifiedTypes.h"
namespace charcoal
{
namespace builtin
{
namespace specified
{
class Batch : public SpecifiedPoseableBatch<Vertex, Index, Renderable>
{
protected:
void setup_vao() override;
};
}
}
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <charcoal/Poseable.h>
namespace charcoal
{
namespace builtin
{
template <typename SpecifiedPoseableBatchType>
class SpecifiedPoseable : public Poseable
{
public:
SpecifiedPoseable(SpecifiedPoseableBatchType& batch) : m_batch(batch)
{}
void render()
{
m_batch.add_specified_poseable(this);
}
private:
SpecifiedPoseableBatchType& m_batch;
};
}
}

View File

@ -0,0 +1,29 @@
#pragma once
#include "PoseableBatch.h"
#include "SpecifiedPoseable.h"
namespace charcoal
{
namespace builtin
{
// Note: If anything is changed in this file, it must also be changed in Poseable2DBatch
template <typename VertexType, typename IndexType, typename RenderableT = RenderableT<VertexType, IndexType> >
class SpecifiedPoseableBatch : public PoseableBatch<VertexType, IndexType, RenderableT>
{
public:
using PoseableBatch<VertexType, IndexType, RenderableT>::PoseableBatch;
void reset()
{
m_element_render_count = 0;
}
void add_specified_poseable(SpecifiedPoseable<SpecifiedPoseableBatch<VertexType, IndexType, RenderableT> >* poseable)
{
PoseableBatch<VertexType, IndexType, RenderableT>::m_pose_elements[m_element_render_count++].set_orientation_matrix(poseable->get_orientation_matrix());
}
};
}
}

View File

@ -0,0 +1,47 @@
#include "SpecifiedScene.h"
#include <charcoal/deps.h>
#include "GLUtil.h"
namespace charcoal
{
namespace builtin
{
namespace specified
{
void Scene::init()
{
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
Batch& batch = *iter;
batch.init();
add_prerenderable(&batch);
}
}
void Scene::use()
{
// TODO: move to glutil
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
}
void Scene::unuse()
{
}
void Scene::render()
{
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, get_camera()->get_world_to_view_matrix());
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();
}
}
}
}
}

View File

@ -0,0 +1,34 @@
#pragma once
#include "SpecifiedTypes.h"
#include "SpecifiedBatch.h"
#include "AutoPrerenderingScene.h"
#include "Batched.h"
#include "WithCamera.h"
namespace charcoal
{
namespace builtin
{
namespace specified
{
class Scene : public AutoPrerenderingScene, public Batched<Renderable, Batch>, public WithCamera
{
public:
Scene(Application& application) : AutoPrerenderingScene(application) {}
void init() override;
void use() override;
void unuse() override;
void render() override;
private:
ShaderProgram m_shader_program;
};
}
}
}

View File

@ -0,0 +1,23 @@
#pragma once
#include "BuiltinTypes.h"
#include "BasicTypes.h"
#include "BasicShaderProgram.h"
namespace charcoal
{
namespace builtin
{
namespace specified
{
// Just use the basic types
typedef basic::Vertex Vertex;
typedef basic::Index Index;
typedef basic::Renderable Renderable;
typedef basic::ShaderProgram ShaderProgram;
}
}
}

View File

@ -31,6 +31,7 @@
<ClCompile Include="MySimple2DScene.cpp" />
<ClCompile Include="MySimple3DScene.cpp" />
<ClCompile Include="MySimpleCubeScene.cpp" />
<ClCompile Include="MySpecifiedScene.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="MyApplication.h" />
@ -46,6 +47,7 @@
<ClInclude Include="MySimple3DScene.h" />
<ClInclude Include="MySimpleCubeScene.h" />
<ClInclude Include="MySimpleShaderProgram.h" />
<ClInclude Include="MySpecifiedScene.h" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>

View File

@ -51,6 +51,9 @@
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MySpecifiedScene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MyApplication.h">
@ -92,5 +95,8 @@
<ClInclude Include="MySimpleCubeScene.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MySpecifiedScene.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
#include "MySpecifiedScene.h"
#include <charcoal/constants.h>
#include <charcoal-builtin/MeshGenerator.h>
MyBuiltinSpecifiedScene::MyBuiltinSpecifiedScene(Application& application)
: specified::Scene(application),
m_shape(meshgenerator::gen_cube_p<specified::Vertex, specified::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f), DRAW_TRIANGLES),
m_camera((float)TAU_1_4, (float)m_screen_size.x / m_screen_size.y, 1.0f, 10.0f, vec3(0.0f, 0.0f, -5.0f)),
m_batch(add_batch(&m_shape, 2)),
m_pose_a(m_batch),
m_pose_b(m_batch)
{
add_prerenderable(&m_camera);
set_camera(&m_camera);
m_pose_a.update_position(glm::vec3(-4.0f, 0.0f, 0.0f));
m_pose_a.update_position(glm::vec3(4.0f, 0.0f, 0.0f));
}
void MyBuiltinSpecifiedScene::update(float delta_time, clock_t clock)
{
m_batch.reset();
m_pose_a.render();
}

View File

@ -0,0 +1,25 @@
#pragma once
#include <charcoal-builtin/SpecifiedScene.h>
#include <charcoal-builtin/BuiltinCamera3D.h>
#include <charcoal-builtin/SpecifiedPoseable.h>
using namespace charcoal;
using namespace charcoal::builtin;
class MyBuiltinSpecifiedScene : public specified::Scene
{
public:
MyBuiltinSpecifiedScene(Application& application);
void update(float delta_time, clock_t clock) override;
private:
basic::Renderable m_shape;
builtin::Camera3D m_camera;
specified::Batch& m_batch;
SpecifiedPoseable<specified::Batch> m_pose_a;
SpecifiedPoseable<specified::Batch> m_pose_b;
};

View File

@ -26,6 +26,7 @@ namespace charcoal
void translate(const vec3& translation);
void rotate(const vec3& axis, float angle);
void set_orientation_matrix(const mat4& orientation_matrix) { m_orientation_matrix = orientation_matrix; }
const mat4& get_orientation_matrix() const { return m_orientation_matrix; }
protected: