Refactored custom Vectors to have the number of coordinates as template and use an array to store coordinates

This commit is contained in:
trotFunky 2019-10-07 20:40:48 +02:00
parent 89874bae6a
commit 90f294e0cc
2 changed files with 45 additions and 36 deletions

View file

@ -55,36 +55,24 @@ void Model3D::draw_model()
{ {
Vec3i face = faces[i]; Vec3i face = faces[i];
Vec3i face_texture; Vec3i* face_texture;
if (is_textured) if (is_textured)
{ {
face_texture = face_textures[i]; face_texture = &face_textures[i];
} }
// FIXME : Find a better way to draw the three vertices // Draw each vertex and texture it if needed
Vec3f vertex = vertices[face.x]; for (int j = 0; j < 3; j++)
{
Vec3f& vertex = vertices[face.coordinates[j]];
if (is_textured) if (is_textured)
{ {
Vec2f vertex_texture = texture_coordinates[face_texture.x]; Vec2f vertex_texture = texture_coordinates[face_texture->coordinates[j]];
glTexCoord2f(vertex_texture.x,vertex_texture.y); glTexCoord2f(vertex_texture.x,vertex_texture.y);
} }
glVertex3f(vertex.x,vertex.y,vertex.z); glVertex3f(vertex.x,vertex.y,vertex.z);
vertex = vertices[face.y];
if (is_textured)
{
Vec2f vertex_texture = texture_coordinates[face_texture.y];
glTexCoord2f(vertex_texture.x,vertex_texture.y);
} }
glVertex3f(vertex.x,vertex.y,vertex.z);
vertex = vertices[face.z];
if (is_textured)
{
Vec2f vertex_texture = texture_coordinates[face_texture.z];
glTexCoord2f(vertex_texture.x,vertex_texture.y);
}
glVertex3f(vertex.x,vertex.y,vertex.z);
} }
glEnd(); glEnd();

View file

@ -7,14 +7,33 @@
#include <fstream> #include <fstream>
/// Group of coordinates with the input operator overloaded.
/// \tparam T Content of the vector
/// \tparam N Number of coordinates
template <typename T, unsigned int N>
struct CoordinatesVector
{
T coordinates[N];
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,N>& vector)
{
for (unsigned int i = 0;i<N;i++)
{
stream >> vector.coordinates[i];
}
return stream;
}
};
template <typename T> template <typename T>
struct Vec2 struct CoordinatesVector<T,2>
{ {
T x; T coordinates[2];
T y; T& x = coordinates[0];
T& y = coordinates[1];
friend std::fstream& operator>>(std::fstream& stream, Vec2& vec2) friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,2>& vec2)
{ {
stream >> vec2.x >> vec2.y; stream >> vec2.x >> vec2.y;
return stream; return stream;
@ -22,13 +41,15 @@ struct Vec2
}; };
template <typename T> template <typename T>
struct Vec3 struct CoordinatesVector<T,3>
{ {
T x; T coordinates[3];
T y;
T z;
friend std::fstream& operator>>(std::fstream& stream, Vec3& vec3) T& x = coordinates[0];
T& y = coordinates[1];
T& z = coordinates[2];
friend std::fstream& operator>>(std::fstream& stream, CoordinatesVector<T,3>& vec3)
{ {
stream >> vec3.x >> vec3.y >> vec3.z; stream >> vec3.x >> vec3.y >> vec3.z;
return stream; return stream;
@ -37,11 +58,11 @@ struct Vec3
// Define aliases for common types // Define aliases for common types
typedef Vec2<int> Vec2i; typedef CoordinatesVector<int,2> Vec2i;
typedef Vec2<float> Vec2f; typedef CoordinatesVector<float,2> Vec2f;
typedef Vec3<int> Vec3i; typedef CoordinatesVector<int,3> Vec3i;
typedef Vec3<float> Vec3f; typedef CoordinatesVector<float,3> Vec3f;
#endif //TESTS_OPENGL_TYPES_H #endif //TESTS_OPENGL_TYPES_H