model_loading.rasm
src/3.model_loading/1.model_loading/model_loading.rasm · 134 lines
; 1. Model loading
;
; Up to now every shape was typed in. Real ones come from a modelling tool,
; as a file: here a backpack of about 68,000 triangles, stored as Wavefront
; .obj with its textures named in a .mtl beside it.
;
; includes/learn/model.rasm reads it, playing the part Assimp and
; LearnOpenGL's Model class play there: positions, texture coordinates and
; normals, then faces that index into all three, then the material's
; textures. Once loaded it is one vertex buffer, drawn with the lighting from
; chapter 2; model_draw binds its diffuse texture to t0 and its specular to
; t1.
;
; W A S D, Space, Shift move
; right mouse + drag look
; wheel zoom
import "../../../includes/d3d11.rin"
import "../../../includes/calls.rasm"
import "../../../includes/math.rasm"
import "../../../includes/learn/window.rasm"
import "../../../includes/learn/shader.rasm"
import "../../../includes/learn/gpu.rasm"
import "../../../includes/learn/camera.rasm"
import "../../../includes/learn/transforms.rasm"
import "../../../includes/learn/model.rasm"
; Mirrors `cbuffer light` in model_loading.hlsl.
light_constants: struct = {
position: vec3
pad0: f32
}
camera_start: const vec3 = { 0.0, 0.5, 4.5 }
clear_color: [4]const f32 = { 0.05, 0.05, 0.05, 1.0 }
t: transforms = {}
light: light_constants = {}
s: shader = {}
cam: camera = {}
backpack: model = {}
transforms_buffer: *ID3D11Buffer = {}
light_buffer: *ID3D11Buffer = {}
sampler: *ID3D11SamplerState = {}
; timing
current_frame: f32 = 0.0
delta_time: f32 = 0.0
last_frame: f32 = 0.0
main: proc() -> i32 = {
sub rsp, 40 ; 32 shadow; entry 8 + 40 lands on 16
invoke(window_create, "LearnD3D11")
call window_create_depth
invoke(create_sampler, D3D11_FILTER.D3D11_FILTER_ANISOTROPIC, D3D11_TEXTURE_ADDRESS_MODE.D3D11_TEXTURE_ADDRESS_WRAP)
mov sampler, rax
vcall(context, ID3D11DeviceContextVtbl.PSSetSamplers, 0, 1, sampler&)
invoke(shader_create, s&, "src/3.model_loading/1.model_loading/model_loading.hlsl", model_layout&, MODEL_LAYOUT_COUNT)
; load models
; -----------
invoke(model_load, backpack&, "resources/objects/backpack/", "backpack.obj", 0)
invoke(create_constant_buffer, sizeof(transforms))
mov transforms_buffer, rax
invoke(create_constant_buffer, sizeof(light_constants))
mov light_buffer, rax
lea rcx, cam
lea rdx, camera_start
movss xmm2, -90.0 ; yaw
movss xmm3, -5.0 ; pitch
call camera_create
; render loop
; -----------
render_loop:
call window_poll
test eax, eax
jz render_loop_done
; per-frame time logic
; --------------------
call get_time
movss current_frame, xmm0
movss xmm1, xmm0
subss xmm0, last_frame
movss delta_time, xmm0
movss last_frame, xmm1
; input
; -----
invoke(camera_process_input, cam&, delta_time&)
; render
; ------
invoke(window_begin, clear_color&)
; the backpack stays where it was modelled
invoke(transforms_from_camera, t&, cam&)
lea rcx, t.model
call m4_identity
lea rcx, t.normal_matrix
call m4_identity
invoke(update_buffer, transforms_buffer, t&, sizeof(transforms))
; the light circles in front of it: (3 cos(t / 2), 2, 3 sin(t / 2) + 2)
movss xmm0, current_frame
mulss xmm0, 0.5
call cosf
mulss xmm0, 3.0
movss light.position.x, xmm0
mov light.position.y, 0x40000000 ; 2.0
movss xmm0, current_frame
mulss xmm0, 0.5
call sinf
mulss xmm0, 3.0
addss xmm0, 2.0
movss light.position.z, xmm0
invoke(update_buffer, light_buffer, light&, sizeof(light_constants))
invoke(shader_use, s&)
invoke(bind_constants, 0, transforms_buffer)
invoke(bind_constants, 1, light_buffer)
invoke(model_draw, backpack&)
call window_present
jmp render_loop
render_loop_done:
invoke(ExitProcess, 0)
}