Skip to content

Commit

Permalink
Pretty lights
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Apr 9, 2024
1 parent 3862e4a commit 3490254
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 72 deletions.
50 changes: 32 additions & 18 deletions example/lighting.odin
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ import gl "../wasm/webgl"
@(private="file") RING_SPACE :: 30

lighting_state: struct {
cube_angle: f32,
ring_angle: f32,
u_view: i32,
u_local: i32,
u_light_dir: i32,
u_color: i32,
vao: VAO,
positions: [ALL_VERTICES]Vec,
normals: [ALL_VERTICES]Vec,
cube_angle: f32,
ring_angle: f32,
u_view: i32,
u_local: i32,
u_light_dir: i32,
u_light_color: i32,
vao: VAO,
positions: [ALL_VERTICES]Vec,
normals: [ALL_VERTICES]Vec,
}


Expand All @@ -39,26 +39,33 @@ lighting_start :: proc(program: gl.Program) {
gl.BindVertexArray(vao)

a_position := gl.GetAttribLocation(program, "a_position")
a_color := gl.GetAttribLocation(program, "a_normal")
a_normal := gl.GetAttribLocation(program, "a_normal")
a_color := gl.GetAttribLocation(program, "a_color")

u_view = gl.GetUniformLocation(program, "u_view")
u_local = gl.GetUniformLocation(program, "u_local")
u_light_dir = gl.GetUniformLocation(program, "u_light_dir")
u_color = gl.GetUniformLocation(program, "u_color")
u_view = gl.GetUniformLocation(program, "u_view")
u_local = gl.GetUniformLocation(program, "u_local")
u_light_dir = gl.GetUniformLocation(program, "u_light_dir")
u_light_color = gl.GetUniformLocation(program, "u_light_color")

gl.EnableVertexAttribArray(a_position)
gl.EnableVertexAttribArray(a_normal)
gl.EnableVertexAttribArray(a_color)

positions_buffer := gl.CreateBuffer()
normals_buffer := gl.CreateBuffer()
colors_buffer := gl.CreateBuffer()

gl.Enable(gl.CULL_FACE) // don't draw back faces
gl.Enable(gl.DEPTH_TEST) // draw only closest faces


colors: [ALL_VERTICES]RGBA

/* Cube */
copy_array(positions[:], get_cube_positions(0, CUBE_HEIGHT))
copy_array(normals[:], get_cube_normals())
cube_normals: [CUBE_VERTICES]Vec = 1
copy_array(normals[:], cube_normals)
copy_array(colors[:], WHITE_CUBE_COLORS)

/* Ring
Expand All @@ -74,9 +81,12 @@ lighting_start :: proc(program: gl.Program) {
ramp bottom
*/

rings_normals := normals[CUBE_VERTICES:]
rings_normals := normals [CUBE_VERTICES:]
rings_positions := positions[CUBE_VERTICES:]

rings_colors: [RINGS_VERTICES]RGBA = PURPLE_DARK
copy_array(colors[CUBE_VERTICES:], rings_colors)

for ri in 0..<RINGS {
ring_positions := rings_positions[ri*RING_VERTICES:]
ring_normals := rings_normals [ri*RING_VERTICES:]
Expand Down Expand Up @@ -134,9 +144,13 @@ lighting_start :: proc(program: gl.Program) {

gl.BindBuffer(gl.ARRAY_BUFFER, normals_buffer)
gl.BufferDataSlice(gl.ARRAY_BUFFER, normals[:], gl.STATIC_DRAW)
gl.VertexAttribPointer(a_color, 3, gl.FLOAT, false, 0, 0)
gl.VertexAttribPointer(a_normal, 3, gl.FLOAT, false, 0, 0)

gl.BindBuffer(gl.ARRAY_BUFFER, colors_buffer)
gl.BufferDataSlice(gl.ARRAY_BUFFER, colors[:], gl.STATIC_DRAW)
gl.VertexAttribPointer(a_color, 4, gl.UNSIGNED_BYTE, true, 0, 0)

gl.Uniform4fv(u_color, {1, 1, 1, 1})
gl.Uniform4fv(u_light_color, rgba_to_vec4(ORANGE))
}

lighting_frame :: proc(delta: f32) {
Expand Down
11 changes: 4 additions & 7 deletions example/lighting_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// to pick one. mediump is a good default
precision highp float;

// the varied normal passed from the vertex shader
// varring passed from the vertex shader
in vec3 v_normal;
in vec4 v_color;

uniform vec3 u_light_dir;
uniform vec4 u_color;
uniform vec4 u_light_color;

// we need to declare an output for the fragment shader
// equvalent of gl_FragColor in GLSL 100
Expand All @@ -23,9 +24,5 @@ void main() {
// of the normal to the light's reverse direction
float light = dot(normal, u_light_dir);

out_color = u_color;

// Lets multiply just the color portion (not the alpha)
// by the light
out_color.rgb *= light;
out_color = mix(v_color, u_light_color, light);
}
7 changes: 6 additions & 1 deletion example/lighting_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
// It will receive data from a buffer
in vec4 a_position;
in vec3 a_normal;
in vec4 a_color;

uniform mat4 u_view;
uniform mat4 u_local;

// a varying the color to the fragment shader
// varring passed to the fragment shader
out vec3 v_normal;
out vec4 v_color;

void main() {
// project the position
Expand All @@ -23,4 +25,7 @@ void main() {
transpose() + inverse() is to make it work with non-uniform scaling
*/
v_normal = mat3(transpose(inverse(u_local))) * a_normal;

// pass the color to the fragment shader
v_color = a_color;
}
97 changes: 51 additions & 46 deletions example/utils.odin
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package example

import "core:intrinsics"
import "base:intrinsics"
import glm "core:math/linalg/glsl"
import gl "../wasm/webgl"

Expand All @@ -19,6 +19,7 @@ tan :: glm.tan
dot :: glm.dot
cross :: glm.cross


copy_array :: #force_inline proc "contextless" (dst: []$S, src: [$N]S) {
src := src
copy(dst, src[:])
Expand Down Expand Up @@ -153,6 +154,11 @@ BLUE : RGBA : {0, 80, 190, 255}
RED : RGBA : {230, 20, 0, 255}
ORANGE: RGBA : {250, 160, 50, 255}
PURPLE: RGBA : {160, 100, 200, 255}
PURPLE_DARK: RGBA : {80, 30, 30, 255}

rgba_to_vec4 :: proc "contextless" (rgba: RGBA) -> glm.vec4 {
return {f32(rgba.r)/255, f32(rgba.g)/255, f32(rgba.b)/255, f32(rgba.a)/255}
}

CUBE_TRIANGLES :: 6 * 2
CUBE_VERTICES :: CUBE_TRIANGLES * 3
Expand Down Expand Up @@ -207,58 +213,57 @@ CUBE_POSITIONS: [CUBE_VERTICES]Vec : {
{1, 1, 0},
}

CUBE_NORMALS: [CUBE_VERTICES]Vec : {
{0, -1, 0}, // 0
{0, -1, 0},
{0, -1, 0},
{0, -1, 0}, // 1
{0, -1, 0},
{0, -1, 0},

{0, 0, 1}, // 2
{0, 0, 1},
{0, 0, 1},
{0, 0, 1}, // 3
{0, 0, 1},
{0, 0, 1},

{-1, 0, 0}, // 4
{-1, 0, 0},
{-1, 0, 0},
{-1, 0, 0}, // 5
{-1, 0, 0},
{-1, 0, 0},

{1, 0, 0}, // 6
{1, 0, 0},
{1, 0, 0},
{1, 0, 0}, // 7
{1, 0, 0},
{1, 0, 0},

{0, 0, -1}, // 8
{0, 0, -1},
{0, 0, -1},
{0, 0, -1}, // 9
{0, 0, -1},
{0, 0, -1},

{0, 1, 0}, // 10
{0, 1, 0},
{0, 1, 0},
{0, 1, 0}, // 11
{0, 1, 0},
{0, 1, 0},
}

get_cube_positions :: proc(pos: Vec = 0, h: f32 = 1) -> [CUBE_VERTICES]Vec {
positions := CUBE_POSITIONS
for &vec in positions {
vec = pos + (vec - {0.5, 0.5, 0.5}) * h
}
return positions
}
get_cube_normals :: proc() -> [CUBE_VERTICES]Vec {
return {
{0, -1, 0}, // 0
{0, -1, 0},
{0, -1, 0},
{0, -1, 0}, // 1
{0, -1, 0},
{0, -1, 0},

{0, 0, 1}, // 2
{0, 0, 1},
{0, 0, 1},
{0, 0, 1}, // 3
{0, 0, 1},
{0, 0, 1},

{-1, 0, 0}, // 4
{-1, 0, 0},
{-1, 0, 0},
{-1, 0, 0}, // 5
{-1, 0, 0},
{-1, 0, 0},

{1, 0, 0}, // 6
{1, 0, 0},
{1, 0, 0},
{1, 0, 0}, // 7
{1, 0, 0},
{1, 0, 0},

{0, 0, -1}, // 8
{0, 0, -1},
{0, 0, -1},
{0, 0, -1}, // 9
{0, 0, -1},
{0, 0, -1},

{0, 1, 0}, // 10
{0, 1, 0},
{0, 1, 0},
{0, 1, 0}, // 11
{0, 1, 0},
{0, 1, 0},
}
}

WHITE_CUBE_COLORS: [CUBE_VERTICES]RGBA : {
WHITE, WHITE, WHITE, // 0
Expand Down

0 comments on commit 3490254

Please sign in to comment.