# HG changeset patch # User John Tsiombikas # Date 1399030378 -10800 # Node ID 5380ff64e83fd0f1aae9a0d595fbfefe6af254d2 # Parent 8c4859442200f3ed5ac04de56972b405ed9038f2 minor changes from dos, and line endings cleanup diff -r 8c4859442200 -r 5380ff64e83f src/m3drast.c --- a/src/m3drast.c Tue Apr 15 20:52:05 2014 +0300 +++ b/src/m3drast.c Fri May 02 14:32:58 2014 +0300 @@ -1,56 +1,56 @@ -#include "m3dimpl.h" -#include "lines.h" - - -void m3d_draw_point(struct min3d_vertex *v) -{ - int x = v->pos[0] + 0.5; - int y = v->pos[1] + 0.5; - int xsz = m3dctx->cbuf->xsz; - m3dctx->cbuf->pixels[y * xsz + x] = v->color; -} - -void m3d_draw_line(struct min3d_vertex *v) -{ - int x0, y0, x1, y1; - - x0 = v[0].pos[0]; - y0 = v[0].pos[1]; - x1 = v[1].pos[0]; - y1 = v[1].pos[1]; - - if(clip_line2d(&x0, &y0, &x1, &y1, m3dctx->vport)) { - draw_line(m3dctx->cbuf->pixels, m3dctx->cbuf->xsz, v->color, x0, y0, x1, y1); - } -} - -void m3d_draw_poly(struct min3d_vertex *v, int numv) -{ - int i; - struct min3d_vertex last[2]; - - if(ENABLED(M3D_CULL_FACE)) { - float a[2], b[2], crossz = 0; - - for(i=1; i 0) return; - } - - last[0] = v[numv - 1]; - last[1] = v[0]; - - for(i=0; ipos[0] + 0.5; + int y = v->pos[1] + 0.5; + int xsz = m3dctx->cbuf->xsz; + m3dctx->cbuf->pixels[y * xsz + x] = v->color; +} + +void m3d_draw_line(struct min3d_vertex *v) +{ + int x0, y0, x1, y1; + + x0 = v[0].pos[0]; + y0 = v[0].pos[1]; + x1 = v[1].pos[0]; + y1 = v[1].pos[1]; + + if(clip_line2d(&x0, &y0, &x1, &y1, m3dctx->vport)) { + draw_line(m3dctx->cbuf->pixels, m3dctx->cbuf->xsz, v->color, x0, y0, x1, y1); + } +} + +void m3d_draw_poly(struct min3d_vertex *v, int numv) +{ + int i; + struct min3d_vertex last[2]; + + if(ENABLED(M3D_CULL_FACE)) { + float a[2], b[2], crossz = 0; + + for(i=1; i 0) return; + } + + last[0] = v[numv - 1]; + last[1] = v[0]; + + for(i=0; iscreensaver) { + mod->cam_theta = get_msec() / 100.0f; + } + m3d_clear(M3D_COLOR_BUFFER_BIT); m3d_matrix_mode(M3D_MODELVIEW); @@ -173,6 +181,15 @@ } break; + case KB_F1: + mod->screensaver = !mod->screensaver; + if(mod->screensaver) { + mod->orig_theta = mod->cam_theta; + } else { + mod->cam_theta = mod->orig_theta; + } + break; + default: break; } diff -r 8c4859442200 -r 5380ff64e83f src/raytrace.cc --- a/src/raytrace.cc Tue Apr 15 20:52:05 2014 +0300 +++ b/src/raytrace.cc Fri May 02 14:32:58 2014 +0300 @@ -1,78 +1,78 @@ -#include -#include -#include "raytrace.h" -#include "rayzor.h" -#include "scene.h" -#include "logger.h" - -Vector3 ray_trace(const Ray &ray, int iter) -{ - RayHit hit; - if(!scene->intersect(ray, &hit)) { - return scene->get_background(ray); - } - - return shade(hit, iter); -} - -static inline float positive(float x) -{ - return x < 0.0f ? 0.0f : x; -} - -Vector3 shade(const RayHit &hit, int iter) -{ - const Material &mtl = hit.obj->mtl; - - Vector3 pos = hit.ray.origin + hit.ray.dir * hit.dist; - Vector3 norm = hit.obj->hit_normal(hit); - norm = normalize(transform(normal_matrix(hit.obj->get_matrix()), norm)); - Vector3 vdir = -normalize(hit.ray.dir); - - float ior = mtl.ior; - - if(dot(norm, hit.ray.dir) > 0.0) { - norm = -norm; - ior = 1.0 / mtl.ior; - } - - Vector3 color = scene->get_ambient(); - - // for each light, calculate local illumination - int num_lights = scene->get_light_count(); - for(int i=0; iget_light(i); - Vector3 ldir = lt->get_position() - pos; - Vector3 lcol = lt->get_color(pos); - - RayHit hit; - if(!scene->intersect(Ray(pos, ldir), &hit) || hit.dist > 1.0) { - // if we can see the light, calculate and add its contribution - ldir.normalize(); - float ndotl = positive(dot(norm, ldir)); - Vector3 diffuse = mtl.diffuse * lcol * ndotl; - - Vector3 hdir = normalize(ldir + vdir); - float ndoth = positive(dot(norm, hdir)); - Vector3 specular = mtl.specular * lcol * pow(ndoth, mtl.roughness * 128.0); - - color = color + lerp(specular, diffuse, mtl.roughness); - } - } - - if(mtl.reflect > 1e-4 && iter < 6) { - Ray reflray(pos, reflect(vdir, norm)); - - Vector3 rcol = ray_trace(reflray, iter + 1) * mtl.specular * mtl.reflect; - color = color + rcol; - } - - if(mtl.refract > 1e-4 && iter < 6) { - Ray refrray(pos, refract(vdir, norm, ior)); - - Vector3 rcol = ray_trace(refrray, iter + 1) * mtl.specular * mtl.refract; - color = color + rcol; - } - - return color; -} +#include +#include +#include "raytrace.h" +#include "rayzor.h" +#include "scene.h" +#include "logger.h" + +Vector3 ray_trace(const Ray &ray, int iter) +{ + RayHit hit; + if(!scene->intersect(ray, &hit)) { + return scene->get_background(ray); + } + + return shade(hit, iter); +} + +static inline float positive(float x) +{ + return x < 0.0f ? 0.0f : x; +} + +Vector3 shade(const RayHit &hit, int iter) +{ + const Material &mtl = hit.obj->mtl; + + Vector3 pos = hit.ray.origin + hit.ray.dir * hit.dist; + Vector3 norm = hit.obj->hit_normal(hit); + norm = normalize(transform(normal_matrix(hit.obj->get_matrix()), norm)); + Vector3 vdir = -normalize(hit.ray.dir); + + float ior = mtl.ior; + + if(dot(norm, hit.ray.dir) > 0.0) { + norm = -norm; + ior = 1.0 / mtl.ior; + } + + Vector3 color = scene->get_ambient(); + + // for each light, calculate local illumination + int num_lights = scene->get_light_count(); + for(int i=0; iget_light(i); + Vector3 ldir = lt->get_position() - pos; + Vector3 lcol = lt->get_color(pos); + + RayHit hit; + if(!scene->intersect(Ray(pos, ldir), &hit) || hit.dist > 1.0) { + // if we can see the light, calculate and add its contribution + ldir.normalize(); + float ndotl = positive(dot(norm, ldir)); + Vector3 diffuse = mtl.diffuse * lcol * ndotl; + + Vector3 hdir = normalize(ldir + vdir); + float ndoth = positive(dot(norm, hdir)); + Vector3 specular = mtl.specular * lcol * pow(ndoth, mtl.roughness * 128.0); + + color = color + lerp(specular, diffuse, mtl.roughness); + } + } + + if(mtl.reflect > 1e-4 && iter < 6) { + Ray reflray(pos, reflect(vdir, norm)); + + Vector3 rcol = ray_trace(reflray, iter + 1) * mtl.specular * mtl.reflect; + color = color + rcol; + } + + if(mtl.refract > 1e-4 && iter < 6) { + Ray refrray(pos, refract(vdir, norm, ior)); + + Vector3 rcol = ray_trace(refrray, iter + 1) * mtl.specular * mtl.refract; + color = color + rcol; + } + + return color; +} diff -r 8c4859442200 -r 5380ff64e83f src/raytrace.h --- a/src/raytrace.h Tue Apr 15 20:52:05 2014 +0300 +++ b/src/raytrace.h Fri May 02 14:32:58 2014 +0300 @@ -1,20 +1,20 @@ -#ifndef RAYTRACE_H_ -#define RAYTRACE_H_ - -#include "vmath.h" -#include "vmathray.h" - -class Object; - -struct RayHit { - Ray ray; // the ray in world coordinates - Ray lray; // the local coordinate system ray - float dist; // parametric distance along the ray - const Object *obj; // pointer to the object that has been hit - const void *subobj; // object-specific subobject pointer (can be null). -}; - -Vector3 ray_trace(const Ray &ray, int iter = 0); -Vector3 shade(const RayHit &hit, int iter); - -#endif // RAYTRACE_H_ +#ifndef RAYTRACE_H_ +#define RAYTRACE_H_ + +#include "vmath.h" +#include "vmathray.h" + +class Object; + +struct RayHit { + Ray ray; // the ray in world coordinates + Ray lray; // the local coordinate system ray + float dist; // parametric distance along the ray + const Object *obj; // pointer to the object that has been hit + const void *subobj; // object-specific subobject pointer (can be null). +}; + +Vector3 ray_trace(const Ray &ray, int iter = 0); +Vector3 shade(const RayHit &hit, int iter); + +#endif // RAYTRACE_H_ diff -r 8c4859442200 -r 5380ff64e83f src/stl/vector.h --- a/src/stl/vector.h Tue Apr 15 20:52:05 2014 +0300 +++ b/src/stl/vector.h Fri May 02 14:32:58 2014 +0300 @@ -1,127 +1,127 @@ -/* vi:set ft=cpp: */ -#ifndef VECTOR_H_ -#define VECTOR_H_ - -#include - -template -class vector { -private: - T *data; - size_t num_items, max_items; - -public: - vector() - { - data = 0; - num_items = max_items = 0; - } - - vector(const vector &v) - { - data = 0; - num_items = max_items = 0; - resize(v.size()); - - for(size_t i=0; i 0) { - T *newdata = new T[nsz]; - if(!newdata) return; - - size_t num = num_items < nsz ? num_items : nsz; - for(size_t i=0; i= max_items) { - resize(max_items > 0 ? max_items * 2 : 8); - } - data[num_items++] = item; - } - - void pop_back() - { - if(--num_items <= 0) { - num_items = 0; - } - - if(num_items < max_items / 3) { - resize(max_items / 2); - } - } - - T &back() - { - return data[num_items - 1]; - } - - const T &back() const - { - return data[num_items - 1]; - } - - T &operator [](int idx) - { - return data[idx]; - } - - const T &operator [](int idx) const - { - return data[idx]; - } -}; - -#endif // VECTOR_H_ +/* vi:set ft=cpp: */ +#ifndef VECTOR_H_ +#define VECTOR_H_ + +#include + +template +class vector { +private: + T *data; + size_t num_items, max_items; + +public: + vector() + { + data = 0; + num_items = max_items = 0; + } + + vector(const vector &v) + { + data = 0; + num_items = max_items = 0; + resize(v.size()); + + for(size_t i=0; i 0) { + T *newdata = new T[nsz]; + if(!newdata) return; + + size_t num = num_items < nsz ? num_items : nsz; + for(size_t i=0; i= max_items) { + resize(max_items > 0 ? max_items * 2 : 8); + } + data[num_items++] = item; + } + + void pop_back() + { + if(--num_items <= 0) { + num_items = 0; + } + + if(num_items < max_items / 3) { + resize(max_items / 2); + } + } + + T &back() + { + return data[num_items - 1]; + } + + const T &back() const + { + return data[num_items - 1]; + } + + T &operator [](int idx) + { + return data[idx]; + } + + const T &operator [](int idx) const + { + return data[idx]; + } +}; + +#endif // VECTOR_H_ diff -r 8c4859442200 -r 5380ff64e83f src/swapbuf.asm --- a/src/swapbuf.asm Tue Apr 15 20:52:05 2014 +0300 +++ b/src/swapbuf.asm Fri May 02 14:32:58 2014 +0300 @@ -1,46 +1,46 @@ -; vim:set ft=nasm: - segment code use32 - - ; void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp) - ; dest -> eax - ; src -> edx - ; xsz -> ebx - ; ysz -> ecx - ; bpp -> [ebp + 8] (after pushing ebp) - global swap_buffers_asm_ -swap_buffers_asm_: - push ebp - mov ebp, esp - - mov edi, eax ; let's hold dest ptr in edi, frees up eax - mov esi, edx ; let's hold src ptr in esi, frees up edx - ; calculate pixel count -> ecx, frees up ebx - mov eax, ebx - mul ecx - mov ecx, eax ; now ecx = xsz * ysz - - mov eax, [ebp + 8] ; eax <- bpp - cmp eax, 32 - je .bpp32 - cmp eax, 24 - je .bpp24 - cmp eax, 16 - je .bpp16 - ; invalid bpp, ignore - jmp .done - -.bpp32: ; 32bit block transfer, no conversion - rep movsd ; esi, edi, and ecx already loaded, just go... - jmp .done - -.bpp24: ; 32bpp -> 24bpp conversion (LSB-first), 1 byte overrun! - movsd ; transfer a full 32bit chunk and inc esi,edi by 4 - dec edi ; backtrack dest one byte after last transfer - dec ecx - jnz .bpp24 - jmp .done - -.bpp16: ; fuck 16bpp for now (TODO) -.done: - pop ebp - ret +; vim:set ft=nasm: + segment code use32 + + ; void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp) + ; dest -> eax + ; src -> edx + ; xsz -> ebx + ; ysz -> ecx + ; bpp -> [ebp + 8] (after pushing ebp) + global swap_buffers_asm_ +swap_buffers_asm_: + push ebp + mov ebp, esp + + mov edi, eax ; let's hold dest ptr in edi, frees up eax + mov esi, edx ; let's hold src ptr in esi, frees up edx + ; calculate pixel count -> ecx, frees up ebx + mov eax, ebx + mul ecx + mov ecx, eax ; now ecx = xsz * ysz + + mov eax, [ebp + 8] ; eax <- bpp + cmp eax, 32 + je .bpp32 + cmp eax, 24 + je .bpp24 + cmp eax, 16 + je .bpp16 + ; invalid bpp, ignore + jmp .done + +.bpp32: ; 32bit block transfer, no conversion + rep movsd ; esi, edi, and ecx already loaded, just go... + jmp .done + +.bpp24: ; 32bpp -> 24bpp conversion (LSB-first), 1 byte overrun! + movsd ; transfer a full 32bit chunk and inc esi,edi by 4 + dec edi ; backtrack dest one byte after last transfer + dec ecx + jnz .bpp24 + jmp .done + +.bpp16: ; fuck 16bpp for now (TODO) +.done: + pop ebp + ret diff -r 8c4859442200 -r 5380ff64e83f src/timer.c --- a/src/timer.c Tue Apr 15 20:52:05 2014 +0300 +++ b/src/timer.c Fri May 02 14:32:58 2014 +0300 @@ -1,6 +1,6 @@ /* -256-color 3D graphics hack for real-mode DOS. -Copyright (C) 2011 John Tsiombikas +pit8254 timer code for DOS programs. +Copyright (C) 2011-2014 John Tsiombikas This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -22,6 +22,10 @@ #include #include "pit8254.h" +#ifdef BORLANDC +#error borland unsupported +#endif + #define PIT_TIMER_INTR 8 #define DOS_TIMER_INTR 0x1c diff -r 8c4859442200 -r 5380ff64e83f src/timer.h --- a/src/timer.h Tue Apr 15 20:52:05 2014 +0300 +++ b/src/timer.h Fri May 02 14:32:58 2014 +0300 @@ -1,6 +1,6 @@ /* -256-color 3D graphics hack for real-mode DOS. -Copyright (C) 2011 John Tsiombikas +pit8254 timer code for DOS programs. +Copyright (C) 2011-2014 John Tsiombikas This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -29,6 +29,7 @@ void reset_timer(void); unsigned long get_msec(void); +unsigned long get_ticks(void); #ifdef __cplusplus }