|
| 1 | + |
| 2 | +# VecMatLib |
| 3 | + |
| 4 | +VecMatLib provides simple data structures for vectors and matrices and an implementation of all their basic operations that can be integrated in any Java or Scala project. |
| 5 | + |
| 6 | +## Project goals |
| 7 | + |
| 8 | +The goal of VecMatLib is to provide easy-to-use and efficient linear algebra operations, needed by any 3D application. |
| 9 | + |
| 10 | +Vectors and matrices structures are written in Scala to make the best use possible of Scala's operator overloading. |
| 11 | +All methods with symbolic names have an alias for better interoperability with java. |
| 12 | + |
| 13 | +## Vector math |
| 14 | + |
| 15 | +All operations in VecMatLib are designed to **not** modify the object on which the operation is invoked to respect the principles of purity and immutability of functional programming. |
| 16 | + |
| 17 | +Scala example: |
| 18 | + |
| 19 | +```scala |
| 20 | + var a = Vec3f(1.0f, 1.0f, 1.0f) |
| 21 | + var b = Vec3f(0.5f, 0.5f, 0.5f) |
| 22 | + |
| 23 | + // 'a' and 'b' will not change |
| 24 | + val c = a + b |
| 25 | + |
| 26 | + // Increase 'a' by 'b' |
| 27 | + a = a + b |
| 28 | +``` |
| 29 | + |
| 30 | +Java example: |
| 31 | + |
| 32 | +```java |
| 33 | + Vec3f a = new Vec3f(1.0f, 1.0f, 1.0f); |
| 34 | + Vec3f b = new Vec3f(0.5f, 0.5f, 0.5f); |
| 35 | + |
| 36 | + // 'a' and 'b' will not change |
| 37 | + Vec3f c = a.plus(b); |
| 38 | + |
| 39 | + // Increase 'a' by 'b' |
| 40 | + a = a.plus(b); |
| 41 | +``` |
| 42 | + |
| 43 | +The Vector API offers integer, single-precision and double-precision vectors with all their basic operations. |
| 44 | + |
| 45 | +```scala |
| 46 | + var a = Vec3f(1.0f, 1.0f, 1.0f) |
| 47 | + var b = Vec3f(0.5f, 0.5f, 0.5f) |
| 48 | + |
| 49 | + var dotProduct = a dot b |
| 50 | + var normal = a.normalized |
| 51 | + var reflection = b.reflect(normal) |
| 52 | +``` |
| 53 | + |
| 54 | +```java |
| 55 | + Vec3f a = new Vec3f(1.0f, 1.0f, 1.0f); |
| 56 | + Vec3f b = new Vec3f(0.5f, 0.5f, 0.5f); |
| 57 | + |
| 58 | + float dotProduct = a.dot(b); |
| 59 | + Vec3d normal = a.normalized(); |
| 60 | + Vec3d reflection = b.reflect(normal); |
| 61 | +``` |
| 62 | + |
| 63 | +## Matrix API |
| 64 | + |
| 65 | +With VecMatLib you can create matrices for geometric transformations such as translation, rotation, and scale. |
| 66 | +None of these operations modify the matrix on which they are called. |
| 67 | + |
| 68 | +```scala |
| 69 | + var position = Vec4f(x, y, z, 1.0f) |
| 70 | + val translation = Mat4f.translation(tx, ty, tz) |
| 71 | + |
| 72 | + // will result in (x + tx, y + ty, z + tz, 1.0f) |
| 73 | + position = translation * position |
| 74 | +``` |
| 75 | + |
| 76 | +```java |
| 77 | + Vec4f position = new Vec4f(x, y, z, 1.0f); |
| 78 | + Mat4f translation = Mat4f.translation(tx, ty, tz); |
| 79 | + |
| 80 | + // will result in (x + tx, y + ty, z + tz, 1.0f) |
| 81 | + position = translation.multiply(position); |
| 82 | +``` |
| 83 | + |
| 84 | +## Using with [LWJGL](https://lwjgl.org) |
| 85 | + |
| 86 | +VecMatLib can be used together with LWJGL to set uniform variables in shaders. |
| 87 | + |
| 88 | +```java |
| 89 | + Vec3f lightPosition = new Vec3f(-3.0f, 10.0f, 6.0f); |
| 90 | + int location = GL20.glGetUniformLocation(program, "light_position"); |
| 91 | + GL20.glUniform3f(location, lightPosition.x(), lightPosition.y(), lightPosition.z()); |
| 92 | +``` |
| 93 | + |
| 94 | +## Add VecMatLib to your project |
| 95 | + |
| 96 | +VecMatLib can be added to any Java or Scala project as a dependency using Jitpack. |
| 97 | + |
| 98 | +### Gradle |
| 99 | + |
| 100 | +```groovy |
| 101 | + allprojects { |
| 102 | + repositories { |
| 103 | + ... |
| 104 | + maven { url 'https://jitpack.io' } |
| 105 | + } |
| 106 | + } |
| 107 | +``` |
| 108 | + |
| 109 | +```groovy |
| 110 | + dependencies { |
| 111 | + implementation 'com.github.HexagonNico:VecMatLib:0.1' |
| 112 | + } |
| 113 | +``` |
| 114 | + |
| 115 | +### Maven |
| 116 | + |
| 117 | +```xml |
| 118 | + <repositories> |
| 119 | + <repository> |
| 120 | + <id>jitpack.io</id> |
| 121 | + <url>https://jitpack.io</url> |
| 122 | + </repository> |
| 123 | + </repositories> |
| 124 | +``` |
| 125 | + |
| 126 | +```xml |
| 127 | + <dependency> |
| 128 | + <groupId>com.github.HexagonNico</groupId> |
| 129 | + <artifactId>VecMatLib</artifactId> |
| 130 | + <version>0.1</version> |
| 131 | + </dependency> |
| 132 | +``` |
| 133 | + |
| 134 | +### SBT |
| 135 | + |
| 136 | +```sbt |
| 137 | + resolvers += "jitpack" at "https://jitpack.io" |
| 138 | +``` |
| 139 | + |
| 140 | +```sbt |
| 141 | + libraryDependencies += "com.github.HexagonNico" % "VecMatLib" % "0.1" |
| 142 | +``` |
| 143 | + |
| 144 | +## Support the project |
| 145 | + |
| 146 | +VecMatLib was developed by a single person. |
| 147 | + |
| 148 | +Initially a university project, later completed and turned into a full project. |
| 149 | + |
| 150 | +[](https://ko-fi.com/X8X87EZ87) |
| 151 | + |
| 152 | +## Contributing |
| 153 | + |
| 154 | +Your contributions are always welcome! Please submit a pull request or open an issue if you want to contribute with bug fixes, code improvements and better unit test coverage. |
0 commit comments