Browse Source

Added make_vec*

Christophe Riccio 8 years ago
parent
commit
ba4e508e98
3 changed files with 266 additions and 92 deletions
  1. 81 0
      glm/gtc/type_ptr.hpp
  2. 104 92
      glm/gtc/type_ptr.inl
  3. 81 0
      test/gtc/gtc_type_ptr.cpp

+ 81 - 0
glm/gtc/type_ptr.hpp

@@ -36,6 +36,7 @@
 
 // Dependency:
 #include "../gtc/quaternion.hpp"
+#include "../gtc/vec1.hpp"
 #include "../vec2.hpp"
 #include "../vec3.hpp"
 #include "../vec4.hpp"
@@ -64,6 +65,86 @@ namespace glm
 	template<typename genType>
 	GLM_FUNC_DECL typename genType::value_type const * value_ptr(genType const& v);
 
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<1, T, Q> make_vec1(vec<1, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<1, T, Q> make_vec1(vec<2, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<1, T, Q> make_vec1(vec<3, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<1, T, Q> make_vec1(vec<4, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<2, T, Q> make_vec2(vec<1, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<2, T, Q> make_vec2(vec<2, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<2, T, Q> make_vec2(vec<3, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<2, T, Q> make_vec2(vec<4, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<3, T, Q> make_vec3(vec<1, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<3, T, Q> make_vec3(vec<2, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<3, T, Q> make_vec3(vec<3, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<3, T, Q> make_vec3(vec<4, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<4, T, Q> make_vec4(vec<1, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<4, T, Q> make_vec4(vec<2, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<4, T, Q> make_vec4(vec<3, T, Q> const& v);
+
+	/// Build a vector from a pointer.
+	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	GLM_FUNC_DECL vec<4, T, Q> make_vec4(vec<4, T, Q> const& v);
+
 	/// Build a vector from a pointer.
 	/// @see gtc_type_ptr
 	template<typename T>

+ 104 - 92
glm/gtc/type_ptr.inl

@@ -8,216 +8,258 @@ namespace glm
 	/// @addtogroup gtc_type_ptr
 	/// @{
 
-	/// Return the constant address to the data of the vector input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(vec<2, T, Q> const& v)
 	{
 		return &(v.x);
 	}
 
-	//! Return the address to the data of the vector input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(vec<2, T, Q>& v)
 	{
 		return &(v.x);
 	}
 
-	/// Return the constant address to the data of the vector input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const * value_ptr(vec<3, T, Q> const& v)
 	{
 		return &(v.x);
 	}
 
-	//! Return the address to the data of the vector input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(vec<3, T, Q>& v)
 	{
 		return &(v.x);
 	}
-		
-	/// Return the constant address to the data of the vector input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(vec<4, T, Q> const& v)
 	{
 		return &(v.x);
 	}
 
-	//! Return the address to the data of the vector input.
-	//! From GLM_GTC_type_ptr extension.
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(vec<4, T, Q>& v)
 	{
 		return &(v.x);
 	}
 
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<2, 2, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<2, 2, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
 
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<3, 3, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<3, 3, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
-		
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<4, 4, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	//! From GLM_GTC_type_ptr extension.
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<4, 4, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
 
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<2, 3, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<2, 3, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
-		
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<3, 2, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<3, 2, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
-		
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<2, 4, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<2, 4, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
-		
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<4, 2, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<4, 2, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
-		
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<3, 4, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	//! Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(mat<3, 4, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
-		
-	/// Return the constant address to the data of the matrix input.
-	/// @see gtc_type_ptr
+
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const* value_ptr(mat<4, 3, T, Q> const& m)
 	{
 		return &(m[0].x);
 	}
 
-	/// Return the address to the data of the matrix input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T * value_ptr(mat<4, 3, T, Q>& m)
 	{
 		return &(m[0].x);
 	}
 
-	/// Return the constant address to the data of the input parameter.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T const * value_ptr(tquat<T, Q> const& q)
 	{
 		return &(q[0]);
 	}
 
-	/// Return the address to the data of the quaternion input.
-	/// @see gtc_type_ptr
 	template<typename T, qualifier Q>
 	GLM_FUNC_QUALIFIER T* value_ptr(tquat<T, Q>& q)
 	{
 		return &(q[0]);
 	}
 
-	/// Build a vector from a pointer.
-	/// @see gtc_type_ptr
+	template <typename T, qualifier Q>
+	inline vec<1, T, Q> make_vec1(vec<1, T, Q> const& v)
+	{
+		return v;
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<1, T, Q> make_vec1(vec<2, T, Q> const& v)
+	{
+		return vec<1, T, Q>(v);
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<1, T, Q> make_vec1(vec<3, T, Q> const& v)
+	{
+		return vec<1, T, Q>(v);
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<1, T, Q> make_vec1(vec<4, T, Q> const& v)
+	{
+		return vec<1, T, Q>(v);
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<2, T, Q> make_vec2(vec<1, T, Q> const& v)
+	{
+		return vec<2, T, Q>(v.x, static_cast<T>(0));
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<2, T, Q> make_vec2(vec<2, T, Q> const& v)
+	{
+		return v;
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<2, T, Q> make_vec2(vec<3, T, Q> const& v)
+	{
+		return vec<2, T, Q>(v);
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<2, T, Q> make_vec2(vec<4, T, Q> const& v)
+	{
+		return vec<2, T, Q>(v);
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<3, T, Q> make_vec3(vec<1, T, Q> const& v)
+	{
+		return vec<3, T, Q>(v.x, static_cast<T>(0), static_cast<T>(0));
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<3, T, Q> make_vec3(vec<2, T, Q> const& v)
+	{
+		return vec<3, T, Q>(v.x, v.y, static_cast<T>(0));
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<3, T, Q> make_vec3(vec<3, T, Q> const& v)
+	{
+		return v;
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<3, T, Q> make_vec3(vec<4, T, Q> const& v)
+	{
+		return vec<3, T, Q>(v);
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<4, T, Q> make_vec4(vec<1, T, Q> const& v)
+	{
+		return vec<4, T, Q>(v.x, static_cast<T>(0), static_cast<T>(0), static_cast<T>(1));
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<4, T, Q> make_vec4(vec<2, T, Q> const& v)
+	{
+		return vec<4, T, Q>(v.x, v.y, static_cast<T>(0), static_cast<T>(1));
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<4, T, Q> make_vec4(vec<3, T, Q> const& v)
+	{
+		return vec<4, T, Q>(v.x, v.y, v.z, static_cast<T>(1));
+	}
+
+	template <typename T, qualifier Q>
+	inline vec<4, T, Q> make_vec4(vec<4, T, Q> const& v)
+	{
+		return v;
+	}
+
 	template<typename T>
 	GLM_FUNC_QUALIFIER vec<2, T, defaultp> make_vec2(T const *const ptr)
 	{
@@ -226,8 +268,6 @@ namespace glm
 		return Result;
 	}
 
-	/// Build a vector from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER vec<3, T, defaultp> make_vec3(T const *const ptr)
 	{
@@ -236,8 +276,6 @@ namespace glm
 		return Result;
 	}
 
-	/// Build a vector from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER vec<4, T, defaultp> make_vec4(T const *const ptr)
 	{
@@ -246,8 +284,6 @@ namespace glm
 		return Result;
 	}
 
-	/// Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<2, 2, T, defaultp> make_mat2x2(T const *const ptr)
 	{
@@ -256,8 +292,6 @@ namespace glm
 		return Result;
 	}
 
-	/// Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<2, 3, T, defaultp> make_mat2x3(T const *const ptr)
 	{
@@ -266,8 +300,6 @@ namespace glm
 		return Result;
 	}
 
-	/// Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<2, 4, T, defaultp> make_mat2x4(T const *const ptr)
 	{
@@ -276,8 +308,6 @@ namespace glm
 		return Result;
 	}
 
-	/// Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<3, 2, T, defaultp> make_mat3x2(T const *const ptr)
 	{
@@ -286,8 +316,6 @@ namespace glm
 		return Result;
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<3, 3, T, defaultp> make_mat3x3(T const *const ptr)
 	{
@@ -296,8 +324,6 @@ namespace glm
 		return Result;
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<3, 4, T, defaultp> make_mat3x4(T const *const ptr)
 	{
@@ -306,8 +332,6 @@ namespace glm
 		return Result;
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<4, 2, T, defaultp> make_mat4x2(T const *const ptr)
 	{
@@ -316,8 +340,6 @@ namespace glm
 		return Result;
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<4, 3, T, defaultp> make_mat4x3(T const *const ptr)
 	{
@@ -326,8 +348,6 @@ namespace glm
 		return Result;
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> make_mat4x4(T const *const ptr)
 	{
@@ -336,32 +356,24 @@ namespace glm
 		return Result;
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<2, 2, T, defaultp> make_mat2(T const *const ptr)
 	{
 		return make_mat2x2(ptr);
 	}
 
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<3, 3, T, defaultp> make_mat3(T const *const ptr)
 	{
 		return make_mat3x3(ptr);
 	}
-		
-	//! Build a matrix from a pointer.
-	/// @see gtc_type_ptr
+
 	template<typename T>
 	GLM_FUNC_QUALIFIER mat<4, 4, T, defaultp> make_mat4(T const *const ptr)
 	{
 		return make_mat4x4(ptr);
 	}
 
-	//! Build a quaternion from a pointer.
-	/// @see gtc_type_ptr
 	template<typename T>
 	GLM_FUNC_QUALIFIER tquat<T, defaultp> make_quat(T const *const ptr)
 	{

+ 81 - 0
test/gtc/gtc_type_ptr.cpp

@@ -1,4 +1,5 @@
 #include <glm/gtc/type_ptr.hpp>
+#include <glm/gtc/vec1.hpp>
 
 int test_value_ptr_vec()
 {
@@ -237,10 +238,90 @@ int test_make_pointer_vec()
 	return Error;
 }
 
+int test_make_vec1()
+{
+	int Error = 0;
+
+	glm::ivec1 const v1 = glm::make_vec1(glm::ivec1(2));
+	Error += v1 == glm::ivec1(2) ? 0 : 1;
+
+	glm::ivec1 const v2 = glm::make_vec1(glm::ivec2(2));
+	Error += v2 == glm::ivec1(2) ? 0 : 1;
+
+	glm::ivec1 const v3 = glm::make_vec1(glm::ivec3(2));
+	Error += v3 == glm::ivec1(2) ? 0 : 1;
+
+	glm::ivec1 const v4 = glm::make_vec1(glm::ivec4(2));
+	Error += v3 == glm::ivec1(2) ? 0 : 1;
+
+	return Error;
+}
+
+int test_make_vec2()
+{
+	int Error = 0;
+
+	glm::ivec2 const v1 = glm::make_vec2(glm::ivec1(2));
+	Error += v1 == glm::ivec2(2, 0) ? 0 : 1;
+
+	glm::ivec2 const v2 = glm::make_vec2(glm::ivec2(2));
+	Error += v2 == glm::ivec2(2, 2) ? 0 : 1;
+
+	glm::ivec2 const v3 = glm::make_vec2(glm::ivec3(2));
+	Error += v3 == glm::ivec2(2, 2) ? 0 : 1;
+
+	glm::ivec2 const v4 = glm::make_vec2(glm::ivec4(2));
+	Error += v3 == glm::ivec2(2, 2) ? 0 : 1;
+
+	return Error;
+}
+
+int test_make_vec3()
+{
+	int Error = 0;
+
+	glm::ivec3 const v1 = glm::make_vec3(glm::ivec1(2));
+	Error += v1 == glm::ivec3(2, 0, 0) ? 0 : 1;
+
+	glm::ivec3 const v2 = glm::make_vec3(glm::ivec2(2));
+	Error += v2 == glm::ivec3(2, 2, 0) ? 0 : 1;
+
+	glm::ivec3 const v3 = glm::make_vec3(glm::ivec3(2));
+	Error += v3 == glm::ivec3(2, 2, 2) ? 0 : 1;
+
+	glm::ivec3 const v4 = glm::make_vec3(glm::ivec4(2));
+	Error += v3 == glm::ivec3(2, 2, 2) ? 0 : 1;
+
+	return Error;
+}
+
+int test_make_vec4()
+{
+	int Error = 0;
+
+	glm::ivec4 const v1 = glm::make_vec4(glm::ivec1(2));
+	Error += v1 == glm::ivec4(2, 0, 0, 1) ? 0 : 1;
+
+	glm::ivec4 const v2 = glm::make_vec4(glm::ivec2(2));
+	Error += v2 == glm::ivec4(2, 2, 0, 1) ? 0 : 1;
+
+	glm::ivec4 const v3 = glm::make_vec4(glm::ivec3(2));
+	Error += v3 == glm::ivec4(2, 2, 2, 1) ? 0 : 1;
+
+	glm::ivec4 const v4 = glm::make_vec4(glm::ivec4(2));
+	Error += v3 == glm::ivec4(2, 2, 2, 4) ? 0 : 1;
+
+	return Error;
+}
+
 int main()
 {
 	int Error = 0;
 
+	Error += test_make_vec1();
+	Error += test_make_vec2();
+	Error += test_make_vec3();
+	Error += test_make_vec4();
 	Error += test_make_pointer_vec();
 	Error += test_make_pointer_mat();
 	Error += test_value_ptr_vec();