mikymod пре 12 година
родитељ
комит
d42b05c4ef
7 измењених фајлова са 204 додато и 82 уклоњено
  1. 4 3
      game/lua/camera.lua
  2. 13 0
      game/lua/game.lua
  3. 65 22
      game/lua/init.lua
  4. 28 29
      game/lua/mat4.lua
  5. 9 9
      game/lua/quat.lua
  6. 65 0
      game/lua/vec2.lua
  7. 20 19
      game/lua/vec3.lua

+ 4 - 3
game/lua/camera.lua

@@ -21,15 +21,15 @@ ffi.cdef
 		float			m_far;
 	} Camera;
 
-	Camera*				camera(const Vec3* position, float fov, float aspect);
+	Camera*				camera(const Vec3& position, float fov, float aspect);
 
 	const Vec3&			camera_position(Camera* self);
 
-	void				camera_set_position(Camera* self, const Vec3* position);
+	void				camera_set_position(Camera* self, const Vec3& position);
 
 	const Vec3&			camera_look_at(Camera* self);
 
-	void				camera_set_look_at(Camera* self, const Vec3* lookat);
+	void				camera_set_look_at(Camera* self, const Vec3& lookat);
 
 	void				camera_set_rotation(Camera* self, const float x, const float y);
 
@@ -62,6 +62,7 @@ ffi.cdef
 	void				camera_strafe_left(Camera* self, float meters);
 
 	void				camera_strafe_right(Camera* self, float meters);
+
 ]]
 
 Camera = {}

+ 13 - 0
game/lua/game.lua

@@ -0,0 +1,13 @@
+
+function init()
+
+end
+
+function shutdown()
+
+end
+
+
+function render(dt)
+
+end

+ 65 - 22
game/lua/init.lua

@@ -8,7 +8,7 @@ require("vec3")
 require("mat4")
 require("quat")
 require("math_utils")
-require("camera")
+-- require("camera")
 require("script")
 
 --------------------------------------------------------------
@@ -17,6 +17,20 @@ require("script")
 print("-- Testing Vec3 --\n")
 local pos = Vec3.vec3(1.0, 1.0, 1.0)
 
+pos = Vec3.add(pos, Vec3.vec3(1.0, 2.0, 3.0)
+)
+
+print(pos.x)
+print(pos.y)
+print(pos.z)
+
+Vec3.negate(pos)
+
+print(pos.x)
+print(pos.y)
+print(pos.z)
+
+
 --------------------------------------------------------------
 --------------------------------------------------------------
 --------------------------------------------------------------
@@ -24,6 +38,8 @@ local pos = Vec3.vec3(1.0, 1.0, 1.0)
 print("-- Testing Mat4 --\n")
 local m = Mat4.mat4(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)
 local t = Mat4.mat4(9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0)
+local trans = Vec3.vec3(1.0, 1.0, 0.0)
+local scale = Vec3.vec3(10.0, 10.0, 10.0)
 
 Mat4.print(m)
 print("\n")
@@ -40,34 +56,61 @@ m = Mat4.subtract(m, t)
 Mat4.print(m)
 print("\n")
 
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------
-print("-- Testing MathUtils --\n")
+print(".. Mat4.set_translation-- \n")
+Mat4.set_translation(m, trans)
+Mat4.print(m)
+print("\n")
 
+print(".. Mat4.get_translation-- \n")
+local tr = Mat4.get_translation(m)
+print(tr.x)
+print(tr.y)
+print(tr.z)
 
+print(".. Mat4.set_scale-- \n")
+Mat4.set_scale(m, scale)
+Mat4.print(m)
+print("\n")
 
-print("sin of 0 is " .. Math.sin(0.0))
+print(".. Mat4.get_scale-- \n")
+local sc = Mat4.get_scale(m)
+print(sc.x)
+print(sc.y)
+print(sc.z)
 
 --------------------------------------------------------------
 --------------------------------------------------------------
 --------------------------------------------------------------
-print("-- Testing Camera --\n")
-
-local cam = Camera.camera(pos, 90.0, 1.6)
-
-print("@move forward by 1 meter")
-print("x:" .. Camera.position(cam).x)
-print("y:" .. Camera.position(cam).y)
-print("z:" .. Camera.position(cam).z)
-
-for i=1,10 do	
-	Camera.move_forward(cam, 1.0);
-	print("@move forward by 1 meter")
-	print("x:" .. Camera.position(cam).x)
-	print("y:" .. Camera.position(cam).y)
-	print("z:" .. Camera.position(cam).z)
-end
+print("-- Testing MathUtils --\n")
+print("sin of 0 is " .. Math.sin(0.0))
+
+-- --------------------------------------------------------------
+-- --------------------------------------------------------------
+-- --------------------------------------------------------------
+-- print("-- Testing Camera --\n")
+
+-- local cam = Camera.camera(pos, 90.0, 1.6)
+
+-- print("@move forward by 1 meter")
+-- print("x:" .. Camera.position(cam).x)
+-- print("y:" .. Camera.position(cam).y)
+-- print("z:" .. Camera.position(cam).z)
+
+-- for i=1,10 do	
+-- 	Camera.move_forward(cam, 1.0);
+-- 	print("@move forward by 1 meter\n")
+-- 	print("x:" .. Camera.position(cam).x)
+-- 	print("y:" .. Camera.position(cam).y)
+-- 	print("z:" .. Camera.position(cam).z)
+-- end
+
+-- local vm = Camera.view_matrix(cam)
+-- local pm = Camera.projection_matrix(cam)
+
+-- print("@printing view matrix\n")
+-- print(Mat4.print(vm))
+-- print("@printing projection matrix\n")
+-- print(Mat4.print(pm))
 
 --------------------------------------------------------------
 --------------------------------------------------------------

+ 28 - 29
game/lua/mat4.lua

@@ -22,62 +22,61 @@ ffi.cdef
 		float r4c4;
 	} Mat4;
 
-	Mat4*				mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);
+	Mat4&				mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);
 						
-	Mat4*				mat4_add(Mat4* self, Mat4* m);
+	Mat4&				mat4_add(Mat4& self, Mat4& m);
 
-	Mat4*				mat4_subtract(Mat4* self, Mat4* m);
+	Mat4&				mat4_subtract(Mat4& self, Mat4& m);
 
-	Mat4*				mat4_multiply(Mat4* self, Mat4* m);
+	Mat4&				mat4_multiply(Mat4& self, Mat4& m);
 
-	Mat4*				mat4_multiply_by_scalar(Mat4* self, float k);
+	Mat4&				mat4_multiply_by_scalar(Mat4& self, float k);
 
-	Mat4*				mat4_divide_by_scalar(Mat4* self, float k);
+	Mat4&				mat4_divide_by_scalar(Mat4& self, float k);
 
-	void				mat4_build_rotation_x(Mat4* self, float radians);
+	void				mat4_build_rotation_x(Mat4& self, float radians);
 
-	void				mat4_build_rotation_y(Mat4* self, float radians);	
+	void				mat4_build_rotation_y(Mat4& self, float radians);	
 
-	void				mat4_build_rotation_z(Mat4* self, float radians);
+	void				mat4_build_rotation_z(Mat4& self, float radians);
 
-	void				mat4_build_rotation(Mat4* self, const Vec3* n, float radians);
+	void				mat4_build_rotation(Mat4& self, const Vec3& n, float radians);
 
-	void				mat4_build_projection_perspective_rh(Mat4* self, float fovy, float aspect, float near, float far);
+	void				mat4_build_projection_perspective_rh(Mat4& self, float fovy, float aspect, float near, float far);
 
-	void				mat4_build_projection_perspective_lh(Mat4* self, float fovy, float aspect, float near, float far);
+	void				mat4_build_projection_perspective_lh(Mat4& self, float fovy, float aspect, float near, float far);
 
-	void				mat4_build_projection_ortho_rh(Mat4* self, float width, float height, float near, float far);
+	void				mat4_build_projection_ortho_rh(Mat4& self, float width, float height, float near, float far);
 
-	void				mat4_build_projection_ortho_lh(Mat4* self, float width, float height, float near, float far);
+	void				mat4_build_projection_ortho_lh(Mat4& self, float width, float height, float near, float far);
 
-	void				mat4_build_projection_ortho_2d_rh(Mat4* self, float width, float height, float near, float far);
+	void				mat4_build_projection_ortho_2d_rh(Mat4& self, float width, float height, float near, float far);
 
-	void				mat4_build_look_at_rh(Mat4* self, const Vec3* pos, const Vec3* target, const Vec3* up);
+	void				mat4_build_look_at_rh(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& up);
 
-	void				mat4_build_look_at_lh(Mat4* self, const Vec3* pos, const Vec3* target, const Vec3* up);
+	void				mat4_build_look_at_lh(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& up);
 
-	void				mat4_build_viewpoint_billboard(Mat4* self, const Vec3* pos, const Vec3* target, const Vec3* up);
+	void				mat4_build_viewpoint_billboard(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& up);
 
-	void				mat4_build_axis_billboard(Mat4* self, const Vec3* pos, const Vec3* target, const Vec3* axis);
+	void				mat4_build_axis_billboard(Mat4& self, const Vec3& pos, const Vec3& target, const Vec3& axis);
 
-	Mat4*				mat4_transpose(Mat4* self);
+	Mat4&				mat4_transpose(Mat4& self);
 
-	float				mat4_determinant(Mat4* self);
+	float				mat4_determinant(Mat4& self);
 
-	Mat4*				mat4_invert(Mat4* self);
+	Mat4&				mat4_invert(Mat4& self);
 
-	void				mat4_load_identity(Mat4* self);
+	void				mat4_load_identity(Mat4& self);
 
-	Vec3*				mat4_get_translation(Mat4* self);
+	Vec3&				mat4_get_translation(Mat4& self);
 
-	void				mat4_set_translation(Mat4* self, const Vec3* trans);
+	void				mat4_set_translation(Mat4& self, const Vec3& trans);
 
-	Vec3*				mat4_get_scale(Mat4* self);
+	Vec3&				mat4_get_scale(Mat4& self);
 
-	void				mat4_set_scale(Mat4* self, const Vec3* scale);
-
-	void 				mat4_print(Mat4* self);
+	void				mat4_set_scale(Mat4& self, const Vec3& scale);
 
+	void 				mat4_print(Mat4& self);
 ]]
 
 Mat4 = {}

+ 9 - 9
game/lua/quat.lua

@@ -11,23 +11,23 @@ ffi.cdef
 
 	} Quat;
 
-	Quat*		quat(float angle, const Vec3* v);
+	Quat&		quat(float angle, const Vec3& v);
 
-	void		quat_negate(Quat* self);
+	void		quat_negate(Quat& self);
 
-	void		quat_load_identity(Quat* self);
+	void		quat_load_identity(Quat& self);
 
-	float		quat_length(Quat* self);	
+	float		quat_length(Quat& self);	
 
-	void		quat_conjugate(Quat* self);
+	void		quat_conjugate(Quat& self);
 
-	Quat*		quat_inverse(Quat* self);			
+	Quat&		quat_inverse(Quat& self);			
 
-	Quat*		quat_cross(Quat* self, const Quat* b);
+	Quat&		quat_cross(Quat& self, const Quat& b);
 
-	Quat*		quat_multiply(Quat* self, const float& k);
+	Quat&		quat_multiply(Quat& self, const float& k);
 
-	Quat*		quat_power(Quat* self, float exp);
+	Quat&		quat_power(Quat& self, float exp);
 ]]
 
 Quat = {}

+ 65 - 0
game/lua/vec2.lua

@@ -0,0 +1,65 @@
+local ffi = require("ffi")
+
+ffi.cdef
+[[
+	typedef struct 
+	{
+		float x;
+		float y;
+	}
+	Vec2;
+
+	Vec2&				vec2(float nx, float ny);					
+
+	Vec2&				vec2_add(Vec2& self, const Vec2& a);			
+
+	Vec2& 				vec2_subtract(Vec2& self, const Vec2& a);			
+
+	Vec2&				vec2_multiply(Vec2& self, float k);			
+
+	Vec2&				vec2_divide(Vec2& self, float k);
+
+	float				vec2_dot(Vec2& self, const Vec2& a);				
+
+	bool				vec2_equals(Vec2& self, const Vec2& other);	
+
+	bool				vec2_lower(Vec2& self, const Vec2& other);		
+
+	bool				vec2_greater(Vec2& self, const Vec2& other);
+
+	float				vec2_length(Vec2& self);
+
+	float				vec2_squared_length(Vec2& self);
+
+	void				vec2_set_length(Vec2& self, float len);					
+
+	Vec2&				vec2_normalize(Vec2& self);
+
+	Vec2&				vec2_negate(Vec2& self);
+
+	float				vec2_get_distance_to(Vec2& self, const Vec2& a);
+
+	float				vec2_get_angle_between(Vec2& self, const Vec2& a);
+
+	void				vec2_zero(Vec2& self);
+]]
+
+Vec2 = {}
+
+Vec2.vec2  				= lib.vec2
+Vec2.add				= lib.vec2_add			
+Vec2.subtract			= lib.vec2_subtract			
+Vec2.multiply			= lib.vec2_multiply
+Vec2.divide				= lib.vec2_divide
+Vec2.dot				= lib.vec2_dot				
+Vec2.equals				= lib.vec2_equals	
+Vec2.lower				= lib.vec2_lower		
+Vec2.greater			= lib.vec2_greater
+Vec2.length				= lib.vec2_length
+Vec2.squared_length		= lib.vec2_squared_length
+Vec2.set_length			= lib.vec2_set_length					
+Vec2.normalize			= lib.vec2_normalize
+Vec2.negate				= lib.vec2_negate
+Vec2.get_distance_to	= lib.vec2_get_distance_to
+Vec2.get_angle_between	= lib.vec2_get_angle_between
+Vec2.zero				= lib.vec2_zero

+ 20 - 19
game/lua/vec3.lua

@@ -7,43 +7,44 @@ ffi.cdef
 		float x;
 		float y;
 		float z;
-	} Vec3;
+	}
+	Vec3;
 
-	Vec3* 				vec3(float nx, float ny, float nz);
+	Vec3&				vec3(float nx, float ny, float nz);
 
-	Vec3*				vec3_add(Vec3* self, const Vec3* v);
+	Vec3&				vec3_add(Vec3& self, const Vec3& v);
 
-	Vec3*				vec3_subtract(Vec3* self, const Vec3* v);
+	Vec3&				vec3_subtract(Vec3& self, const Vec3& v);
 
-	Vec3*				vec3_multiply(Vec3* self, const float s);
+	Vec3&				vec3_multiply(Vec3& self, const float s);
 
-	Vec3*				vec3_divide(Vec3* self, const float s);
+	Vec3&				vec3_divide(Vec3& self, const float s);
 
-	float				vec3_dot(Vec3* self, const Vec3* v);
+	float				vec3_dot(Vec3& self, const Vec3& v);
 
-	Vec3*				vec3_cross(Vec3* self, const Vec3* v);				
+	Vec3&				vec3_cross(Vec3& self, const Vec3& v);
 
-	bool				vec3_equal(Vec3* self, const Vec3* other);	
+	bool				vec3_equal(Vec3& self, const Vec3& other);	
 	
-	bool				vec3_lower(Vec3* self, const Vec3* other);
+	bool				vec3_lower(Vec3& self, const Vec3& other);
 
-	bool				vec3_greater(Vec3* self, const Vec3* other);		
+	bool				vec3_greater(Vec3& self, const Vec3& other);		
 
-	float				vec3_length(Vec3* self);	
+	float				vec3_length(Vec3& self);	
 
-	float				vec3_squared_length(Vec3* self);
+	float				vec3_squared_length(Vec3& self);
 
-	void				vec3_set_length(Vec3* self, float len);
+	void				vec3_set_length(Vec3& self, float len);
 
-	Vec3*				vec3_normalize(Vec3* self);
+	Vec3&				vec3_normalize(Vec3& self);
 
-	Vec3*				vec3_negate(Vec3* self);					
+	Vec3&				vec3_negate(Vec3& self);					
 
-	float				vec3_get_distance_to(Vec3* self, const Vec3* a);	
+	float				vec3_get_distance_to(Vec3& self, const Vec3& a);	
 
-	float				vec3_get_angle_between(Vec3* self, const Vec3* a);	
+	float				vec3_get_angle_between(Vec3& self, const Vec3& a);	
 
-	void				vec3_zero(Vec3* self);					
+	void				vec3_zero(Vec3& self);						
 ]]
 
 -- Encapsulate method in Vec3 table