Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ jobs:
odin check directx/d3d11_minimal_sdl3 -target:windows_amd64 $FLAGS

odin check opengl/minimal_sdl2 $FLAGS
odin check opengl/model_loading_and_font_rendering $FLAGS

odin check vulkan/triangle_glfw $FLAGS

Expand Down
11 changes: 11 additions & 0 deletions opengl/model_loading_and_font_rendering/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
////////////////////////////////////////////////////////////////////////////////
// LICENSE
////////////////////////////////////////////////////////////////////////////////

The included font.png image is based on "ToshibaSat 8x16" from the

Oldschool PC Font Pack by VileR (https://int10h.org/oldschool-pc-fonts/),

released under a Creative Commons Attribution-ShareAlike 4.0 International License.

See https://int10h.org/oldschool-pc-fonts/readme/#legal_stuff for further details.
194 changes: 194 additions & 0 deletions opengl/model_loading_and_font_rendering/app.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package main
import "core:log"
import "core:math"
import "core:math/linalg/glsl"
import "vendor:glfw"
import gl "vendor:OpenGL"

App :: struct {
window: glfw.WindowHandle,
sp_solid: u32,
sp_font: u32,
sp_light: u32,
font_tex: u32,
font_vao: u32,
font_vbo: u32,
font_chars: ^[FONT_MAX_CHARS]u32,
ambient_light: [3]f32,
dir_light: Directional_Light,
point_light: Point_Light,
camera: Camera,
primitives: ^[Primitive]Mesh,
meshes: [dynamic]Mesh,
materials: [dynamic]Material,
models: [dynamic]Model,
frame: u32,
time: f64,
prev_time: f64,
dt: f64,
fps: u32,
}

init :: proc(app: ^App) {
// GLFW and OpenGL initialization
glfw.Init()
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, GL_VERSION_MAJOR)
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, GL_VERSION_MINOR)
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
glfw.WindowHint(glfw.RESIZABLE, glfw.FALSE)
glfw.WindowHint(glfw.DECORATED, glfw.TRUE)
if OPTION_ANTI_ALIAS {
glfw.WindowHint(glfw.SAMPLES, 4)
}
app.window = glfw.CreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, nil, nil)
if app.window == nil {
glfw.Terminate()
log.fatal("GLFW window creation failed.")
}
glfw.MakeContextCurrent(app.window)
if !OPTION_VSYNC {
glfw.SwapInterval(0)
}
gl.load_up_to(GL_VERSION_MAJOR, GL_VERSION_MINOR, glfw.gl_set_proc_address)
gl.Viewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)

// OpenGL settings
gl.Enable(gl.CULL_FACE)
gl.Enable(gl.LINE_SMOOTH)
gl.LineWidth(2)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
if OPTION_GAMMA_CORRECTION {
gl.Enable(gl.FRAMEBUFFER_SRGB)
}
if OPTION_ANTI_ALIAS {
gl.Enable(gl.MULTISAMPLE)
}

// Load shaders
app.sp_solid = gl_shader_load_vs_fs(SHADER_SOLID_VERT, SHADER_SOLID_FRAG)
app.sp_font = gl_shader_load_vs_fs(SHADER_FONT_VERT, SHADER_FONT_FRAG)
app.sp_light = gl_shader_load_vs_fs(SHADER_LIGHT_VERT, SHADER_LIGHT_FRAG)

// Load primitive meshes
mesh_load_primitives(app.primitives)

// Font setup
app.font_chars = new([FONT_MAX_CHARS]u32)
app.font_tex = gl_texture_load(FONT_PATH, filtering = false)
gl.ActiveTexture(gl.TEXTURE1)
gl.BindTexture(gl.TEXTURE_2D, app.font_tex)
gl.GenVertexArrays(1, &app.font_vao)
gl.BindVertexArray(app.font_vao)
gl.GenBuffers(1, &app.font_vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, app.font_vbo)
gl.BufferData(gl.ARRAY_BUFFER, FONT_MAX_CHARS * size_of(u32), nil, gl.DYNAMIC_DRAW)
gl.EnableVertexAttribArray(0)
gl.VertexAttribIPointer(0, 1, gl.UNSIGNED_INT, size_of(u32), 0)
gl.VertexAttribDivisor(0, 1)
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
gl.ActiveTexture(gl.TEXTURE0)
gl.UseProgram(app.sp_font)
gl_shader_set_float(app.sp_font, "spacing", FONT_SPACING)
gl_shader_set_vec2_copy(app.sp_font, "ndc_pixel", {2.0 / WINDOW_WIDTH, 2.0 / WINDOW_HEIGHT})
gl_shader_set_vec2_copy(app.sp_font, "size", {FONT_WIDTH, FONT_HEIGHT})
gl_shader_set_int(app.sp_font, "font_tex", 1)
}

