glm: matrix multiplication; docs: toc

pull/4823/head
Louis Schmieder 2020-05-10 16:49:29 +02:00 committed by GitHub
parent 72dfe11fa5
commit 3ec2608d6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 148 additions and 5 deletions

View File

@ -18,10 +18,52 @@ you can do in V.
<table>
<tr>
<td><a href='#hello-world'>1. Hello world</a></td>
<td><a href='#comments'>2. Comments</a></td>
<td><a href='#functions'>3. Functions</a></td>
<td><a href='#'>TODO</a></td>
<td><a href='#hello-world'>Hello world</a></td>
<td><a href='#comments'>Comments</a></td>
<td><a href='#functions'>Functions</a></td>
<td><a href='#constants--variables'>Constants & variables</a></td>
<td><a href='#primitive-types'>Primitive types</a></td>
<td><a href='#strings'>Strings</a></td>
<td><a href='#imports'>Imports</a></td>
<td><a href='#arrays'>Arrays</a></td>
<td><a href='#maps'>Maps</a></td>
<td><a href='#if'>If</a></td>
<td><a href='#in-operator'>In Operator</a></td>
<td><a href='#for-loop'>For loop</a></td>
<td><a href='#match'>Match</a></td>
<td><a href='#structs'>Structs</a></td>
<td><a href='#access-modifiers'>Access modifiers</a></td>
<td><a href='#methods'>Methods</a></td>
<td><a href='#pure-functions-by-default'>Pure functions by default</a></td>
<td><a href='#anonymous--high-order-functions'>Anonymous & high order functions</a></td>
<td><a href='#references'>References</a></td>
<td><a href='#constants'>Constants</a></td>
<td><a href='#println'>println</a></td>
<td><a href='#modules'>Modules</a></td>
<td><a href='#interfaces'>Interfaces</a></td>
<td><a href='#enums'>Enums</a></td>
<td><a href='#sum-types'>Sum types</a></td>
<td><a href='#optionresult-types-and-error-handtdng'>Option/Result types and error handtdng</a></td>
<td><a href='#generics'>Generics</a></td>
<td><a href='#concurrency'>Concurrency</a></td>
<td><a href='#decoding-json'>Decoding JSON</a></td>
<td><a href='#testing'>Testing</a></td>
<td><a href='#memory-management'>Memory managment</a></td>
<td><a href='#defer'>Defer</a></td>
<td><a href='#orm'>ORM</a></td>
<td><a href='#vfmt'>vfmt</a></td>
<td><a href='#writing-documentation'>Writing documentation</a></td>
<td><a href='caltdng-c-functions-from-v'>Caltdng C functions from V</a></td>
<td><a href='conditional-compilation'>Conditional compilation</a></td>
<td><a href='reflection-via-codegen'>Reflection via codegen</a></td>
<td><a href='tdmited-operator-overloading'>tdmited operator overloading</a></td>
<td><a href='intdne-assembly'>Intdne assembly</a></td>
<td><a href='translating-cc-to-v'>Translating C/C++ to V</a></td>
<td><a href='hot-code-reloading'>Hot code reloading</a></td>
<td><a href='cross-compilation'>Cross compilation</a></td>
<td><a href='cross-platform-shell-scripts-in-v'>Cross-platform shell scripts in V</a></td>
<td><a href='appendix-i-keywords'>Appendix I: Keywords</a></td>
<td><a href='appendix-ii-keywords'>Appendix II: Operators</a></td>
</tr>
</table>

View File

@ -179,7 +179,6 @@ pub fn (s Shader) use() {
gl.use_program(s.program_id)
}
fn C.glGetUniformLocation() int
fn C.glUniformMatrix4fv()
fn C.glUniform1i()
fn C.glUniform3f()

View File

