Spheres/primitive built-in meshes, cull-modes, vec4 uniforms
This commit is contained in:
177
renderer.odin
177
renderer.odin
@@ -23,6 +23,9 @@ Renderer :: struct {
|
||||
|
||||
active_camera: ^Camera, // NOTE: SS - Hardcoded to 1 active camera. Split-screen is likely not possible due to this. Fix(?).
|
||||
|
||||
// Primitive meshes.
|
||||
default_cube_mesh, default_quad_mesh, default_sphere_mesh: Mesh,
|
||||
|
||||
fullscreen_vertex_shader, fullscreen_fragment_shader: Shader,
|
||||
fullscreen_shader_program: Shader_Program,
|
||||
fullscreen_mesh: Mesh,
|
||||
@@ -65,6 +68,20 @@ create :: proc(surface_ptr: rawptr) -> (^Renderer, bool) {
|
||||
|
||||
set_vsync(renderer, true)
|
||||
|
||||
{ // Create the default mesh-primitives.
|
||||
// default_cube_mesh, default_quad_mesh, default_sphere_mesh: Mesh,
|
||||
|
||||
if m, ok := create_mesh(renderer, .Cube); ok {
|
||||
renderer.default_cube_mesh = m
|
||||
}
|
||||
if m, ok := create_mesh(renderer, .Quad); ok {
|
||||
renderer.default_quad_mesh = m
|
||||
}
|
||||
if m, ok := create_mesh(renderer, .Sphere); ok {
|
||||
renderer.default_sphere_mesh = m
|
||||
}
|
||||
}
|
||||
|
||||
{ // Create the fullscreen shaders, material and mesh.
|
||||
fs_vertex_shader, fs_vertex_shader_ok := create_shader(renderer, .Vertex, "fs_vertex.glsl")
|
||||
assert(fs_vertex_shader_ok)
|
||||
@@ -135,13 +152,13 @@ set_vsync :: proc(renderer: ^Renderer, on: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
@(private="file") set_clear_color :: proc(renderer: ^Renderer, color: Color) {
|
||||
@(private) set_clear_color :: proc(renderer: ^Renderer, color: Color) {
|
||||
when RENDER_BACKEND_OPENGL {
|
||||
opengl_set_clear_color(renderer, color)
|
||||
}
|
||||
}
|
||||
|
||||
@(private="file") clear_screen :: proc(renderer: ^Renderer, clear_color: bool, clear_depth: bool) {
|
||||
@(private) clear_screen :: proc(renderer: ^Renderer, clear_color: bool, clear_depth: bool) {
|
||||
when RENDER_BACKEND_OPENGL {
|
||||
opengl_clear_screen(renderer, clear_color, clear_depth)
|
||||
}
|
||||
@@ -202,75 +219,6 @@ render_frame :: proc(renderer: ^Renderer, texture_to_present: ^Texture, clear_co
|
||||
}
|
||||
}
|
||||
|
||||
execute_pass :: proc(renderer: ^Renderer, pass: ^Pass, view_matrix, projection_matrix: linalg.Matrix4x4f32) { // TODO: SS - Move to 'pass.odin'.
|
||||
// fmt.printfln("Executing pass '%v'.", pass.name)
|
||||
|
||||
assert(renderer != nil)
|
||||
assert(pass != nil)
|
||||
|
||||
switch &t in &pass.type {
|
||||
case Scene_Pass: {
|
||||
apply_polygon_mode(renderer, renderer.polygon_mode)
|
||||
|
||||
assert(t.output_rt != nil)
|
||||
bind_render_target(renderer, t.output_rt)
|
||||
defer bind_render_target(renderer, nil)
|
||||
|
||||
should_write_depth := t.output_rt.depth_texture != nil
|
||||
should_test_depth := should_write_depth
|
||||
should_clear_depth := should_write_depth
|
||||
|
||||
should_clear_color := true
|
||||
set_clear_color(renderer, RGBA_Color { 0, 0, 0, 0 })
|
||||
|
||||
clear_screen(renderer, should_clear_color, should_clear_depth)
|
||||
apply_depth(renderer, should_test_depth, should_write_depth)
|
||||
|
||||
apply_blend_mode(renderer, t.blend_mode)
|
||||
defer apply_blend_mode(renderer, .None)
|
||||
|
||||
sort_draw_commands(renderer, &t)
|
||||
|
||||
for &dc in &t.draw_commands { // TODO: SS - Don't think we need the address of the draw-commands.
|
||||
model_matrix := linalg.identity(linalg.Matrix4x4f32)
|
||||
|
||||
// Translate.
|
||||
translation := linalg.matrix4_translate(dc.position)
|
||||
|
||||
// Rotate.
|
||||
rot_x := linalg.matrix4_rotate(linalg.to_radians(dc.rotation.x), [3]f32 { 1, 0, 0 })
|
||||
rot_y := linalg.matrix4_rotate(linalg.to_radians(dc.rotation.y), [3]f32 { 0, 1, 0 })
|
||||
rot_z := linalg.matrix4_rotate(linalg.to_radians(dc.rotation.z), [3]f32 { 0, 0, 1 })
|
||||
rotation := rot_z * rot_y * rot_x
|
||||
|
||||
// Scale.
|
||||
scale := linalg.matrix4_scale(dc.scale)
|
||||
|
||||
model_matrix *= translation * rotation * scale
|
||||
|
||||
activate_material(&dc.material, model_matrix, view_matrix, projection_matrix)
|
||||
|
||||
draw_mesh(&dc.mesh)
|
||||
}
|
||||
|
||||
// Clear the pass' draw-commands.
|
||||
clear(&t.draw_commands)
|
||||
|
||||
// TODO: SS - "Deactivate" the pass?
|
||||
|
||||
apply_polygon_mode(renderer, .Fill)
|
||||
}
|
||||
case Post_Processing_Pass: {
|
||||
// Execute the post-processing nodes.
|
||||
for &pp in &t.post_processing_nodes {
|
||||
execute_post_processing_node(renderer, &pp, view_matrix, projection_matrix)
|
||||
}
|
||||
|
||||
// TODO: SS - "Deactivate" the pass?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
execute_post_processing_node :: proc(renderer: ^Renderer, node: ^Post_Processing_Node, view_matrix, projection_matrix: linalg.Matrix4x4f32) {
|
||||
assert(renderer != nil)
|
||||
assert(node != nil)
|
||||
@@ -286,46 +234,16 @@ execute_post_processing_node :: proc(renderer: ^Renderer, node: ^Post_Processing
|
||||
clear_screen(renderer, true, false)
|
||||
|
||||
// fmt.printfln("TODO: SS - Execute post-processing node '%v' (VS: '%v', FS: '%v').", "NAME", node.program.vertex_shader.path, node.program.fragment_shader.path)
|
||||
|
||||
set_shader_uniforms(node.program, node.uniforms)
|
||||
|
||||
mat: Material
|
||||
mat.shader_program = node.program
|
||||
|
||||
fs_path := node.program.fragment_shader != nil ? node.program.fragment_shader.path : "nil"
|
||||
vs_path := node.program.vertex_shader != nil ? node.program.vertex_shader.path : "nil"
|
||||
|
||||
for u, i in node.uniforms {
|
||||
switch &t in u {
|
||||
case Uniform_Texture: {
|
||||
if mat.texture_count > MATERIAL_MAX_TEXTURES {
|
||||
continue
|
||||
}
|
||||
|
||||
mat.textures[mat.texture_count] = t.value
|
||||
mat.texture_count += 1
|
||||
if !set_shader_uniform(node.program, t) {
|
||||
fmt.printfln("Failed to set uniform-texture %v in program (vs: '%s', fs: '%s').", t.index, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Float: {
|
||||
if !set_shader_uniform(node.program, t) {
|
||||
fmt.printfln("Failed to set uniform-float '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Matrix4f32: {
|
||||
if !set_shader_uniform(node.program, t) {
|
||||
fmt.printfln("Failed to set uniform-matrix4f32 '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Vector3: {
|
||||
if !set_shader_uniform(node.program, t) {
|
||||
fmt.printfln("Failed to set uniform-vector3 '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Color: {
|
||||
if !set_shader_uniform(node.program, t) {
|
||||
fmt.printfln("Failed to set uniform-color '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
for u in node.uniforms {
|
||||
if t, is_texture := u.(Uniform_Texture); is_texture {
|
||||
mat.textures[mat.texture_count] = t.value
|
||||
mat.texture_count += 1
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,6 +255,10 @@ destroy :: proc(renderer: ^Renderer) {
|
||||
when RENDER_BACKEND_OPENGL {
|
||||
opengl_destroy(renderer)
|
||||
}
|
||||
|
||||
// TODO: SS - Destroy 'default_cube_mesh'
|
||||
// TODO: SS - Destroy 'default_quad_mesh'
|
||||
// TODO: SS - Destroy 'default_sphere_mesh'
|
||||
|
||||
assert(renderer != nil)
|
||||
free(renderer)
|
||||
@@ -354,40 +276,8 @@ destroy :: proc(renderer: ^Renderer) {
|
||||
p := material.shader_program
|
||||
|
||||
activate_shader_program(p)
|
||||
|
||||
fs_path := p.fragment_shader != nil ? p.fragment_shader.path : "nil"
|
||||
vs_path := p.vertex_shader != nil ? p.vertex_shader.path : "nil"
|
||||
set_shader_uniforms(material.shader_program, material.uniforms)
|
||||
|
||||
for u in material.uniforms {
|
||||
switch &t in u {
|
||||
case Uniform_Texture: {
|
||||
if !set_shader_uniform(p, t) {
|
||||
fmt.printfln("Failed to set uniform-texture %v in program (vs: '%s', fs: '%s').", t.index, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Float: {
|
||||
if !set_shader_uniform(p, t) {
|
||||
fmt.printfln("Failed to set uniform-float '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Matrix4f32: {
|
||||
if !set_shader_uniform(p, t) {
|
||||
fmt.printfln("Failed to set uniform-matrix4f32 '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Vector3: {
|
||||
if !set_shader_uniform(p, t) {
|
||||
fmt.printfln("Failed to set uniform-vector3 '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
case Uniform_Color: {
|
||||
if !set_shader_uniform(p, t) {
|
||||
fmt.printfln("Failed to set uniform-color '%s' in program (vs: '%s', fs: '%s').", t.name, vs_path, fs_path)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
when RENDER_BACKEND_OPENGL {
|
||||
opengl_activate_material(material, model_matrix, view_matrix, projection_matrix)
|
||||
}
|
||||
@@ -415,7 +305,14 @@ destroy :: proc(renderer: ^Renderer) {
|
||||
}
|
||||
}
|
||||
|
||||
@(private) apply_cull_mode :: proc(renderer: ^Renderer, cull_mode: Cull_Mode) {
|
||||
when RENDER_BACKEND_OPENGL {
|
||||
opengl_set_cull_mode(renderer, cull_mode)
|
||||
}
|
||||
}
|
||||
|
||||
@(private) sort_draw_commands :: proc(renderer: ^Renderer, pass: ^Scene_Pass) {
|
||||
// TODO: SS - Set the pass' 'renderer' variable here instead?
|
||||
switch pass.sort_mode {
|
||||
case .None: {}
|
||||
case .Back_To_Front: {
|
||||
|
||||
Reference in New Issue
Block a user