summary refs log tree commit diff
path: root/src/main.odin
diff options
context:
space:
mode:
authoriurii <[email protected]>2024-09-07 12:51:36 +0200
committeriurii <[email protected]>2024-09-07 12:51:36 +0200
commit7019eba5e9fe2500e30b542e8a982a4edae40940 (patch)
tree23b8a9de581f30022e7c97b2fa0913bfc9ac30db /src/main.odin
parentc78784795449732a4d7edb72af28506ab16d0117 (diff)
downloadfunhalla-7019eba5e9fe2500e30b542e8a982a4edae40940.tar.gz
refactored and wip texture loader
Diffstat (limited to '')
-rw-r--r--src/main.odin322
1 files changed, 72 insertions, 250 deletions
diff --git a/src/main.odin b/src/main.odin
index 67f4c4c..8aa1844 100644
--- a/src/main.odin
+++ b/src/main.odin
@@ -1,7 +1,6 @@
-package main
+package funhalla
 import gl "vendor:OpenGL"
 import "vendor:glfw"
-import "vendor:stb/image"
 
 import "base:intrinsics"
 import "base:runtime"
@@ -11,14 +10,12 @@ import "core:math"
 import "core:math/linalg"
 import "core:os"
 
-import cam "camera"
-import "shader"
+Vec3 :: linalg.Vector3f32
+Mat4 :: linalg.Matrix4x4f32
 
 GL_MAJOR_VERSION :: 3
 GL_MINOR_VERSION :: 3
 
-Vec3 :: linalg.Vector3f32
-
 
 delta_time: f32 = 0.0
 last_frame: f32 = 0.0
@@ -27,7 +24,7 @@ SCREEN_WIDTH: f32 : 800.0
 SCREEN_HEIGTH: f32 : 600.0
 
 
-camera := cam.camera_init(Vec3{0.0, 0.0, 3.0})
+camera := camera_init(Vec3{0.0, 0.0, 3.0})
 
 first_mouse := true
 last_x: f32 = 800.0 / 2.0
