1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:47:44 +00:00

3DFileViewer: Replace lambertian lighting with GL Lighting

This commit is contained in:
Jesse Buhagiar 2022-01-10 01:13:33 +11:00 committed by Linus Groh
parent 9aae648482
commit b397db9948
2 changed files with 39 additions and 13 deletions

View file

@ -33,13 +33,6 @@ Mesh::Mesh(Vector<Vertex> vertices, Vector<TexCoord> tex_coords, Vector<Vertex>
void Mesh::draw(float uv_scale) void Mesh::draw(float uv_scale)
{ {
// Light direction
const FloatVector3 light_direction(1.f, 1.f, 1.f);
// Mesh color
const FloatVector4 mesh_ambient_color(0.2f, 0.2f, 0.2f, 1.f);
const FloatVector4 mesh_diffuse_color(0.6f, 0.6f, 0.6f, 1.f);
for (u32 i = 0; i < m_triangle_list.size(); i++) { for (u32 i = 0; i < m_triangle_list.size(); i++) {
const auto& triangle = m_triangle_list[i]; const auto& triangle = m_triangle_list[i];
@ -83,17 +76,14 @@ void Mesh::draw(float uv_scale)
normal = vec_ab.cross(vec_ac).normalized(); normal = vec_ab.cross(vec_ac).normalized();
} }
// Compute lighting with a Lambertian color model
const auto light_intensity = max(light_direction.dot(normal), 0.f);
const FloatVector4 color = mesh_ambient_color
+ mesh_diffuse_color * light_intensity;
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
glColor4f(color.x(), color.y(), color.z(), color.w());
if (is_textured()) if (is_textured())
glTexCoord2f(m_tex_coords.at(triangle.tex_coord_index0).u * uv_scale, (1.0f - m_tex_coords.at(triangle.tex_coord_index0).v) * uv_scale); glTexCoord2f(m_tex_coords.at(triangle.tex_coord_index0).u * uv_scale, (1.0f - m_tex_coords.at(triangle.tex_coord_index0).v) * uv_scale);
// Upload the face normal
glNormal3f(normal.x(), normal.y(), normal.z());
// Vertex 1 // Vertex 1
glVertex3f( glVertex3f(
m_vertex_list.at(triangle.a).x, m_vertex_list.at(triangle.a).x,

View file

@ -69,6 +69,12 @@ private:
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
// Set projection matrix // Set projection matrix
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); glLoadIdentity();
@ -159,6 +165,7 @@ void GLContextWidget::mousewheel_event(GUI::MouseEvent& event)
void GLContextWidget::timer_event(Core::TimerEvent&) void GLContextWidget::timer_event(Core::TimerEvent&)
{ {
auto timer = Core::ElapsedTimer::start_new(); auto timer = Core::ElapsedTimer::start_new();
static unsigned int light_counter = 0;
glCallList(m_init_list); glCallList(m_init_list);
@ -175,6 +182,23 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
glRotatef(m_angle_y, 0, 1, 0); glRotatef(m_angle_y, 0, 1, 0);
glRotatef(m_angle_z, 0, 0, 1); glRotatef(m_angle_z, 0, 0, 1);
glPushMatrix();
glLoadIdentity();
// Disco time ;)
GLfloat const light0_position[4] = { -4.0f, 0.0f, 0.0f, 0.0f };
GLfloat const light0_diffuse[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
GLfloat const light1_position[4] = { 4.0f, 0.0f, 0.0f, 0.0f };
GLfloat const light1_diffuse[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
GLfloat const light2_position[4] = { 0.0f, 5.0f, 0.0f, 0.0f };
GLfloat const light2_diffuse[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
glLightfv(GL_LIGHT0, GL_POSITION, &light0_position[0]);
glLightfv(GL_LIGHT0, GL_DIFFUSE, &light0_diffuse[0]);
glLightfv(GL_LIGHT1, GL_POSITION, &light1_position[0]);
glLightfv(GL_LIGHT1, GL_DIFFUSE, &light1_diffuse[0]);
glLightfv(GL_LIGHT2, GL_POSITION, &light2_position[0]);
glLightfv(GL_LIGHT2, GL_DIFFUSE, &light2_diffuse[0]);
glPopMatrix();
if (m_texture_enabled) { if (m_texture_enabled) {
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_wrap_s_mode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_wrap_s_mode);
@ -195,6 +219,18 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
auto frame_rate = render_time > 0 ? 1000 / render_time : 0; auto frame_rate = render_time > 0 ? 1000 / render_time : 0;
m_stats->set_text(String::formatted("{:.0f} fps, {:.1f} ms", frame_rate, render_time)); m_stats->set_text(String::formatted("{:.0f} fps, {:.1f} ms", frame_rate, render_time));
m_accumulated_time = 0; m_accumulated_time = 0;
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);
light_counter++;
if ((light_counter % 3) == 0)
glDisable(GL_LIGHT0);
else if ((light_counter % 3) == 1)
glDisable(GL_LIGHT1);
else
glDisable(GL_LIGHT2);
} }
update(); update();