-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.odin
72 lines (54 loc) · 2.45 KB
/
main.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import "core:math"
import rl "vendor:raylib"
main :: proc() {
rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Renderer")
rl.SetTargetFPS(60)
cube := MakeCube()
texture := LoadTextureFromFile("assets/uv_checker.png")
camera := MakeCamera({0.0, 0.0, -5.0})
light := MakeLight({0.0, -1.0, 0.0})
rotation := Vector3{0.0, 0.0, 0.0}
translation := Vector3{0.0, 0.0, 0.0}
scale: f32 = 1.0
zBuffer := MakeZBuffer(SCREEN_WIDTH, SCREEN_HEIGHT)
renderModesCount :: 6
renderMode: i8 = renderModesCount - 1
for !rl.WindowShouldClose() {
HandleInputs(&translation, &rotation, &scale, &renderMode, renderModesCount)
// Translation
translationMatrix := MakeTranslationMatrix(translation.x, translation.y, translation.z)
// Rotation
rotationX := MakeRotationMatrixX(rotation.x)
rotationY := MakeRotationMatrixY(rotation.y)
rotationZ := MakeRotationMatrixZ(rotation.z)
// Scale
scaleMatrix := MakeScaleMatrix(scale, scale, scale)
// Transformations
viewMatrix := MakeViewMatrix(camera.position, camera.target)
modelMatrix := Mat4Mul(&rotationX, &rotationY)
modelMatrix = Mat4Mul(&modelMatrix, &rotationZ)
modelMatrix = Mat4Mul(&translationMatrix, &modelMatrix)
modelMatrix = Mat4Mul(&scaleMatrix, &modelMatrix)
finalMatrix := Mat4Mul(&viewMatrix, &modelMatrix)
TransformVertices(&cube.transformedVertices, &cube.vertices, &finalMatrix)
rl.BeginDrawing()
rl.ClearBackground(rl.BLACK)
ClearZBuffer(&zBuffer)
switch renderMode {
case 5: DrawTexturedShaded(&cube.transformedVertices, &cube.triangles, &cube.uvs, light, &texture, &zBuffer)
case 4: DrawTextured(&cube.transformedVertices, &cube.triangles, &cube.uvs, &texture, &zBuffer)
case 3: DrawFlatShaded(&cube.transformedVertices, &cube.triangles, light, rl.WHITE)
case 2: DrawLit(&cube.transformedVertices, &cube.triangles, rl.WHITE)
case 1: DrawWireframe(&cube.transformedVertices, &cube.triangles, rl.RED)
case 0: DrawWireframe(&cube.transformedVertices, &cube.triangles, rl.RED, false)
}
rl.EndDrawing()
}
rl.CloseWindow()
}
TransformVertices :: proc(transformedVertices, vertices: ^[]Vector3, mat: ^Matrix4x4) {
for i in 0..<len(vertices) {
transformedVertices[i] = Mat4MulVec3(mat, &vertices[i])
}
}