input :: proc(app: ^App) {
if glfw.GetKey(app.window, glfw.KEY_ESCAPE) == glfw.PRESS { glfw.SetWindowShouldClose(app.window, true) }
}

update :: proc(app: ^App) {
// Update timekeeping variables
app.time = glfw.GetTime()
app.dt = app.time - app.prev_time
if app.dt > 0.0 && app.frame % 60 == 0 {
app.fps = u32(1.0 / app.dt)
}
app.prev_time = app.time
app.frame += 1

// Update light color / position and model rotation
col: glsl.vec3 = {f32(math.sin_f64(glfw.GetTime() * 0.2)), f32(math.sin_f64(glfw.GetTime() * 0.3)), f32(math.sin_f64(glfw.GetTime() * 0.4))}
app.point_light.pos.z = math.cos_f32(f32(glfw.GetTime() * 0.2)) * 2 - 5
app.point_light.pos.x = math.sin_f32(f32(glfw.GetTime() * 0.2)) * 2
app.point_light.pos.y = math.cos_f32(f32(glfw.GetTime() * 1.5)) * 0.5 + 1
app.point_light.diffuse = col * 0.3 + 0.5
app.point_light.specular = col * 0.5 + 0.5
app.models[0].rot = {math.PI / 2, 0, 0} + {0, 0, -1} * f32(glfw.GetTime()) * glsl.radians_f32(20.0)
}


render :: proc(app: ^App) {
// Clear screen
gl.ClearColor(BACKGROUND_COLOR.r, BACKGROUND_COLOR.g, BACKGROUND_COLOR.b, 1.0)
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.Enable(gl.DEPTH_TEST)
gl.Viewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT)
gl.BindFramebuffer(gl.FRAMEBUFFER, 0)

// Set projection and view matrix
projection_mat := glsl.mat4Perspective(glsl.radians_f32(app.camera.fov), f32(f32(WINDOW_WIDTH) / f32(WINDOW_HEIGHT)), CAM_CLIP_NEAR, CAM_CLIP_FAR)
view_mat := glsl.mat4LookAt(app.camera.pos, app.camera.pos + app.camera.forward, app.camera.up)

// Render models
gl.UseProgram(app.sp_solid)
gl_shader_set_mat4(app.sp_solid, "projection_mat", projection_mat)
gl_shader_set_mat4(app.sp_solid, "view_mat", view_mat)
gl_shader_set_vec3(app.sp_solid, "view_pos", app.camera.pos)
gl_shader_set_vec3(app.sp_solid, "ambient_light", app.ambient_light)
gl_shader_set_vec3(app.sp_solid, "dir_light.dir", app.dir_light.dir)
gl_shader_set_vec3(app.sp_solid, "dir_light.diffuse", app.dir_light.diffuse)
gl_shader_set_vec3(app.sp_solid, "dir_light.specular", app.dir_light.specular)
gl_shader_set_vec3(app.sp_solid, "point_light.pos", app.point_light.pos)
gl_shader_set_vec3(app.sp_solid, "point_light.diffuse", app.point_light.diffuse)
gl_shader_set_vec3(app.sp_solid, "point_light.specular", app.point_light.specular)
gl_shader_set_float(app.sp_solid, "point_light.constant", app.point_light.constant)
gl_shader_set_float(app.sp_solid, "point_light.linear", app.point_light.linear)
gl_shader_set_float(app.sp_solid, "point_light.quadratic", app.point_light.quadratic)
for &model in app.models {
model_render(&model, app.sp_solid)
}

