charcoal/OpenGLEngine/MyBuiltinLitScene.cpp
elipzer 1eea92a3af LitScene Phong Lighting Complete
Now, LitScene allows for simple lighting. (Shadows still to come).
Ambient, specular, and diffuse lighting available and each mesh's
vertex can define a material that defines its reflectivity. An
example scene was added to the MyApplication as the scene for the
6 button.
2018-09-15 03:46:42 -04:00

70 lines
2.2 KiB
C++

#include "MyBuiltinLitScene.h"
#include "MeshGenerator.h"
#include "constants.h"
MyBuiltinLitScene::MyBuiltinLitScene(Application& application)
: LitScene(application),
m_shape(
meshgenerator::set_material<LitVertex, LitIndex>(
meshgenerator::gen_cube_pn<LitVertex, LitIndex>(DRAW_TRIANGLES, 2.0f, 2.0f, 2.0f),
Material(1.0f, 1.0f, 0.2f, 1.0f)
), DrawMode::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, 1))
{
add_prerenderable(&m_camera);
set_camera(&m_camera);
add_light(
Position(0.0f, 2.0f, -2.0f),
Light::Power(0.2f, 1.0f, 1.0f),
ColorRGB(1.0f, 1.0f, 1.0f),
ColorRGB(1.0f, 1.0f, 1.0f),
ColorRGB(1.0f, 1.0f, 1.0f),
Light::Fade(1.0f, 0.1f, 0.01f)
);
}
void MyBuiltinLitScene::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.rotate(glm::normalize(vec3(1.0f, 1.0f, 0.0f)), (float)TAU_1_8 * delta_time);
pose.update_position(vec3(3 * (float)cos(radians), 0.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);
}