Scene renders. Need to do collision and whatnot

This commit is contained in:
elipzer 2018-10-17 19:21:58 -04:00
parent fd4cd2a407
commit d2afef5090
7 changed files with 245 additions and 1 deletions

View File

@ -650,6 +650,79 @@ namespace charcoal
// TODO: Mesh<PNTVertex, Index>* gen_cube(const DrawMode& draw_mode, float width, float height, float depth);
template <typename VertexType, typename IndexType>
Mesh<VertexType, IndexType>* gen_rect_p(const DrawMode& draw_mode, float width, float height, float offset_x = 0.0f, float offset_y = 0.0f)
{
float half_width = width / 2.0f;
float half_height = height / 2.0f;
Mesh<VertexType, IndexType>* mesh;
Position pos0(offset_x - half_width, offset_y - half_height, 0.0f);
Position pos1(offset_x - half_width, offset_y + half_height, 0.0f);
Position pos2(offset_x + half_width, offset_y - half_height, 0.0f);
Position pos3(offset_x + half_width, offset_y + half_height, 0.0f);
switch (draw_mode)
{
case DrawMode::DRAW_TRIANGLES:
mesh = MeshFactory<VertexType, IndexType>::create_mesh(4, 6);
mesh->vertices[0].set_position(pos0);
mesh->vertices[1].set_position(pos1);
mesh->vertices[2].set_position(pos2);
mesh->vertices[3].set_position(pos3);
mesh->indices[0] = 1;
mesh->indices[1] = 3;
mesh->indices[2] = 0;
mesh->indices[3] = 0;
mesh->indices[4] = 3;
mesh->indices[5] = 2;
return mesh;
case DrawMode::DRAW_TRIANGLE_STRIP:
mesh = MeshFactory<VertexType, IndexType>::create_mesh(4, 4);
mesh->vertices[0].set_position(pos0);
mesh->vertices[1].set_position(pos1);
mesh->vertices[2].set_position(pos2);
mesh->vertices[3].set_position(pos3);
mesh->indices[0] = 1;
mesh->indices[1] = 3;
mesh->indices[2] = 0;
mesh->indices[3] = 2;
return mesh;
case DrawMode::DRAW_TRIANGLE_FAN:
mesh = MeshFactory<VertexType, IndexType>::create_mesh(4, 4);
mesh->vertices[0].set_position(pos0);
mesh->vertices[1].set_position(pos1);
mesh->vertices[2].set_position(pos2);
mesh->vertices[3].set_position(pos3);
mesh->indices[0] = 1;
mesh->indices[1] = 3;
mesh->indices[2] = 2;
mesh->indices[3] = 0;
return mesh;
case DrawMode::DRAW_POINTS: // TODO: Point, lines, line_strip, line_loop
case DrawMode::DRAW_LINES:
case DrawMode::DRAW_LINE_STRIP:
case DrawMode::DRAW_LINE_LOOP:
case DrawMode::DRAW_LINE_STRIP_ADJACENCY:
case DrawMode::DRAW_LINES_ADJACENCY:
case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY:
case DrawMode::DRAW_TRIANGLES_ADJACENCY:
case DrawMode::DRAW_PATCHES:
default:
throw EXCEPTION("Unable to gen_rect_pt for current draw mode: " + std::to_string(draw_mode));
}
}
template <typename VertexType, typename IndexType>
Mesh<VertexType, IndexType>* gen_rect_pt(const DrawMode& draw_mode, float width, float height, float offset_x = 0.0f, float offset_y = 0.0f)
{

View File

@ -26,6 +26,7 @@
<ClCompile Include="MyBuiltinCubeScene.cpp" />
<ClCompile Include="MyBuiltinLitScene.cpp" />
<ClCompile Include="MyBuiltinTexturedScene.cpp" />
<ClCompile Include="MyPongScene.cpp" />
<ClCompile Include="MySimple2DScene.cpp" />
<ClCompile Include="MySimple3DScene.cpp" />
<ClCompile Include="MySimpleCubeScene.cpp" />
@ -38,6 +39,7 @@
<ClInclude Include="MyBuiltinCubeScene.h" />
<ClInclude Include="MyBuiltinTexturedScene.h" />
<ClInclude Include="MyBuiltinLitScene.h" />
<ClInclude Include="MyPongScene.h" />
<ClInclude Include="MySimple2DScene.h" />
<ClInclude Include="MySimple3DScene.h" />
<ClInclude Include="MySimpleCubeScene.h" />

View File

@ -45,6 +45,9 @@
<ClCompile Include="MyBuiltinLitScene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MyPongScene.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="MyApplication.h">
@ -80,5 +83,8 @@
<ClInclude Include="MyBuiltinLitScene.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MyPongScene.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -8,7 +8,8 @@ MyApplication::MyApplication(int width, int height)
m_simple_cube_scene(*this),
m_builtin_basic_cube_scene(*this),
m_builtin_textured_scene(*this),
m_builtin_lit_scene(*this)
m_builtin_lit_scene(*this),
m_pong_scene(*this)
{}
void MyApplication::init()
@ -20,6 +21,7 @@ void MyApplication::init()
m_builtin_basic_cube_scene.init();
m_builtin_textured_scene.init();
m_builtin_lit_scene.init();
m_pong_scene.init();
m_p_current_scene = &m_basic_scene;
m_p_current_scene->use();
@ -55,6 +57,10 @@ void MyApplication::update(float delta_time, clock_t clock)
{
swap_scene(&m_builtin_lit_scene);
}
else if (m_glfw_input_manager.is_key_pressed(GLFW_KEY_8))
{
swap_scene(&m_pong_scene);
}
m_p_current_scene->update(delta_time, clock);
}

