# HG changeset patch # User John Tsiombikas # Date 1396839851 -10800 # Node ID a68dbf80d547d041ea23781d33e0726c6c5d90f0 # Parent 5fcf72837b69eeada6cd1812a4a2b136b8a72e72 finally showing something ... :) diff -r 5fcf72837b69 -r a68dbf80d547 Makefile --- a/Makefile Sun Apr 06 02:43:24 2014 +0300 +++ b/Makefile Mon Apr 07 06:04:11 2014 +0300 @@ -5,15 +5,17 @@ obj = $(modelobj) $(rendobj) $(scnobj) $(sysobj) bin = rayzor.exe +#dbg = -d2 + CC = wcc386 CXX = wpp386 -CFLAGS = -5 -fp5 -otexan -zq -bt=dos -Isrc\stl +CFLAGS = $(dbg) -5 -fp5 -otexan -zq -bt=dos -Isrc\stl CXXFLAGS = $(CFLAGS) LD = wlink $(bin): $(obj) %write objects.lnk file { $(obj) } - $(LD) name $@ @objects $(LDFLAGS) + $(LD) debug all name $@ @objects $(LDFLAGS) .c: src\ .cc: src\ diff -r 5fcf72837b69 -r a68dbf80d547 src/gfx.c --- a/src/gfx.c Sun Apr 06 02:43:24 2014 +0300 +++ b/src/gfx.c Mon Apr 07 06:04:11 2014 +0300 @@ -5,6 +5,7 @@ #include #include #include "vbe.h" +#include "logger.h" #define REALPTR(s, o) (void*)(((uint32_t)(s) << 4) + (uint32_t)(o)) #define VBEPTR(x) REALPTR(((x) & 0xffff0000) >> 16, (x) & 0xffff) @@ -33,15 +34,15 @@ return 0; } - printf("VBE Version: %x.%x\n", vbe_info->version >> 8, vbe_info->version & 0xff); + printlog("VBE Version: %x.%x\n", vbe_info->version >> 8, vbe_info->version & 0xff); if(vbe_info->version < 0x200) { fprintf(stderr, "This program requires VBE 2.0 or greater. Try running UniVBE\n"); return 0; } - printf("Graphics adapter: %s, %s (%s)\n", VBEPTR(vbe_info->oem_vendor_name_ptr), + printlog("Graphics adapter: %s, %s (%s)\n", VBEPTR(vbe_info->oem_vendor_name_ptr), VBEPTR(vbe_info->oem_product_name_ptr), VBEPTR(vbe_info->oem_product_rev_ptr)); - printf("Video memory: %dmb\n", vbe_info->total_mem << 6); + printlog("Video memory: %dmb\n", vbe_info->total_mem << 6); modes = VBEPTR(vbe_info->vid_mode_ptr); } @@ -75,7 +76,7 @@ /* attempt to set 8 bits of color per component in palettized modes */ if(bpp <= 8) { pal_bits = vbe_set_palette_bits(8); - printf("palette bits per color primary: %d\n", pal_bits); + printlog("palette bits per color primary: %d\n", pal_bits); } fbsize = xsz * ysz * mode_info->num_img_pages * (bpp / CHAR_BIT); diff -r 5fcf72837b69 -r a68dbf80d547 src/logger.c --- a/src/logger.c Sun Apr 06 02:43:24 2014 +0300 +++ b/src/logger.c Mon Apr 07 06:04:11 2014 +0300 @@ -6,6 +6,12 @@ static FILE *logfile; +void logger_output(FILE *fp) +{ + if(logfile) fclose(logfile); + logfile = fp; +} + void printlog(const char *fmt, ...) { va_list ap; diff -r 5fcf72837b69 -r a68dbf80d547 src/logger.h --- a/src/logger.h Sun Apr 06 02:43:24 2014 +0300 +++ b/src/logger.h Mon Apr 07 06:04:11 2014 +0300 @@ -1,10 +1,13 @@ #ifndef LOGGER_H_ #define LOGGER_H_ +#include + #ifdef __cplusplus extern "C" { #endif +void logger_output(FILE *fp); void printlog(const char *fmt, ...); #ifdef __cplusplus diff -r 5fcf72837b69 -r a68dbf80d547 src/m3dimpl.h --- a/src/m3dimpl.h Sun Apr 06 02:43:24 2014 +0300 +++ b/src/m3dimpl.h Mon Apr 07 06:04:11 2014 +0300 @@ -17,24 +17,35 @@ float tex[2]; }; +#define IM_VBSIZE 4 + struct min3d_context { struct m3d_image *cbuf; uint16_t *zbuf; unsigned long state; + int clear_color[3]; int mmode; /* matrix mode */ struct min3d_mstack mstack[2]; - const float *vert_array; - const float *norm_array; - const float *col_array; - const float *tc_array; + int vport[4]; + + float *vert_array; + float *norm_array; + float *col_array; + float *tc_array; /* immediate mode state */ - float cur_color[4]; - float cur_normal[3]; - float cur_texcoord[2]; + float im_varr[IM_VBSIZE * 3]; + float im_narr[IM_VBSIZE * 3]; + float im_carr[IM_VBSIZE * 3]; + float im_tarr[IM_VBSIZE * 2]; + + int im_prim, im_idx; + float im_color[4]; + float im_normal[3]; + float im_texcoord[2]; }; extern struct min3d_context *m3dctx; diff -r 5fcf72837b69 -r a68dbf80d547 src/main.cc --- a/src/main.cc Sun Apr 06 02:43:24 2014 +0300 +++ b/src/main.cc Mon Apr 07 06:04:11 2014 +0300 @@ -7,17 +7,22 @@ #include "keyb.h" #include "mouse.h" #include "logger.h" +#include "min3d.h" +#include "scene.h" +static bool init(); +static void cleanup(); static void display(); static void swap_buffers(); static void handle_keyboard(); static void handle_mouse(); static bool parse_args(int argc, char **argv); -static int xsz = 800; -static int ysz = 600; +static int xsz = 640; +static int ysz = 480; static int bpp = 16; static int bytespp; +static bool novideo; static unsigned char *fb; static unsigned char *backbuf; static int rbits, gbits, bbits; @@ -26,31 +31,17 @@ static bool quit; +struct m3d_image rbuf; +static Scene *scn; + int main(int argc, char **argv) { if(!parse_args(argc, argv)) { return 1; } - if(kb_init(32) == -1) { - fprintf(stderr, "failed to initialize keyboard driver\n"); + if(!init()) { return 1; } - if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) { - set_text_mode(); - fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp); - return 1; - } - bpp = get_color_depth(); - get_color_bits(&rbits, &gbits, &bbits); - get_color_shift(&rshift, &gshift, &bshift); - get_color_mask(&rmask, &gmask, &bmask); - bytespp = (int)ceil(bpp / 8.0); - - printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits); - printlog("shift: %d %d %d\n", rshift, gshift, bshift); - printlog("mask: %x %x %x\n", rmask, gmask, bmask); - - backbuf = new unsigned char[xsz * ysz * 3]; // main loop for(;;) { @@ -59,33 +50,96 @@ if(quit) break; display(); + + if(novideo) break; } - delete [] backbuf; - - set_text_mode(); - kb_shutdown(); - + cleanup(); printf("Thank you for using Rayzor!\n"); return 0; } +static bool init() +{ + if(!novideo) { + if(kb_init(32) == -1) { + fprintf(stderr, "failed to initialize keyboard driver\n"); + return false; + } + + if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) { + set_text_mode(); + fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp); + return false; + } + bpp = get_color_depth(); + get_color_bits(&rbits, &gbits, &bbits); + get_color_shift(&rshift, &gshift, &bshift); + get_color_mask(&rmask, &gmask, &bmask); + bytespp = (int)ceil(bpp / 8.0); + + printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits); + printlog("shift: %d %d %d\n", rshift, gshift, bshift); + printlog("mask: %x %x %x\n", rmask, gmask, bmask); + } else { + logger_output(stdout); + printlog("novideo (debug) mode\n"); + bpp = 24; + rbits = gbits = bbits = 8; + bytespp = 3; + } + + backbuf = new unsigned char[xsz * ysz * 3]; + if(!backbuf) { + return false; + } + + m3d_init(); + rbuf.pixels = backbuf; + rbuf.xsz = xsz; + rbuf.ysz = ysz; + m3d_set_buffers(&rbuf, 0); + + m3d_matrix_mode(M3D_PROJECTION); + m3d_load_identity(); + m3d_perspective(50.0, (float)xsz / (float)ysz, 0.5, 500.0); + + scn = new Scene; + + Sphere *sph = new Sphere; + scn->add_object(sph); + + return true; +} + +static void cleanup() +{ + delete scn; + + m3d_shutdown(); + delete [] backbuf; + + if(!novideo) { + set_text_mode(); + kb_shutdown(); + } +} + + static void display() { - unsigned char *fbptr = backbuf; + m3d_clear(M3D_COLOR_BUFFER_BIT); - for(int i=0; idraw(); + + if(!novideo) { + swap_buffers(); + wait_vsync(); } - - swap_buffers(); - wait_vsync(); } #define PACK_RGB(r, g, b) \ @@ -136,6 +190,8 @@ { int key; + if(novideo) return; + while((key = kb_getkey()) != -1) { switch(key) { case 27: @@ -155,6 +211,7 @@ const char *desc; } options[] = { {'s', "size", "resolution x[:bpp]"}, + {'n', "novid", "don't switch video mode (for debugging)"}, {'h', "help", "print usage information and exit"}, {-1, 0, 0} }; @@ -196,6 +253,10 @@ } break; + case 'n': + novideo = true; + break; + case 'h': print_usage(argv[0]); // doesn't return break; diff -r 5fcf72837b69 -r a68dbf80d547 src/min3d.c --- a/src/min3d.c Sun Apr 06 02:43:24 2014 +0300 +++ b/src/min3d.c Mon Apr 07 06:04:11 2014 +0300 @@ -3,6 +3,7 @@ #include #include "min3d.h" #include "m3dimpl.h" +#include "logger.h" #ifndef M_PI #define M_PI 3.141592653 @@ -21,6 +22,8 @@ m3d_load_identity(); m3d_matrix_mode(M3D_MODELVIEW); m3d_load_identity(); + + m3d_color(1, 1, 1); return 0; } @@ -33,13 +36,33 @@ { m3dctx->cbuf = cbuf; m3dctx->zbuf = zbuf; + + m3dctx->vport[0] = m3dctx->vport[1] = 0; + m3dctx->vport[2] = cbuf->xsz; + m3dctx->vport[3] = cbuf->ysz; +} + +void m3d_clear_color(float r, float g, float b) +{ + m3dctx->clear_color[0] = (int)((r > 1.0 ? 1.0 : r) * 255.0); + m3dctx->clear_color[1] = (int)((g > 1.0 ? 1.0 : g) * 255.0); + m3dctx->clear_color[2] = (int)((b > 1.0 ? 1.0 : b) * 255.0); } void m3d_clear(unsigned int bmask) { - int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz; + int i, num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz; if(bmask & M3D_COLOR_BUFFER_BIT) { - memset(m3dctx->cbuf->pixels, 0, num_pixels * 3); + /*memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);*/ + unsigned char *ptr = m3dctx->cbuf->pixels; + unsigned char r = m3dctx->clear_color[0]; + unsigned char g = m3dctx->clear_color[1]; + unsigned char b = m3dctx->clear_color[2]; + for(i=0; izbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf); @@ -64,6 +87,25 @@ m3dctx->mmode = mode; } +void m3d_push_matrix(void) +{ + int mm = m3dctx->mmode; + int top = m3dctx->mstack[mm].top; + if(top < MSTACK_SIZE) { + float *cur = m3dctx->mstack[mm].m[top++]; + memcpy(m3dctx->mstack[mm].m[top], cur, 16 * sizeof *cur); + m3dctx->mstack[mm].top = top; + } +} + +void m3d_pop_matrix(void) +{ + int mm = m3dctx->mmode; + if(m3dctx->mstack[mm].top > 0) { + --m3dctx->mstack[mm].top; + } +} + void m3d_load_identity(void) { static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; @@ -171,10 +213,10 @@ static void xform4(float *mat, float *vec) { - float x = mat[0] * vec[0] + mat[1] * vec[1] + mat[2] * vec[2] + mat[3]; - float y = mat[4] * vec[0] + mat[5] * vec[1] + mat[6] * vec[2] + mat[7]; - float z = mat[8] * vec[0] + mat[9] * vec[1] + mat[10] * vec[2] + mat[11]; - float w = mat[12] * vec[0] + mat[13] * vec[1] + mat[14] * vec[2] + mat[15]; + float x = mat[0] * vec[0] + mat[4] * vec[1] + mat[8] * vec[2] + mat[12]; + float y = mat[1] * vec[0] + mat[5] * vec[1] + mat[9] * vec[2] + mat[13]; + float z = mat[2] * vec[0] + mat[6] * vec[1] + mat[10] * vec[2] + mat[14]; + float w = mat[3] * vec[0] + mat[7] * vec[1] + mat[11] * vec[2] + mat[15]; vec[0] = x; vec[1] = y; @@ -188,6 +230,7 @@ int vcount = prim; int mvtop, ptop; float *mvmat, *pmat; + int *vport = m3dctx->vport; mvtop = m3dctx->mstack[M3D_MODELVIEW].top; mvmat = m3dctx->mstack[M3D_MODELVIEW].m[mvtop]; @@ -225,11 +268,14 @@ break; /* TODO */ } - /* perspective division */ + /* perspective division & viewport */ for(i=0; ivert_array = varr; + m3dctx->vert_array = (float*)varr; } void m3d_normal_array(const float *narr) { - m3dctx->norm_array = narr; + m3dctx->norm_array = (float*)narr; } void m3d_color_array(const float *carr) { - m3dctx->col_array = carr; + m3dctx->col_array = (float*)carr; } void m3d_texcoord_array(const float *tcarr) { - m3dctx->tc_array = tcarr; + m3dctx->tc_array = (float*)tcarr; } @@ -272,9 +318,10 @@ v[idx].pos[0] = *varr++; v[idx].pos[1] = *varr++; v[idx].pos[2] = *varr++; - v[idx].color[0] = carr ? *carr++ : m3dctx->cur_color[0]; - v[idx].color[1] = carr ? *carr++ : m3dctx->cur_color[1]; - v[idx].color[2] = carr ? *carr++ : m3dctx->cur_color[2]; + v[idx].pos[3] = 1.0; + v[idx].color[0] = carr ? *carr++ : m3dctx->im_color[0]; + v[idx].color[1] = carr ? *carr++ : m3dctx->im_color[1]; + v[idx].color[2] = carr ? *carr++ : m3dctx->im_color[2]; if(idx == prim - 1) { int resnum = proc_prim(prim, resv, v); @@ -299,3 +346,73 @@ /* TODO */ } +void m3d_begin(int prim) +{ + m3dctx->im_prim = prim; + m3dctx->im_idx = 0; + + m3dctx->vert_array = m3dctx->im_varr; + m3dctx->norm_array = 0; + m3dctx->col_array = 0; + m3dctx->tc_array = 0; +} + +void m3d_end(void) +{ +} + +void m3d_vertex(float x, float y, float z) +{ + int nverts = m3dctx->im_prim; + int idx = m3dctx->im_idx; + float *v = m3dctx->vert_array + idx * 3; + + v[0] = x; + v[1] = y; + v[2] = z; + + if(m3dctx->norm_array) { + float *ptr = m3dctx->im_narr + idx * 3; + ptr[0] = m3dctx->im_normal[0]; + ptr[1] = m3dctx->im_normal[1]; + ptr[2] = m3dctx->im_normal[2]; + } + if(m3dctx->col_array) { + float *ptr = m3dctx->im_carr + idx * 3; + ptr[0] = m3dctx->im_color[0]; + ptr[1] = m3dctx->im_color[1]; + ptr[2] = m3dctx->im_color[2]; + } + if(m3dctx->tc_array) { + float *ptr = m3dctx->im_texcoord + idx * 2; + ptr[0] = m3dctx->im_texcoord[0]; + ptr[1] = m3dctx->im_texcoord[1]; + } + + if(++idx == nverts) { + m3d_draw(m3dctx->im_prim, nverts); + idx = 0; + } + + m3dctx->im_idx = idx; +} + +void m3d_normal(float x, float y, float z) +{ + m3dctx->im_normal[0] = x; + m3dctx->im_normal[1] = y; + m3dctx->im_normal[2] = z; +} + +void m3d_color(float x, float y, float z) +{ + m3dctx->im_color[0] = x; + m3dctx->im_color[1] = y; + m3dctx->im_color[2] = z; +} + +void m3d_texcoord(float x, float y) +{ + m3dctx->im_texcoord[0] = x; + m3dctx->im_texcoord[1] = y; +} diff -r 5fcf72837b69 -r a68dbf80d547 src/min3d.h --- a/src/min3d.h Sun Apr 06 02:43:24 2014 +0300 +++ b/src/min3d.h Mon Apr 07 06:04:11 2014 +0300 @@ -43,7 +43,11 @@ extern "C" { #endif +int m3d_init(void); +void m3d_shutdown(void); + void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf); +void m3d_clear_color(float r, float g, float b); void m3d_clear(unsigned int bmask); void m3d_enable(int bit); @@ -51,6 +55,8 @@ /* matrix stack */ void m3d_matrix_mode(int mode); +void m3d_push_matrix(void); +void m3d_pop_matrix(void); void m3d_load_identity(void); void m3d_load_matrix(const float *m); void m3d_mult_matrix(const float *m); @@ -69,7 +75,13 @@ void m3d_draw(int prim, int vcount); void m3d_draw_indexed(int prim, const int *idxarr, int icount); -/* TODO immediate mode */ +/* immediate mode interface */ +void m3d_begin(int prim); +void m3d_end(void); +void m3d_vertex(float x, float y, float z); +void m3d_normal(float x, float y, float z); +void m3d_color(float x, float y, float z); +void m3d_texcoord(float x, float y); #ifdef __cplusplus } diff -r 5fcf72837b69 -r a68dbf80d547 src/object.cc --- a/src/object.cc Sun Apr 06 02:43:24 2014 +0300 +++ b/src/object.cc Mon Apr 07 06:04:11 2014 +0300 @@ -19,8 +19,8 @@ { } -#define USUB 16 -#define VSUB 8 +#define USUB 32 +#define VSUB 16 void Sphere::draw() const { diff -r 5fcf72837b69 -r a68dbf80d547 src/object.h --- a/src/object.h Sun Apr 06 02:43:24 2014 +0300 +++ b/src/object.h Mon Apr 07 06:04:11 2014 +0300 @@ -9,7 +9,7 @@ virtual void draw() const = 0; }; -class Sphere { +class Sphere : public Object { public: Sphere(); ~Sphere(); diff -r 5fcf72837b69 -r a68dbf80d547 src/scene.cc --- a/src/scene.cc Sun Apr 06 02:43:24 2014 +0300 +++ b/src/scene.cc Mon Apr 07 06:04:11 2014 +0300 @@ -4,6 +4,7 @@ Scene::Scene() { name = 0; + active_cam = 0; } Scene::~Scene() @@ -39,6 +40,76 @@ return name ? name : ""; } +void Scene::add_object(Object *obj) +{ + objects.push_back(obj); +} + +void Scene::add_light(Light *lt) +{ + lights.push_back(lt); +} + +void Scene::add_camera(Camera *cam) +{ + cameras.push_back(cam); +} + + +int Scene::get_object_count() const +{ + return (int)objects.size(); +} + +int Scene::get_light_count() const +{ + return (int)lights.size(); +} + +int Scene::get_camera_count() const +{ + return (int)cameras.size(); +} + + +Object *Scene::get_object(int idx) +{ + return objects[idx]; +} + +const Object *Scene::get_object(int idx) const +{ + return objects[idx]; +} + +Light *Scene::get_light(int idx) +{ + return lights[idx]; +} + +const Light *Scene::get_light(int idx) const +{ + return lights[idx]; +} + +Camera *Scene::get_camera(int idx) +{ + return cameras[idx]; +} + +const Camera *Scene::get_camera(int idx) const +{ + return cameras[idx]; +} + void Scene::draw() const { + if(active_cam) { + // TODO + } + + int nobj = get_object_count(); + for(int i=0; idraw(); + } } diff -r 5fcf72837b69 -r a68dbf80d547 src/scene.h --- a/src/scene.h Sun Apr 06 02:43:24 2014 +0300 +++ b/src/scene.h Mon Apr 07 06:04:11 2014 +0300 @@ -13,6 +13,7 @@ vector objects; vector lights; vector cameras; + Camera *active_cam; public: Scene(); @@ -23,6 +24,21 @@ void set_name(const char *name); const char *get_name() const; + void add_object(Object *obj); + void add_light(Light *lt); + void add_camera(Camera *cam); + + int get_object_count() const; + int get_light_count() const; + int get_camera_count() const; + + Object *get_object(int idx); + const Object *get_object(int idx) const; + Light *get_light(int idx); + const Light *get_light(int idx) const; + Camera *get_camera(int idx); + const Camera *get_camera(int idx) const; + void draw() const; };