charcoal/Example/MyBuiltinTexturedScene.cpp

99 lines
3.1 KiB
C++
Raw Normal View History

#include "MyBuiltinTexturedScene.h"
#include <charcoal/constants.h>
#include <charcoal/TextureFactory.h>
#include <charcoal-builtin/MeshGenerator.h>
#include <charcoal-builtin/TextureGenerator.h>
#include <charcoal-builtin/GLUtil.h>
MyBuiltinTexturedScene::MyBuiltinTexturedScene(Application& application)
: Scene(application),
m_sprite_image(image_loader::load_file(IMAGE_PATH "uber.png")),
m_cube(
meshgenerator::gen_cube_pt<textured::Vertex, textured::Index>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
DRAW_TRIANGLES,
texturegenerator::gen_quick_cube_texture(),
texturegenerator::gen_quick_sampler()
),
m_sprite(
meshgenerator::gen_rect_pt<textured::Vertex, textured::Index>(DRAW_TRIANGLES, 2.0f, 2.0f),
DRAW_TRIANGLES,
TextureFactory::gen_image_texture(m_sprite_image),
texturegenerator::gen_quick_sampler()
),
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_cube_batch(&m_cube, 2),
m_sprite_batch(&m_sprite, 1)
{
m_pipeline.add_batch(&m_cube_batch);
m_pipeline.add_batch(&m_sprite_batch);
m_pipeline.set_camera(&m_camera);
}
void MyBuiltinTexturedScene::init()
{
m_cube_batch.init();
m_sprite_batch.init();
}
void MyBuiltinTexturedScene::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;
m_cube_pose_a.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_2 * delta_time);
m_cube_pose_a.update_position(vec3(3 * (float)cos(radians), 1.0f, 0.0f));
m_cube_pose_b.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_2 * delta_time);
m_cube_pose_b.update_position(vec3(-3 * (float)cos(radians), -1.0f, 0.0f));
vec3 camera_translation(0.0f, 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;
if (m_input_manager.is_key_down(GLFW_KEY_Q)) camera_translation.z -= 1;
if (m_input_manager.is_key_down(GLFW_KEY_E)) camera_translation.z += 1;
float camera_rotation = 0.0f;
if (m_input_manager.is_key_down(GLFW_KEY_Z)) camera_rotation += 1;
if (m_input_manager.is_key_down(GLFW_KEY_C)) camera_rotation -= 1;
m_camera.translate(camera_translation * delta_time);
m_camera.rotate(vec3(0.0f, 1.0f, 0.0f), camera_rotation * (float)TAU_1_8 * delta_time);
m_cube_batch.reset_rendered();
m_cube_batch.add_rendered(m_cube_pose_a);
m_cube_batch.add_rendered(m_cube_pose_b);
m_sprite_batch.reset_rendered();
m_sprite_batch.add_rendered(m_sprite_pose);
}
void MyBuiltinTexturedScene::prerender()
{
m_camera.prerender();
m_cube_batch.prerender();
m_sprite_batch.prerender();
}
void MyBuiltinTexturedScene::render()
{
glutil::clear_screen();
m_pipeline.render();
}