View File

@ -10,6 +10,7 @@
#include "MyBuiltinCubeScene.h"
#include "MyBuiltinTexturedScene.h"
#include "MyBuiltinLitScene.h"
#include "MyPongScene.h"
using namespace charcoal;
@ -40,5 +41,6 @@ private:
MyBuiltinCubeScene m_builtin_basic_cube_scene;
MyBuiltinTexturedScene m_builtin_textured_scene;
MyBuiltinLitScene m_builtin_lit_scene;
MyPongScene m_pong_scene;
};

102
Example/MyPongScene.cpp Normal file
View File

@ -0,0 +1,102 @@
#include "MyPongScene.h"
#include <charcoal/constants.h>
#include <charcoal-builtin/MeshGenerator.h>
#include <charcoal-builtin/GLUtil.h>
#define OUTLINE_THICKNESS 25.0f
#define OUTLINE_OFFSET 25.0f
#define OUTLINE_INWARDS OUTLINE_OFFSET + OUTLINE_THICKNESS
#define PADDLE_THICKNESS 25.0f
#define PADDLE_HEIGHT 150.0f
#define BALL_THICKNESS 25.0f
#define PADDLE_SPEED 5.0f
MyPongScene::MyPongScene(Application& application)
: Scene(application),
m_outline_column(meshgenerator::gen_rect_p<basic::Vertex, basic::Index>(DRAW_TRIANGLES, m_screen_size.x - 2 * OUTLINE_THICKNESS, OUTLINE_THICKNESS), DRAW_TRIANGLES),
m_outline_row(meshgenerator::gen_rect_p<basic::Vertex, basic::Index>(DRAW_TRIANGLES, OUTLINE_THICKNESS, m_screen_size.y - 2 * OUTLINE_THICKNESS), DRAW_TRIANGLES),
m_ball(meshgenerator::gen_rect_p<basic::Vertex, basic::Index>(DRAW_TRIANGLES, BALL_THICKNESS, BALL_THICKNESS), DRAW_TRIANGLES),
m_paddle(meshgenerator::gen_rect_p<basic::Vertex, basic::Index>(DRAW_TRIANGLES, PADDLE_THICKNESS, PADDLE_HEIGHT), DRAW_TRIANGLES),
m_outline_column_batch(&m_outline_column, 2),
m_outline_row_batch(&m_outline_row, 2),
m_ball_batch(&m_ball, 1),
m_paddle_batch(&m_paddle, 2),
m_outline_top_pose(glm::vec3(0.0f, (m_screen_size.y - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET, 0.0f)),
m_outline_bottom_pose(glm::vec3(0.0f, -((m_screen_size.y - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET), 0.0f)),
m_outline_left_pose(glm::vec3(-((m_screen_size.x - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET), 0.0f, 0.0f)),
m_outline_right_pose(glm::vec3((m_screen_size.x - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET, 0.0f, 0.0f)),
m_ball_pose(glm::vec3(0.0f, 0.0f, 0.0f)),
m_paddle_left_pose(glm::vec3(-(m_screen_size.x / 2.0f - OUTLINE_INWARDS - 3.5f * PADDLE_THICKNESS), 0.0f, 0.0f)),
m_paddle_right_pose(glm::vec3(m_screen_size.x / 2.0f - OUTLINE_INWARDS - 3.5f * PADDLE_THICKNESS, 0.0f, 0.0f)),
m_camera(m_screen_size)
{
m_pipeline.add_batch(&m_outline_column_batch);
m_pipeline.add_batch(&m_outline_row_batch);
m_pipeline.add_batch(&m_ball_batch);
m_pipeline.add_batch(&m_paddle_batch);
m_pipeline.set_camera(&m_camera);
}
void MyPongScene::init()
{
// Batches
m_outline_column_batch.init();
m_outline_row_batch.init();
m_ball_batch.init();
m_paddle_batch.init();
// Set these once here since they will never change
m_outline_column_batch.reset_rendered();
m_outline_column_batch.add_rendered(m_outline_top_pose);
m_outline_column_batch.add_rendered(m_outline_bottom_pose);
m_outline_row_batch.reset_rendered();
m_outline_row_batch.add_rendered(m_outline_left_pose);
m_outline_row_batch.add_rendered(m_outline_right_pose);
// Ball
m_ball_speed = 250.0f;
m_ball_direction = glm::normalize(glm::vec2(1.0f, 1.0f));
}
void MyPongScene::update(float delta_time, clock_t clock)
{
m_ball_pose.translate(glm::vec3(m_ball_direction * m_ball_speed * delta_time, 0.0f));
m_ball_batch.reset_rendered();
m_ball_batch.add_rendered(m_ball_pose);
m_paddle_batch.reset_rendered();
m_paddle_batch.add_rendered(m_paddle_left_pose);
m_paddle_batch.add_rendered(m_paddle_right_pose);
}
void MyPongScene::prerender()
{
m_camera.prerender();
m_outline_column_batch.prerender();
m_outline_row_batch.prerender();
m_ball_batch.prerender();
m_paddle_batch.prerender();
}
void MyPongScene::render()
{
glutil::clear_screen();
m_pipeline.render();
}

53
Example/MyPongScene.h Normal file
View File

@ -0,0 +1,53 @@
#pragma once
#include <charcoal/Poseable.h>
#include <charcoal/Scene.h>
#include <charcoal-builtin/BuiltinCamera2D.h>
#include <charcoal-builtin/BasicPipeline.h>
using namespace charcoal;
using namespace charcoal::builtin;
class MyPongScene : public Scene
{
public:
MyPongScene(Application& application);
void init() override;
void use() override {}
void unuse() override {}
void update(float delta_time, clock_t clock) override;
void prerender() override;
void render() override;
private:
basic::Renderable m_outline_column;
basic::Renderable m_outline_row;
basic::Renderable m_ball;
basic::Renderable m_paddle;
basic::Batch m_outline_column_batch;
basic::Batch m_outline_row_batch;
basic::Batch m_ball_batch;
basic::Batch m_paddle_batch;
Poseable m_outline_top_pose;
Poseable m_outline_bottom_pose;
Poseable m_outline_left_pose;
Poseable m_outline_right_pose;
Poseable m_ball_pose;
Poseable m_paddle_left_pose;
Poseable m_paddle_right_pose;
builtin::Camera2D m_camera;
basic::Pipeline m_pipeline;
float m_ball_speed;
glm::vec2 m_ball_direction;
};