From afde6754684dc6be5a09b5a4659732c0d7b46741 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Sun, 8 Oct 2023 07:45:02 +0300 Subject: [PATCH] 3D setup --- Makefile | 2 +- src/3dgfx/3dgfx.c | 4 ++-- src/3dgfx/polyfill.c | 2 -- src/3dgfx/polyfill.h | 4 ++-- src/game.c | 50 +++++++++++++++++++++++++++++++++++--------------- src/game.h | 1 + src/kern/main.c | 5 +++++ src/loader.asm | 9 ++++++++- src/macros.inc | 2 +- src/util.h | 18 ++++++++++++++++++ 10 files changed, 73 insertions(+), 24 deletions(-) diff --git a/Makefile b/Makefile index ec659d8..9607e6f 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ bin = game.com warn = -pedantic -Wall -Wno-unused-function opt = -O2 -inc = -Isrc -Isrc/kern -Isrc/libc +inc = -Isrc -Isrc/3dgfx -Isrc/kern -Isrc/libc AS = nasm diff --git a/src/3dgfx/3dgfx.c b/src/3dgfx/3dgfx.c index 6df2bbe..ac50929 100644 --- a/src/3dgfx/3dgfx.c +++ b/src/3dgfx/3dgfx.c @@ -203,10 +203,10 @@ void g3d_clear_depth(float z) void g3d_clear(unsigned int mask) { if(mask & G3D_COLOR_BUFFER_BIT) { - memset16(pfill_fb.pixels, st->clear_color, pfill_fb.width * pfill_fb.height); + memset(pfill_fb.pixels, st->clear_color, pfill_fb.width * pfill_fb.height); } if(mask & G3D_DEPTH_BUFFER_BIT) { - memset16(pfill_zbuf, st->clear_depth, pfill_fb.width * pfill_fb.height * sizeof *pfill_zbuf / 2); + memset32(pfill_zbuf, st->clear_depth, pfill_fb.width * pfill_fb.height); } } diff --git a/src/3dgfx/polyfill.c b/src/3dgfx/polyfill.c index d195ff0..9d12351 100644 --- a/src/3dgfx/polyfill.c +++ b/src/3dgfx/polyfill.c @@ -26,7 +26,6 @@ void (*fillfunc[])(struct pvertex*) = { polyfill_tex_flat, polyfill_tex_gouraud, 0, - 0, 0, 0, 0, 0, 0, 0, 0, polyfill_wire, polyfill_flat_zbuf, polyfill_gouraud_zbuf, @@ -35,7 +34,6 @@ void (*fillfunc[])(struct pvertex*) = { polyfill_tex_flat_zbuf, polyfill_tex_gouraud_zbuf, 0, - 0, 0, 0, 0, 0, 0, 0, 0 }; struct pimage pfill_fb, pfill_tex; diff --git a/src/3dgfx/polyfill.h b/src/3dgfx/polyfill.h index aa7bf60..5a4515c 100644 --- a/src/3dgfx/polyfill.h +++ b/src/3dgfx/polyfill.h @@ -17,11 +17,11 @@ enum { POLYFILL_TEX_FLAT, POLYFILL_TEX_GOURAUD, - POLYFILL_WIRE_ZBUF = 16, + POLYFILL_WIRE_ZBUF = 8, POLYFILL_FLAT_ZBUF, POLYFILL_GOURAUD_ZBUF, - POLYFILL_TEX_WIRE_ZBUF = 20, + POLYFILL_TEX_WIRE_ZBUF = 12, POLYFILL_TEX_FLAT_ZBUF, POLYFILL_TEX_GOURAUD_ZBUF }; diff --git a/src/game.c b/src/game.c index 4609b46..c22135c 100644 --- a/src/game.c +++ b/src/game.c @@ -1,10 +1,33 @@ #include #include "game.h" #include "colormgr.h" +#include "3dgfx.h" +#include "mesh.h" + +static struct g3d_mesh mesh; int game_init(void) { init_colormgr(); + + g3d_init(); + g3d_framebuffer(320, 200, framebuf); + g3d_viewport(0, 0, 320, 200); + + g3d_clear_color(0, 0, 0); + + g3d_matrix_mode(G3D_PROJECTION); + g3d_load_identity(); + g3d_perspective(50.0f, 1.33333, 0.5, 500.0); + + g3d_enable(G3D_CULL_FACE); + g3d_enable(G3D_DEPTH_TEST); + g3d_enable(G3D_LIGHTING); + g3d_enable(G3D_LIGHT0); + + g3d_polygon_mode(G3D_GOURAUD); + + gen_torus_mesh(&mesh, 2.0, 0.7, 24, 12); return 0; } @@ -14,21 +37,18 @@ void game_shutdown(void) void game_draw(void) { - int i, j; - unsigned char *fbptr = framebuf; - - for(i=0; i<200; i++) { - for(j=0; j<320; j++) { - int r, b; - int idx = i + (rand() & 0x1f) - 16; - if(idx < 0) idx = 0; - if(idx > 199) idx = 199; - - r = 255 * idx / 199; - b = 255 - r; - *fbptr++ = find_color(r, 0, b); - } - } + unsigned long msec = game_getmsec(); + float tsec = (float)msec / 1000.0f; + + g3d_clear(G3D_COLOR_BUFFER_BIT | G3D_DEPTH_BUFFER_BIT); + + g3d_matrix_mode(G3D_MODELVIEW); + g3d_load_identity(); + g3d_translate(0, 0, -8); + g3d_rotate(tsec * 50.0f, 1, 0, 0); + g3d_rotate(tsec * 30.0f, 0, 0, 1); + + draw_mesh(&mesh); game_swap_buffers(); } diff --git a/src/game.h b/src/game.h index 9eb4cc9..c416037 100644 --- a/src/game.h +++ b/src/game.h @@ -16,6 +16,7 @@ void game_keyboard(int key, int press); void game_mouse(int bn, int press, int x, int y); void game_motion(int x, int y); +unsigned long game_getmsec(void); void game_quit(void); void game_swap_buffers(void); diff --git a/src/kern/main.c b/src/kern/main.c index 6cf5de3..6c37116 100644 --- a/src/kern/main.c +++ b/src/kern/main.c @@ -52,6 +52,11 @@ end: return 0; } +unsigned long game_getmsec(void) +{ + return TICKS_TO_MSEC(nticks); +} + void game_quit(void) { quit = 1; diff --git a/src/loader.asm b/src/loader.asm index 0ef2177..7ecec04 100644 --- a/src/loader.asm +++ b/src/loader.asm @@ -177,8 +177,14 @@ _start: mov ax, 1 call far [vmswitch] ; broadcast windows exit + xor ax, ax + mov bx, ax + mov si, ax + mov es, ax + mov ds, ax + mov cx, ax + mov dx, ax mov ax, 1606h - xor dx, dx int 2fh exit: mov ax, 4c00h @@ -188,6 +194,7 @@ str_gemmis db 'Memory manager detected, trying to take control...',0 str_errvm86 db 'Error: memory manager running. Stop it and try again (e.g. emm386 off)',10,0 str_enterpm db 'Entering 32bit protected mode ...',10,0 + align 4 vmswitch: vmswitch_off dw 0 vmswitch_seg dw 0 diff --git a/src/macros.inc b/src/macros.inc index ce5250a..b734a69 100644 --- a/src/macros.inc +++ b/src/macros.inc @@ -1,4 +1,4 @@ -%define BUG_WARNING +%undef BUG_WARNING %define CON_SERIAL UART_BASE equ 2f8h ; COM1: 3f8, COM2: 2f8 diff --git a/src/util.h b/src/util.h index 0ec72f3..2e8c1d8 100644 --- a/src/util.h +++ b/src/util.h @@ -48,6 +48,24 @@ extern uint32_t perf_start_count, perf_interval_count; : "flags", "memory"); \ } while(0) +/* +#define memset32(dest, val, count) \ + asm volatile ( \ + "cld\n\t" \ + "rep stosl\n\t" \ + :: "a"(val), "c"(count), "D"(dest) \ + : "flags", "memory") +*/ +#define memset32(dest, val, count) \ + do { \ + int i; \ + uint32_t *ptr = (uint32_t*)dest; \ + for(i=0; i