linear-algebra

所属分类:图形图象
开发工具:C
文件大小:0KB
下载次数:0
上传日期:2023-08-23 19:37:39
上 传 者sh-1993
说明:  图形编程的线性代数,
(Linear algebra for graphics programming,)

# linear-algebra Linear algebra functionality for graphics programming written in C (-std=c11) - [Matrix operations](https://github.com/MorcilloSanz/linear-algebra/blob/master/#matrix-operations) - [Vector operations](https://github.com/MorcilloSanz/linear-algebra/blob/master/#vector-operations) - [Predefined matrices](https://github.com/MorcilloSanz/linear-algebra/blob/master/#predefined-matrices) - [OpenGL uniforms](https://github.com/MorcilloSanz/linear-algebra/blob/master/#opengl) ## Compilation In order to use this code, just copy all the files into your project and compile them like other source files. >Keep in mind it is under the MIT license ## Matrix operations >**Notation** You can replace 2x2, 3x3 and 4x4 in every function or type in order to work with 2x2, 3x3 and 4x4 matrices ```c #include "matrix.h" ``` **Create a matrix**: It is possible to create 2x2, 3x3 and 4x4 matrices ```c Matrix3x3(example) = { 1.0f, 0.0f, 0.0f, 0.0f, 2.0f, 0.0f, 0.0f, 0.0f, 4.0f }; ``` **Set and get values**: Set and get a value in a position (i, j) of a matrix ```c // Set a value of 3.0f in the position (0, 2) set3x3(example, 0, 2, 3.0f); // Get the value in the position (0, 2) float value = get3x3(example, 0, 2); ``` **Print a matrix**: Print a matrix in the console ```c printMatrix3x3(example); ``` **Transpose**: Calculate the transpose of a matrix ```c // Create transpose matrix Matrix3x3(transposeMatrix); // Calculate the transpose of the example matrix and save it in transposeMatrix transpose3x3(example, transposeMatrix); ``` **Sum matrices**: Calculate the sum of two matrices ```c // Create the sum matrix Matrix3x3(exampleSum); // Calculate the sum of the example matrix and its transpose matrix and save it in example sum sumMatrixMatrix3x3(example, transposeMatrix, exampleSum); ``` **Matrix multiplication**: Calculate the product of two matrices ```c // Create the multiplication matrix Matrix3x3(exampleMult); // Calculate the multiplication of the example matrix and the exampleSum matrix and save it in exampleMult multiplyMatrixMatrix3x3(example, exampleSum, exampleMult); ``` **Determinant**: Calculate the determinant of a matrix ```c Matrix3x3(determinantMatrix) = { 1.0f, 2.0f, 1.0f, 3.0f, 1.0f, 1.0f, 4.0f, 3.0f, 2.0f }; float det = determinant3x3(determinantMatrix); ``` **Inverse of a matrix**: Calculate the inverse of a matrix ```c // Create the inverse matrix Matrix4x4(invMatrix); // Calculate the inverse of the determinantMatrix and save it in invMatrix inverse4x4(determinantMatrix, invMatrix); ``` *To see more matrix operations go to the file ***matrix.h**** ## Vector operations >**Notation** You can replace 2, 3 and 4 in every function or type in order to work with R^2, R^3 and R^4 vectors ```c #include "vector.h" ``` **Create a vector**: It is posible to create R^2, R^2 and R^4 vectors: ```c Vector4(position) = { 1.0f, 1.0f, 1.0f, 1.0f }; ``` **Set and get values**: A vector is just an array so you can simply set and get values like: ```c // Set a value of 2.0f in the position 0 position[0] = 2.0f; // Get the value of the vector position in the position 0 float value = position[0]; ``` **Print a vector**: Print a vector in the console ```c printVector4(position); ``` **Transform a vector**: Transform a vector by a matrix ```c // Create the transform matrix Matrix4x4(translateMatrix) = { 1.0f, 0.0f, 0.0f, 2.5f, 0.0f, 1.0f, 0.0f, 2.5f, 0.0f, 0.0f, 1.0f, 2.5f, 0.0f, 0.0f, 0.0f, 1.0f }; // Create the transform vector Vector4(transformed); // Calculate the transform of the vector position and save it in the vector transformed transform4(position, translateMatrix, transformed); ``` *To see more vector operations like cross, dot, hadamard product... go to the file ***vector.h**** ## Predefined matrices There are some predefined 4x4 matrices often used in graphics programming ```c #include "matrices.h" ``` **Perspective projection matrix**: Create a right handed perspective projection matrix ```c // Create the projection matrix Matrix4x4(projection); // Perspective parameters float fovy = M_PI / 4; float aspect = (float) width / height; float zNear = 0.1f; float zFar = 1000.0f; // Calculate the perspective projection matrix and save it in the matrix projection perspective(projection, fovy, aspect, zNear, zFar); ``` **Look at matrix**: Create a right handed look at matrix ```c // Create the view matrix Matrix4x4(view); // Vectors Vector3(position) = { 0.0f, 0.0f, 1.0f }; Vector3(target) = { 0.0f, 0.0f, 0.0f }; Vector3(up) = { 0.0f, 1.0f, 0.0f }; // Calculate the look at matrix and save it in the matrix view lookAt(view, position, target, up); ``` *To see more predefined matrices go to the file ***matrices.h**** ## OpenGL OpenGL vector and matrix uniforms **Matrix4x4 uniform**: Keep in mind that we are working with row-major order matrices and OpenGL works with column-major order matrices ```c Matrix4x4(matrix) = { 1.0f, 0.0f, 0.0f, 2.5f, 0.0f, 1.0f, 0.0f, 2.5f, 0.0f, 0.0f, 1.0f, 2.5f, 0.0f, 0.0f, 0.0f, 1.0f }; int location = glGetUniformLocation(shaderProgramID, uniform); // GL_TRUE for transposing the matrix as we deal with row-major order matrices glUniformMatrix4fv(location, 1, GL_TRUE, matrix); ``` **Vector uniform**: In this example we are using a R^3 vector ```c Vector3(vec) = { 1.0f, 2.0f, 3.0f }; int location = glGetUniformLocation(shaderProgramID, uniform); glUniform3fv(location, 1, vec); ```

近期下载者

相关文件


收藏者