Demos: Add indexed meshes in GLTeapot Demos

Improved the basic Wavefront OBJ loader to index vertices.
Uses less memory for the same mesh.
This commit is contained in:
Mathieu Gaillard 2021-05-08 12:29:20 -07:00 committed by Linus Groh
parent 782dc348fd
commit 7426c2fe45
Notes: sideshowbarker 2024-07-18 18:26:05 +09:00
4 changed files with 40 additions and 16 deletions

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Mathieu Gaillard <gaillard.mathieu.39@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -17,7 +18,7 @@ struct Vertex {
// A triangle defines a single "face" of a mesh
struct Triangle {
Vertex a;
Vertex b;
Vertex c;
GLuint a;
GLuint b;
GLuint c;
};

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Mathieu Gaillard <gaillard.mathieu.39@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -20,6 +21,12 @@ const Color colors[] {
Color::Yellow,
};
Mesh::Mesh(Vector<Vertex> vertices, Vector<Triangle> triangles)
: m_vertex_list(move(vertices))
, m_triangle_list(move(triangles))
{
}
void Mesh::draw()
{
u32 color_index = 0;
@ -31,9 +38,23 @@ void Mesh::draw()
glBegin(GL_TRIANGLES);
glColor4ub(cur_color.red(), cur_color.green(), cur_color.blue(), 255);
glVertex3f(triangle.a.x, triangle.a.y, triangle.a.z); // Vertex 1
glVertex3f(triangle.b.x, triangle.b.y, triangle.b.z); // Vertex 2
glVertex3f(triangle.c.x, triangle.c.y, triangle.c.z); // Vertex 3
// Vertex 1
glVertex3f(
m_vertex_list.at(triangle.a).x,
m_vertex_list.at(triangle.a).y,
m_vertex_list.at(triangle.a).z);
// Vertex 2
glVertex3f(
m_vertex_list.at(triangle.b).x,
m_vertex_list.at(triangle.b).y,
m_vertex_list.at(triangle.b).z);
// Vertex 3
glVertex3f(
m_vertex_list.at(triangle.c).x,
m_vertex_list.at(triangle.c).y,
m_vertex_list.at(triangle.c).z);
glEnd();

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Mathieu Gaillard <gaillard.mathieu.39@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -11,19 +12,19 @@
#include "Common.h"
// NOTE: We don't support indexed
class Mesh : public RefCounted<Mesh> {
public:
Mesh() = delete;
Mesh(const Vector<Triangle>& triangles)
: m_triangle_list(triangles)
{
}
void draw();
Mesh(Vector<Vertex> vertices, Vector<Triangle> triangles);
size_t vertex_count() const { return m_vertex_list.size(); }
size_t triangle_count() const { return m_triangle_list.size(); }
void draw();
private:
Vector<Vertex> m_vertex_list;
Vector<Triangle> m_triangle_list;
};

View File

@ -1,5 +1,6 @@
/*
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
* Copyright (c) 2021, Mathieu Gaillard <gaillard.mathieu.39@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -47,13 +48,13 @@ RefPtr<Mesh> WavefrontOBJLoader::load(const String& fname)
// Create a new triangle
triangles.append(
{
vertices.at(face_line.at(1).to_uint().value() - 1),
vertices.at(face_line.at(2).to_uint().value() - 1),
vertices.at(face_line.at(3).to_uint().value() - 1),
face_line.at(1).to_uint().value() - 1,
face_line.at(2).to_uint().value() - 1,
face_line.at(3).to_uint().value() - 1,
});
}
}
dbgln("Wavefront: Done.");
return adopt_ref(*new Mesh(triangles));
return adopt_ref(*new Mesh(vertices, triangles));
}