/// <summary> /// This test shows that glm.rotate() and Quaternion.ToRotationMatrix() give the same result. /// </summary> public static void Test() { using (var writer = new StreamWriter("test-quaternion.txt")) { int length = 5; for (int angleDegree = 1; angleDegree < 361; angleDegree++) { for (int x = -length; x < length; x++) { for (int y = -length; y < length; y++) { for (int z = -length; z < length; z++) { var quaternion = new Quaternion(angleDegree, new vec3(x, y, z)); mat3 matrix1 = quaternion.ToRotationMatrix(); //mat4 tmp = glm.rotate((float)(angleDegree * Math.PI / 180.0f), new vec3(x, y, z)); mat4 tmp = glm.rotate(angleDegree, new vec3(x, y, z)); mat3 matrix2 = tmp.to_mat3(); writer.WriteLine("===================="); writer.WriteLine("{3}° x[{0}] y[{1}] z[{2}]", x, y, z, angleDegree); writer.WriteLine(matrix1.ToArray().PrintVectors(3, ",", ";" + Environment.NewLine)); writer.WriteLine("------------"); writer.WriteLine(matrix2.ToArray().PrintVectors(3, ",", ";" + Environment.NewLine)); //} } } } } writer.WriteLine("Test finished."); } }
public static unsafe void glusRaytraceLookAtf(this float[] positionBuffer, float[] directionBuffer, float[] originDirectionBuffer, byte padding, int width, int height, float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ) { //float forward[3], side[3], up[3]; //float rotation[9]; vec3 forward, side, up; float[] rotation; int i, k; forward = new vec3(centerX - eyeX, centerY - eyeY, centerZ - eyeZ); forward = forward.normalize(); up = new vec3(upX, upY, upZ); side = forward.cross(up); side = side.normalize(); up = side.cross(forward); mat3 matrix = new mat3(side, up, -forward); rotation = matrix.ToArray(); for (i = 0; i < width * height; i++) { positionBuffer[i * 4 + 0] = eyeX; positionBuffer[i * 4 + 1] = eyeY; positionBuffer[i * 4 + 2] = eyeZ; positionBuffer[i * 4 + 3] = 1.0f; //glusMatrix3x3MultiplyVector3f(&directionBuffer[i * (3 + padding)], rotation, &originDirectionBuffer[i * (3 + padding)]); float[] result = new float[3]; result[0] = directionBuffer[i * (3 + padding) + 0]; result[1] = directionBuffer[i * (3 + padding) + 1]; result[2] = directionBuffer[i * (3 + padding) + 2]; float[] vector = new float[3]; vector[0] = originDirectionBuffer[i * (3 + padding) + 0]; vector[1] = originDirectionBuffer[i * (3 + padding) + 1]; vector[2] = originDirectionBuffer[i * (3 + padding) + 2]; glusMatrix3x3MultiplyVector3f(result, rotation, vector); directionBuffer[i * (3 + padding) + 0] = result[0]; directionBuffer[i * (3 + padding) + 1] = result[1]; directionBuffer[i * (3 + padding) + 2] = result[2]; for (k = 0; k < padding; k++) { directionBuffer[i * (3 + padding) + 3 + k] = originDirectionBuffer[i * (3 + padding) + 3 + k]; } } }
/// <summary> /// </summary> /// <param name="uniformName"></param> /// <param name="m"></param> internal int glUniform(string uniformName, mat3 m) { int location = GetUniformLocation(uniformName); if (location >= 0) { if (glUniformMatrix3fv == null) { glUniformMatrix3fv = GL.Instance.GetDelegateFor("glUniformMatrix3fv", GLDelegates.typeof_void_int_int_bool_floatN) as GLDelegates.void_int_int_bool_floatN; } float[] array = m.ToArray(); glUniformMatrix3fv(location, 1, false, array); } return(location); }
/// <summary> /// </summary> /// <param name="uniformName"></param> /// <param name="m"></param> public int glUniform(string uniformName, mat3 m) { int location = GetUniformLocation(uniformName); if (location >= 0) { if (glUniformMatrix3fv == null) { glUniformMatrix3fv = OpenGL.GetDelegateFor <OpenGL.glUniformMatrix3fv>(); } float[] array = m.ToArray(); glUniformMatrix3fv(location, 1, false, array); } return(location); }