@@ -41,7 +38,6 @@ framebuffer_size_callback :: proc "cdecl" (window: glfw.WindowHandle, width, hei
 
 mouse_callback :: proc "cdecl" (window: glfw.WindowHandle, xpos_in, ypos_in: f64) {
 	context = runtime.default_context()
-	using cam
 	xpos: f32 = f32(xpos_in)
 	ypos: f32 = f32(ypos_in)
 
@@ -60,13 +56,11 @@ mouse_callback :: proc "cdecl" (window: glfw.WindowHandle, xpos_in, ypos_in: f64
 }
 
 mouse_scroll_callback :: proc "cdecl" (window: glfw.WindowHandle, xoffset, yoffset: f64) {
-	using cam
 	context = runtime.default_context()
 	process_mouse_scroll(camera, f32(yoffset))
 }
 
 process_input :: proc(window: ^glfw.WindowHandle) {
-	using cam
 	if glfw.GetKey(window^, glfw.KEY_ESCAPE) == glfw.PRESS {
 		glfw.SetWindowShouldClose(window^, true)
 	}
@@ -121,240 +115,66 @@ main :: proc() {
 	glfw.SetScrollCallback(window, mouse_scroll_callback)
 	glfw.SetInputMode(window, glfw.CURSOR, glfw.CURSOR_DISABLED)
 
-	light_cube_shader, err := shader.shader_init(
+	light_cube_shader, err := shader_init(
 		"res/shaders/light_cube.vs",
 		"res/shaders/light_cube.fs",
 	)
-	if err == shader.SHADER_LOAD_ERROR {
+	if err == SHADER_LOAD_ERROR {
 		fmt.eprintln("Could not initialize shader")
 		return
 	}
 
-	lighting_shader: ^shader.Shader
-	lighting_shader, err = shader.shader_init("res/shaders/colors.vs", "res/shaders/colors.fs")
+	lighting_shader: ^Shader
+	lighting_shader, err = shader_init("res/shaders/colors.vs", "res/shaders/colors.fs")
 
-	if err == shader.SHADER_LOAD_ERROR {
+	if err == SHADER_LOAD_ERROR {
 		fmt.eprintln("Could not initialize shader")
 		return
 	}
 
 	vertices: []f32 = {
-		-0.5,
-		-0.5,
-		-0.5,
-		0.0,
-		0.0,
-		-1.0,
-		0.5,
-		-0.5,
-		-0.5,
-		0.0,
-		0.0,
-		-1.0,
-		0.5,
-		0.5,
-		-0.5,
-		0.0,
-		0.0,
-		-1.0,
-		0.5,
-		0.5,
-		-0.5,
-		0.0,
-		0.0,
-		-1.0,
-		-0.5,
-		0.5,
-		-0.5,
-		0.0,
-		0.0,
-		-1.0,
-		-0.5,
-		-0.5,
-		-0.5,
-		0.0,
-		0.0,
-		-1.0,
-		-0.5,
-		-0.5,
-		0.5,
-		0.0,
-		0.0,
-		1.0,
-		0.5,
-		-0.5,
-		0.5,
-		0.0,
-		0.0,
-		1.0,
-		0.5,
-		0.5,
-		0.5,
-		0.0,
-		0.0,
-		1.0,
-		0.5,
-		0.5,
-		0.5,
-		0.0,
-		0.0,
-		1.0,
-		-0.5,
-		0.5,
-		0.5,
-		0.0,
-		0.0,
-		1.0,
-		-0.5,
-		-0.5,
-		0.5,
-		0.0,
-		0.0,
-		1.0,
-		-0.5,
-		0.5,
-		0.5,
-		-1.0,
-		0.0,
-		0.0,
-		-0.5,
-		0.5,
-		-0.5,
-		-1.0,
-		0.0,
-		0.0,
-		-0.5,
-		-0.5,
-		-0.5,
-		-1.0,
-		0.0,
-		0.0,
-		-0.5,
-		-0.5,
-		-0.5,
-		-1.0,
-		0.0,
-		0.0,
-		-0.5,
-		-0.5,
-		0.5,
-		-1.0,
-		0.0,
-		0.0,
-		-0.5,
-		0.5,
-		0.5,
-		-1.0,
-		0.0,
-		0.0,
-		0.5,
-		0.5,
-		0.5,
-		1.0,
-		0.0,
-		0.0,
-		0.5,
-		0.5,
-		-0.5,
-		1.0,
-		0.0,
-		0.0,
-		0.5,
-		-0.5,
-		-0.5,
-		1.0,
-		0.0,
-		0.0,
-		0.5,
-		-0.5,
-		-0.5,
-		1.0,
-		0.0,
-		0.0,
-		0.5,
-		-0.5,
-		0.5,
-		1.0,
-		0.0,
-		0.0,
-		0.5,
-		0.5,
-		0.5,
-		1.0,
-		0.0,
-		0.0,
-		-0.5,
-		-0.5,
-		-0.5,
-		0.0,
-		-1.0,
-		0.0,
-		0.5,
-		-0.5,
-		-0.5,
-		0.0,
-		-1.0,
-		0.0,
-		0.5,
-		-0.5,
-		0.5,
-		0.0,
-		-1.0,
-		0.0,
-		0.5,
-		-0.5,
-		0.5,
-		0.0,
-		-1.0,
-		0.0,
-		-0.5,
-		-0.5,
-		0.5,
-		0.0,
-		-1.0,
-		0.0,
-		-0.5,
-		-0.5,
-		-0.5,
-		0.0,
-		-1.0,
-		0.0,
-		-0.5,
-		0.5,
-		-0.5,
-		0.0,
-		1.0,
-		0.0,
-		0.5,
-		0.5,
-		-0.5,
-		0.0,
-		1.0,
-		0.0,
-		0.5,
-		0.5,
-		0.5,
-		0.0,
-		1.0,
-		0.0,
-		0.5,
-		0.5,
-		0.5,
-		0.0,
-		1.0,
-		0.0,
-		-0.5,
-		0.5,
-		0.5,
-		0.0,
-		1.0,
-		0.0,
-		-0.5,
-		0.5,
-		-0.5,
-		0.0,
-		1.0,
-		0.0,
+    // positions          // normals           // texture coords
+    -0.5, -0.5, -0.5,  0.0,  0.0, -1.0,  0.0, 0.0,
+     0.5, -0.5, -0.5,  0.0,  0.0, -1.0,  1.0, 0.0,
+     0.5,  0.5, -0.5,  0.0,  0.0, -1.0,  1.0, 1.0,
+     0.5,  0.5, -0.5,  0.0,  0.0, -1.0,  1.0, 1.0,
+    -0.5,  0.5, -0.5,  0.0,  0.0, -1.0,  0.0, 1.0,
+    -0.5, -0.5, -0.5,  0.0,  0.0, -1.0,  0.0, 0.0,
+
+    -0.5, -0.5,  0.5,  0.0,  0.0, 1.0,   0.0, 0.0,
+     0.5, -0.5,  0.5,  0.0,  0.0, 1.0,   1.0, 0.0,
+     0.5,  0.5,  0.5,  0.0,  0.0, 1.0,   1.0, 1.0,
+     0.5,  0.5,  0.5,  0.0,  0.0, 1.0,   1.0, 1.0,
+    -0.5,  0.5,  0.5,  0.0,  0.0, 1.0,   0.0, 1.0,
+    -0.5, -0.5,  0.5,  0.0,  0.0, 1.0,   0.0, 0.0,
+
+    -0.5,  0.5,  0.5, -1.0,  0.0,  0.0,  1.0, 0.0,
+    -0.5,  0.5, -0.5, -1.0,  0.0,  0.0,  1.0, 1.0,
+    -0.5, -0.5, -0.5, -1.0,  0.0,  0.0,  0.0, 1.0,
+    -0.5, -0.5, -0.5, -1.0,  0.0,  0.0,  0.0, 1.0,
+    -0.5, -0.5,  0.5, -1.0,  0.0,  0.0,  0.0, 0.0,
+    -0.5,  0.5,  0.5, -1.0,  0.0,  0.0,  1.0, 0.0,
+
+     0.5,  0.5,  0.5,  1.0,  0.0,  0.0,  1.0, 0.0,
+     0.5,  0.5, -0.5,  1.0,  0.0,  0.0,  1.0, 1.0,
+     0.5, -0.5, -0.5,  1.0,  0.0,  0.0,  0.0, 1.0,
+     0.5, -0.5, -0.5,  1.0,  0.0,  0.0,  0.0, 1.0,
+     0.5, -0.5,  0.5,  1.0,  0.0,  0.0,  0.0, 0.0,
+     0.5,  0.5,  0.5,  1.0,  0.0,  0.0,  1.0, 0.0,
+
+    -0.5, -0.5, -0.5,  0.0, -1.0,  0.0,  0.0, 1.0,
+     0.5, -0.5, -0.5,  0.0, -1.0,  0.0,  1.0, 1.0,
+     0.5, -0.5,  0.5,  0.0, -1.0,  0.0,  1.0, 0.0,
+     0.5, -0.5,  0.5,  0.0, -1.0,  0.0,  1.0, 0.0,
+    -0.5, -0.5,  0.5,  0.0, -1.0,  0.0,  0.0, 0.0,
+    -0.5, -0.5, -0.5,  0.0, -1.0,  0.0,  0.0, 1.0,
+
+    -0.5,  0.5, -0.5,  0.0,  1.0,  0.0,  0.0, 1.0,
+     0.5,  0.5, -0.5,  0.0,  1.0,  0.0,  1.0, 1.0,
+     0.5,  0.5,  0.5,  0.0,  1.0,  0.0,  1.0, 0.0,
+     0.5,  0.5,  0.5,  0.0,  1.0,  0.0,  1.0, 0.0,
+    -0.5,  0.5,  0.5,  0.0,  1.0,  0.0,  0.0, 0.0,
+    -0.5,  0.5, -0.5,  0.0,  1.0,  0.0,  0.0, 1.0
 	}
 
 
@@ -371,15 +191,17 @@ main :: proc() {
 
 	gl.GenVertexArrays(1, &cube_vao)
 	gl.BindVertexArray(cube_vao)
-	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0)
+	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 0)
 	gl.EnableVertexAttribArray(0)
-	gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 3 * size_of(f32))
+	gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 3 * size_of(f32))
 	gl.EnableVertexAttribArray(1)
+  gl.VertexAttribPointer(2, 2, gl.FLOAT, gl.FALSE, 8 * size_of(f32), 6 * size_of(f32));
+	gl.EnableVertexAttribArray(2)
 
 	gl.GenVertexArrays(1, &light_cube_vao)
 	gl.BindVertexArray(light_cube_vao)
 	gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
-	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0)
+	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 8  * size_of(f32), 0)
 	gl.EnableVertexAttribArray(0)
 
 	//gl.PolygonMode(gl.FRONT_AND_BACK, gl.LINE)
@@ -397,7 +219,7 @@ main :: proc() {
 		gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
 
 
-		shader.use(lighting_shader)
+		shader_use(lighting_shader)
 
 		light_color := Vec3 {
 			cast(f32)math.sin(glfw.GetTime() * 2.0),
@@ -407,20 +229,20 @@ main :: proc() {
 		material_ambient := Vec3{1.0, 0.5, 0.31}
 		material_diffuse := Vec3{1.0, 0.5, 0.31}
 		material_specular := Vec3{0.5, 0.5, 0.5}
-		shader.set_vec3(lighting_shader, cstring("material.ambient"), &material_ambient)
-		shader.set_vec3(lighting_shader, cstring("material.diffuse"), &material_diffuse)
-		shader.set_vec3(lighting_shader, cstring("material.specular"), &material_specular)
-		shader.set_f32(lighting_shader, cstring("material.shininess"), 32.0)
+		shader_set_vec3(lighting_shader, cstring("material.ambient"), &material_ambient)
+		shader_set_vec3(lighting_shader, cstring("material.diffuse"), &material_diffuse)
+		shader_set_vec3(lighting_shader, cstring("material.specular"), &material_specular)
+		shader_set_f32(lighting_shader, cstring("material.shininess"), 32.0)
 
 		light_diffuse := light_color * Vec3{0.5, 0.5, 0.05}
 		light_ambient := light_diffuse * Vec3{0.2, 0.2, 0.2}
 		light_specular := Vec3{1.0, 1.0, 1.0}
-		shader.set_vec3(lighting_shader, cstring("light.ambient"), &light_ambient)
-		shader.set_vec3(lighting_shader, cstring("light.diffuse"), &light_diffuse)
-		shader.set_vec3(lighting_shader, cstring("light.specular"), &light_specular)
+		shader_set_vec3(lighting_shader, cstring("light.ambient"), &light_ambient)
+		shader_set_vec3(lighting_shader, cstring("light.diffuse"), &light_diffuse)
+		shader_set_vec3(lighting_shader, cstring("light.specular"), &light_specular)
 
-		shader.set_vec3(lighting_shader, cstring("light_position"), &light_pos)
-		shader.set_vec3(lighting_shader, cstring("view_position"), &camera.position)
+		shader_set_vec3(lighting_shader, cstring("light_position"), &light_pos)
+		shader_set_vec3(lighting_shader, cstring("view_position"), &camera.position)
 
 		aspect: f32 = 800.0 / 600.0
 		projection := linalg.matrix4_perspective_f32(
@@ -430,7 +252,7 @@ main :: proc() {
 			100.0,
 		)
 
-		view := cam.get_view_matrix(camera)
+		view := get_view_matrix(camera)
 
 		view_location := gl.GetUniformLocation(lighting_shader.id, "view")
 		gl.UniformMatrix4fv(view_location, 1, gl.FALSE, &view[0][0])
@@ -439,19 +261,19 @@ main :: proc() {
 		gl.UniformMatrix4fv(projection_location, 1, gl.FALSE, &projection[0][0])
 
 		model := linalg.MATRIX4F32_IDENTITY
-		shader.set_mat4(lighting_shader, cstring("model"), &model)
+		shader_set_mat4(lighting_shader, cstring("model"), &model)
 
 		gl.BindVertexArray(cube_vao)
 		gl.DrawArrays(gl.TRIANGLES, 0, 36)
 
 		// lamp cube object drawing
-		shader.use(light_cube_shader)
-		shader.set_mat4(light_cube_shader, cstring("projection"), &projection)
-		shader.set_mat4(light_cube_shader, cstring("view"), &view)
+		shader_use(light_cube_shader)
+		shader_set_mat4(light_cube_shader, cstring("projection"), &projection)
+		shader_set_mat4(light_cube_shader, cstring("view"), &view)
 
 		model = linalg.matrix4_translate_f32(light_pos)
 		model *= linalg.matrix4_scale_f32(Vec3{0.2, 0.2, 0.2})
-		shader.set_mat4(light_cube_shader, cstring("model"), &model)
+		shader_set_mat4(light_cube_shader, cstring("model"), &model)
 
 		gl.BindVertexArray(light_cube_vao)
 		gl.DrawArrays(gl.TRIANGLES, 0, 36)