Compare commits

...

3 commits

Author SHA1 Message Date
f9dc00712d Added cross and dot products
Fixed T initializers
Constified functions that could be marked const.
2019-10-22 01:11:01 +02:00
18bad8389a Fixed useless copying of vectors during model.draw(), now references.
Fixed copy constructor of Vec3 (Copy-pasting is bad)
Now exits on escape
2019-10-19 04:24:08 +02:00
8cdcc9750b Specified copy constructor for Vec2 and Vec3 to change the x,y and z references and not keep references to the coordinates of the original object.
Fixed the SFINAE not triggering on normalize() because there was no template substitution.
2019-10-19 00:56:56 +02:00
3 changed files with 64 additions and 9 deletions

View file

@ -53,7 +53,7 @@ void Model3D::draw_model()
for (uint32_t i = 0;i<face_count;i++)
{
Vec3i face = faces[i];
Vec3i& face = faces[i];
Vec3i* face_texture;
if (is_textured)
@ -68,7 +68,7 @@ void Model3D::draw_model()
if (is_textured)
{
Vec2f vertex_texture = texture_coordinates[face_texture->coordinates[j]];
Vec2f& vertex_texture = texture_coordinates[face_texture->coordinates[j]];
glTexCoord2f(vertex_texture.x,vertex_texture.y);
}
glVertex3f(vertex.x,vertex.y,vertex.z);

View file

@ -18,7 +18,7 @@ struct CoordinatesVector
/// Computes the euclidean norm of the vector.
/// \return The magnitude.
double magnitude()
double magnitude() const
{
// TODO : Save magnitude if the vector has not been modified.
double sum_of_squares = 0;
@ -29,10 +29,12 @@ struct CoordinatesVector
return sqrt(sum_of_squares);
}
// Use SFINAE to allow normalization of floating point
// Use SFINAE to allow normalization of floating point.
// The template argument is used to force substitution and thus checking the enable_if.
/// Normalizes the vector using its euclidean norm.
/// \return Nothing, the vector is normalized in-place.
typename std::enable_if<std::is_floating_point<T>::value,void>::type
template <typename U = T>
typename std::enable_if<std::is_floating_point<U>::value,void>::type
normalize()
{
double norm = magnitude();
@ -42,6 +44,19 @@ struct CoordinatesVector
}
}
template <typename U = T>
typename std::enable_if<std::is_arithmetic<U>::value,T>::type
dot_product(const CoordinatesVector<T,N>& operand) const
{
T result = 0;
for (unsigned int i = 0;i<N;i++)
{
result += coordinates[i]*operand.coordinates[i];
}
return result;
}
// Use SFINAE to declare the function if and only if the type is arithmetic
template <typename Scalar>
typename std::enable_if<std::is_arithmetic<Scalar>::value,CoordinatesVector<T,N>>::type
@ -104,6 +119,17 @@ struct Vec2 : public CoordinatesVector<T,2>
Vec2() = default;
Vec2(T x_axis, T y_axis)
{
x = x_axis;
y = y_axis;
}
Vec2(const Vec2<T>& original): x(CoordinatesVector<T,2>::coordinates[0]), y(CoordinatesVector<T,2>::coordinates[1])
{
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,2>::coordinates));
}
// Cast-copy constructor in order to use the operators of the mother class.
Vec2(const CoordinatesVector<T,2>& origin)
{
@ -133,13 +159,37 @@ struct Vec3 : CoordinatesVector<T,3>
Vec3() = default;
// Cast-copy constructor in order to use the operators of the mother class.
Vec3(const CoordinatesVector<T,3>& origin)
Vec3(T x_axis, T y_axis, T z_axis)
{
if (this != &origin)
{
std::copy(std::begin(origin.coordinates),std::end(origin.coordinates),std::begin(CoordinatesVector<T,2>::coordinates));
x = x_axis;
y = y_axis;
z = z_axis;
}
Vec3(const Vec3<T>& original): x(CoordinatesVector<T,3>::coordinates[0]), y(CoordinatesVector<T,3>::coordinates[1]), z(CoordinatesVector<T,3>::coordinates[2])
{
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,3>::coordinates));
}
// Cast-copy constructor in order to use the operators of the mother class.
Vec3(const CoordinatesVector<T,3>& original)
{
if (this != &original)
{
std::copy(std::begin(original.coordinates), std::end(original.coordinates), std::begin(CoordinatesVector<T,3>::coordinates));
}
}
template <typename U = T>
typename std::enable_if<std::is_arithmetic<U>::value,Vec3<T>>::type
cross_product(const Vec3<T>& operand) const
{
Vec3<T> normal_vector{0,0,0};
normal_vector.x = y*operand.z - z*operand.y;
normal_vector.y = z*operand.x - x*operand.z;
normal_vector.z = x*operand.y - y*operand.x;
return normal_vector;
}
Vec3& operator=(const Vec3<T>& original)

View file

@ -21,6 +21,11 @@ void manage_inputs()
{
glClearColor(0.5,0,0,1);
}
else if (KeyStateManager::is_key_pressed(0x1B))
{
glutDestroyWindow(glutGetWindow());
exit(EXIT_SUCCESS);
}
else
{
glClearColor(0,0,0,1);