More builtin abstraction

Got the builtin basicscene to render. For some reason, it seems
like the model matrices are not working correctly. The two shapes
that are supposed to be rendering on screen are not moving around
as they should be.
This commit is contained in:
elipzer 2018-09-13 19:21:34 -04:00
parent a933c19fa9
commit d63f341d89
15 changed files with 249 additions and 100 deletions

View File

@ -0,0 +1,20 @@
#include "AutoPrerenderingScene.h"
namespace charcoal
{
namespace builtin
{
void AutoPrerenderingScene::prerender()
{
for (auto iter = m_prerenderables.begin(); iter != m_prerenderables.end(); ++iter)
{
(*iter)->prerender();
}
}
void AutoPrerenderingScene::add_prerenderable(Prerenderable* prerenderable)
{
m_prerenderables.push_back(prerenderable);
}
}
}

View File

@ -0,0 +1,28 @@
#pragma once
#include <vector>
#include "Application.h"
#include "Scene.h"
#include "Prerenderable.h"
namespace charcoal
{
namespace builtin
{
class AutoPrerenderingScene : public Scene
{
public:
AutoPrerenderingScene(Application& application) : Scene(application) {}
virtual ~AutoPrerenderingScene() {}
void prerender() override;
protected:
void add_prerenderable(Prerenderable* p_prerenderable);
private:
std::vector<Prerenderable*> m_prerenderables;
};
}
}

View File

@ -3,7 +3,7 @@
#include <vector>
#include "MeshTypes.h"
#include "Batch.h"
#include "BuiltinBatch.h"
#include "Poseable.h"
namespace charcoal

View File

@ -9,26 +9,14 @@ namespace charcoal
{
namespace builtin
{
BasicScene::BasicScene(Application& application)
: Scene(application),
m_shape(MeshFactory<BasicVertex, BasicIndex>::gen(
DrawMode::DRAW_TRIANGLES,
BasicVertex({ -50.0f, 50.0f, 0.0f }),
BasicVertex({ 50.0f, 150.0f, 0.0f }),
BasicVertex({ -100.0f, -50.0f, 0.0f }),
BasicVertex({ 100.0f, -50.0f, 0.0f })
), DrawMode::DRAW_TRIANGLES),
m_batch(&m_shape, 2),
m_camera(m_screen_size)
{}
BasicScene::~BasicScene() {}
void BasicScene::init()
{
sizeof(BasicVertex);
sizeof(vec3);
m_batch.init();
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
BasicBatch& batch = *iter;
batch.init();
add_prerenderable(&batch);
}
}
void BasicScene::use()
@ -43,55 +31,26 @@ namespace charcoal
}
void BasicScene::update(float delta_time, clock_t clock)
{
float brightness;
float radians;
clock_t c;
const clock_t intervals = 512 * CLOCKS_PER_SEC / 100;
const clock_t half_interval = 256 * CLOCKS_PER_SEC / 100;
c = clock % intervals;
if (c < half_interval)
brightness = (float)c / half_interval;
else
brightness = (float)(intervals - c) / half_interval;
radians = (float)TAU * c / intervals;
{
Poseable& pose = m_batch.get_pose(0);
pose.update_position(vec3(cos(radians) * 50, 0.0f, 0.0f));
}
{
Poseable& pose = m_batch.get_pose(1);
pose.update_position(vec3(0.0f, sin(radians) * 50, 0.0f));
}
vec2 camera_translation(0.0f, 0.0f);
if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1;
if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1;
m_camera.translate(camera_translation * delta_time * 100.0f);
}
void BasicScene::prerender()
{
m_camera.prerender();
m_batch.prerender();
}
void BasicScene::render()
{
// TODO: This is not rendering :(
glutil::clear_screen();
m_shader_program.use();
glutil::uniform_matrix(0, m_camera.get_world_to_view_matrix());
m_batch.render();
glutil::uniform_matrix(0, m_p_camera->get_world_to_view_matrix());
for (auto iter = m_batches.begin(); iter != m_batches.end(); ++iter)
{
iter->render();
}
}
BasicBatch& BasicScene::add_batch(BasicRenderable* renderable, int element_count)
{
return add_batch(renderable, element_count, element_count);
}
BasicBatch& BasicScene::add_batch(BasicRenderable* renderable, int element_count, int element_render_count)
{
m_batches.emplace_back(renderable, element_count, element_render_count);
return m_batches.back();
}
}
}

View File

