Skip to content

Commit

Permalink
3DFileViewer: Replace lambertian lighting with GL Lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
Quaker762 authored and linusg committed Jan 12, 2022
1 parent 9aae648 commit b397db9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
16 changes: 3 additions & 13 deletions Userland/Applications/3DFileViewer/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ Mesh::Mesh(Vector<Vertex> vertices, Vector<TexCoord> tex_coords, Vector<Vertex>

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++) {
const auto& triangle = m_triangle_list[i];

Expand Down Expand Up @@ -83,17 +76,14 @@ void Mesh::draw(float uv_scale)
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);
glColor4f(color.x(), color.y(), color.z(), color.w());

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);

// Upload the face normal
glNormal3f(normal.x(), normal.y(), normal.z());

// Vertex 1
glVertex3f(
m_vertex_list.at(triangle.a).x,
Expand Down
36 changes: 36 additions & 0 deletions Userland/Applications/3DFileViewer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ class GLContextWidget final : public GUI::Frame {
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);

// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHT2);

// Set projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
Expand Down Expand Up @@ -159,6 +165,7 @@ void GLContextWidget::mousewheel_event(GUI::MouseEvent& event)
void GLContextWidget::timer_event(Core::TimerEvent&)
{
auto timer = Core::ElapsedTimer::start_new();
static unsigned int light_counter = 0;

glCallList(m_init_list);

Expand All @@ -175,6 +182,23 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
glRotatef(m_angle_y, 0, 1, 0);
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) {
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_wrap_s_mode);
Expand All @@ -195,6 +219,18 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
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_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();
Expand Down

0 comments on commit b397db9

Please sign in to comment.