feat: major engine overhaul - scene demo with lighting, sphere, ground
- Scene Demo: cube + sphere + ground plane with proper lighting - Audio: XAudio2 dynamic loading + WAV playback - Physics: expanded collision system - Renderer: improved pipeline with materials - UI: extended widget system - VisualScript: enhanced serializer + block interactions - ASM: optimized math/memory/profiling routines - Window: mouse input + scroll support
This commit is contained in:
1 parent
2653926d85
commit
0669d102ce
35 files changed
+1844
-314
No files matched your search
@@ -1,5 +1,18 @@
|
||||
#include "renderer.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static void fill_constant_buffer(float* cb, const Mat4* world, const Mat4* wvp,
|
||||
Vec3 light_dir, Vec4 light_color, Vec4 ambient_color,
|
||||
int use_texture, Vec4 diffuse_color) {
|
||||
memcpy(cb + 0, world->m, sizeof(float) * 16);
|
||||
memcpy(cb + 16, wvp->m, sizeof(float) * 16);
|
||||
cb[32] = light_dir.x; cb[33] = light_dir.y; cb[34] = light_dir.z; cb[35] = 0.0f;
|
||||
cb[36] = light_color.x; cb[37] = light_color.y; cb[38] = light_color.z; cb[39] = light_color.w;
|
||||
cb[40] = ambient_color.x; cb[41] = ambient_color.y; cb[42] = ambient_color.z; cb[43] = ambient_color.w;
|
||||
cb[44] = use_texture ? 1.0f : 0.0f; cb[45] = 0.0f; cb[46] = 0.0f; cb[47] = 0.0f;
|
||||
cb[48] = diffuse_color.x; cb[49] = diffuse_color.y; cb[50] = diffuse_color.z; cb[51] = diffuse_color.w;
|
||||
}
|
||||
|
||||
Renderer* renderer_create(GraphicsDevice* device) {
|
||||
Renderer* r = (Renderer*)calloc(1, sizeof(Renderer));
|
||||
@@ -15,7 +28,14 @@ Renderer* renderer_create(GraphicsDevice* device) {
|
||||
void renderer_destroy(Renderer* r) { free(r); }
|
||||
|
||||
void renderer_begin_frame(Renderer* r) {
|
||||
ID3D11DeviceContext* ctx = r->device->context;
|
||||
graphics_device_clear(r->device, 0.1f, 0.1f, 0.15f, 1.0f);
|
||||
D3D11_VIEWPORT vp = {0};
|
||||
vp.Width = (float)r->device->width;
|
||||
vp.Height = (float)r->device->height;
|
||||
vp.MaxDepth = 1.0f;
|
||||
ctx->lpVtbl->RSSetViewports(ctx, 1, &vp);
|
||||
ctx->lpVtbl->IASetPrimitiveTopology(ctx, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
||||
}
|
||||
|
||||
void renderer_end_frame(Renderer* r) {
|
||||
@@ -37,9 +57,41 @@ void renderer_draw_mesh(Renderer* r, Mesh* m) {
|
||||
}
|
||||
|
||||
void renderer_draw_mesh_with_shader(Renderer* r, Mesh* m, Shader* s, const Mat4* world, Texture* t) {
|
||||
if (!m || !s) return;
|
||||
if (!m || !s || !r) return;
|
||||
ID3D11DeviceContext* ctx = r->device->context;
|
||||
shader_bind(s, ctx);
|
||||
|
||||
Mat4 wvp;
|
||||
mat4_multiply(world, &r->view, &wvp);
|
||||
mat4_multiply(&wvp, &r->projection, &wvp);
|
||||
|
||||
float cb[64];
|
||||
memset(cb, 0, sizeof(cb));
|
||||
fill_constant_buffer(cb, world, &wvp, r->light_dir, r->light_color, r->ambient_color, t ? 1 : 0, (Vec4){1,1,1,1});
|
||||
|
||||
shader_set_matrix(s, ctx, cb);
|
||||
if (t) texture_bind(t, ctx, 0);
|
||||
|
||||
renderer_draw_mesh(r, m);
|
||||
}
|
||||
|
||||
void renderer_draw_mesh_with_material(Renderer* r, Mesh* m, Material* mat, const Mat4* world) {
|
||||
if (!m || !mat || !mat->shader || !r) return;
|
||||
ID3D11DeviceContext* ctx = r->device->context;
|
||||
shader_bind(mat->shader, ctx);
|
||||
|
||||
Mat4 wvp;
|
||||
mat4_multiply(world, &r->view, &wvp);
|
||||
mat4_multiply(&wvp, &r->projection, &wvp);
|
||||
|
||||
float cb[64];
|
||||
memset(cb, 0, sizeof(cb));
|
||||
fill_constant_buffer(cb, world, &wvp, r->light_dir, r->light_color, r->ambient_color,
|
||||
mat->use_texture, mat->color);
|
||||
|
||||
shader_set_matrix(mat->shader, ctx, cb);
|
||||
if (mat->use_texture && mat->texture) texture_bind(mat->texture, ctx, 0);
|
||||
|
||||
renderer_draw_mesh(r, m);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user