@ -1,6 +1,8 @@
#pragma once
#include "Scene.h"
#include <vector>
#include "AutoPrerenderingScene.h"
#include "BasicShaderProgram.h"
#include "BasicBatch.h"
@ -12,11 +14,11 @@ namespace charcoal
{
namespace builtin
{
class BasicScene : public Scene
class BasicScene : public AutoPrerenderingScene
{
public:
BasicScene(Application& application);
~BasicScene();
BasicScene(Application& application) : AutoPrerenderingScene(application) {}
virtual ~BasicScene() {}
void init() override;
@ -24,17 +26,19 @@ namespace charcoal
void unuse() override;
void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override;
protected:
void set_camera(const Camera* p_camera) { m_p_camera = p_camera; }
BasicBatch& add_batch(BasicRenderable* renderable, int element_count);
BasicBatch& add_batch(BasicRenderable* renderable, int element_count, int element_render_count);
private:
BasicShaderProgram m_shader_program;
BasicRenderable m_shape;
BasicBatch m_batch;
Camera2D m_camera;
std::vector<BasicBatch> m_batches;
const Camera* m_p_camera = nullptr;
};
}
}

View File

@ -0,0 +1,22 @@
#pragma once
#include "Prerenderable.h"
#include "Batch.h"
namespace charcoal
{
namespace builtin
{
template <typename VertexType, typename IndexType, int element_buffer_count, typename Renderable = Renderable<VertexType, IndexType> >
class Batch : public Prerenderable, public charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable>
{
public:
Batch(
const Renderable* renderable,
const charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable>::SizeType& element_render_count
) : charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable>(renderable, element_render_count) {}
void prerender() override { charcoal::Batch<VertexType, IndexType, element_buffer_count, Renderable>::prerender(); }
};
}
}

View File

@ -0,0 +1,21 @@
#pragma once
#include "Prerenderable.h"
#include "Camera2D.h"
namespace charcoal
{
namespace builtin
{
class Camera2D : public Prerenderable, public charcoal::Camera2D
{
public:
Camera2D(const vec2& size, const vec2& position = vec2(0.0f, 0.0f))
: charcoal::Camera2D(size, position) {}
Camera2D(const vec3& size = vec3(2.0f, 2.0f, 2.0f), const vec3& position = vec3(0.0f, 0.0f, 0.0f))
: charcoal::Camera2D(size, position) {}
void prerender() override { charcoal::Camera2D::prerender(); }
};
}
}

View File

@ -31,25 +31,5 @@ namespace charcoal
typedef Index BasicIndex;
typedef Renderable<BasicVertex, BasicIndex> BasicRenderable;
struct PNVertex
{
void set_position(const Position& position) { this->position = position; }
void set_normal(const Normal& normal) { this->normal = normal; }
Position position;
Normal normal;
};
struct PNTVertex
{
void set_position(const Position& position) { this->position = position; }
void set_normal(const Normal& normal) { this->normal = normal; }
void set_uv(const UV& uv) { this->uv = uv; }
Position position;
Normal normal;
UV uv;
};
}
}

View File

@ -6,7 +6,7 @@ MyApplication::MyApplication(int width, int height)
m_simple_2d_scene(*this),
m_simple_3d_scene(*this),
m_simple_cube_scene(*this),
m_builtin_basic_scene(*this)
m_builtin_basic_cube_scene(*this)
{}
void MyApplication::init()
@ -15,7 +15,7 @@ void MyApplication::init()
m_simple_2d_scene.init();
m_simple_3d_scene.init();
m_simple_cube_scene.init();
m_builtin_basic_scene.init();
m_builtin_basic_cube_scene.init();
m_p_current_scene = &m_basic_scene;
m_p_current_scene->use();
@ -41,7 +41,7 @@ void MyApplication::update(float delta_time, clock_t clock)
}
else if (m_glfw_input_manager.is_key_pressed(GLFW_KEY_5))
{
swap_scene(&m_builtin_basic_scene);
swap_scene(&m_builtin_basic_cube_scene);
}
m_p_current_scene->update(delta_time, clock);
}

View File

@ -5,7 +5,7 @@
#include "MySimple2DScene.h"
#include "MySimple3DScene.h"
#include "MySimpleCubeScene.h"
#include "BasicScene.h"
#include "MyBuiltinCubeScene.h"
using namespace charcoal;
@ -34,6 +34,6 @@ private:
MySimple2DScene m_simple_2d_scene;
MySimple3DScene m_simple_3d_scene;
MySimpleCubeScene m_simple_cube_scene;
builtin::BasicScene m_builtin_basic_scene;
MyBuiltinCubeScene m_builtin_basic_cube_scene;
};

View File

