glm: add uniform functions to gl, add matrix rotation to glm
parent
047e982318
commit
a9999ee10d
19
vlib/gl/gl.v
19
vlib/gl/gl.v
|
@ -63,6 +63,9 @@ fn C.glGetUniformLocation() int
|
|||
fn C.glGetAttribLocation() int
|
||||
fn C.glBindAttribLocation()
|
||||
|
||||
//Uniform functions
|
||||
fn C.glUniform1f()
|
||||
|
||||
|
||||
pub fn init_glad() {
|
||||
ok := C.gladLoadGL()
|
||||
|
@ -292,4 +295,20 @@ pub fn generate_mipmap(typ int) {
|
|||
// set mat4 at uniform location
|
||||
pub fn set_mat4fv(loc, count int, transpose bool, val glm.Mat4) {
|
||||
C.glUniformMatrix4fv(loc, count, transpose, val.data)
|
||||
}
|
||||
|
||||
pub fn set_f32(loc int, val f32) {
|
||||
C.glUniform1f(loc, val)
|
||||
}
|
||||
|
||||
pub fn set_vec(loc int, x, y, z f32) {
|
||||
C.glUniform3f(loc, x, y, z)
|
||||
}
|
||||
|
||||
pub fn set_bool(loc int, val bool) {
|
||||
if val {
|
||||
set_f32(loc, 1)
|
||||
} else {
|
||||
set_f32(loc, 0)
|
||||
}
|
||||
}
|
|
@ -65,7 +65,7 @@ pub fn (m Mat4) str() string {
|
|||
}
|
||||
for j in 0..4 {
|
||||
val := m.data[i * 4 + j]
|
||||
s += '${val:.2f} '
|
||||
s += '${val:5.2f} '
|
||||
}
|
||||
if i != 3 {
|
||||
s += '\n'
|
||||
|
@ -141,9 +141,9 @@ pub fn translate(m Mat4, v Vec3) Mat4 {
|
|||
x := v.x
|
||||
y := v.y
|
||||
z := v.z
|
||||
a00 := a[0]a01 := a[1]a02 := a[2]a03 := a[3]
|
||||
a10 := a[4]a11 := a[5]a12 := a[6]a13 := a[7]
|
||||
a20 := a[8]a21 := a[9]a22 := a[10]a23 := a[11]
|
||||
a00 := a[0] a01 := a[1] a02 := a[2] a03 := a[3]
|
||||
a10 := a[4] a11 := a[5] a12 := a[6] a13 := a[7]
|
||||
a20 := a[8] a21 := a[9] a22 := a[10] a23 := a[11]
|
||||
out[0] = a00 out[1] = a01 out[2] = a02 out[3] = a03
|
||||
out[4] = a10 out[5] = a11 out[6] = a12 out[7] = a13
|
||||
out[8] = a20 out[9] = a21 out[10] = a22 out[11] = a23
|
||||
|
@ -272,6 +272,55 @@ fn mult_mat_point(a Mat4, point Mat4) Mat4 {
|
|||
return mat4(out)
|
||||
}
|
||||
|
||||
pub fn rotate(angle f32, axis Vec3, src Mat4) Mat4 {
|
||||
c := math.cos(angle)
|
||||
s := math.sin(angle)
|
||||
oneminusc := 1.0 - c
|
||||
|
||||
xy := axis.x * axis.y
|
||||
yz := axis.y * axis.z
|
||||
xz := axis.x * axis.z
|
||||
xs := axis.x * s
|
||||
ys := axis.y * s
|
||||
zs := axis.z * s
|
||||
|
||||
f00 := axis.x * axis.x * oneminusc + c
|
||||
f01 := xy * oneminusc + zs
|
||||
f02 := xz * oneminusc - ys
|
||||
|
||||
f10 := xy * oneminusc-zs
|
||||
f11 := axis.y * axis.y * oneminusc + c
|
||||
f12 := yz * oneminusc + xs
|
||||
|
||||
f20 := xz * oneminusc + ys
|
||||
f21 := yz * oneminusc - xs
|
||||
f22 := axis.z *axis.z * oneminusc + c
|
||||
|
||||
data := src.data
|
||||
|
||||
t00 := data[0] * f00 + data[4] * f01 + data[8] * f02
|
||||
t01 := data[1] * f00 + data[5] * f01 + data[9] * f02
|
||||
t02 := data[2] * f00 + data[6] * f01 + data[10] * f02
|
||||
t03 := data[3] * f00 + data[7] * f01 + data[11] * f02
|
||||
|
||||
t10 := data[0] * f10 + data[4] * f11 + data[8] * f12
|
||||
t11 := data[1] * f10 + data[5] * f11 + data[9] * f12
|
||||
t12 := data[2] * f10 + data[6] * f11 + data[10] * f12
|
||||
t13 := data[3] * f10 + data[7] * f11 + data[11] * f12
|
||||
|
||||
mut dest := src.data
|
||||
|
||||
dest[8] = data[0] * f20 + data[4] * f21 + data[8] * f22
|
||||
dest[9] = data[1] * f20 + data[5] * f21 + data[9] * f22
|
||||
dest[10] = data[2] * f20 + data[6] * f21 + data[10] * f22
|
||||
dest[11] = data[3] * f20 + data[7] * f21 + data[11] * f22
|
||||
|
||||
dest[0] = t00 dest[1] = t01 dest[2] = t02 dest[3] = t03
|
||||
dest[4] = t10 dest[5] = t11 dest[6] = t12 dest[7] = t13
|
||||
|
||||
return mat4(dest)
|
||||
}
|
||||
|
||||
// fn rotate_z(a *f32, rad f32) *f32 {
|
||||
pub fn rotate_z(m Mat4, rad f32) Mat4 {
|
||||
a := m.data
|
||||
|
|
|
@ -67,6 +67,20 @@ fn test_rotate() {
|
|||
$if debug {
|
||||
println(m)
|
||||
}
|
||||
|
||||
mut m1 := glm.identity()
|
||||
mut m2 := glm.identity()
|
||||
|
||||
m1 = glm.rotate(1, glm.vec3(1, 0, 0), m1)
|
||||
m2 = glm.rotate(1, glm.vec3(0, 1, 0), m2)
|
||||
|
||||
mut same := true
|
||||
for i in 0..15 {
|
||||
if m1.data[i] != m2.data[i] {
|
||||
same = false
|
||||
}
|
||||
}
|
||||
assert !same
|
||||
}
|
||||
|
||||
fn test_translate() {
|
||||
|
@ -75,11 +89,25 @@ fn test_translate() {
|
|||
$if debug {
|
||||
println(m)
|
||||
}
|
||||
// TODO
|
||||
// mat4x4((1.000000, 0.000000, 0.000000, 0.000000),
|
||||
// (0.000000, 1.000000, 0.000000, 0.000000),
|
||||
// (0.000000, 0.000000, 1.000000, 0.000000),
|
||||
// (0.000000, 0.000000, -0.500000, 1.000000))
|
||||
assert m.data[0] == 1.0
|
||||
assert m.data[1] == 0.0
|
||||
assert m.data[2] == 0.0
|
||||
assert m.data[3] == 0.0
|
||||
//
|
||||
assert m.data[4] == 0.0
|
||||
assert m.data[5] == 1.0
|
||||
assert m.data[6] == 0.0
|
||||
assert m.data[7] == 0.0
|
||||
|
||||
assert m.data[8] == 0.0
|
||||
assert m.data[9] == 0.0
|
||||
assert m.data[10] == 1.0
|
||||
assert m.data[11] == 0.0
|
||||
//
|
||||
assert m.data[12] == 0.0
|
||||
assert m.data[13] == 0.0
|
||||
assert m.data[14] == -0.5
|
||||
assert m.data[15] == 1.0
|
||||
}
|
||||
|
||||
fn test_mult() {
|
||||
|
@ -91,4 +119,4 @@ fn test_mult() {
|
|||
for i in 0..15 {
|
||||
assert a.data[i] == c.data[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue