rayzor

changeset 1:a826bf0fb169

fixed line endings
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Apr 2014 09:05:26 +0300
parents 2a5340a6eee4
children c273c6f799a4
files src/camera.cc src/camera.h src/inttypes.h src/keyb.h src/light.cc src/light.h src/logger.c src/logger.h src/m3dimpl.h src/main.cc src/min3d.c src/min3d.h src/mouse.c src/mouse.h src/object.cc src/object.h src/pit8254.h src/rend.cc src/rend.h src/scancode.h src/scene.cc src/scene.h src/timer.c src/timer.h src/vmath.cc src/vmath.h src/vmathmat.h
diffstat 27 files changed, 1494 insertions(+), 1494 deletions(-) [+]
line diff
     1.1 --- a/src/camera.cc	Sat Apr 05 08:46:27 2014 +0300
     1.2 +++ b/src/camera.cc	Sat Apr 05 09:05:26 2014 +0300
     1.3 @@ -1,50 +1,50 @@
     1.4 -#include "camera.h"
     1.5 -
     1.6 -Camera::Camera()
     1.7 -	: pos(0, 0, 10)
     1.8 -{
     1.9 -	fov = M_PI;
    1.10 -}
    1.11 -
    1.12 -void Camera::set_position(const Vector3 &pos)
    1.13 -{
    1.14 -	this->pos = pos;
    1.15 -}
    1.16 -
    1.17 -const Vector3 &Camera::get_position() const
    1.18 -{
    1.19 -	return pos;
    1.20 -}
    1.21 -
    1.22 -void Camera::set_target(const Vector3 &target)
    1.23 -{
    1.24 -	this->target = target;
    1.25 -}
    1.26 -
    1.27 -const Vector3 &Camera::get_target() const
    1.28 -{
    1.29 -	return target;
    1.30 -}
    1.31 -
    1.32 -void Camera::set_fov(float fov)
    1.33 -{
    1.34 -	this->fov = fov;
    1.35 -}
    1.36 -
    1.37 -float Camera::get_fov() const
    1.38 -{
    1.39 -	return fov;
    1.40 -}
    1.41 -
    1.42 -Matrix4x4 Camera::get_matrix() const
    1.43 -{
    1.44 -	Matrix4x4 res;
    1.45 -	res.lookat(pos, target, Vector3(0, 1, 0));
    1.46 -	return res;
    1.47 -}
    1.48 -
    1.49 -Matrix4x4 Camera::get_inv_matrix() const
    1.50 -{
    1.51 -	Matrix4x4 res;
    1.52 -	return res;	// TODO
    1.53 -}
    1.54 +#include "camera.h"
    1.55 +
    1.56 +Camera::Camera()
    1.57 +	: pos(0, 0, 10)
    1.58 +{
    1.59 +	fov = M_PI;
    1.60 +}
    1.61 +
    1.62 +void Camera::set_position(const Vector3 &pos)
    1.63 +{
    1.64 +	this->pos = pos;
    1.65 +}
    1.66 +
    1.67 +const Vector3 &Camera::get_position() const
    1.68 +{
    1.69 +	return pos;
    1.70 +}
    1.71 +
    1.72 +void Camera::set_target(const Vector3 &target)
    1.73 +{
    1.74 +	this->target = target;
    1.75 +}
    1.76 +
    1.77 +const Vector3 &Camera::get_target() const
    1.78 +{
    1.79 +	return target;
    1.80 +}
    1.81 +
    1.82 +void Camera::set_fov(float fov)
    1.83 +{
    1.84 +	this->fov = fov;
    1.85 +}
    1.86 +
    1.87 +float Camera::get_fov() const
    1.88 +{
    1.89 +	return fov;
    1.90 +}
    1.91 +
    1.92 +Matrix4x4 Camera::get_matrix() const
    1.93 +{
    1.94 +	Matrix4x4 res;
    1.95 +	res.lookat(pos, target, Vector3(0, 1, 0));
    1.96 +	return res;
    1.97 +}
    1.98 +
    1.99 +Matrix4x4 Camera::get_inv_matrix() const
   1.100 +{
   1.101 +	Matrix4x4 res;
   1.102 +	return res;	// TODO
   1.103 +}
     2.1 --- a/src/camera.h	Sat Apr 05 08:46:27 2014 +0300
     2.2 +++ b/src/camera.h	Sat Apr 05 09:05:26 2014 +0300
     2.3 @@ -1,28 +1,28 @@
     2.4 -#ifndef CAMERA_H_
     2.5 -#define CAMERA_H_
     2.6 -
     2.7 -#include "vmath.h"
     2.8 -
     2.9 -class Camera {
    2.10 -private:
    2.11 -	Vector3 pos;
    2.12 -	Vector3 target;
    2.13 -	float fov;
    2.14 -
    2.15 -public:
    2.16 -	Camera();
    2.17 -
    2.18 -	void set_position(const Vector3 &pos);
    2.19 -	const Vector3 &get_position() const;
    2.20 -
    2.21 -	void set_target(const Vector3 &target);
    2.22 -	const Vector3 &get_target() const;
    2.23 -
    2.24 -	void set_fov(float fov);
    2.25 -	float get_fov() const;
    2.26 -
    2.27 -	Matrix4x4 get_matrix() const;
    2.28 -	Matrix4x4 get_inv_matrix() const;
    2.29 -};
    2.30 -
    2.31 -#endif	// CAMERA_H_
    2.32 +#ifndef CAMERA_H_
    2.33 +#define CAMERA_H_
    2.34 +
    2.35 +#include "vmath.h"
    2.36 +
    2.37 +class Camera {
    2.38 +private:
    2.39 +	Vector3 pos;
    2.40 +	Vector3 target;
    2.41 +	float fov;
    2.42 +
    2.43 +public:
    2.44 +	Camera();
    2.45 +
    2.46 +	void set_position(const Vector3 &pos);
    2.47 +	const Vector3 &get_position() const;
    2.48 +
    2.49 +	void set_target(const Vector3 &target);
    2.50 +	const Vector3 &get_target() const;
    2.51 +
    2.52 +	void set_fov(float fov);
    2.53 +	float get_fov() const;
    2.54 +
    2.55 +	Matrix4x4 get_matrix() const;
    2.56 +	Matrix4x4 get_inv_matrix() const;
    2.57 +};
    2.58 +
    2.59 +#endif	// CAMERA_H_
     3.1 --- a/src/inttypes.h	Sat Apr 05 08:46:27 2014 +0300
     3.2 +++ b/src/inttypes.h	Sat Apr 05 09:05:26 2014 +0300
     3.3 @@ -1,16 +1,16 @@
     3.4 -#ifndef INT_TYPES_H_
     3.5 -#define INT_TYPES_H_
     3.6 -
     3.7 -#if defined(__DOS__) || defined(WIN32)
     3.8 -typedef char int8_t;
     3.9 -typedef short int16_t;
    3.10 -typedef long int32_t;
    3.11 -
    3.12 -typedef unsigned char uint8_t;
    3.13 -typedef unsigned short uint16_t;
    3.14 -typedef unsigned long uint32_t;
    3.15 -#else
    3.16 -#include <stdint.h>
    3.17 -#endif
    3.18 -
    3.19 -#endif	/* INT_TYPES_H_ */
    3.20 +#ifndef INT_TYPES_H_
    3.21 +#define INT_TYPES_H_
    3.22 +
    3.23 +#if defined(__DOS__) || defined(WIN32)
    3.24 +typedef char int8_t;
    3.25 +typedef short int16_t;
    3.26 +typedef long int32_t;
    3.27 +
    3.28 +typedef unsigned char uint8_t;
    3.29 +typedef unsigned short uint16_t;
    3.30 +typedef unsigned long uint32_t;
    3.31 +#else
    3.32 +#include <stdint.h>
    3.33 +#endif
    3.34 +
    3.35 +#endif	/* INT_TYPES_H_ */
     4.1 --- a/src/keyb.h	Sat Apr 05 08:46:27 2014 +0300
     4.2 +++ b/src/keyb.h	Sat Apr 05 09:05:26 2014 +0300
     4.3 @@ -1,48 +1,48 @@
     4.4 -/*
     4.5 -DOS interrupt-based keyboard driver.
     4.6 -Copyright (C) 2013  John Tsiombikas <nuclear@member.fsf.org>
     4.7 -
     4.8 -This program is free software: you can redistribute it and/or modify
     4.9 -it under the terms of the GNU General Public License as published by
    4.10 -the Free Software Foundation, either version 3 of the License, or
    4.11 -(at your option) any later version.
    4.12 -
    4.13 -This program is distributed in the hope that it will be useful,
    4.14 -but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    4.16 -GNU General Public License  for more details.
    4.17 -
    4.18 -You should have received a copy of the GNU General Public License
    4.19 -along with the program. If not, see <http://www.gnu.org/licenses/>
    4.20 -*/
    4.21 -#ifndef KEYB_H_
    4.22 -#define KEYB_H_
    4.23 -
    4.24 -#define KB_ANY	(-1)
    4.25 -
    4.26 -#ifdef __cplusplus
    4.27 -extern "C" {
    4.28 -#endif
    4.29 -
    4.30 -int kb_init(int bufsz);	/* bufsz can be 0 for no buffered keys */
    4.31 -void kb_shutdown(void); /* don't forget to call this at the end! */
    4.32 -
    4.33 -/* Boolean predicate for testing the current state of a particular key.
    4.34 - * You may also pass KB_ANY to test if any key is held down.
    4.35 - */
    4.36 -int kb_isdown(int key);
    4.37 -
    4.38 -/* waits for any keypress */
    4.39 -void kb_wait(void);
    4.40 -
    4.41 -/* removes and returns a single key from the input buffer.
    4.42 - * If buffering is disabled (initialized with kb_init(0)), then it always
    4.43 - * returns the last key pressed.
    4.44 - */
    4.45 -int kb_getkey(void);
    4.46 -
    4.47 -#ifdef __cplusplus
    4.48 -}
    4.49 -#endif
    4.50 -
    4.51 -#endif	/* KEYB_H_ */
    4.52 +/*
    4.53 +DOS interrupt-based keyboard driver.
    4.54 +Copyright (C) 2013  John Tsiombikas <nuclear@member.fsf.org>
    4.55 +
    4.56 +This program is free software: you can redistribute it and/or modify
    4.57 +it under the terms of the GNU General Public License as published by
    4.58 +the Free Software Foundation, either version 3 of the License, or
    4.59 +(at your option) any later version.
    4.60 +
    4.61 +This program is distributed in the hope that it will be useful,
    4.62 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.63 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    4.64 +GNU General Public License  for more details.
    4.65 +
    4.66 +You should have received a copy of the GNU General Public License
    4.67 +along with the program. If not, see <http://www.gnu.org/licenses/>
    4.68 +*/
    4.69 +#ifndef KEYB_H_
    4.70 +#define KEYB_H_
    4.71 +
    4.72 +#define KB_ANY	(-1)
    4.73 +
    4.74 +#ifdef __cplusplus
    4.75 +extern "C" {
    4.76 +#endif
    4.77 +
    4.78 +int kb_init(int bufsz);	/* bufsz can be 0 for no buffered keys */
    4.79 +void kb_shutdown(void); /* don't forget to call this at the end! */
    4.80 +
    4.81 +/* Boolean predicate for testing the current state of a particular key.
    4.82 + * You may also pass KB_ANY to test if any key is held down.
    4.83 + */
    4.84 +int kb_isdown(int key);
    4.85 +
    4.86 +/* waits for any keypress */
    4.87 +void kb_wait(void);
    4.88 +
    4.89 +/* removes and returns a single key from the input buffer.
    4.90 + * If buffering is disabled (initialized with kb_init(0)), then it always
    4.91 + * returns the last key pressed.
    4.92 + */
    4.93 +int kb_getkey(void);
    4.94 +
    4.95 +#ifdef __cplusplus
    4.96 +}
    4.97 +#endif
    4.98 +
    4.99 +#endif	/* KEYB_H_ */
     5.1 --- a/src/light.cc	Sat Apr 05 08:46:27 2014 +0300
     5.2 +++ b/src/light.cc	Sat Apr 05 09:05:26 2014 +0300
     5.3 @@ -1,41 +1,41 @@
     5.4 -#include "light.h"
     5.5 -
     5.6 -Light::Light()
     5.7 -{
     5.8 -	color.x = color.y = color.z = 1.0;
     5.9 -	atten.x = 1.0;
    5.10 -	atten.y = 0.0;
    5.11 -	atten.z = 0.0;
    5.12 -}
    5.13 -
    5.14 -void Light::set_position(const Vector3 &pos)
    5.15 -{
    5.16 -	this->pos = pos;
    5.17 -}
    5.18 -
    5.19 -const Vector3 &Light::get_position() const
    5.20 -{
    5.21 -	return pos;
    5.22 -}
    5.23 -
    5.24 -
    5.25 -void Light::set_color(const Vector3 &color)
    5.26 -{
    5.27 -	this->color = color;
    5.28 -}
    5.29 -
    5.30 -const Vector3 &Light::get_color() const
    5.31 -{
    5.32 -	return color;
    5.33 -}
    5.34 -
    5.35 -
    5.36 -void Light::set_attenuation(const Vector3 &atten)
    5.37 -{
    5.38 -	this->atten = atten;
    5.39 -}
    5.40 -
    5.41 -const Vector3 &Light::get_attenuation() const
    5.42 -{
    5.43 -	return atten;
    5.44 -}
    5.45 +#include "light.h"
    5.46 +
    5.47 +Light::Light()
    5.48 +{
    5.49 +	color.x = color.y = color.z = 1.0;
    5.50 +	atten.x = 1.0;
    5.51 +	atten.y = 0.0;
    5.52 +	atten.z = 0.0;
    5.53 +}
    5.54 +
    5.55 +void Light::set_position(const Vector3 &pos)
    5.56 +{
    5.57 +	this->pos = pos;
    5.58 +}
    5.59 +
    5.60 +const Vector3 &Light::get_position() const
    5.61 +{
    5.62 +	return pos;
    5.63 +}
    5.64 +
    5.65 +
    5.66 +void Light::set_color(const Vector3 &color)
    5.67 +{
    5.68 +	this->color = color;
    5.69 +}
    5.70 +
    5.71 +const Vector3 &Light::get_color() const
    5.72 +{
    5.73 +	return color;
    5.74 +}
    5.75 +
    5.76 +
    5.77 +void Light::set_attenuation(const Vector3 &atten)
    5.78 +{
    5.79 +	this->atten = atten;
    5.80 +}
    5.81 +
    5.82 +const Vector3 &Light::get_attenuation() const
    5.83 +{
    5.84 +	return atten;
    5.85 +}
     6.1 --- a/src/light.h	Sat Apr 05 08:46:27 2014 +0300
     6.2 +++ b/src/light.h	Sat Apr 05 09:05:26 2014 +0300
     6.3 @@ -1,25 +1,25 @@
     6.4 -#ifndef LIGHT_H_
     6.5 -#define LIGHT_H_
     6.6 -
     6.7 -#include "vmath.h"
     6.8 -
     6.9 -class Light {
    6.10 -private:
    6.11 -	Vector3 pos;
    6.12 -	Vector3 color;
    6.13 -	Vector3 atten;
    6.14 -
    6.15 -public:
    6.16 -	Light();
    6.17 -
    6.18 -	void set_position(const Vector3 &pos);
    6.19 -	const Vector3 &get_position() const;
    6.20 -
    6.21 -	void set_color(const Vector3 &color);
    6.22 -	const Vector3 &get_color() const;
    6.23 -
    6.24 -	void set_attenuation(const Vector3 &atten);
    6.25 -	const Vector3 &get_attenuation() const;
    6.26 -};
    6.27 -
    6.28 -#endif	// LIGHT_H_
    6.29 +#ifndef LIGHT_H_
    6.30 +#define LIGHT_H_
    6.31 +
    6.32 +#include "vmath.h"
    6.33 +
    6.34 +class Light {
    6.35 +private:
    6.36 +	Vector3 pos;
    6.37 +	Vector3 color;
    6.38 +	Vector3 atten;
    6.39 +
    6.40 +public:
    6.41 +	Light();
    6.42 +
    6.43 +	void set_position(const Vector3 &pos);
    6.44 +	const Vector3 &get_position() const;
    6.45 +
    6.46 +	void set_color(const Vector3 &color);
    6.47 +	const Vector3 &get_color() const;
    6.48 +
    6.49 +	void set_attenuation(const Vector3 &atten);
    6.50 +	const Vector3 &get_attenuation() const;
    6.51 +};
    6.52 +
    6.53 +#endif	// LIGHT_H_
     7.1 --- a/src/logger.c	Sat Apr 05 08:46:27 2014 +0300
     7.2 +++ b/src/logger.c	Sat Apr 05 09:05:26 2014 +0300
     7.3 @@ -1,23 +1,23 @@
     7.4 -#include <stdio.h>
     7.5 -#include <stdarg.h>
     7.6 -#include "logger.h"
     7.7 -
     7.8 -#define LOGFNAME	"rayzor.log"
     7.9 -
    7.10 -static FILE *logfile;
    7.11 -
    7.12 -void printlog(const char *fmt, ...)
    7.13 -{
    7.14 -	va_list ap;
    7.15 -
    7.16 -	if(!logfile) {
    7.17 -		if(!(logfile = fopen(LOGFNAME, "w"))) {
    7.18 -			return;
    7.19 -		}
    7.20 -		setvbuf(logfile, 0, _IOLBF, 0);
    7.21 -	}
    7.22 -
    7.23 -	va_start(ap, fmt);
    7.24 -	vfprintf(logfile, fmt, ap);
    7.25 -	va_end(ap);
    7.26 -}
    7.27 +#include <stdio.h>
    7.28 +#include <stdarg.h>
    7.29 +#include "logger.h"
    7.30 +
    7.31 +#define LOGFNAME	"rayzor.log"
    7.32 +
    7.33 +static FILE *logfile;
    7.34 +
    7.35 +void printlog(const char *fmt, ...)
    7.36 +{
    7.37 +	va_list ap;
    7.38 +
    7.39 +	if(!logfile) {
    7.40 +		if(!(logfile = fopen(LOGFNAME, "w"))) {
    7.41 +			return;
    7.42 +		}
    7.43 +		setvbuf(logfile, 0, _IOLBF, 0);
    7.44 +	}
    7.45 +
    7.46 +	va_start(ap, fmt);
    7.47 +	vfprintf(logfile, fmt, ap);
    7.48 +	va_end(ap);
    7.49 +}
     8.1 --- a/src/logger.h	Sat Apr 05 08:46:27 2014 +0300
     8.2 +++ b/src/logger.h	Sat Apr 05 09:05:26 2014 +0300
     8.3 @@ -1,14 +1,14 @@
     8.4 -#ifndef LOGGER_H_
     8.5 -#define LOGGER_H_
     8.6 -
     8.7 -#ifdef __cplusplus
     8.8 -extern "C" {
     8.9 -#endif
    8.10 -
    8.11 -void printlog(const char *fmt, ...);
    8.12 -
    8.13 -#ifdef __cplusplus
    8.14 -}
    8.15 -#endif
    8.16 -
    8.17 -#endif	/* LOGGER_H_ */
    8.18 +#ifndef LOGGER_H_
    8.19 +#define LOGGER_H_
    8.20 +
    8.21 +#ifdef __cplusplus
    8.22 +extern "C" {
    8.23 +#endif
    8.24 +
    8.25 +void printlog(const char *fmt, ...);
    8.26 +
    8.27 +#ifdef __cplusplus
    8.28 +}
    8.29 +#endif
    8.30 +
    8.31 +#endif	/* LOGGER_H_ */
     9.1 --- a/src/m3dimpl.h	Sat Apr 05 08:46:27 2014 +0300
     9.2 +++ b/src/m3dimpl.h	Sat Apr 05 09:05:26 2014 +0300
     9.3 @@ -1,23 +1,23 @@
     9.4 -#ifndef M3DIMPL_H_
     9.5 -#define M3DIMPL_H_
     9.6 -
     9.7 -#include "min3d.h"
     9.8 -
     9.9 -#define MSTACK_SIZE		16
    9.10 -
    9.11 -struct min3d_mstack {
    9.12 -	float m[MSTACK_SIZE][16];
    9.13 -	int top;
    9.14 -};
    9.15 -
    9.16 -struct min3d_context {
    9.17 -	struct m3d_image *cbuf;
    9.18 -	uint16_t *zbuf;
    9.19 -
    9.20 -	unsigned long state;
    9.21 -
    9.22 -	int mmode;	/* matrix mode */
    9.23 -	struct min3d_mstack mstack[2];
    9.24 -} *m3dctx;
    9.25 -
    9.26 -#endif	/* M3DIMPL_H_ */
    9.27 +#ifndef M3DIMPL_H_
    9.28 +#define M3DIMPL_H_
    9.29 +
    9.30 +#include "min3d.h"
    9.31 +
    9.32 +#define MSTACK_SIZE		16
    9.33 +
    9.34 +struct min3d_mstack {
    9.35 +	float m[MSTACK_SIZE][16];
    9.36 +	int top;
    9.37 +};
    9.38 +
    9.39 +struct min3d_context {
    9.40 +	struct m3d_image *cbuf;
    9.41 +	uint16_t *zbuf;
    9.42 +
    9.43 +	unsigned long state;
    9.44 +
    9.45 +	int mmode;	/* matrix mode */
    9.46 +	struct min3d_mstack mstack[2];
    9.47 +} *m3dctx;
    9.48 +
    9.49 +#endif	/* M3DIMPL_H_ */
    10.1 --- a/src/main.cc	Sat Apr 05 08:46:27 2014 +0300
    10.2 +++ b/src/main.cc	Sat Apr 05 09:05:26 2014 +0300
    10.3 @@ -1,212 +1,212 @@
    10.4 -#include <stdio.h>
    10.5 -#include <stdlib.h>
    10.6 -#include <string.h>
    10.7 -#include <math.h>
    10.8 -#include "inttypes.h"
    10.9 -#include "gfx.h"
   10.10 -#include "keyb.h"
   10.11 -#include "mouse.h"
   10.12 -#include "logger.h"
   10.13 -
   10.14 -static void display();
   10.15 -static void swap_buffers();
   10.16 -static void handle_keyboard();
   10.17 -static void handle_mouse();
   10.18 -static bool parse_args(int argc, char **argv);
   10.19 -
   10.20 -static int xsz = 800;
   10.21 -static int ysz = 600;
   10.22 -static int bpp = 16;
   10.23 -static int bytespp;
   10.24 -static unsigned char *fb;
   10.25 -static unsigned char *backbuf;
   10.26 -static int rbits, gbits, bbits;
   10.27 -static int rshift, gshift, bshift;
   10.28 -static unsigned int rmask, gmask, bmask;
   10.29 -
   10.30 -static bool quit;
   10.31 -
   10.32 -int main(int argc, char **argv)
   10.33 -{
   10.34 -	if(!parse_args(argc, argv)) {
   10.35 -		return 1;
   10.36 -	}
   10.37 -	if(kb_init(32) == -1) {
   10.38 -		fprintf(stderr, "failed to initialize keyboard driver\n");
   10.39 -		return 1;
   10.40 -	}
   10.41 -	if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) {
   10.42 -		set_text_mode();
   10.43 -		fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp);
   10.44 -		return 1;
   10.45 -	}
   10.46 -	bpp = get_color_depth();
   10.47 -	get_color_bits(&rbits, &gbits, &bbits);
   10.48 -	get_color_shift(&rshift, &gshift, &bshift);
   10.49 -	get_color_mask(&rmask, &gmask, &bmask);
   10.50 -	bytespp = (int)ceil(bpp / 8.0);
   10.51 -
   10.52 -	printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits);
   10.53 -	printlog("shift: %d %d %d\n", rshift, gshift, bshift);
   10.54 -	printlog("mask: %x %x %x\n", rmask, gmask, bmask);
   10.55 -
   10.56 -	backbuf = new unsigned char[xsz * ysz * 3];
   10.57 -
   10.58 -	// main loop
   10.59 -	for(;;) {
   10.60 -		handle_keyboard();
   10.61 -		handle_mouse();
   10.62 -		if(quit) break;
   10.63 -
   10.64 -		display();
   10.65 -	}
   10.66 -
   10.67 -	delete [] backbuf;
   10.68 -
   10.69 -	set_text_mode();
   10.70 -	kb_shutdown();
   10.71 -
   10.72 -	printf("Thank you for using Rayzor!\n");
   10.73 -	return 0;
   10.74 -}
   10.75 -
   10.76 -static void display()
   10.77 -{
   10.78 -	unsigned char *fbptr = backbuf;
   10.79 -
   10.80 -	for(int i=0; i<ysz; i++) {
   10.81 -		for(int j=0; j<xsz; j++) {
   10.82 -			bool chess = ((i / 16) & 1) == ((j / 16) & 1);
   10.83 -			fbptr[chess ? 0 : 2] = 255;
   10.84 -			fbptr[1] = 128;
   10.85 -			fbptr[chess ? 2 : 0] = 32;
   10.86 -			fbptr += 3;
   10.87 -		}
   10.88 -	}
   10.89 -
   10.90 -	swap_buffers();
   10.91 -}
   10.92 -
   10.93 -#define PACK_RGB(r, g, b) \
   10.94 -	((((r) << rshift) & rmask) | \
   10.95 -	 (((g) << gshift) & gmask) | \
   10.96 -	 (((b) << bshift) & bmask))
   10.97 -
   10.98 -static void swap_buffers()
   10.99 -{
  10.100 -	unsigned char *src = backbuf;
  10.101 -	int num_pixels = xsz * ysz;
  10.102 -
  10.103 -	switch(bpp) {
  10.104 -	case 32:
  10.105 -		{
  10.106 -			uint32_t *dest = (uint32_t*)fb;
  10.107 -			for(int i=0; i<num_pixels; i++) {
  10.108 -				*dest++ = PACK_RGB(src[0], src[1], src[2]);
  10.109 -				src += 3;
  10.110 -			}
  10.111 -		}
  10.112 -		break;
  10.113 -
  10.114 -	case 24:
  10.115 -		memcpy(fb, backbuf, num_pixels * 3);
  10.116 -		break;
  10.117 -
  10.118 -	case 16:
  10.119 -	case 15:
  10.120 -		{
  10.121 -			int srs = 8 - rbits;
  10.122 -			int sgs = 8 - gbits;
  10.123 -			int sbs = 8 - bbits;
  10.124 -			uint16_t *dest = (uint16_t*)fb;
  10.125 -			for(int i=0; i<num_pixels; i++) {
  10.126 -				*dest++ = PACK_RGB(src[0] >> srs, src[1] >> sgs, src[2] >> sbs);
  10.127 -				src += 3;
  10.128 -			}
  10.129 -		}
  10.130 -		break;
  10.131 -
  10.132 -	default:
  10.133 -		break;
  10.134 -	}
  10.135 -}
  10.136 -
  10.137 -static void handle_keyboard()
  10.138 -{
  10.139 -	if(!kb_isdown(KB_ANY))
  10.140 -		return;
  10.141 -
  10.142 -	int c = kb_getkey();
  10.143 -	switch(c) {
  10.144 -	case 27:
  10.145 -		quit = true;
  10.146 -		return;
  10.147 -	}
  10.148 -}
  10.149 -
  10.150 -static void handle_mouse()
  10.151 -{
  10.152 -}
  10.153 -
  10.154 -static struct {
  10.155 -	int opt;
  10.156 -	const char *lopt;
  10.157 -	const char *desc;
  10.158 -} options[] = {
  10.159 -	{'s', "size", "resolution <xres>x<yres>[:bpp]"},
  10.160 -	{'h', "help", "print usage information and exit"},
  10.161 -	{-1, 0, 0}
  10.162 -};
  10.163 -
  10.164 -static void print_usage(const char *argv0)
  10.165 -{
  10.166 -	printf("%s usage\n", argv0);
  10.167 -	for(int i=0; options[i].opt != -1; i++) {
  10.168 -		printf("  -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
  10.169 -	}
  10.170 -	exit(0);
  10.171 -}
  10.172 -
  10.173 -static bool parse_args(int argc, char **argv)
  10.174 -{
  10.175 -	for(int i=1; i<argc; i++) {
  10.176 -		if(argv[i][0] == '-') {
  10.177 -			int opt = -1;
  10.178 -
  10.179 -			for(int j=0; options[j].opt != -1; j++) {
  10.180 -				if(argv[i][2] == 0) {
  10.181 -					if(argv[i][1] == options[j].opt) {
  10.182 -						opt = options[j].opt;
  10.183 -						break;
  10.184 -					}
  10.185 -				} else {
  10.186 -					if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
  10.187 -						opt = options[j].opt;
  10.188 -						break;
  10.189 -					}
  10.190 -				}
  10.191 -			}
  10.192 -
  10.193 -			switch(opt) {
  10.194 -			case 's':
  10.195 -				if(sscanf(argv[++i], "%dx%d:%d", &xsz, &ysz, &bpp) < 2) {
  10.196 -					fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
  10.197 -					return false;
  10.198 -				}
  10.199 -				break;
  10.200 -
  10.201 -			case 'h':
  10.202 -				print_usage(argv[0]);	// doesn't return
  10.203 -				break;
  10.204 -
  10.205 -			default:
  10.206 -				fprintf(stderr, "unknown option: %s\n", argv[i]);
  10.207 -				return false;
  10.208 -			}
  10.209 -		} else {
  10.210 -			fprintf(stderr, "unexpected argument: %s\n", argv[i]);
  10.211 -			return false;
  10.212 -		}
  10.213 -	}
  10.214 -	return true;
  10.215 -}
  10.216 +#include <stdio.h>
  10.217 +#include <stdlib.h>
  10.218 +#include <string.h>
  10.219 +#include <math.h>
  10.220 +#include "inttypes.h"
  10.221 +#include "gfx.h"
  10.222 +#include "keyb.h"
  10.223 +#include "mouse.h"
  10.224 +#include "logger.h"
  10.225 +
  10.226 +static void display();
  10.227 +static void swap_buffers();
  10.228 +static void handle_keyboard();
  10.229 +static void handle_mouse();
  10.230 +static bool parse_args(int argc, char **argv);
  10.231 +
  10.232 +static int xsz = 800;
  10.233 +static int ysz = 600;
  10.234 +static int bpp = 16;
  10.235 +static int bytespp;
  10.236 +static unsigned char *fb;
  10.237 +static unsigned char *backbuf;
  10.238 +static int rbits, gbits, bbits;
  10.239 +static int rshift, gshift, bshift;
  10.240 +static unsigned int rmask, gmask, bmask;
  10.241 +
  10.242 +static bool quit;
  10.243 +
  10.244 +int main(int argc, char **argv)
  10.245 +{
  10.246 +	if(!parse_args(argc, argv)) {
  10.247 +		return 1;
  10.248 +	}
  10.249 +	if(kb_init(32) == -1) {
  10.250 +		fprintf(stderr, "failed to initialize keyboard driver\n");
  10.251 +		return 1;
  10.252 +	}
  10.253 +	if(!(fb = (unsigned char*)set_video_mode(xsz, ysz, bpp))) {
  10.254 +		set_text_mode();
  10.255 +		fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", xsz, ysz, bpp);
  10.256 +		return 1;
  10.257 +	}
  10.258 +	bpp = get_color_depth();
  10.259 +	get_color_bits(&rbits, &gbits, &bbits);
  10.260 +	get_color_shift(&rshift, &gshift, &bshift);
  10.261 +	get_color_mask(&rmask, &gmask, &bmask);
  10.262 +	bytespp = (int)ceil(bpp / 8.0);
  10.263 +
  10.264 +	printlog("bpp: %d (%d %d %d)\n", bpp, rbits, gbits, bbits);
  10.265 +	printlog("shift: %d %d %d\n", rshift, gshift, bshift);
  10.266 +	printlog("mask: %x %x %x\n", rmask, gmask, bmask);
  10.267 +
  10.268 +	backbuf = new unsigned char[xsz * ysz * 3];
  10.269 +
  10.270 +	// main loop
  10.271 +	for(;;) {
  10.272 +		handle_keyboard();
  10.273 +		handle_mouse();
  10.274 +		if(quit) break;
  10.275 +
  10.276 +		display();
  10.277 +	}
  10.278 +
  10.279 +	delete [] backbuf;
  10.280 +
  10.281 +	set_text_mode();
  10.282 +	kb_shutdown();
  10.283 +
  10.284 +	printf("Thank you for using Rayzor!\n");
  10.285 +	return 0;
  10.286 +}
  10.287 +
  10.288 +static void display()
  10.289 +{
  10.290 +	unsigned char *fbptr = backbuf;
  10.291 +
  10.292 +	for(int i=0; i<ysz; i++) {
  10.293 +		for(int j=0; j<xsz; j++) {
  10.294 +			bool chess = ((i / 16) & 1) == ((j / 16) & 1);
  10.295 +			fbptr[chess ? 0 : 2] = 255;
  10.296 +			fbptr[1] = 128;
  10.297 +			fbptr[chess ? 2 : 0] = 32;
  10.298 +			fbptr += 3;
  10.299 +		}
  10.300 +	}
  10.301 +
  10.302 +	swap_buffers();
  10.303 +}
  10.304 +
  10.305 +#define PACK_RGB(r, g, b) \
  10.306 +	((((r) << rshift) & rmask) | \
  10.307 +	 (((g) << gshift) & gmask) | \
  10.308 +	 (((b) << bshift) & bmask))
  10.309 +
  10.310 +static void swap_buffers()
  10.311 +{
  10.312 +	unsigned char *src = backbuf;
  10.313 +	int num_pixels = xsz * ysz;
  10.314 +
  10.315 +	switch(bpp) {
  10.316 +	case 32:
  10.317 +		{
  10.318 +			uint32_t *dest = (uint32_t*)fb;
  10.319 +			for(int i=0; i<num_pixels; i++) {
  10.320 +				*dest++ = PACK_RGB(src[0], src[1], src[2]);
  10.321 +				src += 3;
  10.322 +			}
  10.323 +		}
  10.324 +		break;
  10.325 +
  10.326 +	case 24:
  10.327 +		memcpy(fb, backbuf, num_pixels * 3);
  10.328 +		break;
  10.329 +
  10.330 +	case 16:
  10.331 +	case 15:
  10.332 +		{
  10.333 +			int srs = 8 - rbits;
  10.334 +			int sgs = 8 - gbits;
  10.335 +			int sbs = 8 - bbits;
  10.336 +			uint16_t *dest = (uint16_t*)fb;
  10.337 +			for(int i=0; i<num_pixels; i++) {
  10.338 +				*dest++ = PACK_RGB(src[0] >> srs, src[1] >> sgs, src[2] >> sbs);
  10.339 +				src += 3;
  10.340 +			}
  10.341 +		}
  10.342 +		break;
  10.343 +
  10.344 +	default:
  10.345 +		break;
  10.346 +	}
  10.347 +}
  10.348 +
  10.349 +static void handle_keyboard()
  10.350 +{
  10.351 +	if(!kb_isdown(KB_ANY))
  10.352 +		return;
  10.353 +
  10.354 +	int c = kb_getkey();
  10.355 +	switch(c) {
  10.356 +	case 27:
  10.357 +		quit = true;
  10.358 +		return;
  10.359 +	}
  10.360 +}
  10.361 +
  10.362 +static void handle_mouse()
  10.363 +{
  10.364 +}
  10.365 +
  10.366 +static struct {
  10.367 +	int opt;
  10.368 +	const char *lopt;
  10.369 +	const char *desc;
  10.370 +} options[] = {
  10.371 +	{'s', "size", "resolution <xres>x<yres>[:bpp]"},
  10.372 +	{'h', "help", "print usage information and exit"},
  10.373 +	{-1, 0, 0}
  10.374 +};
  10.375 +
  10.376 +static void print_usage(const char *argv0)
  10.377 +{
  10.378 +	printf("%s usage\n", argv0);
  10.379 +	for(int i=0; options[i].opt != -1; i++) {
  10.380 +		printf("  -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
  10.381 +	}
  10.382 +	exit(0);
  10.383 +}
  10.384 +
  10.385 +static bool parse_args(int argc, char **argv)
  10.386 +{
  10.387 +	for(int i=1; i<argc; i++) {
  10.388 +		if(argv[i][0] == '-') {
  10.389 +			int opt = -1;
  10.390 +
  10.391 +			for(int j=0; options[j].opt != -1; j++) {
  10.392 +				if(argv[i][2] == 0) {
  10.393 +					if(argv[i][1] == options[j].opt) {
  10.394 +						opt = options[j].opt;
  10.395 +						break;
  10.396 +					}
  10.397 +				} else {
  10.398 +					if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
  10.399 +						opt = options[j].opt;
  10.400 +						break;
  10.401 +					}
  10.402 +				}
  10.403 +			}
  10.404 +
  10.405 +			switch(opt) {
  10.406 +			case 's':
  10.407 +				if(sscanf(argv[++i], "%dx%d:%d", &xsz, &ysz, &bpp) < 2) {
  10.408 +					fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
  10.409 +					return false;
  10.410 +				}
  10.411 +				break;
  10.412 +
  10.413 +			case 'h':
  10.414 +				print_usage(argv[0]);	// doesn't return
  10.415 +				break;
  10.416 +
  10.417 +			default:
  10.418 +				fprintf(stderr, "unknown option: %s\n", argv[i]);
  10.419 +				return false;
  10.420 +			}
  10.421 +		} else {
  10.422 +			fprintf(stderr, "unexpected argument: %s\n", argv[i]);
  10.423 +			return false;
  10.424 +		}
  10.425 +	}
  10.426 +	return true;
  10.427 +}
    11.1 --- a/src/min3d.c	Sat Apr 05 08:46:27 2014 +0300
    11.2 +++ b/src/min3d.c	Sat Apr 05 09:05:26 2014 +0300
    11.3 @@ -1,178 +1,178 @@
    11.4 -#include <stdlib.h>
    11.5 -#include "min3d.h"
    11.6 -#include "m3dimpl.h"
    11.7 -
    11.8 -#ifndef M_PI
    11.9 -#define M_PI	3.141592653
   11.10 -#endif
   11.11 -
   11.12 -int m3d_init(void)
   11.13 -{
   11.14 -	if(!(m3dctx = malloc(sizeof *m3dctx))) {
   11.15 -		return -1;
   11.16 -	}
   11.17 -	memset(m3dctx, 0, sizeof *m3dctx);
   11.18 -
   11.19 -	m3d_matrix_mode(M3D_PROJECTION);
   11.20 -	m3d_load_identity();
   11.21 -	m3d_matrix_mode(M3D_MODELVIEW);
   11.22 -	m3d_load_identity();
   11.23 -	return 0;
   11.24 -}
   11.25 -
   11.26 -void m3d_shutdown(void)
   11.27 -{
   11.28 -	free(m3dctx);
   11.29 -}
   11.30 -
   11.31 -void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
   11.32 -{
   11.33 -	m3dctx->cbuf = cbuf;
   11.34 -	m3dctx->zbuf = zbuf;
   11.35 -}
   11.36 -
   11.37 -void m3d_clear(unsigned int bmask)
   11.38 -{
   11.39 -	int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
   11.40 -	if(bmask & M3D_COLOR_BUFFER_BIT) {
   11.41 -		memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
   11.42 -	}
   11.43 -	if(bmask & M3D_DEPTH_BUFFER_BIT) {
   11.44 -		memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
   11.45 -	}
   11.46 -}
   11.47 -
   11.48 -
   11.49 -void m3d_enable(int bit)
   11.50 -{
   11.51 -	m3dctx->state |= (1 << bit);
   11.52 -}
   11.53 -
   11.54 -void m3d_disable(int bit)
   11.55 -{
   11.56 -	m3dctx->state &= ~(1 << bit);
   11.57 -}
   11.58 -
   11.59 -
   11.60 -/* matrix stack */
   11.61 -void m3d_matrix_mode(int mode)
   11.62 -{
   11.63 -	m3dctx->mmode = mode;
   11.64 -}
   11.65 -
   11.66 -void m3d_load_identity(void)
   11.67 -{
   11.68 -	static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
   11.69 -	m3d_load_matrix(mid);
   11.70 -}
   11.71 -
   11.72 -void m3d_load_matrix(const float *m)
   11.73 -{
   11.74 -	int top = m3dctx->mstack[m3dctx->mmode].top;
   11.75 -	memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
   11.76 -}
   11.77 -
   11.78 -#define M(i,j)	(((i) << 2) + (j))
   11.79 -void m3d_mult_matrix(const float *m2)
   11.80 -{
   11.81 -	int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
   11.82 -	float m1[16];
   11.83 -	float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
   11.84 -
   11.85 -	memcpy(m1, dest, sizeof m1);
   11.86 -
   11.87 -	for(i=0; i<4; i++) {
   11.88 -		for(j=0; j<4; j++) {
   11.89 -			dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
   11.90 -				m1[M(1,j)] * m2[M(i,1)] +
   11.91 -				m1[M(2,j)] * m2[M(i,2)] +
   11.92 -				m1[M(3,j)] * m2[M(i,3)];
   11.93 -		}
   11.94 -	}
   11.95 -}
   11.96 -
   11.97 -void m3d_translate(float x, float y, float z)
   11.98 -{
   11.99 -	float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.100 -	m[12] = x;
  11.101 -	m[13] = y;
  11.102 -	m[14] = z;
  11.103 -	m3d_mult_matrix(m);
  11.104 -}
  11.105 -
  11.106 -void m3d_rotate(float deg, float x, float y, float z)
  11.107 -{
  11.108 -	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.109 -
  11.110 -	float angle = M_PI * deg / 180.0f;
  11.111 -	float sina = sin(angle);
  11.112 -	float cosa = cos(angle);
  11.113 -	float one_minus_cosa = 1.0f - cosa;
  11.114 -	float nxsq = x * x;
  11.115 -	float nysq = y * y;
  11.116 -	float nzsq = z * z;
  11.117 -
  11.118 -	xform[0] = nxsq + (1.0f - nxsq) * cosa;
  11.119 -	xform[4] = x * y * one_minus_cosa - z * sina;
  11.120 -	xform[8] = x * z * one_minus_cosa + y * sina;
  11.121 -	xform[1] = x * y * one_minus_cosa + z * sina;
  11.122 -	xform[5] = nysq + (1.0 - nysq) * cosa;
  11.123 -	xform[9] = y * z * one_minus_cosa - x * sina;
  11.124 -	xform[2] = x * z * one_minus_cosa - y * sina;
  11.125 -	xform[6] = y * z * one_minus_cosa + x * sina;
  11.126 -	xform[10] = nzsq + (1.0 - nzsq) * cosa;
  11.127 -
  11.128 -	m3d_mult_matrix(xform);
  11.129 -}
  11.130 -
  11.131 -void m3d_scale(float x, float y, float z)
  11.132 -{
  11.133 -	static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.134 -	m[0] = x;
  11.135 -	m[5] = y;
  11.136 -	m[10] = z;
  11.137 -	m3d_mult_matrix(m);
  11.138 -}
  11.139 -
  11.140 -void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
  11.141 -{
  11.142 -	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.143 -
  11.144 -	float dx = right - left;
  11.145 -	float dy = top - bottom;
  11.146 -	float dz = fr - nr;
  11.147 -
  11.148 -	float a = (right + left) / dx;
  11.149 -	float b = (top + bottom) / dy;
  11.150 -	float c = -(fr + nr) / dz;
  11.151 -	float d = -2.0 * fr * nr / dz;
  11.152 -
  11.153 -	xform[0] = 2.0 * nr / dx;
  11.154 -	xform[5] = 2.0 * nr / dy;
  11.155 -	xform[8] = a;
  11.156 -	xform[9] = b;
  11.157 -	xform[10] = c;
  11.158 -	xform[11] = -1.0f;
  11.159 -	xform[14] = d;
  11.160 -
  11.161 -	m3d_mult_matrix(xform);
  11.162 -}
  11.163 -
  11.164 -void m3d_perspective(float vfov, float aspect, float nr, float fr)
  11.165 -{
  11.166 -	float vfov_rad = M_PI * vfov / 180.0;
  11.167 -	float x = nr * tan(vfov_rad / 2.0);
  11.168 -	m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
  11.169 -}
  11.170 -
  11.171 -/* drawing */
  11.172 -void m3d_draw(int prim, const float *varr, int vcount)
  11.173 -{
  11.174 -	/* TODO */
  11.175 -}
  11.176 -
  11.177 -void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
  11.178 -{
  11.179 -	/* TODO */
  11.180 -}
  11.181 -
  11.182 +#include <stdlib.h>
  11.183 +#include "min3d.h"
  11.184 +#include "m3dimpl.h"
  11.185 +
  11.186 +#ifndef M_PI
  11.187 +#define M_PI	3.141592653
  11.188 +#endif
  11.189 +
  11.190 +int m3d_init(void)
  11.191 +{
  11.192 +	if(!(m3dctx = malloc(sizeof *m3dctx))) {
  11.193 +		return -1;
  11.194 +	}
  11.195 +	memset(m3dctx, 0, sizeof *m3dctx);
  11.196 +
  11.197 +	m3d_matrix_mode(M3D_PROJECTION);
  11.198 +	m3d_load_identity();
  11.199 +	m3d_matrix_mode(M3D_MODELVIEW);
  11.200 +	m3d_load_identity();
  11.201 +	return 0;
  11.202 +}
  11.203 +
  11.204 +void m3d_shutdown(void)
  11.205 +{
  11.206 +	free(m3dctx);
  11.207 +}
  11.208 +
  11.209 +void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf)
  11.210 +{
  11.211 +	m3dctx->cbuf = cbuf;
  11.212 +	m3dctx->zbuf = zbuf;
  11.213 +}
  11.214 +
  11.215 +void m3d_clear(unsigned int bmask)
  11.216 +{
  11.217 +	int num_pixels = m3dctx->cbuf->xsz * m3dctx->cbuf->ysz;
  11.218 +	if(bmask & M3D_COLOR_BUFFER_BIT) {
  11.219 +		memset(m3dctx->cbuf->pixels, 0, num_pixels * 3);
  11.220 +	}
  11.221 +	if(bmask & M3D_DEPTH_BUFFER_BIT) {
  11.222 +		memset(m3dctx->zbuf, 0xff, num_pixels * sizeof *m3dctx->zbuf);
  11.223 +	}
  11.224 +}
  11.225 +
  11.226 +
  11.227 +void m3d_enable(int bit)
  11.228 +{
  11.229 +	m3dctx->state |= (1 << bit);
  11.230 +}
  11.231 +
  11.232 +void m3d_disable(int bit)
  11.233 +{
  11.234 +	m3dctx->state &= ~(1 << bit);
  11.235 +}
  11.236 +
  11.237 +
  11.238 +/* matrix stack */
  11.239 +void m3d_matrix_mode(int mode)
  11.240 +{
  11.241 +	m3dctx->mmode = mode;
  11.242 +}
  11.243 +
  11.244 +void m3d_load_identity(void)
  11.245 +{
  11.246 +	static const float mid[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.247 +	m3d_load_matrix(mid);
  11.248 +}
  11.249 +
  11.250 +void m3d_load_matrix(const float *m)
  11.251 +{
  11.252 +	int top = m3dctx->mstack[m3dctx->mmode].top;
  11.253 +	memcpy(m3dctx->mstack[m3dctx->mmode].m[top], m, 16 * sizeof *m);
  11.254 +}
  11.255 +
  11.256 +#define M(i,j)	(((i) << 2) + (j))
  11.257 +void m3d_mult_matrix(const float *m2)
  11.258 +{
  11.259 +	int i, j, top = m3dctx->mstack[m3dctx->mmode].top;
  11.260 +	float m1[16];
  11.261 +	float *dest = m3dctx->mstack[m3dctx->mmode].m[top];
  11.262 +
  11.263 +	memcpy(m1, dest, sizeof m1);
  11.264 +
  11.265 +	for(i=0; i<4; i++) {
  11.266 +		for(j=0; j<4; j++) {
  11.267 +			dest[M(i,j)] = m1[M(0,j)] * m2[M(i,0)] +
  11.268 +				m1[M(1,j)] * m2[M(i,1)] +
  11.269 +				m1[M(2,j)] * m2[M(i,2)] +
  11.270 +				m1[M(3,j)] * m2[M(i,3)];
  11.271 +		}
  11.272 +	}
  11.273 +}
  11.274 +
  11.275 +void m3d_translate(float x, float y, float z)
  11.276 +{
  11.277 +	float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.278 +	m[12] = x;
  11.279 +	m[13] = y;
  11.280 +	m[14] = z;
  11.281 +	m3d_mult_matrix(m);
  11.282 +}
  11.283 +
  11.284 +void m3d_rotate(float deg, float x, float y, float z)
  11.285 +{
  11.286 +	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.287 +
  11.288 +	float angle = M_PI * deg / 180.0f;
  11.289 +	float sina = sin(angle);
  11.290 +	float cosa = cos(angle);
  11.291 +	float one_minus_cosa = 1.0f - cosa;
  11.292 +	float nxsq = x * x;
  11.293 +	float nysq = y * y;
  11.294 +	float nzsq = z * z;
  11.295 +
  11.296 +	xform[0] = nxsq + (1.0f - nxsq) * cosa;
  11.297 +	xform[4] = x * y * one_minus_cosa - z * sina;
  11.298 +	xform[8] = x * z * one_minus_cosa + y * sina;
  11.299 +	xform[1] = x * y * one_minus_cosa + z * sina;
  11.300 +	xform[5] = nysq + (1.0 - nysq) * cosa;
  11.301 +	xform[9] = y * z * one_minus_cosa - x * sina;
  11.302 +	xform[2] = x * z * one_minus_cosa - y * sina;
  11.303 +	xform[6] = y * z * one_minus_cosa + x * sina;
  11.304 +	xform[10] = nzsq + (1.0 - nzsq) * cosa;
  11.305 +
  11.306 +	m3d_mult_matrix(xform);
  11.307 +}
  11.308 +
  11.309 +void m3d_scale(float x, float y, float z)
  11.310 +{
  11.311 +	static float m[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.312 +	m[0] = x;
  11.313 +	m[5] = y;
  11.314 +	m[10] = z;
  11.315 +	m3d_mult_matrix(m);
  11.316 +}
  11.317 +
  11.318 +void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr)
  11.319 +{
  11.320 +	float xform[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  11.321 +
  11.322 +	float dx = right - left;
  11.323 +	float dy = top - bottom;
  11.324 +	float dz = fr - nr;
  11.325 +
  11.326 +	float a = (right + left) / dx;
  11.327 +	float b = (top + bottom) / dy;
  11.328 +	float c = -(fr + nr) / dz;
  11.329 +	float d = -2.0 * fr * nr / dz;
  11.330 +
  11.331 +	xform[0] = 2.0 * nr / dx;
  11.332 +	xform[5] = 2.0 * nr / dy;
  11.333 +	xform[8] = a;
  11.334 +	xform[9] = b;
  11.335 +	xform[10] = c;
  11.336 +	xform[11] = -1.0f;
  11.337 +	xform[14] = d;
  11.338 +
  11.339 +	m3d_mult_matrix(xform);
  11.340 +}
  11.341 +
  11.342 +void m3d_perspective(float vfov, float aspect, float nr, float fr)
  11.343 +{
  11.344 +	float vfov_rad = M_PI * vfov / 180.0;
  11.345 +	float x = nr * tan(vfov_rad / 2.0);
  11.346 +	m3d_frustum(-aspect * x, aspect * x, -x, x, nr, fr);
  11.347 +}
  11.348 +
  11.349 +/* drawing */
  11.350 +void m3d_draw(int prim, const float *varr, int vcount)
  11.351 +{
  11.352 +	/* TODO */
  11.353 +}
  11.354 +
  11.355 +void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount)
  11.356 +{
  11.357 +	/* TODO */
  11.358 +}
  11.359 +
    12.1 --- a/src/min3d.h	Sat Apr 05 08:46:27 2014 +0300
    12.2 +++ b/src/min3d.h	Sat Apr 05 09:05:26 2014 +0300
    12.3 @@ -1,73 +1,73 @@
    12.4 -#ifndef MIN3D_H_
    12.5 -#define MIN3D_H_
    12.6 -
    12.7 -#include "inttypes.h"
    12.8 -
    12.9 -/* state toggles */
   12.10 -enum {
   12.11 -	M3D_DEPTH_TEST,
   12.12 -	M3D_CULL_FACE,
   12.13 -	M3D_LIGHTING,
   12.14 -	M3D_LIGHT0,
   12.15 -	M3D_LIGHT1,
   12.16 -	M3D_LIGHT2,
   12.17 -	M3D_LIGHT3
   12.18 -};
   12.19 -
   12.20 -/* buffer bits */
   12.21 -enum {
   12.22 -	M3D_COLOR_BUFFER_BIT	= 1,
   12.23 -	M3D_DEPTH_BUFFER_BIT	= 2
   12.24 -};
   12.25 -
   12.26 -/* primitives */
   12.27 -enum {
   12.28 -	M3D_POINTS = 1,
   12.29 -	M3D_LINES = 2,
   12.30 -	M3D_TRIANGLES = 3,
   12.31 -	M3D_QUADS = 4
   12.32 -};
   12.33 -
   12.34 -/* matrix mode */
   12.35 -enum {
   12.36 -	M3D_MODELVIEW,
   12.37 -	M3D_PROJECTION
   12.38 -};
   12.39 -
   12.40 -struct m3d_image {
   12.41 -	int xsz, ysz;
   12.42 -	unsigned char *pixels;
   12.43 -};
   12.44 -
   12.45 -#ifdef __cplusplus
   12.46 -extern "C" {
   12.47 -#endif
   12.48 -
   12.49 -void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf);
   12.50 -void m3d_clear(unsigned int bmask);
   12.51 -
   12.52 -void m3d_enable(int bit);
   12.53 -void m3d_disable(int bit);
   12.54 -
   12.55 -/* matrix stack */
   12.56 -void m3d_matrix_mode(int mode);
   12.57 -void m3d_load_identity(void);
   12.58 -void m3d_load_matrix(const float *m);
   12.59 -void m3d_mult_matrix(const float *m);
   12.60 -void m3d_translate(float x, float y, float z);
   12.61 -void m3d_rotate(float angle, float x, float y, float z);
   12.62 -void m3d_scale(float x, float y, float z);
   12.63 -void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr);
   12.64 -void m3d_perspective(float vfov, float aspect, float znear, float zfar);
   12.65 -
   12.66 -/* drawing */
   12.67 -void m3d_draw(int prim, const float *varr, int vcount);
   12.68 -void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount);
   12.69 -
   12.70 -/* TODO immediate mode */
   12.71 -
   12.72 -#ifdef __cplusplus
   12.73 -}
   12.74 -#endif
   12.75 -
   12.76 -#endif	/* MIN3D_H_ */
   12.77 +#ifndef MIN3D_H_
   12.78 +#define MIN3D_H_
   12.79 +
   12.80 +#include "inttypes.h"
   12.81 +
   12.82 +/* state toggles */
   12.83 +enum {
   12.84 +	M3D_DEPTH_TEST,
   12.85 +	M3D_CULL_FACE,
   12.86 +	M3D_LIGHTING,
   12.87 +	M3D_LIGHT0,
   12.88 +	M3D_LIGHT1,
   12.89 +	M3D_LIGHT2,
   12.90 +	M3D_LIGHT3
   12.91 +};
   12.92 +
   12.93 +/* buffer bits */
   12.94 +enum {
   12.95 +	M3D_COLOR_BUFFER_BIT	= 1,
   12.96 +	M3D_DEPTH_BUFFER_BIT	= 2
   12.97 +};
   12.98 +
   12.99 +/* primitives */
  12.100 +enum {
  12.101 +	M3D_POINTS = 1,
  12.102 +	M3D_LINES = 2,
  12.103 +	M3D_TRIANGLES = 3,
  12.104 +	M3D_QUADS = 4
  12.105 +};
  12.106 +
  12.107 +/* matrix mode */
  12.108 +enum {
  12.109 +	M3D_MODELVIEW,
  12.110 +	M3D_PROJECTION
  12.111 +};
  12.112 +
  12.113 +struct m3d_image {
  12.114 +	int xsz, ysz;
  12.115 +	unsigned char *pixels;
  12.116 +};
  12.117 +
  12.118 +#ifdef __cplusplus
  12.119 +extern "C" {
  12.120 +#endif
  12.121 +
  12.122 +void m3d_set_buffers(struct m3d_image *cbuf, uint16_t *zbuf);
  12.123 +void m3d_clear(unsigned int bmask);
  12.124 +
  12.125 +void m3d_enable(int bit);
  12.126 +void m3d_disable(int bit);
  12.127 +
  12.128 +/* matrix stack */
  12.129 +void m3d_matrix_mode(int mode);
  12.130 +void m3d_load_identity(void);
  12.131 +void m3d_load_matrix(const float *m);
  12.132 +void m3d_mult_matrix(const float *m);
  12.133 +void m3d_translate(float x, float y, float z);
  12.134 +void m3d_rotate(float angle, float x, float y, float z);
  12.135 +void m3d_scale(float x, float y, float z);
  12.136 +void m3d_frustum(float left, float right, float bottom, float top, float nr, float fr);
  12.137 +void m3d_perspective(float vfov, float aspect, float znear, float zfar);
  12.138 +
  12.139 +/* drawing */
  12.140 +void m3d_draw(int prim, const float *varr, int vcount);
  12.141 +void m3d_draw_indexed(int prim, const float *varr, const int *idxarr, int icount);
  12.142 +
  12.143 +/* TODO immediate mode */
  12.144 +
  12.145 +#ifdef __cplusplus
  12.146 +}
  12.147 +#endif
  12.148 +
  12.149 +#endif	/* MIN3D_H_ */
    13.1 --- a/src/mouse.c	Sat Apr 05 08:46:27 2014 +0300
    13.2 +++ b/src/mouse.c	Sat Apr 05 09:05:26 2014 +0300
    13.3 @@ -1,72 +1,72 @@
    13.4 -/* TODO: try NOT using the v8086 interrupts to avoid the overhead */
    13.5 -#include "mouse.h"
    13.6 -#include "inttypes.h"
    13.7 -#include "dpmi.h"
    13.8 -
    13.9 -#define INTR	0x33
   13.10 -
   13.11 -#define QUERY	0
   13.12 -#define SHOW	1
   13.13 -#define HIDE	2
   13.14 -#define READ	3
   13.15 -#define WRITE	4
   13.16 -
   13.17 -#define XLIM	7
   13.18 -#define YLIM	8
   13.19 -
   13.20 -int have_mouse(void)
   13.21 -{
   13.22 -	struct dpmi_real_regs regs;
   13.23 -	memset(&regs, 0, sizeof regs);
   13.24 -	regs.eax = QUERY;
   13.25 -	dpmi_real_int(INTR, &regs);
   13.26 -	return regs.eax & 0xffff;
   13.27 -}
   13.28 -
   13.29 -void show_mouse(int show)
   13.30 -{
   13.31 -	struct dpmi_real_regs regs;
   13.32 -	memset(&regs, 0, sizeof regs);
   13.33 -	regs.eax = show ? SHOW : HIDE;
   13.34 -	dpmi_real_int(INTR, &regs);
   13.35 -}
   13.36 -
   13.37 -int read_mouse(int *xp, int *yp)
   13.38 -{
   13.39 -	struct dpmi_real_regs regs;
   13.40 -	memset(&regs, 0, sizeof regs);
   13.41 -
   13.42 -	regs.eax = READ;
   13.43 -	dpmi_real_int(INTR, &regs);
   13.44 -
   13.45 -	if(xp) *xp = regs.ecx & 0xffff;
   13.46 -	if(yp) *yp = regs.edx & 0xffff;
   13.47 -	return regs.ebx & 0xffff;
   13.48 -}
   13.49 -
   13.50 -void set_mouse(int x, int y)
   13.51 -{
   13.52 -	struct dpmi_real_regs regs;
   13.53 -	memset(&regs, 0, sizeof regs);
   13.54 -
   13.55 -	regs.eax = WRITE;
   13.56 -	regs.ecx = x;
   13.57 -	regs.edx = y;
   13.58 -	dpmi_real_int(INTR, &regs);
   13.59 -}
   13.60 -
   13.61 -void set_mouse_limits(int xmin, int ymin, int xmax, int ymax)
   13.62 -{
   13.63 -	struct dpmi_real_regs regs;
   13.64 -	memset(&regs, 0, sizeof regs);
   13.65 -	regs.eax = XLIM;
   13.66 -	regs.ecx = xmin;
   13.67 -	regs.edx = xmax;
   13.68 -	dpmi_real_int(INTR, &regs);
   13.69 -
   13.70 -	memset(&regs, 0, sizeof regs);
   13.71 -	regs.eax = YLIM;
   13.72 -	regs.ecx = ymin;
   13.73 -	regs.edx = ymax;
   13.74 -	dpmi_real_int(INTR, &regs);
   13.75 -}
   13.76 +/* TODO: try NOT using the v8086 interrupts to avoid the overhead */
   13.77 +#include "mouse.h"
   13.78 +#include "inttypes.h"
   13.79 +#include "dpmi.h"
   13.80 +
   13.81 +#define INTR	0x33
   13.82 +
   13.83 +#define QUERY	0
   13.84 +#define SHOW	1
   13.85 +#define HIDE	2
   13.86 +#define READ	3
   13.87 +#define WRITE	4
   13.88 +
   13.89 +#define XLIM	7
   13.90 +#define YLIM	8
   13.91 +
   13.92 +int have_mouse(void)
   13.93 +{
   13.94 +	struct dpmi_real_regs regs;
   13.95 +	memset(&regs, 0, sizeof regs);
   13.96 +	regs.eax = QUERY;
   13.97 +	dpmi_real_int(INTR, &regs);
   13.98 +	return regs.eax & 0xffff;
   13.99 +}
  13.100 +
  13.101 +void show_mouse(int show)
  13.102 +{
  13.103 +	struct dpmi_real_regs regs;
  13.104 +	memset(&regs, 0, sizeof regs);
  13.105 +	regs.eax = show ? SHOW : HIDE;
  13.106 +	dpmi_real_int(INTR, &regs);
  13.107 +}
  13.108 +
  13.109 +int read_mouse(int *xp, int *yp)
  13.110 +{
  13.111 +	struct dpmi_real_regs regs;
  13.112 +	memset(&regs, 0, sizeof regs);
  13.113 +
  13.114 +	regs.eax = READ;
  13.115 +	dpmi_real_int(INTR, &regs);
  13.116 +
  13.117 +	if(xp) *xp = regs.ecx & 0xffff;
  13.118 +	if(yp) *yp = regs.edx & 0xffff;
  13.119 +	return regs.ebx & 0xffff;
  13.120 +}
  13.121 +
  13.122 +void set_mouse(int x, int y)
  13.123 +{
  13.124 +	struct dpmi_real_regs regs;
  13.125 +	memset(&regs, 0, sizeof regs);
  13.126 +
  13.127 +	regs.eax = WRITE;
  13.128 +	regs.ecx = x;
  13.129 +	regs.edx = y;
  13.130 +	dpmi_real_int(INTR, &regs);
  13.131 +}
  13.132 +
  13.133 +void set_mouse_limits(int xmin, int ymin, int xmax, int ymax)
  13.134 +{
  13.135 +	struct dpmi_real_regs regs;
  13.136 +	memset(&regs, 0, sizeof regs);
  13.137 +	regs.eax = XLIM;
  13.138 +	regs.ecx = xmin;
  13.139 +	regs.edx = xmax;
  13.140 +	dpmi_real_int(INTR, &regs);
  13.141 +
  13.142 +	memset(&regs, 0, sizeof regs);
  13.143 +	regs.eax = YLIM;
  13.144 +	regs.ecx = ymin;
  13.145 +	regs.edx = ymax;
  13.146 +	dpmi_real_int(INTR, &regs);
  13.147 +}
    14.1 --- a/src/mouse.h	Sat Apr 05 08:46:27 2014 +0300
    14.2 +++ b/src/mouse.h	Sat Apr 05 09:05:26 2014 +0300
    14.3 @@ -1,22 +1,22 @@
    14.4 -#ifndef MOUSE_H_
    14.5 -#define MOUSE_H_
    14.6 -
    14.7 -#define MOUSE_LEFT		1
    14.8 -#define MOUSE_RIGHT		2
    14.9 -#define MOUSE_MIDDLE	4
   14.10 -
   14.11 -#ifdef __cplusplus
   14.12 -extern "C" {
   14.13 -#endif
   14.14 -
   14.15 -int have_mouse(void);
   14.16 -void show_mouse(int show);
   14.17 -int read_mouse(int *xp, int *yp);
   14.18 -void set_mouse(int x, int y);
   14.19 -void set_mouse_limits(int xmin, int ymin, int xmax, int ymax);
   14.20 -
   14.21 -#ifdef __cplusplus
   14.22 -}
   14.23 -#endif
   14.24 -
   14.25 -#endif	/* MOUSE_H_ */
   14.26 +#ifndef MOUSE_H_
   14.27 +#define MOUSE_H_
   14.28 +
   14.29 +#define MOUSE_LEFT		1
   14.30 +#define MOUSE_RIGHT		2
   14.31 +#define MOUSE_MIDDLE	4
   14.32 +
   14.33 +#ifdef __cplusplus
   14.34 +extern "C" {
   14.35 +#endif
   14.36 +
   14.37 +int have_mouse(void);
   14.38 +void show_mouse(int show);
   14.39 +int read_mouse(int *xp, int *yp);
   14.40 +void set_mouse(int x, int y);
   14.41 +void set_mouse_limits(int xmin, int ymin, int xmax, int ymax);
   14.42 +
   14.43 +#ifdef __cplusplus
   14.44 +}
   14.45 +#endif
   14.46 +
   14.47 +#endif	/* MOUSE_H_ */
    15.1 --- a/src/object.cc	Sat Apr 05 08:46:27 2014 +0300
    15.2 +++ b/src/object.cc	Sat Apr 05 09:05:26 2014 +0300
    15.3 @@ -1,53 +1,53 @@
    15.4 -#include "object.h"
    15.5 -#include "vmath.h"
    15.6 -#include "min3d.h"
    15.7 -
    15.8 -Object::Object()
    15.9 -{
   15.10 -}
   15.11 -
   15.12 -Object::~Object()
   15.13 -{
   15.14 -}
   15.15 -
   15.16 -// ---- sphere ----
   15.17 -Sphere::Sphere()
   15.18 -{
   15.19 -}
   15.20 -
   15.21 -Sphere::~Sphere()
   15.22 -{
   15.23 -}
   15.24 -
   15.25 -#define USUB	16
   15.26 -#define VSUB	8
   15.27 -
   15.28 -void Sphere::draw() const
   15.29 -{
   15.30 -	static Vector3 *varr;
   15.31 -	static int num_verts;
   15.32 -	if(!varr) {
   15.33 -		int uverts = USUB;
   15.34 -		int vverts = VSUB + 1;
   15.35 -		num_verts = uverts * vverts;
   15.36 -		varr = new Vector3[num_verts];
   15.37 -
   15.38 -		Vector3 *vptr = varr;
   15.39 -		for(int i=0; i<uverts; i++) {
   15.40 -			float u = (float)i / (float)USUB;
   15.41 -			float theta = u * M_PI * 2.0;
   15.42 -			for(int j=0; j<vverts; j++) {
   15.43 -				float v = (float)j / (float)VSUB;
   15.44 -				float phi = (v - 0.5) * M_PI;
   15.45 -
   15.46 -				float x = sin(theta) * cos(phi);
   15.47 -				float y = sin(phi);
   15.48 -				float z = cos(theta) * cos(phi);
   15.49 -
   15.50 -				*vptr++ = Vector3(x, y, z);
   15.51 -			}
   15.52 -		}
   15.53 -	}
   15.54 -
   15.55 -	m3d_draw(M3D_POINTS, &varr->x, num_verts);
   15.56 -}
   15.57 +#include "object.h"
   15.58 +#include "vmath.h"
   15.59 +#include "min3d.h"
   15.60 +
   15.61 +Object::Object()
   15.62 +{
   15.63 +}
   15.64 +
   15.65 +Object::~Object()
   15.66 +{
   15.67 +}
   15.68 +
   15.69 +// ---- sphere ----
   15.70 +Sphere::Sphere()
   15.71 +{
   15.72 +}
   15.73 +
   15.74 +Sphere::~Sphere()
   15.75 +{
   15.76 +}
   15.77 +
   15.78 +#define USUB	16
   15.79 +#define VSUB	8
   15.80 +
   15.81 +void Sphere::draw() const
   15.82 +{
   15.83 +	static Vector3 *varr;
   15.84 +	static int num_verts;
   15.85 +	if(!varr) {
   15.86 +		int uverts = USUB;
   15.87 +		int vverts = VSUB + 1;
   15.88 +		num_verts = uverts * vverts;
   15.89 +		varr = new Vector3[num_verts];
   15.90 +
   15.91 +		Vector3 *vptr = varr;
   15.92 +		for(int i=0; i<uverts; i++) {
   15.93 +			float u = (float)i / (float)USUB;
   15.94 +			float theta = u * M_PI * 2.0;
   15.95 +			for(int j=0; j<vverts; j++) {
   15.96 +				float v = (float)j / (float)VSUB;
   15.97 +				float phi = (v - 0.5) * M_PI;
   15.98 +
   15.99 +				float x = sin(theta) * cos(phi);
  15.100 +				float y = sin(phi);
  15.101 +				float z = cos(theta) * cos(phi);
  15.102 +
  15.103 +				*vptr++ = Vector3(x, y, z);
  15.104 +			}
  15.105 +		}
  15.106 +	}
  15.107 +
  15.108 +	m3d_draw(M3D_POINTS, &varr->x, num_verts);
  15.109 +}
    16.1 --- a/src/object.h	Sat Apr 05 08:46:27 2014 +0300
    16.2 +++ b/src/object.h	Sat Apr 05 09:05:26 2014 +0300
    16.3 @@ -1,20 +1,20 @@
    16.4 -#ifndef OBJECT_H_
    16.5 -#define OBJECT_H_
    16.6 -
    16.7 -class Object {
    16.8 -public:
    16.9 -	Object();
   16.10 -	virtual ~Object();
   16.11 -
   16.12 -	virtual void draw() const = 0;
   16.13 -};
   16.14 -
   16.15 -class Sphere {
   16.16 -public:
   16.17 -	Sphere();
   16.18 -	~Sphere();
   16.19 -
   16.20 -	void draw() const;
   16.21 -};
   16.22 -
   16.23 -#endif	// OBJECT_H_
   16.24 +#ifndef OBJECT_H_
   16.25 +#define OBJECT_H_
   16.26 +
   16.27 +class Object {
   16.28 +public:
   16.29 +	Object();
   16.30 +	virtual ~Object();
   16.31 +
   16.32 +	virtual void draw() const = 0;
   16.33 +};
   16.34 +
   16.35 +class Sphere {
   16.36 +public:
   16.37 +	Sphere();
   16.38 +	~Sphere();
   16.39 +
   16.40 +	void draw() const;
   16.41 +};
   16.42 +
   16.43 +#endif	// OBJECT_H_
    17.1 --- a/src/pit8254.h	Sat Apr 05 08:46:27 2014 +0300
    17.2 +++ b/src/pit8254.h	Sat Apr 05 09:05:26 2014 +0300
    17.3 @@ -1,34 +1,34 @@
    17.4 -#ifndef PIT8254_H_
    17.5 -#define PIT8254_H_
    17.6 -
    17.7 -/* frequency of the oscillator driving the 8254 timer */
    17.8 -#define OSC_FREQ_HZ		1193182
    17.9 -
   17.10 -/* I/O ports connected to the 8254 */
   17.11 -#define PORT_DATA0	0x40
   17.12 -#define PORT_DATA1	0x41
   17.13 -#define PORT_DATA2	0x42
   17.14 -#define PORT_CMD	0x43
   17.15 -
   17.16 -/* command bits */
   17.17 -#define CMD_CHAN0			0
   17.18 -#define CMD_CHAN1			(1 << 6)
   17.19 -#define CMD_CHAN2			(2 << 6)
   17.20 -#define CMD_RDBACK			(3 << 6)
   17.21 -
   17.22 -#define CMD_LATCH			0
   17.23 -#define CMD_ACCESS_LOW		(1 << 4)
   17.24 -#define CMD_ACCESS_HIGH		(2 << 4)
   17.25 -#define CMD_ACCESS_BOTH		(3 << 4)
   17.26 -
   17.27 -#define CMD_OP_INT_TERM		0
   17.28 -#define CMD_OP_ONESHOT		(1 << 1)
   17.29 -#define CMD_OP_RATE			(2 << 1)
   17.30 -#define CMD_OP_SQWAVE		(3 << 1)
   17.31 -#define CMD_OP_SW_STROBE	(4 << 1)
   17.32 -#define CMD_OP_HW_STROBE	(5 << 1)
   17.33 -
   17.34 -#define CMD_MODE_BIN		0
   17.35 -#define CMD_MODE_BCD		1
   17.36 -
   17.37 -#endif	/* PIT8254_H_ */
   17.38 +#ifndef PIT8254_H_
   17.39 +#define PIT8254_H_
   17.40 +
   17.41 +/* frequency of the oscillator driving the 8254 timer */
   17.42 +#define OSC_FREQ_HZ		1193182
   17.43 +
   17.44 +/* I/O ports connected to the 8254 */
   17.45 +#define PORT_DATA0	0x40
   17.46 +#define PORT_DATA1	0x41
   17.47 +#define PORT_DATA2	0x42
   17.48 +#define PORT_CMD	0x43
   17.49 +
   17.50 +/* command bits */
   17.51 +#define CMD_CHAN0			0
   17.52 +#define CMD_CHAN1			(1 << 6)
   17.53 +#define CMD_CHAN2			(2 << 6)
   17.54 +#define CMD_RDBACK			(3 << 6)
   17.55 +
   17.56 +#define CMD_LATCH			0
   17.57 +#define CMD_ACCESS_LOW		(1 << 4)
   17.58 +#define CMD_ACCESS_HIGH		(2 << 4)
   17.59 +#define CMD_ACCESS_BOTH		(3 << 4)
   17.60 +
   17.61 +#define CMD_OP_INT_TERM		0
   17.62 +#define CMD_OP_ONESHOT		(1 << 1)
   17.63 +#define CMD_OP_RATE			(2 << 1)
   17.64 +#define CMD_OP_SQWAVE		(3 << 1)
   17.65 +#define CMD_OP_SW_STROBE	(4 << 1)
   17.66 +#define CMD_OP_HW_STROBE	(5 << 1)
   17.67 +
   17.68 +#define CMD_MODE_BIN		0
   17.69 +#define CMD_MODE_BCD		1
   17.70 +
   17.71 +#endif	/* PIT8254_H_ */
    18.1 --- a/src/rend.cc	Sat Apr 05 08:46:27 2014 +0300
    18.2 +++ b/src/rend.cc	Sat Apr 05 09:05:26 2014 +0300
    18.3 @@ -1,10 +1,10 @@
    18.4 -#include "rend.h"
    18.5 -
    18.6 -bool rend_init()
    18.7 -{
    18.8 -	return false;
    18.9 -}
   18.10 -
   18.11 -void rend_shutdown()
   18.12 -{
   18.13 -}
   18.14 +#include "rend.h"
   18.15 +
   18.16 +bool rend_init()
   18.17 +{
   18.18 +	return false;
   18.19 +}
   18.20 +
   18.21 +void rend_shutdown()
   18.22 +{
   18.23 +}
    19.1 --- a/src/rend.h	Sat Apr 05 08:46:27 2014 +0300
    19.2 +++ b/src/rend.h	Sat Apr 05 09:05:26 2014 +0300
    19.3 @@ -1,7 +1,7 @@
    19.4 -#ifndef REND_H_
    19.5 -#define REND_H_
    19.6 -
    19.7 -bool rend_init();
    19.8 -void rend_shutdown();
    19.9 -
   19.10 -#endif	// REND_H_
   19.11 +#ifndef REND_H_
   19.12 +#define REND_H_
   19.13 +
   19.14 +bool rend_init();
   19.15 +void rend_shutdown();
   19.16 +
   19.17 +#endif	// REND_H_
    20.1 --- a/src/scancode.h	Sat Apr 05 08:46:27 2014 +0300
    20.2 +++ b/src/scancode.h	Sat Apr 05 09:05:26 2014 +0300
    20.3 @@ -1,31 +1,31 @@
    20.4 -#ifndef KEYB_C_
    20.5 -#error "do not include scancode.h anywhere..."
    20.6 -#endif
    20.7 -
    20.8 -/* special keys */
    20.9 -enum {
   20.10 -	LALT, RALT,
   20.11 -	LCTRL, RCTRL,
   20.12 -	LSHIFT, RSHIFT,
   20.13 -	F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
   20.14 -	CAPSLK, NUMLK, SCRLK, SYSRQ,
   20.15 -	ESC		= 27,
   20.16 -	INSERT, DEL, HOME, END, PGUP, PGDN, LEFT, RIGHT, UP, DOWN,
   20.17 -	NUM_DOT, NUM_ENTER, NUM_PLUS, NUM_MINUS, NUM_MUL, NUM_DIV,
   20.18 -	NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9,
   20.19 -	BACKSP	= 127
   20.20 -};
   20.21 -
   20.22 -/* table with rough translations from set 1 scancodes to ASCII-ish */
   20.23 -static int scantbl[] = {
   20.24 -	0, ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',		/* 0 - e */
   20.25 -	'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',			/* f - 1c */
   20.26 -	LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',				/* 1d - 29 */
   20.27 -	LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', RSHIFT,			/* 2a - 36 */
   20.28 -	NUM_MUL, LALT, ' ', CAPSLK, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10,			/* 37 - 44 */
   20.29 -	NUMLK, SCRLK, NUM_7, NUM_8, NUM_9, NUM_MINUS, NUM_4, NUM_5, NUM_6, NUM_PLUS,	/* 45 - 4e */
   20.30 -	NUM_1, NUM_2, NUM_3, NUM_0, NUM_DOT, SYSRQ, 0, 0, F11, F12,						/* 4d - 58 */
   20.31 -	0, 0, 0, 0, 0, 0, 0,															/* 59 - 5f */
   20.32 -	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,									/* 60 - 6f */
   20.33 -	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0									/* 70 - 7f */
   20.34 -};
   20.35 +#ifndef KEYB_C_
   20.36 +#error "do not include scancode.h anywhere..."
   20.37 +#endif
   20.38 +
   20.39 +/* special keys */
   20.40 +enum {
   20.41 +	LALT, RALT,
   20.42 +	LCTRL, RCTRL,
   20.43 +	LSHIFT, RSHIFT,
   20.44 +	F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12,
   20.45 +	CAPSLK, NUMLK, SCRLK, SYSRQ,
   20.46 +	ESC		= 27,
   20.47 +	INSERT, DEL, HOME, END, PGUP, PGDN, LEFT, RIGHT, UP, DOWN,
   20.48 +	NUM_DOT, NUM_ENTER, NUM_PLUS, NUM_MINUS, NUM_MUL, NUM_DIV,
   20.49 +	NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9,
   20.50 +	BACKSP	= 127
   20.51 +};
   20.52 +
   20.53 +/* table with rough translations from set 1 scancodes to ASCII-ish */
   20.54 +static int scantbl[] = {
   20.55 +	0, ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b',		/* 0 - e */
   20.56 +	'\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',			/* f - 1c */
   20.57 +	LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`',				/* 1d - 29 */
   20.58 +	LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', RSHIFT,			/* 2a - 36 */
   20.59 +	NUM_MUL, LALT, ' ', CAPSLK, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10,			/* 37 - 44 */
   20.60 +	NUMLK, SCRLK, NUM_7, NUM_8, NUM_9, NUM_MINUS, NUM_4, NUM_5, NUM_6, NUM_PLUS,	/* 45 - 4e */
   20.61 +	NUM_1, NUM_2, NUM_3, NUM_0, NUM_DOT, SYSRQ, 0, 0, F11, F12,						/* 4d - 58 */
   20.62 +	0, 0, 0, 0, 0, 0, 0,															/* 59 - 5f */
   20.63 +	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,									/* 60 - 6f */
   20.64 +	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0									/* 70 - 7f */
   20.65 +};
    21.1 --- a/src/scene.cc	Sat Apr 05 08:46:27 2014 +0300
    21.2 +++ b/src/scene.cc	Sat Apr 05 09:05:26 2014 +0300
    21.3 @@ -1,43 +1,43 @@
    21.4 -#include "scene.h"
    21.5 -
    21.6 -Scene::Scene()
    21.7 -{
    21.8 -	name = 0;
    21.9 -}
   21.10 -
   21.11 -Scene::~Scene()
   21.12 -{
   21.13 -	clear();
   21.14 -}
   21.15 -
   21.16 -void Scene::clear()
   21.17 -{
   21.18 -	delete [] name;
   21.19 -
   21.20 -	size_t i;
   21.21 -	for(i=0; i<objects.size(); i++) {
   21.22 -		delete objects[i];
   21.23 -	}
   21.24 -	for(i=0; i<lights.size(); i++) {
   21.25 -		delete lights[i];
   21.26 -	}
   21.27 -	for(i=0; i<cameras.size(); i++) {
   21.28 -		delete cameras[i];
   21.29 -	}
   21.30 -}
   21.31 -
   21.32 -void Scene::set_name(const char *name)
   21.33 -{
   21.34 -	delete [] this->name;
   21.35 -	this->name = new char[strlen(name) + 1];
   21.36 -	strcpy(this->name, name);
   21.37 -}
   21.38 -
   21.39 -const char *Scene::get_name() const
   21.40 -{
   21.41 -	return name ? name : "<unknown>";
   21.42 -}
   21.43 -
   21.44 -void Scene::draw() const
   21.45 -{
   21.46 -}
   21.47 +#include "scene.h"
   21.48 +
   21.49 +Scene::Scene()
   21.50 +{
   21.51 +	name = 0;
   21.52 +}
   21.53 +
   21.54 +Scene::~Scene()
   21.55 +{
   21.56 +	clear();
   21.57 +}
   21.58 +
   21.59 +void Scene::clear()
   21.60 +{
   21.61 +	delete [] name;
   21.62 +
   21.63 +	size_t i;
   21.64 +	for(i=0; i<objects.size(); i++) {
   21.65 +		delete objects[i];
   21.66 +	}
   21.67 +	for(i=0; i<lights.size(); i++) {
   21.68 +		delete lights[i];
   21.69 +	}
   21.70 +	for(i=0; i<cameras.size(); i++) {
   21.71 +		delete cameras[i];
   21.72 +	}
   21.73 +}
   21.74 +
   21.75 +void Scene::set_name(const char *name)
   21.76 +{
   21.77 +	delete [] this->name;
   21.78 +	this->name = new char[strlen(name) + 1];
   21.79 +	strcpy(this->name, name);
   21.80 +}
   21.81 +
   21.82 +const char *Scene::get_name() const
   21.83 +{
   21.84 +	return name ? name : "<unknown>";
   21.85 +}
   21.86 +
   21.87 +void Scene::draw() const
   21.88 +{
   21.89 +}
    22.1 --- a/src/scene.h	Sat Apr 05 08:46:27 2014 +0300
    22.2 +++ b/src/scene.h	Sat Apr 05 09:05:26 2014 +0300
    22.3 @@ -1,29 +1,29 @@
    22.4 -#ifndef SCENE_H_
    22.5 -#define SCENE_H_
    22.6 -
    22.7 -#include <string>
    22.8 -#include <vector.h>
    22.9 -#include "object.h"
   22.10 -#include "light.h"
   22.11 -#include "camera.h"
   22.12 -
   22.13 -class Scene {
   22.14 -private:
   22.15 -	char *name;
   22.16 -	vector<Object*> objects;
   22.17 -	vector<Light*> lights;
   22.18 -	vector<Camera*> cameras;
   22.19 -
   22.20 -public:
   22.21 -	Scene();
   22.22 -	~Scene();
   22.23 -
   22.24 -	void clear();
   22.25 -
   22.26 -	void set_name(const char *name);
   22.27 -	const char *get_name() const;
   22.28 -
   22.29 -	void draw() const;
   22.30 -};
   22.31 -
   22.32 -#endif	// SCENE_H_
   22.33 +#ifndef SCENE_H_
   22.34 +#define SCENE_H_
   22.35 +
   22.36 +#include <string>
   22.37 +#include <vector.h>
   22.38 +#include "object.h"
   22.39 +#include "light.h"
   22.40 +#include "camera.h"
   22.41 +
   22.42 +class Scene {
   22.43 +private:
   22.44 +	char *name;
   22.45 +	vector<Object*> objects;
   22.46 +	vector<Light*> lights;
   22.47 +	vector<Camera*> cameras;
   22.48 +
   22.49 +public:
   22.50 +	Scene();
   22.51 +	~Scene();
   22.52 +
   22.53 +	void clear();
   22.54 +
   22.55 +	void set_name(const char *name);
   22.56 +	const char *get_name() const;
   22.57 +
   22.58 +	void draw() const;
   22.59 +};
   22.60 +
   22.61 +#endif	// SCENE_H_
    23.1 --- a/src/timer.c	Sat Apr 05 08:46:27 2014 +0300
    23.2 +++ b/src/timer.c	Sat Apr 05 09:05:26 2014 +0300
    23.3 @@ -1,131 +1,131 @@
    23.4 -/*
    23.5 -256-color 3D graphics hack for real-mode DOS.
    23.6 -Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
    23.7 -
    23.8 -This program is free software: you can redistribute it and/or modify
    23.9 -it under the terms of the GNU General Public License as published by
   23.10 -the Free Software Foundation, either version 3 of the License, or
   23.11 -(at your option) any later version.
   23.12 -
   23.13 -This program is distributed in the hope that it will be useful,
   23.14 -but WITHOUT ANY WARRANTY; without even the implied warranty of
   23.15 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   23.16 -GNU General Public License for more details.
   23.17 -
   23.18 -You should have received a copy of the GNU General Public License
   23.19 -along with this program.  If not, see <http://www.gnu.org/licenses/>.
   23.20 -*/
   23.21 -#include <stdio.h>
   23.22 -#include <stdlib.h>
   23.23 -#include <conio.h>
   23.24 -#include <dos.h>
   23.25 -#include <i86.h>
   23.26 -#include "pit8254.h"
   23.27 -
   23.28 -#define PIT_TIMER_INTR	8
   23.29 -#define DOS_TIMER_INTR	0x1c
   23.30 -
   23.31 -/* macro to divide and round to the nearest integer */
   23.32 -#define DIV_ROUND(a, b) \
   23.33 -	((a) / (b) + ((a) % (b)) / ((b) / 2))
   23.34 -
   23.35 -static void set_timer_reload(int reload_val);
   23.36 -static void cleanup(void);
   23.37 -static void __interrupt __far timer_irq();
   23.38 -static void __interrupt __far dos_timer_intr();
   23.39 -
   23.40 -static void (__interrupt __far *prev_timer_intr)();
   23.41 -
   23.42 -static unsigned long ticks;
   23.43 -static unsigned long tick_interval, ticks_per_dos_intr;
   23.44 -static int inum;
   23.45 -
   23.46 -void init_timer(int res_hz)
   23.47 -{
   23.48 -	_disable();
   23.49 -
   23.50 -	if(res_hz > 0) {
   23.51 -		int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
   23.52 -		set_timer_reload(reload_val);
   23.53 -
   23.54 -		tick_interval = DIV_ROUND(1000, res_hz);
   23.55 -		ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
   23.56 -
   23.57 -		inum = PIT_TIMER_INTR;
   23.58 -		prev_timer_intr = _dos_getvect(inum);
   23.59 -		_dos_setvect(inum, timer_irq);
   23.60 -	} else {
   23.61 -		tick_interval = 55;
   23.62 -
   23.63 -		inum = DOS_TIMER_INTR;
   23.64 -		prev_timer_intr = _dos_getvect(inum);
   23.65 -		_dos_setvect(inum, dos_timer_intr);
   23.66 -	}
   23.67 -	_enable();
   23.68 -
   23.69 -	atexit(cleanup);
   23.70 -}
   23.71 -
   23.72 -static void cleanup(void)
   23.73 -{
   23.74 -	if(!prev_timer_intr) {
   23.75 -		return; /* init hasn't ran, there's nothing to cleanup */
   23.76 -	}
   23.77 -
   23.78 -	_disable();
   23.79 -	if(inum == PIT_TIMER_INTR) {
   23.80 -		/* restore the original timer frequency */
   23.81 -		set_timer_reload(65535);
   23.82 -	}
   23.83 -
   23.84 -	/* restore the original interrupt handler */
   23.85 -	_dos_setvect(inum, prev_timer_intr);
   23.86 -	_enable();
   23.87 -}
   23.88 -
   23.89 -void reset_timer(void)
   23.90 -{
   23.91 -	ticks = 0;
   23.92 -}
   23.93 -
   23.94 -unsigned long get_msec(void)
   23.95 -{
   23.96 -	return ticks * tick_interval;
   23.97 -}
   23.98 -
   23.99 -static void set_timer_reload(int reload_val)
  23.100 -{
  23.101 -	outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
  23.102 -	outp(PORT_DATA0, reload_val & 0xff);
  23.103 -	outp(PORT_DATA0, (reload_val >> 8) & 0xff);
  23.104 -}
  23.105 -
  23.106 -static void __interrupt __far dos_timer_intr()
  23.107 -{
  23.108 -	ticks++;
  23.109 -	_chain_intr(prev_timer_intr);	/* DOES NOT RETURN */
  23.110 -}
  23.111 -
  23.112 -/* first PIC command port */
  23.113 -#define PIC1_CMD	0x20
  23.114 -/* end of interrupt control word */
  23.115 -#define OCW2_EOI	(1 << 5)
  23.116 -
  23.117 -static void __interrupt __far timer_irq()
  23.118 -{
  23.119 -	static unsigned long dos_ticks;
  23.120 -
  23.121 -	ticks++;
  23.122 -
  23.123 -	if(++dos_ticks >= ticks_per_dos_intr) {
  23.124 -		/* I suppose the dos irq handler does the EOI so I shouldn't
  23.125 -		 * do it if I am to call the previous function
  23.126 -		 */
  23.127 -		dos_ticks = 0;
  23.128 -		_chain_intr(prev_timer_intr);	/* XXX DOES NOT RETURN */
  23.129 -		return;	/* just for clarity */
  23.130 -	}
  23.131 -
  23.132 -	/* send EOI to the PIC */
  23.133 -	outp(PIC1_CMD, OCW2_EOI);
  23.134 -}
  23.135 +/*
  23.136 +256-color 3D graphics hack for real-mode DOS.
  23.137 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
  23.138 +
  23.139 +This program is free software: you can redistribute it and/or modify
  23.140 +it under the terms of the GNU General Public License as published by
  23.141 +the Free Software Foundation, either version 3 of the License, or
  23.142 +(at your option) any later version.
  23.143 +
  23.144 +This program is distributed in the hope that it will be useful,
  23.145 +but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.146 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.147 +GNU General Public License for more details.
  23.148 +
  23.149 +You should have received a copy of the GNU General Public License
  23.150 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23.151 +*/
  23.152 +#include <stdio.h>
  23.153 +#include <stdlib.h>
  23.154 +#include <conio.h>
  23.155 +#include <dos.h>
  23.156 +#include <i86.h>
  23.157 +#include "pit8254.h"
  23.158 +
  23.159 +#define PIT_TIMER_INTR	8
  23.160 +#define DOS_TIMER_INTR	0x1c
  23.161 +
  23.162 +/* macro to divide and round to the nearest integer */
  23.163 +#define DIV_ROUND(a, b) \
  23.164 +	((a) / (b) + ((a) % (b)) / ((b) / 2))
  23.165 +
  23.166 +static void set_timer_reload(int reload_val);
  23.167 +static void cleanup(void);
  23.168 +static void __interrupt __far timer_irq();
  23.169 +static void __interrupt __far dos_timer_intr();
  23.170 +
  23.171 +static void (__interrupt __far *prev_timer_intr)();
  23.172 +
  23.173 +static unsigned long ticks;
  23.174 +static unsigned long tick_interval, ticks_per_dos_intr;
  23.175 +static int inum;
  23.176 +
  23.177 +void init_timer(int res_hz)
  23.178 +{
  23.179 +	_disable();
  23.180 +
  23.181 +	if(res_hz > 0) {
  23.182 +		int reload_val = DIV_ROUND(OSC_FREQ_HZ, res_hz);
  23.183 +		set_timer_reload(reload_val);
  23.184 +
  23.185 +		tick_interval = DIV_ROUND(1000, res_hz);
  23.186 +		ticks_per_dos_intr = DIV_ROUND(65535L, reload_val);
  23.187 +
  23.188 +		inum = PIT_TIMER_INTR;
  23.189 +		prev_timer_intr = _dos_getvect(inum);
  23.190 +		_dos_setvect(inum, timer_irq);
  23.191 +	} else {
  23.192 +		tick_interval = 55;
  23.193 +
  23.194 +		inum = DOS_TIMER_INTR;
  23.195 +		prev_timer_intr = _dos_getvect(inum);
  23.196 +		_dos_setvect(inum, dos_timer_intr);
  23.197 +	}
  23.198 +	_enable();
  23.199 +
  23.200 +	atexit(cleanup);
  23.201 +}
  23.202 +
  23.203 +static void cleanup(void)
  23.204 +{
  23.205 +	if(!prev_timer_intr) {
  23.206 +		return; /* init hasn't ran, there's nothing to cleanup */
  23.207 +	}
  23.208 +
  23.209 +	_disable();
  23.210 +	if(inum == PIT_TIMER_INTR) {
  23.211 +		/* restore the original timer frequency */
  23.212 +		set_timer_reload(65535);
  23.213 +	}
  23.214 +
  23.215 +	/* restore the original interrupt handler */
  23.216 +	_dos_setvect(inum, prev_timer_intr);
  23.217 +	_enable();
  23.218 +}
  23.219 +
  23.220 +void reset_timer(void)
  23.221 +{
  23.222 +	ticks = 0;
  23.223 +}
  23.224 +
  23.225 +unsigned long get_msec(void)
  23.226 +{
  23.227 +	return ticks * tick_interval;
  23.228 +}
  23.229 +
  23.230 +static void set_timer_reload(int reload_val)
  23.231 +{
  23.232 +	outp(PORT_CMD, CMD_CHAN0 | CMD_ACCESS_BOTH | CMD_OP_SQWAVE);
  23.233 +	outp(PORT_DATA0, reload_val & 0xff);
  23.234 +	outp(PORT_DATA0, (reload_val >> 8) & 0xff);
  23.235 +}
  23.236 +
  23.237 +static void __interrupt __far dos_timer_intr()
  23.238 +{
  23.239 +	ticks++;
  23.240 +	_chain_intr(prev_timer_intr);	/* DOES NOT RETURN */
  23.241 +}
  23.242 +
  23.243 +/* first PIC command port */
  23.244 +#define PIC1_CMD	0x20
  23.245 +/* end of interrupt control word */
  23.246 +#define OCW2_EOI	(1 << 5)
  23.247 +
  23.248 +static void __interrupt __far timer_irq()
  23.249 +{
  23.250 +	static unsigned long dos_ticks;
  23.251 +
  23.252 +	ticks++;
  23.253 +
  23.254 +	if(++dos_ticks >= ticks_per_dos_intr) {
  23.255 +		/* I suppose the dos irq handler does the EOI so I shouldn't
  23.256 +		 * do it if I am to call the previous function
  23.257 +		 */
  23.258 +		dos_ticks = 0;
  23.259 +		_chain_intr(prev_timer_intr);	/* XXX DOES NOT RETURN */
  23.260 +		return;	/* just for clarity */
  23.261 +	}
  23.262 +
  23.263 +	/* send EOI to the PIC */
  23.264 +	outp(PIC1_CMD, OCW2_EOI);
  23.265 +}
    24.1 --- a/src/timer.h	Sat Apr 05 08:46:27 2014 +0300
    24.2 +++ b/src/timer.h	Sat Apr 05 09:05:26 2014 +0300
    24.3 @@ -1,29 +1,29 @@
    24.4 -/*
    24.5 -256-color 3D graphics hack for real-mode DOS.
    24.6 -Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
    24.7 -
    24.8 -This program is free software: you can redistribute it and/or modify
    24.9 -it under the terms of the GNU General Public License as published by
   24.10 -the Free Software Foundation, either version 3 of the License, or
   24.11 -(at your option) any later version.
   24.12 -
   24.13 -This program is distributed in the hope that it will be useful,
   24.14 -but WITHOUT ANY WARRANTY; without even the implied warranty of
   24.15 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   24.16 -GNU General Public License for more details.
   24.17 -
   24.18 -You should have received a copy of the GNU General Public License
   24.19 -along with this program.  If not, see <http://www.gnu.org/licenses/>.
   24.20 -*/
   24.21 -#ifndef TIMER_H_
   24.22 -#define TIMER_H_
   24.23 -
   24.24 -/* expects the required timer resolution in hertz
   24.25 - * if res_hz is 0, the current resolution is retained
   24.26 - */
   24.27 -void init_timer(int res_hz);
   24.28 -
   24.29 -void reset_timer(void);
   24.30 -unsigned long get_msec(void);
   24.31 -
   24.32 -#endif	/* TIMER_H_ */
   24.33 +/*
   24.34 +256-color 3D graphics hack for real-mode DOS.
   24.35 +Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
   24.36 +
   24.37 +This program is free software: you can redistribute it and/or modify
   24.38 +it under the terms of the GNU General Public License as published by
   24.39 +the Free Software Foundation, either version 3 of the License, or
   24.40 +(at your option) any later version.
   24.41 +
   24.42 +This program is distributed in the hope that it will be useful,
   24.43 +but WITHOUT ANY WARRANTY; without even the implied warranty of
   24.44 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   24.45 +GNU General Public License for more details.
   24.46 +
   24.47 +You should have received a copy of the GNU General Public License
   24.48 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
   24.49 +*/
   24.50 +#ifndef TIMER_H_
   24.51 +#define TIMER_H_
   24.52 +
   24.53 +/* expects the required timer resolution in hertz
   24.54 + * if res_hz is 0, the current resolution is retained
   24.55 + */
   24.56 +void init_timer(int res_hz);
   24.57 +
   24.58 +void reset_timer(void);
   24.59 +unsigned long get_msec(void);
   24.60 +
   24.61 +#endif	/* TIMER_H_ */
    25.1 --- a/src/vmath.cc	Sat Apr 05 08:46:27 2014 +0300
    25.2 +++ b/src/vmath.cc	Sat Apr 05 09:05:26 2014 +0300
    25.3 @@ -1,18 +1,18 @@
    25.4 -#include "vmathmat.h"
    25.5 -#include "vmath.h"
    25.6 -
    25.7 -void Matrix4x4::lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up)
    25.8 -{
    25.9 -	Vector3 vk = normalize(targ - pos);
   25.10 -	Vector3 vj = normalize(up);
   25.11 -	Vector3 vi = normalize(cross(vk, vj));
   25.12 -	vj = cross(vi, vk);
   25.13 -
   25.14 -	Matrix4x4 m(
   25.15 -			vi.x, vi.y, vi.z, 0,
   25.16 -			vj.x, vj.y, vj.z, 0,
   25.17 -			-vk.x, -vk.y, -vk.z, 0,
   25.18 -			0, 0, 0, 1);
   25.19 -	translate(-pos.x, -pos.y, -pos.z);
   25.20 -	*this = *this * m;
   25.21 -}
   25.22 +#include "vmathmat.h"
   25.23 +#include "vmath.h"
   25.24 +
   25.25 +void Matrix4x4::lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up)
   25.26 +{
   25.27 +	Vector3 vk = normalize(targ - pos);
   25.28 +	Vector3 vj = normalize(up);
   25.29 +	Vector3 vi = normalize(cross(vk, vj));
   25.30 +	vj = cross(vi, vk);
   25.31 +
   25.32 +	Matrix4x4 m(
   25.33 +			vi.x, vi.y, vi.z, 0,
   25.34 +			vj.x, vj.y, vj.z, 0,
   25.35 +			-vk.x, -vk.y, -vk.z, 0,
   25.36 +			0, 0, 0, 1);
   25.37 +	translate(-pos.x, -pos.y, -pos.z);
   25.38 +	*this = *this * m;
   25.39 +}
    26.1 --- a/src/vmath.h	Sat Apr 05 08:46:27 2014 +0300
    26.2 +++ b/src/vmath.h	Sat Apr 05 09:05:26 2014 +0300
    26.3 @@ -1,148 +1,148 @@
    26.4 -#ifndef VMATH_H_
    26.5 -#define VMATH_H_
    26.6 -
    26.7 -#include <math.h>
    26.8 -#include "vmathmat.h"
    26.9 -
   26.10 -class Vector3 {
   26.11 -public:
   26.12 -	float x, y, z;
   26.13 -
   26.14 -	Vector3() : x(0), y(0), z(0) {}
   26.15 -	Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
   26.16 -
   26.17 -	float length_sq() const { return x * x + y * y + z * z; }
   26.18 -	float length() const { return sqrt(x * x + y * y + z * z); }
   26.19 -
   26.20 -	void normalize()
   26.21 -	{
   26.22 -		float len = length();
   26.23 -		if(len != 0.0) {
   26.24 -			x /= len;
   26.25 -			y /= len;
   26.26 -			z /= len;
   26.27 -		}
   26.28 -	}
   26.29 -
   26.30 -	float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
   26.31 -	const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
   26.32 -};
   26.33 -
   26.34 -inline Vector3 normalize(const Vector3 &v)
   26.35 -{
   26.36 -	float len = v.length();
   26.37 -	if(len != 0.0) {
   26.38 -		return Vector3(v.x / len, v.y / len, v.z / len);
   26.39 -	}
   26.40 -	return v;
   26.41 -}
   26.42 -
   26.43 -inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
   26.44 -{
   26.45 -	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
   26.46 -}
   26.47 -
   26.48 -inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
   26.49 -{
   26.50 -	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
   26.51 -}
   26.52 -
   26.53 -inline Vector3 operator *(const Vector3 &v, float s)
   26.54 -{
   26.55 -	return Vector3(v.x * s, v.y * s, v.z * s);
   26.56 -}
   26.57 -
   26.58 -inline Vector3 operator /(const Vector3 &v, float s)
   26.59 -{
   26.60 -	return Vector3(v.x / s, v.y / s, v.z / s);
   26.61 -}
   26.62 -
   26.63 -inline float dot(const Vector3 &a, const Vector3 &b)
   26.64 -{
   26.65 -	return a.x * b.x + a.y * b.y + a.z * b.z;
   26.66 -}
   26.67 -
   26.68 -inline Vector3 cross(const Vector3 &a, const Vector3 &b)
   26.69 -{
   26.70 -	return Vector3(a.y * b.z - a.z * b.y,
   26.71 -			a.z * b.z - a.x * b.z,
   26.72 -			a.x * b.y - a.y * b.x);
   26.73 -}
   26.74 -
   26.75 -inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
   26.76 -{
   26.77 -	float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
   26.78 -	float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
   26.79 -	float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
   26.80 -	return Vector3(x, y, z);
   26.81 -}
   26.82 -
   26.83 -// ---- Vector4 ----
   26.84 -
   26.85 -class Vector4 {
   26.86 -public:
   26.87 -	float x, y, z, w;
   26.88 -
   26.89 -	Vector4() : x(0), y(0), z(0), w(1.0) {}
   26.90 -	Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
   26.91 -	Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
   26.92 -
   26.93 -	float length_sq() const { return x * x + y * y + z * z + w * w; }
   26.94 -	float length() const { return sqrt(x * x + y * y + z * z + w * w); }
   26.95 -
   26.96 -	void normalize()
   26.97 -	{
   26.98 -		float len = length();
   26.99 -		if(len != 0.0) {
  26.100 -			x /= len;
  26.101 -			y /= len;
  26.102 -			z /= len;
  26.103 -			w /= len;
  26.104 -		}
  26.105 -	}
  26.106 -
  26.107 -	float &operator [](int idx)
  26.108 -	{
  26.109 -		return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
  26.110 -	}
  26.111 -	const float &operator [](int idx) const
  26.112 -	{
  26.113 -		return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
  26.114 -	}
  26.115 -};
  26.116 -
  26.117 -inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
  26.118 -{
  26.119 -	return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  26.120 -}
  26.121 -
  26.122 -inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
  26.123 -{
  26.124 -	return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  26.125 -}
  26.126 -
  26.127 -inline Vector4 operator *(const Vector4 &v, float s)
  26.128 -{
  26.129 -	return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
  26.130 -}
  26.131 -
  26.132 -inline Vector4 operator /(const Vector4 &v, float s)
  26.133 -{
  26.134 -	return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
  26.135 -}
  26.136 -
  26.137 -inline float dot(const Vector4 &a, const Vector4 &b)
  26.138 -{
  26.139 -	return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  26.140 -}
  26.141 -
  26.142 -inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
  26.143 -{
  26.144 -	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;
  26.145 -	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;
  26.146 -	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;
  26.147 -	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;
  26.148 -	return Vector4(x, y, z, w);
  26.149 -}
  26.150 -
  26.151 -#endif	// VMATH_H_
  26.152 +#ifndef VMATH_H_
  26.153 +#define VMATH_H_
  26.154 +
  26.155 +#include <math.h>
  26.156 +#include "vmathmat.h"
  26.157 +
  26.158 +class Vector3 {
  26.159 +public:
  26.160 +	float x, y, z;
  26.161 +
  26.162 +	Vector3() : x(0), y(0), z(0) {}
  26.163 +	Vector3(float xx, float yy, float zz) : x(xx), y(yy), z(zz) {}
  26.164 +
  26.165 +	float length_sq() const { return x * x + y * y + z * z; }
  26.166 +	float length() const { return sqrt(x * x + y * y + z * z); }
  26.167 +
  26.168 +	void normalize()
  26.169 +	{
  26.170 +		float len = length();
  26.171 +		if(len != 0.0) {
  26.172 +			x /= len;
  26.173 +			y /= len;
  26.174 +			z /= len;
  26.175 +		}
  26.176 +	}
  26.177 +
  26.178 +	float &operator [](int idx) { return idx == 2 ? z : (idx == 1 ? y : x); }
  26.179 +	const float &operator [](int idx) const { return idx == 2 ? z : (idx == 1 ? y : x); }
  26.180 +};
  26.181 +
  26.182 +inline Vector3 normalize(const Vector3 &v)
  26.183 +{
  26.184 +	float len = v.length();
  26.185 +	if(len != 0.0) {
  26.186 +		return Vector3(v.x / len, v.y / len, v.z / len);
  26.187 +	}
  26.188 +	return v;
  26.189 +}
  26.190 +
  26.191 +inline Vector3 operator +(const Vector3 &a, const Vector3 &b)
  26.192 +{
  26.193 +	return Vector3(a.x + b.x, a.y + b.y, a.z + b.z);
  26.194 +}
  26.195 +
  26.196 +inline Vector3 operator -(const Vector3 &a, const Vector3 &b)
  26.197 +{
  26.198 +	return Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  26.199 +}
  26.200 +
  26.201 +inline Vector3 operator *(const Vector3 &v, float s)
  26.202 +{
  26.203 +	return Vector3(v.x * s, v.y * s, v.z * s);
  26.204 +}
  26.205 +
  26.206 +inline Vector3 operator /(const Vector3 &v, float s)
  26.207 +{
  26.208 +	return Vector3(v.x / s, v.y / s, v.z / s);
  26.209 +}
  26.210 +
  26.211 +inline float dot(const Vector3 &a, const Vector3 &b)
  26.212 +{
  26.213 +	return a.x * b.x + a.y * b.y + a.z * b.z;
  26.214 +}
  26.215 +
  26.216 +inline Vector3 cross(const Vector3 &a, const Vector3 &b)
  26.217 +{
  26.218 +	return Vector3(a.y * b.z - a.z * b.y,
  26.219 +			a.z * b.z - a.x * b.z,
  26.220 +			a.x * b.y - a.y * b.x);
  26.221 +}
  26.222 +
  26.223 +inline Vector3 transform(const Matrix4x4 &m, const Vector3 &v)
  26.224 +{
  26.225 +	float x = m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z + m.m[0][3];
  26.226 +	float y = m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z + m.m[1][3];
  26.227 +	float z = m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z + m.m[2][3];
  26.228 +	return Vector3(x, y, z);
  26.229 +}
  26.230 +
  26.231 +// ---- Vector4 ----
  26.232 +
  26.233 +class Vector4 {
  26.234 +public:
  26.235 +	float x, y, z, w;
  26.236 +
  26.237 +	Vector4() : x(0), y(0), z(0), w(1.0) {}
  26.238 +	Vector4(const Vector3 &v) : x(v.x), y(v.y), z(v.z), w(1.0) {}
  26.239 +	Vector4(float xx, float yy, float zz, float ww) : x(xx), y(yy), z(zz), w(ww) {}
  26.240 +
  26.241 +	float length_sq() const { return x * x + y * y + z * z + w * w; }
  26.242 +	float length() const { return sqrt(x * x + y * y + z * z + w * w); }
  26.243 +
  26.244 +	void normalize()
  26.245 +	{
  26.246 +		float len = length();
  26.247 +		if(len != 0.0) {
  26.248 +			x /= len;
  26.249 +			y /= len;
  26.250 +			z /= len;
  26.251 +			w /= len;
  26.252 +		}
  26.253 +	}
  26.254 +
  26.255 +	float &operator [](int idx)
  26.256 +	{
  26.257 +		return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
  26.258 +	}
  26.259 +	const float &operator [](int idx) const
  26.260 +	{
  26.261 +		return idx == 3 ? w : (idx == 2 ? z : (idx == 1 ? y : x));
  26.262 +	}
  26.263 +};
  26.264 +
  26.265 +inline Vector4 operator +(const Vector4 &a, const Vector4 &b)
  26.266 +{
  26.267 +	return Vector4(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w);
  26.268 +}
  26.269 +
  26.270 +inline Vector4 operator -(const Vector4 &a, const Vector4 &b)
  26.271 +{
  26.272 +	return Vector4(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w);
  26.273 +}
  26.274 +
  26.275 +inline Vector4 operator *(const Vector4 &v, float s)
  26.276 +{
  26.277 +	return Vector4(v.x * s, v.y * s, v.z * s, v.w * s);
  26.278 +}
  26.279 +
  26.280 +inline Vector4 operator /(const Vector4 &v, float s)
  26.281 +{
  26.282 +	return Vector4(v.x / s, v.y / s, v.z / s, v.w / s);
  26.283 +}
  26.284 +
  26.285 +inline float dot(const Vector4 &a, const Vector4 &b)
  26.286 +{
  26.287 +	return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
  26.288 +}
  26.289 +
  26.290 +inline Vector4 transform(const Matrix4x4 &m, const Vector4 &v)
  26.291 +{
  26.292 +	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;
  26.293 +	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;
  26.294 +	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;
  26.295 +	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;
  26.296 +	return Vector4(x, y, z, w);
  26.297 +}
  26.298 +
  26.299 +#endif	// VMATH_H_
    27.1 --- a/src/vmathmat.h	Sat Apr 05 08:46:27 2014 +0300
    27.2 +++ b/src/vmathmat.h	Sat Apr 05 09:05:26 2014 +0300
    27.3 @@ -1,116 +1,116 @@
    27.4 -#ifndef VMATH_MATRIX_H_
    27.5 -#define VMATH_MATRIX_H_
    27.6 -
    27.7 -#include <math.h>
    27.8 -
    27.9 -#ifndef M_PI
   27.10 -#define M_PI	3.141592653
   27.11 -#endif
   27.12 -
   27.13 -class Vector3;
   27.14 -
   27.15 -class Matrix4x4 {
   27.16 -public:
   27.17 -	float m[4][4];
   27.18 -
   27.19 -	Matrix4x4()
   27.20 -	{
   27.21 -		set_identity();
   27.22 -	}
   27.23 -
   27.24 -	Matrix4x4(float m00, float m01, float m02, float m03,
   27.25 -			float m10, float m11, float m12, float m13,
   27.26 -			float m20, float m21, float m22, float m23,
   27.27 -			float m30, float m31, float m32, float m33)
   27.28 -	{
   27.29 -		m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
   27.30 -		m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
   27.31 -		m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
   27.32 -		m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
   27.33 -	}
   27.34 -
   27.35 -	inline void set_identity();
   27.36 -	inline void translate(float x, float y, float z);
   27.37 -	inline void rotate(float angle, float x, float y, float z);
   27.38 -	inline void scale(float x, float y, float z);
   27.39 -	inline void perspective(float vfov, float aspect, float znear, float zfar);
   27.40 -	inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up);
   27.41 -
   27.42 -	float *operator [](int idx) { return m[idx]; }
   27.43 -	const float *operator [](int idx) const { return m[idx]; }
   27.44 -};
   27.45 -
   27.46 -inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b)
   27.47 -{
   27.48 -	Matrix4x4 res;
   27.49 -	for(int i=0; i<4; i++) {
   27.50 -		for(int j=0; j<4; j++) {
   27.51 -			res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] +
   27.52 -				a[i][2] * b[2][j] + a[i][3] * b[3][j];
   27.53 -		}
   27.54 -	}
   27.55 -	return res;
   27.56 -}
   27.57 -
   27.58 -inline void Matrix4x4::set_identity()
   27.59 -{
   27.60 -	m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0;
   27.61 -	m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0;
   27.62 -	m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0;
   27.63 -}
   27.64 -
   27.65 -inline void Matrix4x4::translate(float x, float y, float z)
   27.66 -{
   27.67 -	Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
   27.68 -	*this = *this * m;
   27.69 -}
   27.70 -
   27.71 -inline void Matrix4x4::rotate(float angle, float x, float y, float z)
   27.72 -{
   27.73 -	float sina = (float)sin(angle);
   27.74 -	float cosa = (float)cos(angle);
   27.75 -	float rcosa = 1.0f - cosa;
   27.76 -	float nxsq = x * x;
   27.77 -	float nysq = y * y;
   27.78 -	float nzsq = z * z;
   27.79 -
   27.80 -	Matrix4x4 m;
   27.81 -	m[0][0] = nxsq + (1.0f - nxsq) * cosa;
   27.82 -	m[0][1] = x * y * rcosa - z * sina;
   27.83 -	m[0][2] = x * z * rcosa + y * sina;
   27.84 -
   27.85 -	m[1][0] = x * y * rcosa + z * sina;
   27.86 -	m[1][1] = nysq + (1.0f - nysq) * cosa;
   27.87 -	m[1][2] = y * z * rcosa - x * sina;
   27.88 -
   27.89 -	m[2][0] = x * z * rcosa - y * sina;
   27.90 -	m[2][1] = y * z * rcosa + x * sina;
   27.91 -	m[2][2] = nzsq + (1.0f - nzsq) * cosa;
   27.92 -
   27.93 -	*this = *this * m;
   27.94 -}
   27.95 -
   27.96 -inline void Matrix4x4::scale(float x, float y, float z)
   27.97 -{
   27.98 -	Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
   27.99 -	*this = *this * m;
  27.100 -}
  27.101 -
  27.102 -inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar)
  27.103 -{
  27.104 -	float f = 1.0f / tan(vfov * 0.5f);
  27.105 -	float dz = znear - zfar;
  27.106 -
  27.107 -	Matrix4x4 m;
  27.108 -	m[0][0] = f / aspect;
  27.109 -	m[1][1] = f;
  27.110 -	m[2][2] = (zfar + znear) / dz;
  27.111 -	m[3][2] = -1.0f;
  27.112 -	m[2][3] = 2.0f * zfar * znear / dz;
  27.113 -	m[3][3] = 0.0f;
  27.114 -
  27.115 -	*this = *this * m;
  27.116 -}
  27.117 -
  27.118 -
  27.119 -#endif	// VMATH_MATRIX_H_
  27.120 +#ifndef VMATH_MATRIX_H_
  27.121 +#define VMATH_MATRIX_H_
  27.122 +
  27.123 +#include <math.h>
  27.124 +
  27.125 +#ifndef M_PI
  27.126 +#define M_PI	3.141592653
  27.127 +#endif
  27.128 +
  27.129 +class Vector3;
  27.130 +
  27.131 +class Matrix4x4 {
  27.132 +public:
  27.133 +	float m[4][4];
  27.134 +
  27.135 +	Matrix4x4()
  27.136 +	{
  27.137 +		set_identity();
  27.138 +	}
  27.139 +
  27.140 +	Matrix4x4(float m00, float m01, float m02, float m03,
  27.141 +			float m10, float m11, float m12, float m13,
  27.142 +			float m20, float m21, float m22, float m23,
  27.143 +			float m30, float m31, float m32, float m33)
  27.144 +	{
  27.145 +		m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
  27.146 +		m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
  27.147 +		m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
  27.148 +		m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
  27.149 +	}
  27.150 +
  27.151 +	inline void set_identity();
  27.152 +	inline void translate(float x, float y, float z);
  27.153 +	inline void rotate(float angle, float x, float y, float z);
  27.154 +	inline void scale(float x, float y, float z);
  27.155 +	inline void perspective(float vfov, float aspect, float znear, float zfar);
  27.156 +	inline void lookat(const Vector3 &pos, const Vector3 &targ, const Vector3 &up);
  27.157 +
  27.158 +	float *operator [](int idx) { return m[idx]; }
  27.159 +	const float *operator [](int idx) const { return m[idx]; }
  27.160 +};
  27.161 +
  27.162 +inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b)
  27.163 +{
  27.164 +	Matrix4x4 res;
  27.165 +	for(int i=0; i<4; i++) {
  27.166 +		for(int j=0; j<4; j++) {
  27.167 +			res[i][j] = a[i][0] * b[0][j] + a[i][1] * b[1][j] +
  27.168 +				a[i][2] * b[2][j] + a[i][3] * b[3][j];
  27.169 +		}
  27.170 +	}
  27.171 +	return res;
  27.172 +}
  27.173 +
  27.174 +inline void Matrix4x4::set_identity()
  27.175 +{
  27.176 +	m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0;
  27.177 +	m[0][1] = m[0][2] = m[0][3] = m[1][2] = m[1][3] = m[2][3] = 0.0;
  27.178 +	m[1][0] = m[2][0] = m[3][0] = m[2][1] = m[3][1] = m[3][2] = 0.0;
  27.179 +}
  27.180 +
  27.181 +inline void Matrix4x4::translate(float x, float y, float z)
  27.182 +{
  27.183 +	Matrix4x4 m(1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1);
  27.184 +	*this = *this * m;
  27.185 +}
  27.186 +
  27.187 +inline void Matrix4x4::rotate(float angle, float x, float y, float z)
  27.188 +{
  27.189 +	float sina = (float)sin(angle);
  27.190 +	float cosa = (float)cos(angle);
  27.191 +	float rcosa = 1.0f - cosa;
  27.192 +	float nxsq = x * x;
  27.193 +	float nysq = y * y;
  27.194 +	float nzsq = z * z;
  27.195 +
  27.196 +	Matrix4x4 m;
  27.197 +	m[0][0] = nxsq + (1.0f - nxsq) * cosa;
  27.198 +	m[0][1] = x * y * rcosa - z * sina;
  27.199 +	m[0][2] = x * z * rcosa + y * sina;
  27.200 +
  27.201 +	m[1][0] = x * y * rcosa + z * sina;
  27.202 +	m[1][1] = nysq + (1.0f - nysq) * cosa;
  27.203 +	m[1][2] = y * z * rcosa - x * sina;
  27.204 +
  27.205 +	m[2][0] = x * z * rcosa - y * sina;
  27.206 +	m[2][1] = y * z * rcosa + x * sina;
  27.207 +	m[2][2] = nzsq + (1.0f - nzsq) * cosa;
  27.208 +
  27.209 +	*this = *this * m;
  27.210 +}
  27.211 +
  27.212 +inline void Matrix4x4::scale(float x, float y, float z)
  27.213 +{
  27.214 +	Matrix4x4 m(x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1);
  27.215 +	*this = *this * m;
  27.216 +}
  27.217 +
  27.218 +inline void Matrix4x4::perspective(float vfov, float aspect, float znear, float zfar)
  27.219 +{
  27.220 +	float f = 1.0f / tan(vfov * 0.5f);
  27.221 +	float dz = znear - zfar;
  27.222 +
  27.223 +	Matrix4x4 m;
  27.224 +	m[0][0] = f / aspect;
  27.225 +	m[1][1] = f;
  27.226 +	m[2][2] = (zfar + znear) / dz;
  27.227 +	m[3][2] = -1.0f;
  27.228 +	m[2][3] = 2.0f * zfar * znear / dz;
  27.229 +	m[3][3] = 0.0f;
  27.230 +
  27.231 +	*this = *this * m;
  27.232 +}
  27.233 +
  27.234 +
  27.235 +#endif	// VMATH_MATRIX_H_