// Render point light
gl.UseProgram(app.sp_light)
gl_shader_set_mat4(app.sp_light, "projection_mat", projection_mat)
gl_shader_set_mat4(app.sp_light, "view_mat", view_mat)
point_light_render(&app.point_light, app.sp_light, &app.primitives[.Cube])

// Render font
gl.Disable(gl.DEPTH_TEST)
gl.UseProgram(app.sp_font)
gl.BindVertexArray(app.font_vao)
gl.BindBuffer(gl.ARRAY_BUFFER, app.font_vbo)
font_draw({2, 0}, app.fps, app.fps >= 50 ? {0.2, 0.8, 0.2} : (app.fps >= 30 ? {0.8, 0.8, 0.2} : {0.8, 0.2, 0.2} ), app.font_chars, app.sp_font)
font_draw({2, WINDOW_HEIGHT - FONT_HEIGHT}, "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", 1, app.font_chars, app.sp_font)
gl.BindVertexArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, 0)

// Swap buffers (present the framebuffer)
glfw.SwapBuffers(app.window)
gl_check_error()
}


exit :: proc(app: ^App) {
free(app.font_chars)
gl.DeleteProgram(app.sp_solid)
gl.DeleteProgram(app.sp_font)
gl.DeleteProgram(app.sp_light)
gl.DeleteTextures(1, &app.font_tex)
for &mesh in app.primitives {
mesh_destroy(&mesh)
}
for &mesh in app.meshes {
mesh_destroy(&mesh)
}
free(app.primitives)
delete(app.meshes)
delete(app.materials)
delete(app.models)
glfw.Terminate()
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
17 changes: 17 additions & 0 deletions opengl/model_loading_and_font_rendering/camera.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

Camera :: struct {
pos: [3]f32,
up: [3]f32,
forward: [3]f32,
fov: f32,
}

camera_new :: proc(pos: [3]f32 = {0, 1, 0}, up: [3]f32 = {0, 1, 0}, forward: [3]f32 = {0, 0, -1}, fov: f32 = 45) -> Camera {
return Camera{
pos = pos,
up = up,
forward = forward,
fov = fov,
}
}
48 changes: 48 additions & 0 deletions opengl/model_loading_and_font_rendering/font.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main
import gl "vendor:OpenGL"

font_draw :: proc {
font_draw_u32,
font_draw_string,
}

font_draw_u32 :: proc(pos: [2]f32, num: u32, color: [3]f32 = 1, font_chars: ^[FONT_MAX_CHARS]u32, sp_font: u32, scale: f32 = 1) {
count: u32 = 1
for n := num; n >= 10; n /= 10 {
count += 1
}
for i, n := count - 1, num; n > 0; i-= 1 {
n, font_chars[i] = n / 10, (16 + n % 10) | (i << 16)
}
if count > 0 {
font_draw_call(font_chars, sp_font, count, pos, scale, color)
}
}

font_draw_string :: proc(pos: [2]f32, txt: string, color: [3]f32 = 1, font_chars: ^[FONT_MAX_CHARS]u32, sp_font: u32, scale: f32 = 1) {
count, col, line: u32
prev: rune
for r in string(txt) {
if u32(r) == 10 {
col, line = 0, line + 1
} else if n := (u32(r) < 32 || u32(r) > 127) ? 63 - 32 : u32(r) - 32; count < FONT_MAX_CHARS {
if r == 'n' && prev == '\\' {
col, count, line = 0, count - 1, line + 1
} else {
col, count, font_chars[count] = col + 1, count + 1, n | (line << 8) | (col << 16)
}
prev = r
}
}
if count > 0 {
font_draw_call(font_chars, sp_font, count, pos, scale, color)
}
}

font_draw_call :: proc(font_chars: ^[FONT_MAX_CHARS]u32, sp_font: u32, count: u32, pos: [2]f32, scale: f32, color: [3]f32) {
gl_shader_set_vec3(sp_font, "color", color)
gl_shader_set_float(sp_font, "scale", scale)
gl_shader_set_vec2(sp_font, "pos", pos)
gl.BufferSubData(gl.ARRAY_BUFFER, 0, int(count) * size_of(u32), raw_data(font_chars))
gl.DrawArraysInstanced(gl.TRIANGLE_FAN, 0, 4, i32(count))
}
Loading