mirror of
https://github.com/SerenityOS/serenity
synced 2024-09-06 17:06:31 +00:00
Demos: Implement basic Lambertian lighting for the GLTeapot
The teapot now looks more realistic
This commit is contained in:
parent
7426c2fe45
commit
cf93512abe
|
@ -7,7 +7,8 @@
|
|||
|
||||
#include <LibGL/GL/gl.h>
|
||||
#include <LibGfx/Color.h>
|
||||
#include <stdlib.h>
|
||||
#include <LibGfx/Vector3.h>
|
||||
#include <LibGfx/Vector4.h>
|
||||
|
||||
#include "Mesh.h"
|
||||
|
||||
|
@ -15,10 +16,10 @@ const Color colors[] {
|
|||
Color::Red,
|
||||
Color::Green,
|
||||
Color::Blue,
|
||||
Color::Blue,
|
||||
Color::Magenta,
|
||||
Color::White,
|
||||
Color::Yellow,
|
||||
Color::Cyan,
|
||||
Color::White
|
||||
};
|
||||
|
||||
Mesh::Mesh(Vector<Vertex> vertices, Vector<Triangle> triangles)
|
||||
|
@ -29,35 +30,62 @@ Mesh::Mesh(Vector<Vertex> vertices, Vector<Triangle> triangles)
|
|||
|
||||
void Mesh::draw()
|
||||
{
|
||||
u32 color_index = 0;
|
||||
Color cur_color;
|
||||
// Light direction
|
||||
const FloatVector3 light_direction(1.f, 1.f, 1.f);
|
||||
|
||||
for (const auto& triangle : m_triangle_list) {
|
||||
cur_color = colors[color_index];
|
||||
// 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);
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
glColor4ub(cur_color.red(), cur_color.green(), cur_color.blue(), 255);
|
||||
for (u32 i = 0; i < m_triangle_list.size(); i++) {
|
||||
const auto& triangle = m_triangle_list[i];
|
||||
|
||||
// Vertex 1
|
||||
glVertex3f(
|
||||
const FloatVector3 vertex_a(
|
||||
m_vertex_list.at(triangle.a).x,
|
||||
m_vertex_list.at(triangle.a).y,
|
||||
m_vertex_list.at(triangle.a).z);
|
||||
|
||||
// Vertex 2
|
||||
glVertex3f(
|
||||
const FloatVector3 vertex_b(
|
||||
m_vertex_list.at(triangle.b).x,
|
||||
m_vertex_list.at(triangle.b).y,
|
||||
m_vertex_list.at(triangle.b).z);
|
||||
|
||||
// Vertex 3
|
||||
glVertex3f(
|
||||
const FloatVector3 vertex_c(
|
||||
m_vertex_list.at(triangle.c).x,
|
||||
m_vertex_list.at(triangle.c).y,
|
||||
m_vertex_list.at(triangle.c).z);
|
||||
|
||||
glEnd();
|
||||
// Compute the triangle normal
|
||||
const FloatVector3 vec_ab = vertex_b - vertex_a;
|
||||
const FloatVector3 vec_ac = vertex_c - vertex_a;
|
||||
const FloatVector3 normal = vec_ab.cross(vec_ac).normalized();
|
||||
|
||||
color_index = ((color_index + 1) % 7);
|
||||
// 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());
|
||||
|
||||
// Vertex 1
|
||||
glVertex3f(
|
||||
m_vertex_list.at(m_triangle_list[i].a).x,
|
||||
m_vertex_list.at(m_triangle_list[i].a).y,
|
||||
m_vertex_list.at(m_triangle_list[i].a).z);
|
||||
|
||||
// Vertex 2
|
||||
glVertex3f(
|
||||
m_vertex_list.at(m_triangle_list[i].b).x,
|
||||
m_vertex_list.at(m_triangle_list[i].b).y,
|
||||
m_vertex_list.at(m_triangle_list[i].b).z);
|
||||
|
||||
// Vertex 3
|
||||
glVertex3f(
|
||||
m_vertex_list.at(m_triangle_list[i].c).x,
|
||||
m_vertex_list.at(m_triangle_list[i].c).y,
|
||||
m_vertex_list.at(m_triangle_list[i].c).z);
|
||||
|
||||
glEnd();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue