void test_mat4() { /* // Matrix local m = vec.Mat4.frustum(-10, 10, -10, 10, -10, 10) local m = vec.Mat4.perspective(90, 1, 1, 100) local m = vec.Mat4.lookAt(vec.Vec3(10, 9, 8), vec.Vec3(1, 2, 3), vec.Vec3(0, 1, 0)) local m = vec.Mat4.ortho(-10, 100, -10, 10, -10, 10) local m = vec.Mat4.identity() local m = vec.Mat4.translate(vec.Vec3(1, 2, 3)) local m = vec.Mat4.scale(1, 2, 3) local m = vec.Mat4.rotate(vec.Quat.new(1, 2, 3, 8)) local m = vec.Mat4.perspective(90, 1, 1, 100):inverse() local a = vec.Mat4.translate(vec.Vec3(1, 2, 3)) local b = vec.Mat4.perspective(90, 1, 1, 100) local m = a * b local v3 = m * vec.Vec4(1, 2, 3, 4) */ struct vec3 eye = {0, -15, 30}; struct vec3 at = {0, 0, 0}; struct vec3 up = {0, 1, 0}; struct mat4 m = mat4_look(&eye, &at, &up); struct vec4 zero = {0, 0, 0, 1}; struct vec4 v4 = mat4_mul_vec4(&m, &zero); assert(v4.x == 0); assert(v4.y == 0); assert((v4.z - (-33.541019)) < 0.00001); struct mat4 inv = mat4_inverse(&m); struct vec4 v5 = mat4_mul_vec4(&inv, &zero); assert(v5.x == 0); assert(v5.y - (-15) < 0.00001); assert(v5.z - (30) < 0.00001); }
Vec3 Util_GetMouseRay(int screenWidth, int screenHeigth, Mat4 *viewMatrix, Mat4 *projectionMatrix, int mouseX, int mouseY) { Vec4 eyeCoords; Vec3 mouseRay; /* Normalized device coords NOTE: -y becouse for SDL y = 0 is the top of the screen */ GLfloat normalX = ( 2.0f * (GLfloat)mouseX ) / (GLfloat) screenWidth - 1.0f; GLfloat normalY = 1.0f - (2.0f * (GLfloat)mouseY) / (GLfloat) screenHeigth; /* clipCoords include 4th component */ Vec4 clipCoords = { normalX, normalY, -1.0f, 1.0f }; /* Remove perpective */ { Mat4 invertedProjection = mat4_inverse(projectionMatrix); eyeCoords = mat4_mul_vec4(&invertedProjection, &clipCoords); eyeCoords.z = -1.0f; eyeCoords.w = 0.0f; } /* Remove view matrix*/ { Mat4 invertedViewMatrix = mat4_inverse(viewMatrix); Vec4 temp = mat4_mul_vec4(&invertedViewMatrix, &eyeCoords); mouseRay.x = temp.x; mouseRay.y = temp.y; mouseRay.z = temp.z; mouseRay = vec3_normalize(&mouseRay); } /* Return the ray in world coordinates */ return mouseRay; }