# HG changeset patch # User John Tsiombikas # Date 1396676787 -10800 # Node ID 2a5340a6eee41acc0a5d2608f43c0665ffb6581c rayzor first commit diff -r 000000000000 -r 2a5340a6eee4 .hgignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.hgignore Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,4 @@ +\.o$ +\.swp$ +\.obj$ +\.exe$ diff -r 000000000000 -r 2a5340a6eee4 Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Makefile Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,29 @@ +modelobj = main.obj min3d.obj logger.obj +rendobj = rend.obj vmath.obj +scnobj = scene.obj object.obj +sysobj = gfx.obj vbe.obj dpmi.obj timer.obj mouse.obj keyb.obj +obj = $(modelobj) $(rendobj) $(scnobj) $(sysobj) +bin = rayzor.exe + +CC = wcc386 +CXX = wpp386 +CFLAGS = -5 -fp5 -otexan -zq -bt=dos -Isrc\stl +CXXFLAGS = $(CFLAGS) +LD = wlink + +$(bin): $(obj) + %write objects.lnk file { $(obj) } + $(LD) name $@ @objects $(LDFLAGS) + +.c: src\ +.cc: src\ + +.c.obj: .autodepend + $(CC) $(CFLAGS) $[* + +.cc.obj: .autodepend + $(CXX) $(CXXFLAGS) $[* + +clean: .symbolic + del *.obj + del $(bin) diff -r 000000000000 -r 2a5340a6eee4 src/camera.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/camera.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,50 @@ +#include "camera.h" + +Camera::Camera() + : pos(0, 0, 10) +{ + fov = M_PI; +} + +void Camera::set_position(const Vector3 &pos) +{ + this->pos = pos; +} + +const Vector3 &Camera::get_position() const +{ + return pos; +} + +void Camera::set_target(const Vector3 &target) +{ + this->target = target; +} + +const Vector3 &Camera::get_target() const +{ + return target; +} + +void Camera::set_fov(float fov) +{ + this->fov = fov; +} + +float Camera::get_fov() const +{ + return fov; +} + +Matrix4x4 Camera::get_matrix() const +{ + Matrix4x4 res; + res.lookat(pos, target, Vector3(0, 1, 0)); + return res; +} + +Matrix4x4 Camera::get_inv_matrix() const +{ + Matrix4x4 res; + return res; // TODO +} diff -r 000000000000 -r 2a5340a6eee4 src/camera.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/camera.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,28 @@ +#ifndef CAMERA_H_ +#define CAMERA_H_ + +#include "vmath.h" + +class Camera { +private: + Vector3 pos; + Vector3 target; + float fov; + +public: + Camera(); + + void set_position(const Vector3 &pos); + const Vector3 &get_position() const; + + void set_target(const Vector3 &target); + const Vector3 &get_target() const; + + void set_fov(float fov); + float get_fov() const; + + Matrix4x4 get_matrix() const; + Matrix4x4 get_inv_matrix() const; +}; + +#endif // CAMERA_H_ diff -r 000000000000 -r 2a5340a6eee4 src/dpmi.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dpmi.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,55 @@ +#include "dpmi.h" + +void dpmi_real_int(int inum, struct dpmi_real_regs *regs) +{ + unsigned char int_num = (unsigned char)inum; + __asm { + mov eax, 0x300 + mov edi, regs + mov bl, int_num + mov bh, 0 + xor ecx, ecx + int 0x31 + } +} + +void *dpmi_mmap(uint32_t phys_addr, unsigned int size) +{ + uint16_t mem_high, mem_low; + uint16_t phys_high = phys_addr >> 16; + uint16_t phys_low = phys_addr & 0xffff; + uint16_t size_high = size >> 16; + uint16_t size_low = size & 0xffff; + unsigned int err, res = 0; + + __asm { + mov eax, 0x800 + mov bx, phys_high + mov cx, phys_low + mov si, size_high + mov di, size_low + int 0x31 + add res, 1 + mov err, eax + mov mem_high, bx + mov mem_low, cx + } + + if(res == 2) { + return 0; + } + return (void*)(((uint32_t)mem_high << 16) | ((uint32_t)mem_low)); +} + +void dpmi_munmap(void *addr) +{ + uint16_t mem_high = (uint32_t)addr >> 16; + uint16_t mem_low = (uint16_t)addr; + + __asm { + mov eax, 0x801 + mov bx, mem_high + mov cx, mem_low + int 0x31 + } +} diff -r 000000000000 -r 2a5340a6eee4 src/dpmi.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/dpmi.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,26 @@ +#ifndef DPMI_H_ +#define DPMI_H_ + +#include "inttypes.h" + +struct dpmi_real_regs { + uint32_t edi, esi, ebp; + uint32_t reserved; + uint32_t ebx, edx, ecx, eax; + uint16_t flags; + uint16_t es, ds, fs, gs; + uint16_t ip, cs, sp, ss; +}; + +unsigned short dpmi_alloc(unsigned int par); +#pragma aux dpmi_alloc = \ + "mov eax, 0x100" \ + "int 0x31" \ + value[ax] parm[ebx]; + +void dpmi_real_int(int inum, struct dpmi_real_regs *regs); + +void *dpmi_mmap(uint32_t phys_addr, unsigned int size); +void dpmi_munmap(void *addr); + +#endif /* DPMI_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/gfx.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/gfx.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,152 @@ +#ifndef GFX_H_ +#define GFX_H_ + +#include +#include +#include +#include "vbe.h" + +#define REALPTR(s, o) (void*)(((uint32_t)(s) << 4) + (uint32_t)(o)) +#define VBEPTR(x) REALPTR(((x) & 0xffff0000) >> 16, (x) & 0xffff) +#define VBEPTR_SEG(x) (((x) & 0xffff0000) >> 16) +#define VBEPTR_OFF(x) ((x) & 0xffff) + +#define SAME_BPP(a, b) \ + ((a) == (b) || (a) == 16 && (b) == 15 || (a) == 15 && (b) == 16 || (a) == 32 && (b) == 24 || (a) == 24 && (b) == 32) + +static unsigned int make_mask(int sz, int pos); + +static struct vbe_info *vbe_info; +static struct vbe_mode_info *mode_info; +static int pal_bits = 6; + +void *set_video_mode(int xsz, int ysz, int bpp) +{ + int i; + uint16_t *modes, best = 0; + unsigned int fbsize; + + /* check for VBE2 support and output some info */ + if(!vbe_info) { + if(!(vbe_info = vbe_get_info())) { + fprintf(stderr, "VESA BIOS Extensions not available\n"); + return 0; + } + + printf("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), + VBEPTR(vbe_info->oem_product_name_ptr), VBEPTR(vbe_info->oem_product_rev_ptr)); + printf("Video memory: %dmb\n", vbe_info->total_mem << 6); + + modes = VBEPTR(vbe_info->vid_mode_ptr); + } + + for(i=0; i<1024; i++) { /* impose an upper limit to avoid inf-loops */ + if(modes[i] == 0xffff) { + break; /* reached the end */ + } + + mode_info = vbe_get_mode_info(modes[i] | VBE_MODE_LFB); + if(!mode_info || mode_info->xres != xsz || mode_info->yres != ysz) { + continue; + } + if(SAME_BPP(mode_info->bpp, bpp)) { + best = modes[i]; + } + } + + if(best) { + mode_info = vbe_get_mode_info(best); + } else { + fprintf(stderr, "Requested video mode (%dx%d %dbpp) is unavailable\n", xsz, ysz, bpp); + return 0; + } + + if(vbe_set_mode(best | VBE_MODE_LFB) == -1) { + fprintf(stderr, "Failed to set video mode %dx%d %dbpp\n", mode_info->xres, mode_info->yres, mode_info->bpp); + return 0; + } + + /* 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); + } + + fbsize = xsz * ysz * mode_info->num_img_pages * (bpp / CHAR_BIT); + return (void*)dpmi_mmap(mode_info->fb_addr, fbsize); +} + +int set_text_mode(void) +{ + vbe_set_mode(0x3); + return 0; +} + +int get_color_depth(void) +{ + if(!mode_info) { + return -1; + } + return mode_info->bpp; +} + +int get_color_bits(int *rbits, int *gbits, int *bbits) +{ + if(!mode_info) { + return -1; + } + *rbits = mode_info->rmask_size; + *gbits = mode_info->gmask_size; + *bbits = mode_info->bmask_size; + return 0; +} + +int get_color_mask(unsigned int *rmask, unsigned int *gmask, unsigned int *bmask) +{ + if(!mode_info) { + return -1; + } + *rmask = make_mask(mode_info->rmask_size, mode_info->rpos); + *gmask = make_mask(mode_info->gmask_size, mode_info->gpos); + *bmask = make_mask(mode_info->bmask_size, mode_info->bpos); + return 0; +} + +int get_color_shift(int *rshift, int *gshift, int *bshift) +{ + if(!mode_info) { + return -1; + } + *rshift = mode_info->rpos; + *gshift = mode_info->gpos; + *bshift = mode_info->bpos; + return 0; +} + +void set_palette(int idx, int r, int g, int b) +{ + int col[3]; + col[0] = r; + col[1] = g; + col[2] = b; + vbe_set_palette(idx, col, 1, pal_bits); +} + +static unsigned int make_mask(int sz, int pos) +{ + unsigned int i, mask = 0; + + for(i=0; i +#endif + +#endif /* INT_TYPES_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/keyb.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/keyb.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,194 @@ +/* +DOS interrupt-based keyboard driver. +Copyright (C) 2013 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the program. If not, see +*/ +#define KEYB_C_ + +#include +#include +#include +#include +#include +#include +#include "keyb.h" +#include "scancode.h" + +#define KB_INTR 0x9 +#define KB_PORT 0x60 + +#define PIC1_CMD_PORT 0x20 +#define OCW2_EOI (1 << 5) + +#define DONE_INIT (prev_handler) + +static void __interrupt __far kbintr(); + +static void (__interrupt __far *prev_handler)(); + +static int *buffer; +static int buffer_size, buf_ridx, buf_widx; +static int last_key; + +static unsigned int num_pressed; +static unsigned char keystate[256]; + +#define ADVANCE(x) ((x) = ((x) + 1) % buffer_size) + +int kb_init(int bufsz) +{ + if(DONE_INIT) { + fprintf(stderr, "keyboard driver already initialized!\n"); + return 0; + } + + buffer_size = bufsz; + if(buffer_size && !(buffer = malloc(buffer_size * sizeof *buffer))) { + fprintf(stderr, "failed to allocate input buffer, continuing without\n"); + buffer_size = 0; + } + buf_ridx = buf_widx = 0; + last_key = -1; + + memset(keystate, 0, sizeof keystate); + num_pressed = 0; + + /* set our interrupt handler */ + _disable(); + prev_handler = _dos_getvect(KB_INTR); + _dos_setvect(KB_INTR, kbintr); + _enable(); + + return 0; +} + +void kb_shutdown(void) +{ + if(!DONE_INIT) { + return; + } + + /* restore the original interrupt handler */ + _disable(); + _dos_setvect(KB_INTR, prev_handler); + _enable(); + + free(buffer); +} + +int kb_isdown(int key) +{ + if(key == KB_ANY) { + return num_pressed; + } + return (int)keystate[key]; +} + +void kb_wait(void) +{ + int key; + while((key = kb_getkey()) == -1) { + /* put the processor to sleep while waiting for keypresses, but first + * make sure interrupts are enabled, or we'll sleep forever + */ + __asm { + sti + hlt + } + } + kb_putback(key); +} + +int kb_getkey(void) +{ + int res; + + if(buffer) { + if(buf_ridx == buf_widx) { + return -1; + } + res = buffer[buf_ridx]; + ADVANCE(buf_ridx); + } else { + res = last_key; + last_key = -1; + } + return res; +} + +void kb_putback(int key) +{ + if(buffer) { + /* go back a place */ + if(--buf_ridx < 0) { + buf_ridx += buffer_size; + } + + /* if the write end hasn't caught up with us, go back one place + * and put it there, otherwise just overwrite the oldest key which + * is right where we were. + */ + if(buf_ridx == buf_widx) { + ADVANCE(buf_ridx); + } + + buffer[buf_ridx] = key; + } else { + last_key = key; + } +} + +static void __interrupt __far kbintr() +{ + unsigned char code; + int key, press; + + code = inp(KB_PORT); + + if(code >= 128) { + press = 0; + code -= 128; + + if(num_pressed > 0) { + num_pressed--; + } + } else { + press = 1; + + num_pressed++; + } + + key = scantbl[code]; + + if(press) { + /* append to buffer */ + last_key = key; + if(buffer_size > 0) { + buffer[buf_widx] = key; + ADVANCE(buf_widx); + /* if the write end overtook the read end, advance the read end + * too, to discard the oldest keypress from the buffer + */ + if(buf_widx == buf_ridx) { + ADVANCE(buf_ridx); + } + } + } + + /* and update keystate table */ + keystate[key] = press; + + outp(PIC1_CMD_PORT, OCW2_EOI); /* send end-of-interrupt */ +} diff -r 000000000000 -r 2a5340a6eee4 src/keyb.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/keyb.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,48 @@ +/* +DOS interrupt-based keyboard driver. +Copyright (C) 2013 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with the program. If not, see +*/ +#ifndef KEYB_H_ +#define KEYB_H_ + +#define KB_ANY (-1) + +#ifdef __cplusplus +extern "C" { +#endif + +int kb_init(int bufsz); /* bufsz can be 0 for no buffered keys */ +void kb_shutdown(void); /* don't forget to call this at the end! */ + +/* Boolean predicate for testing the current state of a particular key. + * You may also pass KB_ANY to test if any key is held down. + */ +int kb_isdown(int key); + +/* waits for any keypress */ +void kb_wait(void); + +/* removes and returns a single key from the input buffer. + * If buffering is disabled (initialized with kb_init(0)), then it always + * returns the last key pressed. + */ +int kb_getkey(void); + +#ifdef __cplusplus +} +#endif + +#endif /* KEYB_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/light.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/light.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,41 @@ +#include "light.h" + +Light::Light() +{ + color.x = color.y = color.z = 1.0; + atten.x = 1.0; + atten.y = 0.0; + atten.z = 0.0; +} + +void Light::set_position(const Vector3 &pos) +{ + this->pos = pos; +} + +const Vector3 &Light::get_position() const +{ + return pos; +} + + +void Light::set_color(const Vector3 &color) +{ + this->color = color; +} + +const Vector3 &Light::get_color() const +{ + return color; +} + + +void Light::set_attenuation(const Vector3 &atten) +{ + this->atten = atten; +} + +const Vector3 &Light::get_attenuation() const +{ + return atten; +} diff -r 000000000000 -r 2a5340a6eee4 src/light.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/light.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,25 @@ +#ifndef LIGHT_H_ +#define LIGHT_H_ + +#include "vmath.h" + +class Light { +private: + Vector3 pos; + Vector3 color; + Vector3 atten; + +public: + Light(); + + void set_position(const Vector3 &pos); + const Vector3 &get_position() const; + + void set_color(const Vector3 &color); + const Vector3 &get_color() const; + + void set_attenuation(const Vector3 &atten); + const Vector3 &get_attenuation() const; +}; + +#endif // LIGHT_H_ diff -r 000000000000 -r 2a5340a6eee4 src/logger.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/logger.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,23 @@ +#include +#include +#include "logger.h" + +#define LOGFNAME "rayzor.log" + +static FILE *logfile; + +void printlog(const char *fmt, ...) +{ + va_list ap; + + if(!logfile) { + if(!(logfile = fopen(LOGFNAME, "w"))) { + return; + } + setvbuf(logfile, 0, _IOLBF, 0); + } + + va_start(ap, fmt); + vfprintf(logfile, fmt, ap); + va_end(ap); +} diff -r 000000000000 -r 2a5340a6eee4 src/logger.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/logger.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,14 @@ +#ifndef LOGGER_H_ +#define LOGGER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void printlog(const char *fmt, ...); + +#ifdef __cplusplus +} +#endif + +#endif /* LOGGER_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/m3dimpl.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/m3dimpl.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,23 @@ +#ifndef M3DIMPL_H_ +#define M3DIMPL_H_ + +#include "min3d.h" + +#define MSTACK_SIZE 16 + +struct min3d_mstack { + float m[MSTACK_SIZE][16]; + int top; +}; + +struct min3d_context { + struct m3d_image *cbuf; + uint16_t *zbuf; + + unsigned long state; + + int mmode; /* matrix mode */ + struct min3d_mstack mstack[2]; +} *m3dctx; + +#endif /* M3DIMPL_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/main.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include "inttypes.h" +#include "gfx.h" +#include "keyb.h" +#include "mouse.h" +#include "logger.h" + +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 bpp = 16; +static int bytespp; +static unsigned char *fb; +static unsigned char *backbuf; +static int rbits, gbits, bbits; +static int rshift, gshift, bshift; +static unsigned int rmask, gmask, bmask; + +static bool quit; + +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"); + 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(;;) { + handle_keyboard(); + handle_mouse(); + if(quit) break; + + display(); + } + + delete [] backbuf; + + set_text_mode(); + kb_shutdown(); + + printf("Thank you for using Rayzor!\n"); + return 0; +} + +static void display() +{ + unsigned char *fbptr = backbuf; + + for(int i=0; i> srs, src[1] >> sgs, src[2] >> sbs); + src += 3; + } + } + break; + + default: + break; + } +} + +static void handle_keyboard() +{ + if(!kb_isdown(KB_ANY)) + return; + + int c = kb_getkey(); + switch(c) { + case 27: + quit = true; + return; + } +} + +static void handle_mouse() +{ +} + +static struct { + int opt; + const char *lopt; + const char *desc; +} options[] = { + {'s', "size", "resolution x[:bpp]"}, + {'h', "help", "print usage information and exit"}, + {-1, 0, 0} +}; + +static void print_usage(const char *argv0) +{ + printf("%s usage\n", argv0); + for(int i=0; options[i].opt != -1; i++) { + printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc); + } + exit(0); +} + +static bool parse_args(int argc, char **argv) +{ + for(int i=1; i +#include "min3d.h" +#include "m3dimpl.h" + +#ifndef M_PI +#define M_PI 3.141592653 +#endif + +int m3d_init(void) +{ + if(!(m3dctx = malloc(sizeof *m3dctx))) { + return -1; + } + memset(m3dctx, 0, sizeof *m3dctx); + + m3d_matrix_mode(M3D_PROJECTION); + m3d_load_identity(); + m3d_matrix_mode(M3D_MODELVIEW); + m3d_load_identity(); + return 0; +} + +void m3d_shutdown(void) +{ + free(m3dctx); +} + +void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf) +{ + m3dctx->cbuf = cbuf; + m3dctx->zbuf = zbuf; +} + +void m3d_clear(unsigned int bmask) +{ + int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz; + if(bmask & M3D_COLOR_BUFFER_BIT) { + memset(m3dctx->cbuf->pixels, 0, num_pixels * 3); + } + if(bmask & M3D_DEPTH_BUFFER_BIT) { + memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf); + } +} + + +void m3d_enable(int bit) +{ + m3dctx->state |= (1 << bit); +} + +void m3d_disable(int bit) +{ + m3dctx->state &= ~(1 << bit); +} + + +/* matrix stack */ +void m3d_matrix_mode(int mode) +{ + m3dctx->mmode = mode; +} + +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}; + m3d_load_matrix(mid); +} + +void m3d_load_matrix(const float *m) +{ + int top = m3dctx->mstack[m3dctx->mmode].top; + memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m); +} + +#define M(i,j) (((i) << 2) + (j)) +void m3d_mult_matrix(const float *m2) +{ + int i, j, top = m3dctx->mstack[m3dctx->mmode].top; + float m1[16]; + float *dest = m3dctx->mstack[m3dctx->mmode].m[top]; + + memcpy(m1, dest, sizeof m1); + + for(i=0; i<4; i++) { + for(j=0; j<4; j++) { + dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] + + m1[M(1,j)] * m2[M(i,1)] + + m1[M(2,j)] * m2[M(i,2)] + + m1[M(3,j)] * m2[M(i,3)]; + } + } +} + +void m3d_translate(float x, float y, float z) +{ + float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + m[12] = x; + m[13] = y; + m[14] = z; + m3d_mult_matrix(m); +} + +void m3d_rotate(float deg, float x, float y, float z) +{ + float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + + float angle = M_PI * deg / 180.0f; + float sina = sin(angle); + float cosa = cos(angle); + float one_minus_cosa = 1.0f - cosa; + float nxsq = x * x; + float nysq = y * y; + float nzsq = z * z; + + xform[0] = nxsq + (1.0f - nxsq) * cosa; + xform[4] = x * y * one_minus_cosa - z * sina; + xform[8] = x * z * one_minus_cosa + y * sina; + xform[1] = x * y * one_minus_cosa + z * sina; + xform[5] = nysq + (1.0 - nysq) * cosa; + xform[9] = y * z * one_minus_cosa - x * sina; + xform[2] = x * z * one_minus_cosa - y * sina; + xform[6] = y * z * one_minus_cosa + x * sina; + xform[10] = nzsq + (1.0 - nzsq) * cosa; + + m3d_mult_matrix(xform); +} + +void m3d_scale(float x, float y, float z) +{ + static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + m[0] = x; + m[5] = y; + m[10] = z; + m3d_mult_matrix(m); +} + +void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr) +{ + float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; + + float dx = right - left; + float dy = top - bottom; + float dz = fr - nr; + + float a = (right + left) / dx; + float b = (top + bottom) / dy; + float c = -(fr + nr) / dz; + float d = -2.0 * fr * nr / dz; + + xform[0] = 2.0 * nr / dx; + xform[5] = 2.0 * nr / dy; + xform[8] = a; + xform[9] = b; + xform[10] = c; + xform[11] = -1.0f; + xform[14] = d; + + m3d_mult_matrix(xform); +} + +void m3d_perspective(float vfov, float aspect, float nr, float fr) +{ + float vfov_rad = M_PI * vfov / 180.0; + float x = nr * tan(vfov_rad / 2.0); + m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr); +} + +/* drawing */ +void m3d_draw(int prim, const float *varr, int vcount) +{ + /* TODO */ +} + +void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount) +{ + /* TODO */ +} + diff -r 000000000000 -r 2a5340a6eee4 src/min3d.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/min3d.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,73 @@ +#ifndef MIN3D_H_ +#define MIN3D_H_ + +#include "inttypes.h" + +/* state toggles */ +enum { + M3D_DEPTH_TEST, + M3D_CULL_FACE, + M3D_LIGHTING, + M3D_LIGHT0, + M3D_LIGHT1, + M3D_LIGHT2, + M3D_LIGHT3 +}; + +/* buffer bits */ +enum { + M3D_COLOR_BUFFER_BIT = 1, + M3D_DEPTH_BUFFER_BIT = 2 +}; + +/* primitives */ +enum { + M3D_POINTS = 1, + M3D_LINES = 2, + M3D_TRIANGLES = 3, + M3D_QUADS = 4 +}; + +/* matrix mode */ +enum { + M3D_MODELVIEW, + M3D_PROJECTION +}; + +struct m3d_image { + int xsz, ysz; + unsigned char *pixels; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf); +void m3d_clear(unsigned int bmask); + +void m3d_enable(int bit); +void m3d_disable(int bit); + +/* matrix stack */ +void m3d_matrix_mode(int mode); +void m3d_load_identity(void); +void m3d_load_matrix(const float *m); +void m3d_mult_matrix(const float *m); +void m3d_translate(float x, float y, float z); +void m3d_rotate(float angle, float x, float y, float z); +void m3d_scale(float x, float y, float z); +void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr); +void m3d_perspective(float vfov, float aspect, float znear, float zfar); + +/* drawing */ +void m3d_draw(int prim, const float *varr, int vcount); +void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount); + +/* TODO immediate mode */ + +#ifdef __cplusplus +} +#endif + +#endif /* MIN3D_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/mouse.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mouse.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,72 @@ +/* TODO: try NOT using the v8086 interrupts to avoid the overhead */ +#include "mouse.h" +#include "inttypes.h" +#include "dpmi.h" + +#define INTR 0x33 + +#define QUERY 0 +#define SHOW 1 +#define HIDE 2 +#define READ 3 +#define WRITE 4 + +#define XLIM 7 +#define YLIM 8 + +int have_mouse(void) +{ + struct dpmi_real_regs regs; + memset(®s, 0, sizeof regs); + regs.eax = QUERY; + dpmi_real_int(INTR, ®s); + return regs.eax & 0xffff; +} + +void show_mouse(int show) +{ + struct dpmi_real_regs regs; + memset(®s, 0, sizeof regs); + regs.eax = show ? SHOW : HIDE; + dpmi_real_int(INTR, ®s); +} + +int read_mouse(int *xp, int *yp) +{ + struct dpmi_real_regs regs; + memset(®s, 0, sizeof regs); + + regs.eax = READ; + dpmi_real_int(INTR, ®s); + + if(xp) *xp = regs.ecx & 0xffff; + if(yp) *yp = regs.edx & 0xffff; + return regs.ebx & 0xffff; +} + +void set_mouse(int x, int y) +{ + struct dpmi_real_regs regs; + memset(®s, 0, sizeof regs); + + regs.eax = WRITE; + regs.ecx = x; + regs.edx = y; + dpmi_real_int(INTR, ®s); +} + +void set_mouse_limits(int xmin, int ymin, int xmax, int ymax) +{ + struct dpmi_real_regs regs; + memset(®s, 0, sizeof regs); + regs.eax = XLIM; + regs.ecx = xmin; + regs.edx = xmax; + dpmi_real_int(INTR, ®s); + + memset(®s, 0, sizeof regs); + regs.eax = YLIM; + regs.ecx = ymin; + regs.edx = ymax; + dpmi_real_int(INTR, ®s); +} diff -r 000000000000 -r 2a5340a6eee4 src/mouse.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/mouse.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,22 @@ +#ifndef MOUSE_H_ +#define MOUSE_H_ + +#define MOUSE_LEFT 1 +#define MOUSE_RIGHT 2 +#define MOUSE_MIDDLE 4 + +#ifdef __cplusplus +extern "C" { +#endif + +int have_mouse(void); +void show_mouse(int show); +int read_mouse(int *xp, int *yp); +void set_mouse(int x, int y); +void set_mouse_limits(int xmin, int ymin, int xmax, int ymax); + +#ifdef __cplusplus +} +#endif + +#endif /* MOUSE_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/object.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,53 @@ +#include "object.h" +#include "vmath.h" +#include "min3d.h" + +Object::Object() +{ +} + +Object::~Object() +{ +} + +// ---- sphere ---- +Sphere::Sphere() +{ +} + +Sphere::~Sphere() +{ +} + +#define USUB 16 +#define VSUB 8 + +void Sphere::draw() const +{ + static Vector3 *varr; + static int num_verts; + if(!varr) { + int uverts = USUB; + int vverts = VSUB + 1; + num_verts = uverts * vverts; + varr = new Vector3[num_verts]; + + Vector3 *vptr = varr; + for(int i=0; ix, num_verts); +} diff -r 000000000000 -r 2a5340a6eee4 src/object.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/object.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,20 @@ +#ifndef OBJECT_H_ +#define OBJECT_H_ + +class Object { +public: + Object(); + virtual ~Object(); + + virtual void draw() const = 0; +}; + +class Sphere { +public: + Sphere(); + ~Sphere(); + + void draw() const; +}; + +#endif // OBJECT_H_ diff -r 000000000000 -r 2a5340a6eee4 src/pit8254.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pit8254.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,34 @@ +#ifndef PIT8254_H_ +#define PIT8254_H_ + +/* frequency of the oscillator driving the 8254 timer */ +#define OSC_FREQ_HZ 1193182 + +/* I/O ports connected to the 8254 */ +#define PORT_DATA0 0x40 +#define PORT_DATA1 0x41 +#define PORT_DATA2 0x42 +#define PORT_CMD 0x43 + +/* command bits */ +#define CMD_CHAN0 0 +#define CMD_CHAN1 (1 << 6) +#define CMD_CHAN2 (2 << 6) +#define CMD_RDBACK (3 << 6) + +#define CMD_LATCH 0 +#define CMD_ACCESS_LOW (1 << 4) +#define CMD_ACCESS_HIGH (2 << 4) +#define CMD_ACCESS_BOTH (3 << 4) + +#define CMD_OP_INT_TERM 0 +#define CMD_OP_ONESHOT (1 << 1) +#define CMD_OP_RATE (2 << 1) +#define CMD_OP_SQWAVE (3 << 1) +#define CMD_OP_SW_STROBE (4 << 1) +#define CMD_OP_HW_STROBE (5 << 1) + +#define CMD_MODE_BIN 0 +#define CMD_MODE_BCD 1 + +#endif /* PIT8254_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/rend.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rend.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,10 @@ +#include "rend.h" + +bool rend_init() +{ + return false; +} + +void rend_shutdown() +{ +} diff -r 000000000000 -r 2a5340a6eee4 src/rend.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/rend.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,7 @@ +#ifndef REND_H_ +#define REND_H_ + +bool rend_init(); +void rend_shutdown(); + +#endif // REND_H_ diff -r 000000000000 -r 2a5340a6eee4 src/scancode.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scancode.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,31 @@ +#ifndef KEYB_C_ +#error "do not include scancode.h anywhere..." +#endif + +/* special keys */ +enum { + LALT, RALT, + LCTRL, RCTRL, + LSHIFT, RSHIFT, + F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, + CAPSLK, NUMLK, SCRLK, SYSRQ, + ESC = 27, + INSERT, DEL, HOME, END, PGUP, PGDN, LEFT, RIGHT, UP, DOWN, + NUM_DOT, NUM_ENTER, NUM_PLUS, NUM_MINUS, NUM_MUL, NUM_DIV, + NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9, + BACKSP = 127 +}; + +/* table with rough translations from set 1 scancodes to ASCII-ish */ +static int scantbl[] = { + 0, ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', /* 0 - e */ + '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* f - 1c */ + LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', /* 1d - 29 */ + LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', RSHIFT, /* 2a - 36 */ + NUM_MUL, LALT, ' ', CAPSLK, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, /* 37 - 44 */ + NUMLK, SCRLK, NUM_7, NUM_8, NUM_9, NUM_MINUS, NUM_4, NUM_5, NUM_6, NUM_PLUS, /* 45 - 4e */ + NUM_1, NUM_2, NUM_3, NUM_0, NUM_DOT, SYSRQ, 0, 0, F11, F12, /* 4d - 58 */ + 0, 0, 0, 0, 0, 0, 0, /* 59 - 5f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6f */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 70 - 7f */ +}; diff -r 000000000000 -r 2a5340a6eee4 src/scene.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scene.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,43 @@ +#include "scene.h" + +Scene::Scene() +{ + name = 0; +} + +Scene::~Scene() +{ + clear(); +} + +void Scene::clear() +{ + delete [] name; + + size_t i; + for(i=0; iname; + this->name = new char[strlen(name) + 1]; + strcpy(this->name, name); +} + +const char *Scene::get_name() const +{ + return name ? name : ""; +} + +void Scene::draw() const +{ +} diff -r 000000000000 -r 2a5340a6eee4 src/scene.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/scene.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,29 @@ +#ifndef SCENE_H_ +#define SCENE_H_ + +#include +#include +#include "object.h" +#include "light.h" +#include "camera.h" + +class Scene { +private: + char *name; + vector objects; + vector lights; + vector cameras; + +public: + Scene(); + ~Scene(); + + void clear(); + + void set_name(const char *name); + const char *get_name() const; + + void draw() const; +}; + +#endif // SCENE_H_ diff -r 000000000000 -r 2a5340a6eee4 src/timer.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/timer.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,131 @@ +/* +256-color 3D graphics hack for real-mode DOS. +Copyright (C) 2011 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#include +#include +#include +#include +#include +#include "pit8254.h" + +#define PIT_TIMER_INTR 8 +#define DOS_TIMER_INTR 0x1c + +/* macro to divide and round to the nearest integer */ +#define DIV_ROUND(a, b) \ + ((a) / (b) + ((a) % (b)) / ((b) / 2)) + +static void set_timer_reload(int reload_val); +static void cleanup(void); +static void __interrupt __far timer_irq(); +static void __interrupt __far dos_timer_intr(); + +static void (__interrupt __far *prev_timer_intr)(); + +static unsigned long ticks; +static unsigned long tick_interval, ticks_per_dos_intr; +static int inum; + +void init_timer(int res_hz) +{ + _disable(); + + if(res_hz > 0) { + int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz); + set_timer_reload(reload_val); + + tick_interval = DIV_ROUND(1000, res_hz); + ticks_per_dos_intr = DIV_ROUND(65535L, reload_val); + + inum = PIT_TIMER_INTR; + prev_timer_intr = _dos_getvect(inum); + _dos_setvect(inum, timer_irq); + } else { + tick_interval = 55; + + inum = DOS_TIMER_INTR; + prev_timer_intr = _dos_getvect(inum); + _dos_setvect(inum, dos_timer_intr); + } + _enable(); + + atexit(cleanup); +} + +static void cleanup(void) +{ + if(!prev_timer_intr) { + return; /* init hasn't ran, there's nothing to cleanup */ + } + + _disable(); + if(inum == PIT_TIMER_INTR) { + /* restore the original timer frequency */ + set_timer_reload(65535); + } + + /* restore the original interrupt handler */ + _dos_setvect(inum, prev_timer_intr); + _enable(); +} + +void reset_timer(void) +{ + ticks = 0; +} + +unsigned long get_msec(void) +{ + return ticks * tick_interval; +} + +static void set_timer_reload(int reload_val) +{ + outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE); + outp(PORT_DATA0, reload_val & 0xff); + outp(PORT_DATA0, (reload_val >> 8) & 0xff); +} + +static void __interrupt __far dos_timer_intr() +{ + ticks++; + _chain_intr(prev_timer_intr); /* DOES NOT RETURN */ +} + +/* first PIC command port */ +#define PIC1_CMD 0x20 +/* end of interrupt control word */ +#define OCW2_EOI (1 << 5) + +static void __interrupt __far timer_irq() +{ + static unsigned long dos_ticks; + + ticks++; + + if(++dos_ticks >= ticks_per_dos_intr) { + /* I suppose the dos irq handler does the EOI so I shouldn't + * do it if I am to call the previous function + */ + dos_ticks = 0; + _chain_intr(prev_timer_intr); /* XXX DOES NOT RETURN */ + return; /* just for clarity */ + } + + /* send EOI to the PIC */ + outp(PIC1_CMD, OCW2_EOI); +} diff -r 000000000000 -r 2a5340a6eee4 src/timer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/timer.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,29 @@ +/* +256-color 3D graphics hack for real-mode DOS. +Copyright (C) 2011 John Tsiombikas + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ +#ifndef TIMER_H_ +#define TIMER_H_ + +/* expects the required timer resolution in hertz + * if res_hz is 0, the current resolution is retained + */ +void init_timer(int res_hz); + +void reset_timer(void); +unsigned long get_msec(void); + +#endif /* TIMER_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/vbe.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vbe.c Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,153 @@ +#include +#include +#include "vbe.h" +#include "dpmi.h" + +/* VGA DAC registers used for palette setting in 8bpp modes */ +#define VGA_DAC_STATE 0x3c7 +#define VGA_DAC_ADDR_RD 0x3c7 +#define VGA_DAC_ADDR_WR 0x3c8 +#define VGA_DAC_DATA 0x3c9 + +#define MODE_LFB (1 << 14) + + +struct vbe_info *vbe_get_info(void) +{ + static unsigned short info_block_seg; + static struct vbe_info *info; + struct dpmi_real_regs regs; + + if(!info) { + /* allocate 32 paragraphs (512 bytes) */ + info_block_seg = dpmi_alloc(32); + info = (struct vbe_info*)(info_block_seg << 4); + } + + memcpy(info->sig, "VBE2", 4); + + memset(®s, 0, sizeof regs); + regs.es = info_block_seg; + regs.eax = 0x4f00; + + dpmi_real_int(0x10, ®s); + + return info; +} + +struct vbe_mode_info *vbe_get_mode_info(int mode) +{ + static unsigned short mode_info_seg; + static struct vbe_mode_info *mi; + struct dpmi_real_regs regs; + + if(!mi) { + /* allocate 16 paragraphs (256 bytes) */ + mode_info_seg = dpmi_alloc(16); + mi = (struct vbe_mode_info*)(mode_info_seg << 4); + } + + memset(®s, 0, sizeof regs); + regs.es = mode_info_seg; + regs.eax = 0x4f01; + regs.ecx = mode; + regs.es = mode_info_seg; + + dpmi_real_int(0x10, ®s); + if(regs.eax & 0xff00) { + return 0; + } + + return mi; +} + +int vbe_set_mode(int mode) +{ + struct dpmi_real_regs regs; + + memset(®s, 0, sizeof regs); + regs.eax = 0x4f02; + regs.ebx = mode; + dpmi_real_int(0x10, ®s); + + if(regs.eax == 0x100) { + return -1; + } + return 0; +} + +int vbe_set_palette_bits(int bits) +{ + struct dpmi_real_regs regs; + + memset(®s, 0, sizeof regs); + regs.eax = 0x4f08; + regs.ebx = bits << 8; /* bits in bh */ + dpmi_real_int(0x10, ®s); + + if((regs.eax >> 8) & 0xff == 3) { + return -1; + } + return regs.ebx >> 8 & 0xff; /* new color bits in bh */ +} + +/* TODO: implement palette setting through the VBE2 interface for + * non-VGA displays (actually don't). + */ +void vbe_set_palette(int idx, int *col, int count, int bits) +{ + int i, shift = 8 - bits; + + __asm { + mov dx, VGA_DAC_ADDR_WR + mov eax, idx + out dx, al + } + + for(i=0; i>= shift; + g >>= shift; + b >>= shift; + } + + __asm { + mov dx, VGA_DAC_DATA + mov al, r + out dx, al + mov al, g + out dx, al + mov al, b + out dx, al + } + } +} + +static unsigned int get_mask(int sz, int pos) +{ + unsigned int i, mask = 0; + + for(i=0; ixres, mi->yres); + fprintf(fp, "color depth: %d\n", mi->bpp); + fprintf(fp, "mode attributes: %x\n", mi->mode_attr); + fprintf(fp, "bytes per scanline: %d\n", mi->scanline_bytes); + fprintf(fp, "number of planes: %d\n", (int)mi->num_planes); + fprintf(fp, "number of banks: %d\n", (int)mi->num_banks); + fprintf(fp, "mem model: %d\n", (int)mi->mem_model); + fprintf(fp, "red bits: %d (mask: %x)\n", (int)mi->rmask_size, get_mask(mi->rmask_size, mi->rpos)); + fprintf(fp, "green bits: %d (mask: %x)\n", (int)mi->gmask_size, get_mask(mi->gmask_size, mi->gpos)); + fprintf(fp, "blue bits: %d (mask: %x)\n", (int)mi->bmask_size, get_mask(mi->bmask_size, mi->bpos)); + fprintf(fp, "framebuffer address: %x\n", mi->fb_addr); +} diff -r 000000000000 -r 2a5340a6eee4 src/vbe.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vbe.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,70 @@ +#ifndef VBE_H_ +#define VBE_H_ + +#include "inttypes.h" + +#define VBE_ATTR_LFB (1 << 7) +#define VBE_MODE_LFB (1 << 14) + +#pragma pack (push, 0) +struct vbe_info { + uint8_t sig[4]; + uint16_t version; + uint32_t oem_str_ptr; + uint8_t caps[4]; /* capabilities */ + uint32_t vid_mode_ptr; /* vbefarptr to video mode list */ + uint16_t total_mem; /* num of 64k mem blocks */ + uint16_t oem_sw_rev; /* VBE implementation software revision */ + uint32_t oem_vendor_name_ptr; + uint32_t oem_product_name_ptr; + uint32_t oem_product_rev_ptr; + uint8_t reserved[222]; + uint8_t oem_data[256]; +}; + +struct vbe_mode_info { + uint16_t mode_attr; + uint8_t wina_attr, winb_attr; + uint16_t win_gran, win_size; + uint16_t wina_seg, winb_seg; + uint32_t win_func; + uint16_t scanline_bytes; + + /* VBE 1.2 and above */ + uint16_t xres, yres; + uint8_t xcharsz, ycharsz; + uint8_t num_planes; + uint8_t bpp; + uint8_t num_banks; + uint8_t mem_model; + uint8_t bank_size; /* bank size in KB */ + uint8_t num_img_pages; + uint8_t reserved1; + + /* direct color fields */ + uint8_t rmask_size, rpos; + uint8_t gmask_size, gpos; + uint8_t bmask_size, bpos; + uint8_t xmask_size, xpos; + uint8_t cmode_info; /* direct color mode attributes */ + + /* VBE 2.0 and above */ + uint32_t fb_addr; /* physical address of the linear framebuffer */ + uint32_t reserved2; + uint16_t reserved3; + + uint8_t reserved4[206]; +}; +#pragma pack (pop) + +struct vbe_info *vbe_get_info(void); +struct vbe_mode_info *vbe_get_mode_info(unsigned int mode); + +int vbe_set_mode(unsigned int mode); + +int vbe_set_palette_bits(int bits); +void vbe_set_palette(int idx, int *col, int count, int bits); + +void print_mode_info(FILE *fp, struct vbe_mode_info *modei); + +#endif /* VBE_H_ */ diff -r 000000000000 -r 2a5340a6eee4 src/vmath.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vmath.cc Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,18 @@ +#include "vmathmat.h" +#include "vmath.h" + +void Matrix4x4::lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up) +{ + Vector3 vk = normalize(targ - pos); + Vector3 vj = normalize(up); + Vector3 vi = normalize(cross(vk, vj)); + vj = cross(vi, vk); + + Matrix4x4 m( + vi.x, vi.y, vi.z, 0, + vj.x, vj.y, vj.z, 0, + -vk.x, -vk.y, -vk.z, 0, + 0, 0, 0, 1); + translate(-pos.x, -pos.y, -pos.z); + *this = *this * m; +} diff -r 000000000000 -r 2a5340a6eee4 src/vmath.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vmath.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,148 @@ +#ifndef VMATH_H_ +#define VMATH_H_ + +#include +#include "vmathmat.h" + +class Vector3 { +public: + float x, y, z; + + Vector3() : x(0), y(0), z(0) {} + Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {} + + float length_sq() const { return x * x + y * y + z * z; } + float length() const { return sqrt(x * x + y * y + z * z); } + + void normalize() + { + float len = length(); + if(len != 0.0) { + x /= len; + y /= len; + z /= len; + } + } + + float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); } + const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); } +}; + +inline Vector3 normalize(const Vector3 &v) +{ + float len = v.length(); + if(len != 0.0) { + return Vector3(v.x / len, v.y / len, v.z / len); + } + return v; +} + +inline Vector3 operator +(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x + b.x, a.y + b.y, a.z + b.z); +} + +inline Vector3 operator -(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); +} + +inline Vector3 operator *(const Vector3 &v, float s) +{ + return Vector3(v.x * s, v.y * s, v.z * s); +} + +inline Vector3 operator /(const Vector3 &v, float s) +{ + return Vector3(v.x / s, v.y / s, v.z / s); +} + +inline float dot(const Vector3 &a, const Vector3 &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +inline Vector3 cross(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.y * b.z - a.z * b.y, + a.z * b.z - a.x * b.z, + a.x * b.y - a.y * b.x); +} + +inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v) +{ + float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3]; + float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3]; + float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3]; + return Vector3(x, y, z); +} + +// ---- Vector4 ---- + +class Vector4 { +public: + float x, y, z, w; + + Vector4() : x(0), y(0), z(0), w(1.0) {} + Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {} + Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {} + + float length_sq() const { return x * x + y * y + z * z + w * w; } + float length() const { return sqrt(x * x + y * y + z * z + w * w); } + + void normalize() + { + float len = length(); + if(len != 0.0) { + x /= len; + y /= len; + z /= len; + w /= len; + } + } + + float &operator [](int idx) + { + return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x)); + } + const float &operator [](int idx) const + { + return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x)); + } +}; + +inline Vector4 operator +(const Vector4 &a, const Vector4 &b) +{ + return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w); +} + +inline Vector4 operator -(const Vector4 &a, const Vector4 &b) +{ + return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w); +} + +inline Vector4 operator *(const Vector4 &v, float s) +{ + return Vector4(v.x * s, v.y * s, v.z * s, v.w * s); +} + +inline Vector4 operator /(const Vector4 &v, float s) +{ + return Vector4(v.x / s, v.y / s, v.z / s, v.w / s); +} + +inline float dot(const Vector4 &a, const Vector4 &b) +{ + return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w; +} + +inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v) +{ + float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3] * v.w; + float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3] * v.w; + float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3] * v.w; + float w = m.m[3][0] * v.x + m.m[3][1] * v.y + m.m[3][2] * v.z + m.m[3][3] * v.w; + return Vector4(x, y, z, w); +} + +#endif // VMATH_H_ diff -r 000000000000 -r 2a5340a6eee4 src/vmathmat.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/vmathmat.h Sat Apr 05 08:46:27 2014 +0300 @@ -0,0 +1,116 @@ +#ifndef VMATH_MATRIX_H_ +#define VMATH_MATRIX_H_ + +#include + +#ifndef M_PI +#define M_PI 3.141592653 +#endif + +class Vector3; + +class Matrix4x4 { +public: + float m[4][4]; + + Matrix4x4() + { + set_identity(); + } + + Matrix4x4(float m00, float m01, float m02, float m03, + float m10, float m11, float m12, float m13, + float m20, float m21, float m22, float m23, + float m30, float m31, float m32, float m33) + { + m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03; + m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13; + m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23; + m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33; + } + + inline void set_identity(); + inline void translate(float x, float y, float z); + inline void rotate(float angle, float x, float y, float z); + inline void scale(float x, float y, float z); + inline void perspective(float vfov, float aspect, float znear, float zfar); + inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up); + + float *operator [](int idx) { return m[idx]; } + const float *operator [](int idx) const { return m[idx]; } +}; + +inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) +{ + Matrix4x4 res; + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] + + a[i][2] * b[2][j] + a[i][3] * b[3][j]; + } + } + return res; +} + +inline void Matrix4x4::set_identity() +{ + m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0; + m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0; + m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0; +} + +inline void Matrix4x4::translate(float x, float y, float z) +{ + Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1); + *this = *this * m; +} + +inline void Matrix4x4::rotate(float angle, float x, float y, float z) +{ + float sina = (float)sin(angle); + float cosa = (float)cos(angle); + float rcosa = 1.0f - cosa; + float nxsq = x * x; + float nysq = y * y; + float nzsq = z * z; + + Matrix4x4 m; + m[0][0] = nxsq + (1.0f - nxsq) * cosa; + m[0][1] = x * y * rcosa - z * sina; + m[0][2] = x * z * rcosa + y * sina; + + m[1][0] = x * y * rcosa + z * sina; + m[1][1] = nysq + (1.0f - nysq) * cosa; + m[1][2] = y * z * rcosa - x * sina; + + m[2][0] = x * z * rcosa - y * sina; + m[2][1] = y * z * rcosa + x * sina; + m[2][2] = nzsq + (1.0f - nzsq) * cosa; + + *this = *this * m; +} + +inline void Matrix4x4::scale(float x, float y, float z) +{ + Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1); + *this = *this * m; +} + +inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar) +{ + float f = 1.0f / tan(vfov * 0.5f); + float dz = znear - zfar; + + Matrix4x4 m; + m[0][0] = f / aspect; + m[1][1] = f; + m[2][2] = (zfar + znear) / dz; + m[3][2] = -1.0f; + m[2][3] = 2.0f * zfar * znear / dz; + m[3][3] = 0.0f; + + *this = *this * m; +} + + +#endif // VMATH_MATRIX_H_