@ -4,10 +4,13 @@
module gl
import glm
#flag -I @VROOT/thirdparty/glad
#include "glad.h"
#flag @VROOT/thirdparty/glad/glad.o
// joe-c: fix & remove
pub enum TmpGlImportHack{ non_empty }
@ -51,6 +54,8 @@ fn C.glPixelStorei()
fn C.glBlendFunc()
fn C.glPolygonMode()
fn C.glDeleteBuffers()
fn C.glGetUniformLocation() int
fn C.glGetAttribLocation() int
pub fn init_glad() {
@ -181,6 +186,17 @@ pub fn set_ebo(ebo u32, indices []int, draw_typ int) {
// fn gen_vertex_arrays(a int, vao uint) {
// # glGenVertexArrays(a, &VAO);
// }
// gets the uniform location for key
pub fn get_uniform_location(program int, key string) int {
return C.glGetUniformLocation(program, key.str)
}
//gets the attribute location for key
pub fn get_attrib_location(program int, key string) int {
return C.glGetAttribLocation(program, key.str)
}
pub fn draw_arrays(typ, start, len int) {
C.glDrawArrays(typ, start, len)
}
@ -239,3 +255,7 @@ pub fn generate_mipmap(typ int) {
C.glGenerateMipmap(typ)
}
// set mat4 at uniform location
pub fn set_mat4fv(loc, count int, transpose bool, val glm.Mat4) {
C.glUniformMatrix4fv(loc, count, transpose, val.data)
}

View File

@ -175,6 +175,22 @@ pub fn ortho(left, right, bottom, top f32) Mat4 {
return mat4(res)
}
// https://github.com/g-truc/glm/blob/0ceb2b755fb155d593854aefe3e45d416ce153a4/glm/ext/matrix_clip_space.inl
pub fn ortho_zo(left, right, bottom, top, zNear, zFar f32) Mat4 {
//println('glm ortho($left, $right, $bottom, $top)')
// mat<4, 4, T, defaultp> Result(static_cast<T>(1));
n := 16
mut res := f32_calloc(n)
res[0] = 2.0 / (right - left)
res[5] = 2.0 / (top - bottom)
res[10] = 1.0
res[12] = - (right + left) / (right - left)
res[13] = - (top + bottom) / (top - bottom)
res[14] = - zNear / (zFar - zNear)
res[15] = 1.0
return mat4(res)
}
// fn scale(a *f32, v Vec3) *f32 {
pub fn scale(m Mat4, v Vec3) Mat4 {
a := m.data
@ -201,6 +217,62 @@ pub fn scale(m Mat4, v Vec3) Mat4 {
return mat4(out)
}
// multiplicates two matrices
pub fn mult(a, b Mat4) Mat4 {
da := a.data
db := b.data
mut out := f32_calloc(16)
mut row0 := f32_calloc(4)
mut row1 := f32_calloc(4)
mut row2 := f32_calloc(4)
mut row3 := f32_calloc(4)
row0[0] = db[0]row0[1] = db[1]row0[2] = db[2]row0[3] = db[3]
row1[0] = db[4]row1[1] = db[5]row1[2] = db[6]row1[3] = db[7]
row2[0] = db[8]row2[1] = db[9]row2[2] = db[10]row2[3] = db[11]
row3[0] = db[12]row3[1] = db[13]row3[2] = db[14]row3[3] = db[15]
a_ := mult_mat_point(a, mat4(row0))
b_ := mult_mat_point(a, mat4(row1))
c_ := mult_mat_point(a, mat4(row2))
d_ := mult_mat_point(a, mat4(row3))
res0 := a_.data res1 := b_.data res2 := c_.data res3 := d_.data
out[0] = res0[0] out[1] = res0[1] out[2] = res0[2] out[3] = res0[3]
out[4] = res1[0] out[5] = res1[1] out[6] = res1[2] out[7] = res1[3]
out[8] = res2[0] out[9] = res2[1] out[10] = res2[2] out[11] = res2[3]
out[12] = res3[0] out[13] = res3[1] out[14] = res3[2] out[15] = res3[3]
return mat4(out)
}
// helper function for mult
fn mult_mat_point(a Mat4, point Mat4) Mat4 {
data := a.data
c0r0 := data[0]c1r0 := data[1]c2r0 := data[2]c3r0 := data[3]
c0r1 := data[4]c1r1 := data[5]c2r1 := data[6]c3r1 := data[7]
c0r2 := data[8]c1r2 := data[9]c2r2 := data[10]c3r2 := data[11]
c0r3 := data[12]c1r3 := data[13]c2r3 := data[14]c3r3 := data[15]
pdata := point.data
x := pdata[0]
y := pdata[1]
z := pdata[2]
w := pdata[3]
mut out := f32_calloc(4)
rx := (x * c0r0) + (y * c0r1) + (z * c0r2) + (w * c0r3)
ry := (x * c1r0) + (y * c1r1) + (z * c1r2) + (w * c1r3)
rz := (x * c2r0) + (y * c2r1) + (z * c2r2) + (w * c2r3)
rw := (x * c3r0) + (y * c3r1) + (z * c3r2) + (w * c3r3)
out[0] = rx out[1] = ry out[2] = rz out[3] = rw
return mat4(out)
}
// fn rotate_z(a *f32, rad f32) *f32 {
pub fn rotate_z(m Mat4, rad f32) Mat4 {
a := m.data

View File

@ -82,3 +82,13 @@ fn test_translate() {
// (0.000000, 0.000000, -0.500000, 1.000000))
}
fn test_mult() {
//TODO improve test
mut a := glm.identity()
mut b := glm.identity()
mut c := glm.identity()
a = glm.mult(a, b)
for i in 0..15 {
assert a.data[i] == c.data[i]
}
}