summary refs log tree commit diff
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
parentc78784795449732a4d7edb72af28506ab16d0117 (diff)
downloadfunhalla-7019eba5e9fe2500e30b542e8a982a4edae40940.tar.gz
refactored and wip texture loader
-rw-r--r--res/images/container.pngbin0 -> 467893 bytes
-rw-r--r--res/shaders/colors.fs8
-rw-r--r--res/shaders/colors.vs2
-rw-r--r--src/camera.odin (renamed from src/camera/camera.odin)5
-rw-r--r--src/main.odin322
-rw-r--r--src/shader.odin (renamed from src/shader/shader.odin)29
-rw-r--r--src/texture.odin12
7 files changed, 104 insertions, 274 deletions
diff --git a/res/images/container.png b/res/images/container.png
new file mode 100644
index 0000000..596e8da
--- /dev/null
+++ b/res/images/container.png
Binary files differdiff --git a/res/shaders/colors.fs b/res/shaders/colors.fs
index 053e1c4..79e243f 100644
--- a/res/shaders/colors.fs
+++ b/res/shaders/colors.fs
@@ -1,7 +1,6 @@
 #version 330 core
 struct Material {
-  vec3 ambient;
-  vec3 diffuse;
+  Sampler2D diffuse;
   vec3 specular;
   float shininess;
 };
@@ -17,6 +16,7 @@ out vec4 FragColor;
 
 in vec3 Normal;
 in vec3 FragPos;
+in vec3 TexCoords;
 
 uniform vec3 light_position;
 uniform vec3 view_position;
@@ -26,13 +26,13 @@ uniform Light light;
 
 void main() {
   // ambient
-  vec3 ambient = light.ambient * material.ambient;
+  vec3 ambient = light.ambient * vec3(texture(material.diffuse, TexCoords));
 
   // diffuse
   vec3 norm = normalize(Normal);
   vec3 light_dir = normalize(light_position - FragPos);
   float diff = max(dot(norm, light_dir), 0.0);
-  vec3 diffuse = light.diffuse * (diff * material.diffuse);
+  vec3 diffuse = light.diffuse * diff * vec3(texture(material.diffuse, TexCoords));
 
   // specular
   vec3 view_dir = normalize(view_position - FragPos);
diff --git a/res/shaders/colors.vs b/res/shaders/colors.vs
index 15cc265..9128abc 100644
--- a/res/shaders/colors.vs
+++ b/res/shaders/colors.vs
@@ -2,6 +2,7 @@
 
 layout (location = 0) in vec3 a_pos;
 layout (location = 1) in vec3 a_normal;
+layout (location = 2) in vec2 a_tex_coords;
 
 uniform mat4 model;
 uniform mat4 view;
@@ -9,6 +10,7 @@ uniform mat4 projection;
 
 out vec3 Normal;
 out vec3 FragPos;
+out vec2 TexCoords;
 
 void main() {
   FragPos = vec3(model * vec4(a_pos, 1.0));
diff --git a/src/camera/camera.odin b/src/camera.odin
index 809e3c3..d5c1201 100644
--- a/src/camera/camera.odin
+++ b/src/camera.odin
@@ -1,11 +1,8 @@
-package camera
+package funhalla
 
 import "core:math"
 import la "core:math/linalg"
 
-Vec3 :: la.Vector3f32
-Mat4 :: la.Matrix4x4f32
-
 CameraMovement :: enum {
 	FORWARD,
 	BACKWARD,
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)
diff --git a/src/shader/shader.odin b/src/shader.odin
index 720efde..cfee01f 100644
--- a/src/shader/shader.odin
+++ b/src/shader.odin
@@ -1,4 +1,4 @@
-package shader
+package funhalla
 
 import "core:math/linalg"
 import "core:strings"
@@ -11,9 +11,6 @@ Shader :: struct {
 SHADER_LOAD_ERROR :: -1
 SHADER_OK :: 0
 
-Vec3 :: linalg.Vector3f32
-Mat4 :: linalg.Matrix4x4f32
-
 shader_init :: proc(vsp, fsp: string) -> (^Shader, int) {
 	program_id, ok := gl.load_shaders_file(vsp, fsp)
 
@@ -27,34 +24,34 @@ shader_init :: proc(vsp, fsp: string) -> (^Shader, int) {
 	return shader, SHADER_OK
 }
 
-use :: proc(using shader: ^Shader) {
+shader_use :: proc(using shader: ^Shader) {
 	gl.UseProgram(id)
 }
 
-set_bool :: proc(using shader: ^Shader, name: cstring, value: bool) {
+shader_set_bool :: proc(using shader: ^Shader, name: cstring, value: bool) {
 	gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value))
 }
 
-set_i32 :: proc(using shader: ^Shader, name: cstring, value: i32) {
+shader_set_i32 :: proc(using shader: ^Shader, name: cstring, value: i32) {
 	gl.Uniform1i(gl.GetUniformLocation(id, name), value)
 }
 
-set_f32 :: proc(using shader: ^Shader, name: cstring, value: f32) {
+shader_set_f32 :: proc(using shader: ^Shader, name: cstring, value: f32) {
 	gl.Uniform1f(gl.GetUniformLocation(id, name), value)
 }
 
-set_vec3 :: proc(using shader: ^Shader, name: cstring, value: ^Vec3) {
+shader_set_vec3 :: proc(using shader: ^Shader, name: cstring, value: ^Vec3) {
 	gl.Uniform3fv(gl.GetUniformLocation(id, name), 1, &value[0])
 }
 
-set_mat4 :: proc(using shader: ^Shader, name: cstring, value: ^Mat4) {
+shader_set_mat4 :: proc(using shader: ^Shader, name: cstring, value: ^Mat4) {
 	gl.UniformMatrix4fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, &value[0][0])
 }
 
-set_value :: proc {
-	set_i32,
-	set_f32,
-	set_vec3,
-	set_mat4,
-	set_bool,
+shader_set_value :: proc {
+	shader_set_i32,
+	shader_set_f32,
+	shader_set_vec3,
+	shader_set_mat4,
+	shader_set_bool,
 }
diff --git a/src/texture.odin b/src/texture.odin
new file mode 100644
index 0000000..45edf56
--- /dev/null
+++ b/src/texture.odin
@@ -0,0 +1,12 @@
+package funhalla
+
+import gl "vendor:OpenGL"
+import "vendor:stb/image"
+
+load_texture :: proc(path: cstring) {
+  texture_id : u32
+
+  gl.GenTextures(1, &texture_id)
+
+  width, height, nr_components : u32
+}