# HG changeset patch # User John Tsiombikas # Date 1396677926 -10800 # Node ID a826bf0fb169e0dab23c48b0c40434c9f3fce1d7 # Parent 2a5340a6eee41acc0a5d2608f43c0665ffb6581c fixed line endings diff -r 2a5340a6eee4 -r a826bf0fb169 src/camera.cc --- a/src/camera.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/camera.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,50 +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 -} +#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 2a5340a6eee4 -r a826bf0fb169 src/camera.h --- a/src/camera.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/camera.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,28 +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_ +#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 2a5340a6eee4 -r a826bf0fb169 src/inttypes.h --- a/src/inttypes.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/inttypes.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,16 +1,16 @@ -#ifndef INT_TYPES_H_ -#define INT_TYPES_H_ - -#if defined(__DOS__) || defined(WIN32) -typedef char int8_t; -typedef short int16_t; -typedef long int32_t; - -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned long uint32_t; -#else -#include -#endif - -#endif /* INT_TYPES_H_ */ +#ifndef INT_TYPES_H_ +#define INT_TYPES_H_ + +#if defined(__DOS__) || defined(WIN32) +typedef char int8_t; +typedef short int16_t; +typedef long int32_t; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned long uint32_t; +#else +#include +#endif + +#endif /* INT_TYPES_H_ */ diff -r 2a5340a6eee4 -r a826bf0fb169 src/keyb.h --- a/src/keyb.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/keyb.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,48 +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_ */ +/* +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 2a5340a6eee4 -r a826bf0fb169 src/light.cc --- a/src/light.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/light.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,41 +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; -} +#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 2a5340a6eee4 -r a826bf0fb169 src/light.h --- a/src/light.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/light.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,25 +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_ +#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 2a5340a6eee4 -r a826bf0fb169 src/logger.c --- a/src/logger.c Sat Apr 05 08:46:27 2014 +0300 +++ b/src/logger.c Sat Apr 05 09:05:26 2014 +0300 @@ -1,23 +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); -} +#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 2a5340a6eee4 -r a826bf0fb169 src/logger.h --- a/src/logger.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/logger.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,14 +1,14 @@ -#ifndef LOGGER_H_ -#define LOGGER_H_ - -#ifdef __cplusplus -extern "C" { -#endif - -void printlog(const char *fmt, ...); - -#ifdef __cplusplus -} -#endif - -#endif /* LOGGER_H_ */ +#ifndef LOGGER_H_ +#define LOGGER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void printlog(const char *fmt, ...); + +#ifdef __cplusplus +} +#endif + +#endif /* LOGGER_H_ */ diff -r 2a5340a6eee4 -r a826bf0fb169 src/m3dimpl.h --- a/src/m3dimpl.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/m3dimpl.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,23 +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_ */ +#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 2a5340a6eee4 -r a826bf0fb169 src/main.cc --- a/src/main.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/main.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,212 +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 +#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 */ -} - +#include +#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 2a5340a6eee4 -r a826bf0fb169 src/min3d.h --- a/src/min3d.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/min3d.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,73 +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_ */ +#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 2a5340a6eee4 -r a826bf0fb169 src/mouse.c --- a/src/mouse.c Sat Apr 05 08:46:27 2014 +0300 +++ b/src/mouse.c Sat Apr 05 09:05:26 2014 +0300 @@ -1,72 +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); -} +/* 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 2a5340a6eee4 -r a826bf0fb169 src/mouse.h --- a/src/mouse.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/mouse.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,22 +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_ */ +#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 2a5340a6eee4 -r a826bf0fb169 src/object.cc --- a/src/object.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/object.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,53 +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); -} +#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 2a5340a6eee4 -r a826bf0fb169 src/object.h --- a/src/object.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/object.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,20 +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_ +#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 2a5340a6eee4 -r a826bf0fb169 src/pit8254.h --- a/src/pit8254.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/pit8254.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,34 +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_ */ +#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 2a5340a6eee4 -r a826bf0fb169 src/rend.cc --- a/src/rend.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/rend.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,10 +1,10 @@ -#include "rend.h" - -bool rend_init() -{ - return false; -} - -void rend_shutdown() -{ -} +#include "rend.h" + +bool rend_init() +{ + return false; +} + +void rend_shutdown() +{ +} diff -r 2a5340a6eee4 -r a826bf0fb169 src/rend.h --- a/src/rend.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/rend.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,7 +1,7 @@ -#ifndef REND_H_ -#define REND_H_ - -bool rend_init(); -void rend_shutdown(); - -#endif // REND_H_ +#ifndef REND_H_ +#define REND_H_ + +bool rend_init(); +void rend_shutdown(); + +#endif // REND_H_ diff -r 2a5340a6eee4 -r a826bf0fb169 src/scancode.h --- a/src/scancode.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/scancode.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,31 +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 */ -}; +#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 2a5340a6eee4 -r a826bf0fb169 src/scene.cc --- a/src/scene.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/scene.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,43 +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 -{ -} +#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 2a5340a6eee4 -r a826bf0fb169 src/scene.h --- a/src/scene.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/scene.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,29 +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_ +#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 2a5340a6eee4 -r a826bf0fb169 src/timer.c --- a/src/timer.c Sat Apr 05 08:46:27 2014 +0300 +++ b/src/timer.c Sat Apr 05 09:05:26 2014 +0300 @@ -1,131 +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); -} +/* +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 2a5340a6eee4 -r a826bf0fb169 src/timer.h --- a/src/timer.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/timer.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,29 +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_ */ +/* +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 2a5340a6eee4 -r a826bf0fb169 src/vmath.cc --- a/src/vmath.cc Sat Apr 05 08:46:27 2014 +0300 +++ b/src/vmath.cc Sat Apr 05 09:05:26 2014 +0300 @@ -1,18 +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; -} +#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 2a5340a6eee4 -r a826bf0fb169 src/vmath.h --- a/src/vmath.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/vmath.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,148 +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_ +#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 2a5340a6eee4 -r a826bf0fb169 src/vmathmat.h --- a/src/vmathmat.h Sat Apr 05 08:46:27 2014 +0300 +++ b/src/vmathmat.h Sat Apr 05 09:05:26 2014 +0300 @@ -1,116 +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_ +#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_