Mesh.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2021, Mathieu Gaillard <gaillard.mathieu.39@gmail.com>
  4. * Copyright (c) 2021, Pedro Pereira <pmh.pereira@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <LibGL/GL/gl.h>
  9. #include <LibGfx/Color.h>
  10. #include <LibGfx/Vector3.h>
  11. #include <LibGfx/Vector4.h>
  12. #include "Mesh.h"
  13. const Color colors[] {
  14. Color::Red,
  15. Color::Green,
  16. Color::Blue,
  17. Color::Magenta,
  18. Color::Yellow,
  19. Color::Cyan,
  20. Color::White
  21. };
  22. Mesh::Mesh(Vector<Vertex> vertices, Vector<TexCoord> tex_coords, Vector<Vertex> normals, Vector<Triangle> triangles)
  23. : m_vertex_list(move(vertices))
  24. , m_tex_coords(move(tex_coords))
  25. , m_normal_list(move(normals))
  26. , m_triangle_list(move(triangles))
  27. {
  28. }
  29. void Mesh::draw(float uv_scale)
  30. {
  31. // Light direction
  32. const FloatVector3 light_direction(1.f, 1.f, 1.f);
  33. // Mesh color
  34. const FloatVector4 mesh_ambient_color(0.2f, 0.2f, 0.2f, 1.f);
  35. const FloatVector4 mesh_diffuse_color(0.6f, 0.6f, 0.6f, 1.f);
  36. for (u32 i = 0; i < m_triangle_list.size(); i++) {
  37. const auto& triangle = m_triangle_list[i];
  38. const FloatVector3 vertex_a(
  39. m_vertex_list.at(triangle.a).x,
  40. m_vertex_list.at(triangle.a).y,
  41. m_vertex_list.at(triangle.a).z);
  42. const FloatVector3 vertex_b(
  43. m_vertex_list.at(triangle.b).x,
  44. m_vertex_list.at(triangle.b).y,
  45. m_vertex_list.at(triangle.b).z);
  46. const FloatVector3 vertex_c(
  47. m_vertex_list.at(triangle.c).x,
  48. m_vertex_list.at(triangle.c).y,
  49. m_vertex_list.at(triangle.c).z);
  50. FloatVector3 normal;
  51. if (has_normals()) {
  52. const FloatVector3 normal_a(
  53. m_normal_list.at(triangle.normal_index0).x,
  54. m_normal_list.at(triangle.normal_index0).y,
  55. m_normal_list.at(triangle.normal_index0).z);
  56. const FloatVector3 normal_b(
  57. m_normal_list.at(triangle.normal_index1).x,
  58. m_normal_list.at(triangle.normal_index1).y,
  59. m_normal_list.at(triangle.normal_index1).z);
  60. const FloatVector3 normal_c(
  61. m_normal_list.at(triangle.normal_index2).x,
  62. m_normal_list.at(triangle.normal_index2).y,
  63. m_normal_list.at(triangle.normal_index2).z);
  64. normal = (normal_a + normal_b + normal_c).normalized();
  65. } else {
  66. // Compute the triangle normal
  67. const FloatVector3 vec_ab = vertex_b - vertex_a;
  68. const FloatVector3 vec_ac = vertex_c - vertex_a;
  69. normal = vec_ab.cross(vec_ac).normalized();
  70. }
  71. // Compute lighting with a Lambertian color model
  72. const auto light_intensity = max(light_direction.dot(normal), 0.f);
  73. const FloatVector4 color = mesh_ambient_color
  74. + mesh_diffuse_color * light_intensity;
  75. glBegin(GL_TRIANGLES);
  76. glColor4f(color.x(), color.y(), color.z(), color.w());
  77. if (is_textured())
  78. glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index0).u * uv_scale, (1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index0).v) * uv_scale);
  79. // Vertex 1
  80. glVertex3f(
  81. m_vertex_list.at(m_triangle_list[i].a).x,
  82. m_vertex_list.at(m_triangle_list[i].a).y,
  83. m_vertex_list.at(m_triangle_list[i].a).z);
  84. if (is_textured())
  85. glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index1).u * uv_scale, (1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index1).v) * uv_scale);
  86. // Vertex 2
  87. glVertex3f(
  88. m_vertex_list.at(m_triangle_list[i].b).x,
  89. m_vertex_list.at(m_triangle_list[i].b).y,
  90. m_vertex_list.at(m_triangle_list[i].b).z);
  91. if (is_textured())
  92. glTexCoord2f(m_tex_coords.at(m_triangle_list[i].tex_coord_index2).u * uv_scale, (1.0f - m_tex_coords.at(m_triangle_list[i].tex_coord_index2).v) * uv_scale);
  93. // Vertex 3
  94. glVertex3f(
  95. m_vertex_list.at(m_triangle_list[i].c).x,
  96. m_vertex_list.at(m_triangle_list[i].c).y,
  97. m_vertex_list.at(m_triangle_list[i].c).z);
  98. glEnd();
  99. }
  100. }