@ -0,0 +1,55 @@
#include "MyBuiltinCubeScene.h"
#include "MeshFactory.h"
MyBuiltinCubeScene::MyBuiltinCubeScene(Application& application)
: BasicScene(application),
m_shape(MeshFactory<BasicVertex, BasicIndex>::gen(
DrawMode::DRAW_TRIANGLES,
BasicVertex({ -50.0f, 50.0f, 0.0f }),
BasicVertex({ 50.0f, 150.0f, 0.0f }),
BasicVertex({ -100.0f, -50.0f, 0.0f }),
BasicVertex({ 100.0f, -50.0f, 0.0f })
), DrawMode::DRAW_TRIANGLES),
m_camera(m_screen_size),
m_batch(add_batch(&m_shape, 2))
{
set_camera(&m_camera);
}
void MyBuiltinCubeScene::update(float delta_time, clock_t clock)
{
float brightness;
float radians;
clock_t c;
const clock_t intervals = 512 * CLOCKS_PER_SEC / 100;
const clock_t half_interval = 256 * CLOCKS_PER_SEC / 100;
c = clock % intervals;
if (c < half_interval)
brightness = (float)c / half_interval;
else
brightness = (float)(intervals - c) / half_interval;
radians = (float)TAU * c / intervals;
{
Poseable& pose = m_batch.get_pose(0);
pose.update_position(vec3(cos(radians) * 50, 0.0f, 0.0f));
}
{
Poseable& pose = m_batch.get_pose(1);
pose.update_position(vec3(0.0f, sin(radians) * 50, 0.0f));
}
vec2 camera_translation(0.0f, 0.0f);
if (m_input_manager.is_key_down(GLFW_KEY_W)) camera_translation.y += 1;
if (m_input_manager.is_key_down(GLFW_KEY_S)) camera_translation.y -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_A)) camera_translation.x -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_D)) camera_translation.x += 1;
m_camera.translate(camera_translation * delta_time * 100.0f);
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "BasicScene.h"
#include "Camera2D.h"
using namespace charcoal;
using namespace charcoal::builtin;
class MyBuiltinCubeScene : public BasicScene
{
public:
MyBuiltinCubeScene(Application& application);
void update(float delta_time, clock_t clock) override;
private:
BasicRenderable m_shape;
Camera2D m_camera;
BasicBatch m_batch;
};

View File

@ -152,11 +152,13 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Application.cpp" />
<ClCompile Include="AutoPrerenderingScene.cpp" />
<ClCompile Include="BasicBatch.cpp" />
<ClCompile Include="BasicScene.cpp" />
<ClCompile Include="Camera2D.cpp" />
<ClCompile Include="Camera3D.cpp" />
<ClCompile Include="FPS.cpp" />
<ClCompile Include="MyBuiltinCubeScene.cpp" />
<ClCompile Include="VertexFragmentShaderProgram.cpp" />
<ClCompile Include="GLFWInputManager.cpp" />
<ClCompile Include="GLUtil.cpp" />
@ -179,10 +181,13 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="Application.h" />
<ClInclude Include="AutoPrerenderingScene.h" />
<ClInclude Include="BasicBatch.h" />
<ClInclude Include="BasicScene.h" />
<ClInclude Include="BasicShaderProgram.h" />
<ClInclude Include="Batch.h" />
<ClInclude Include="BuiltinBatch.h" />
<ClInclude Include="BuiltinCamera2D.h" />
<ClInclude Include="Camera.h" />
<ClInclude Include="Camera2D.h" />
<ClInclude Include="Camera3D.h" />
@ -190,6 +195,8 @@
<ClInclude Include="DrawMode.h" />
<ClInclude Include="Exception.h" />
<ClInclude Include="FPS.h" />
<ClInclude Include="MyBuiltinCubeScene.h" />
<ClInclude Include="Prerenderable.h" />
<ClInclude Include="VertexFragmentShaderProgram.h" />
<ClInclude Include="GLFWInputManager.h" />
<ClInclude Include="GLUtil.h" />

View File

@ -129,6 +129,12 @@
<ClCompile Include="BasicScene.cpp">
<Filter>Source Files\Engine\builtin</Filter>
</ClCompile>
<ClCompile Include="MyBuiltinCubeScene.cpp">
<Filter>Source Files\Example\Application</Filter>
</ClCompile>
<ClCompile Include="AutoPrerenderingScene.cpp">
<Filter>Source Files\Engine\builtin</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Exception.h">
@ -236,6 +242,21 @@
<ClInclude Include="BasicScene.h">
<Filter>Header Files\Engine\builtin</Filter>
</ClInclude>
<ClInclude Include="MyBuiltinCubeScene.h">
<Filter>Header Files\Example\Application</Filter>
</ClInclude>
<ClInclude Include="Prerenderable.h">
<Filter>Header Files\Engine\builtin</Filter>
</ClInclude>
<ClInclude Include="BuiltinCamera2D.h">
<Filter>Header Files\Engine\builtin</Filter>
</ClInclude>
<ClInclude Include="AutoPrerenderingScene.h">
<Filter>Header Files\Engine\builtin</Filter>
</ClInclude>
<ClInclude Include="BuiltinBatch.h">
<Filter>Header Files\Engine\builtin</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="MySimpleVS.glsl">

View File

@ -0,0 +1,13 @@
#pragma once
namespace charcoal
{
namespace builtin
{
class Prerenderable
{
public:
virtual void prerender() = 0;
};
}
}