# HG changeset patch # User John Tsiombikas # Date 1397334504 -10800 # Node ID d94a69933a7105a04e8b2350b58c7033f9c51795 # Parent fe94d9e986ae2d9423a4c3ee504f00a35c7b5b2e lots of stuff, can't remember diff -r fe94d9e986ae -r d94a69933a71 GNUmakefile --- a/GNUmakefile Thu Apr 10 08:42:33 2014 +0300 +++ b/GNUmakefile Sat Apr 12 23:28:24 2014 +0300 @@ -1,7 +1,7 @@ baseobj = src/main.o src/logger.o src/screen.o src/scrman.o modelobj = src/modeller.o src/min3d.o src/m3drast.o src/lines.o rendobj = src/renderer.o src/vmath.o -scnobj = src/scene.o src/object.o +scnobj = src/scene.o src/object.o src/xfnode.o sysobj = src/dosemu/dosemu.o obj = $(baseobj) $(modelobj) $(rendobj) $(scnobj) $(sysobj) dep = $(obj:.o=.d) diff -r fe94d9e986ae -r d94a69933a71 Makefile --- a/Makefile Thu Apr 10 08:42:33 2014 +0300 +++ b/Makefile Sat Apr 12 23:28:24 2014 +0300 @@ -1,7 +1,7 @@ baseobj = main.obj logger.obj screen.obj scrman.obj swapbuf.obj modelobj = modeller.obj min3d.obj m3drast.obj lines.obj rendobj = renderer.obj vmath.obj -scnobj = scene.obj object.obj +scnobj = scene.obj object.obj xfnode.obj sysobj = gfx.obj vbe.obj dpmi.obj timer.obj mouse.obj keyb.obj obj = $(baseobj) $(modelobj) $(rendobj) $(scnobj) $(sysobj) bin = rayzor.exe diff -r fe94d9e986ae -r d94a69933a71 src/camera.cc --- a/src/camera.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/camera.cc Sat Apr 12 23:28:24 2014 +0300 @@ -1,29 +1,20 @@ #include "camera.h" Camera::Camera() - : pos(0, 0, 10) { + type = NODE_CAMERA; fov = M_PI; -} - -void Camera::set_position(const Vector3 &pos) -{ - this->pos = pos; -} - -const Vector3 &Camera::get_position() const -{ - return pos; + set_position(Vector3(0, 0, 10)); } void Camera::set_target(const Vector3 &target) { - this->target = target; + this->target->set_position(target); } const Vector3 &Camera::get_target() const { - return target; + return target->get_position(); } void Camera::set_fov(float fov) @@ -36,15 +27,23 @@ return fov; } -Matrix4x4 Camera::get_matrix() const +void Camera::calc_matrix() const { - Matrix4x4 res; - res.lookat(pos, target, Vector3(0, 1, 0)); - return res; + xform.set_identity(); + xform.lookat(pos, target, Vector3(0, 1, 0)); + xform_valid = true; } -Matrix4x4 Camera::get_inv_matrix() const +void Camera::calc_inv_matrix() const { - Matrix4x4 res; - return res; // TODO + // TODO } + +void Camera::draw() const +{ +} + +bool Camera::intersect(const Ray &ray, float *dist) +{ + return false; +} diff -r fe94d9e986ae -r d94a69933a71 src/camera.h --- a/src/camera.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/camera.h Sat Apr 12 23:28:24 2014 +0300 @@ -2,27 +2,28 @@ #define CAMERA_H_ #include "vmath.h" +#include "xfnode.h" -class Camera { +class Camera : public SceneNode { private: - Vector3 pos; - Vector3 target; + SceneNode target; float fov; + void calc_matrix() const; + void calc_inv_matrix() const; + public: Camera(); - void set_position(const Vector3 &pos); - const Vector3 &get_position() const; - void set_target(const Vector3 &target); const Vector3 &get_target() const; void set_fov(float fov); float get_fov() const; - Matrix4x4 get_matrix() const; - Matrix4x4 get_inv_matrix() const; + void draw() const; + + bool intersect(const Ray &ray, float *dist = 0) const; }; #endif // CAMERA_H_ diff -r fe94d9e986ae -r d94a69933a71 src/dosemu/dosemu.c --- a/src/dosemu/dosemu.c Thu Apr 10 08:42:33 2014 +0300 +++ b/src/dosemu/dosemu.c Sat Apr 12 23:28:24 2014 +0300 @@ -12,6 +12,8 @@ static void cleanup(void); static void proc_events(void); +static int translate_sdlkey(int sym); +static void update_modkeys(void); static void capture_frame(unsigned char *frame); static void init_sdl(void) @@ -314,11 +316,51 @@ } /* ----- improved event handling (keyb.h) ---- */ +static char sdlkey_tbl[SDLK_LAST]; int kb_init(int bufsz) { + int i; init_sdl(); + /* construct the SDL key translation table */ + for(i=0; i<256; i++) { + sdlkey_tbl[i] = i; /* start from an identity mapping for the first 256 */ + } + /* then change the special keys */ + sdlkey_tbl[SDLK_LALT] = KB_LALT; + sdlkey_tbl[SDLK_RALT] = KB_RALT; + sdlkey_tbl[SDLK_LCTRL] = KB_LCTRL; + sdlkey_tbl[SDLK_RCTRL] = KB_RCTRL; + sdlkey_tbl[SDLK_LSHIFT] = KB_LSHIFT; + for(i=0; i<12; i++) { + sdlkey_tbl[SDLK_F1 + i] = KB_F1 + i; + } + sdlkey_tbl[SDLK_CAPSLOCK] = KB_CAPSLK; + sdlkey_tbl[SDLK_NUMLOCK] = KB_NUMLK; + sdlkey_tbl[SDLK_SCROLLOCK] = KB_SCRLK; + sdlkey_tbl[SDLK_SYSREQ] = KB_SYSRQ; + sdlkey_tbl[SDLK_ESCAPE] = KB_ESC; + sdlkey_tbl[SDLK_INSERT] = KB_INSERT; + sdlkey_tbl[SDLK_DELETE] = KB_DEL; + sdlkey_tbl[SDLK_HOME] = KB_HOME; + sdlkey_tbl[SDLK_END] = KB_END; + sdlkey_tbl[SDLK_PAGEUP] = KB_PGUP; + sdlkey_tbl[SDLK_PAGEDOWN] = KB_PGDN; + sdlkey_tbl[SDLK_LEFT] = KB_LEFT; + sdlkey_tbl[SDLK_RIGHT] = KB_RIGHT; + sdlkey_tbl[SDLK_UP] = KB_UP; + sdlkey_tbl[SDLK_DOWN] = KB_DOWN; + sdlkey_tbl[SDLK_KP_PERIOD] = KB_NUM_DOT; + sdlkey_tbl[SDLK_KP_ENTER] = KB_NUM_ENTER; + sdlkey_tbl[SDLK_KP_PLUS] = KB_NUM_PLUS; + sdlkey_tbl[SDLK_KP_MINUS] = KB_NUM_MINUS; + sdlkey_tbl[SDLK_KP_MULTIPLY] = KB_NUM_MUL; + sdlkey_tbl[SDLK_KP_DIVIDE] = KB_NUM_DIV; + /* TODO missing numeric keypad numbers */ + sdlkey_tbl[SDLK_BACKSPACE] = KB_BACKSP; + + return 0; } @@ -332,7 +374,7 @@ proc_events(); if(keybev) { - res = keybev->key.keysym.sym; + res = translate_sdlkey(keybev->key.keysym.sym); keybev = 0; } return res; @@ -340,8 +382,15 @@ int kb_isdown(int key) { - if(key == KB_ANY) { + switch(key) { + case KB_ANY: return num_pressed; + + case KB_ALT: + return keystate[KB_LALT] + keystate[KB_RALT]; + + case KB_CTRL: + return keystate[KB_LCTRL] + keystate[KB_RCTRL]; } return keystate[key]; } @@ -388,7 +437,7 @@ switch(ev.type) { case SDL_KEYDOWN: { - int key = ev.key.keysym.sym; + int key = translate_sdlkey(ev.key.keysym.sym); if(!keybev) { keybev = &ev; @@ -398,12 +447,14 @@ keystate[key] = 1; num_pressed++; } + + update_modkeys(); } break; case SDL_KEYUP: { - int key = ev.key.keysym.sym; + int key = translate_sdlkey(ev.key.keysym.sym); if(keystate[key]) { keystate[key] = 0; @@ -411,6 +462,8 @@ num_pressed = 0; } } + + update_modkeys(); } break; @@ -453,6 +506,26 @@ } } +static int translate_sdlkey(int sym) +{ + if(sym >= 0 && sym < 256) { + return sdlkey_tbl[sym]; + } + return sym; +} + +static void update_modkeys(void) +{ + unsigned int mod = SDL_GetModState(); + + keystate[KB_LALT] = mod & KMOD_LALT; + keystate[KB_RALT] = mod & KMOD_RALT; + keystate[KB_LCTRL] = mod & KMOD_LCTRL; + keystate[KB_RCTRL] = mod & KMOD_RCTRL; + keystate[KB_LSHIFT] = mod & KMOD_LSHIFT; + keystate[KB_RSHIFT] = mod & KMOD_RSHIFT; +} + /* ---- timer.c implementation ---- */ static Uint32 start_time; diff -r fe94d9e986ae -r d94a69933a71 src/keyb.c --- a/src/keyb.c Thu Apr 10 08:42:33 2014 +0300 +++ b/src/keyb.c Sat Apr 12 23:28:24 2014 +0300 @@ -90,10 +90,17 @@ int kb_isdown(int key) { - if(key == KB_ANY) { + switch(key) { + case KB_ANY: return num_pressed; + + case KB_ALT: + return keystate[KB_LALT] + keystate[KB_RALT]; + + case KB_CTRL: + return keystate[KB_LCTRL] + keystate[KB_RCTRL]; } - return (int)keystate[key]; + return keystate[key]; } void kb_wait(void) diff -r fe94d9e986ae -r d94a69933a71 src/keyb.h --- a/src/keyb.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/keyb.h Sat Apr 12 23:28:24 2014 +0300 @@ -18,7 +18,28 @@ #ifndef KEYB_H_ #define KEYB_H_ -#define KB_ANY (-1) +#define KB_ANY (-1) +#define KB_ALT (-2) +#define KB_CTRL (-3) +#define KB_SHIFT (-4) + +/* special keys */ +enum { + KB_LALT, KB_RALT, + KB_LCTRL, KB_RCTRL, + KB_LSHIFT, KB_RSHIFT, + KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, + KB_F7, KB_F8, KB_F9, KB_F10, KB_F11, KB_F12, + KB_CAPSLK, KB_NUMLK, KB_SCRLK, KB_SYSRQ, + KB_ESC = 27, + KB_INSERT, KB_DEL, KB_HOME, KB_END, KB_PGUP, KB_PGDN, + KB_LEFT, KB_RIGHT, KB_UP, KB_DOWN, + KB_NUM_DOT, KB_NUM_ENTER, KB_NUM_PLUS, KB_NUM_MINUS, KB_NUM_MUL, KB_NUM_DIV, + KB_NUM_0, KB_NUM_1, KB_NUM_2, KB_NUM_3, KB_NUM_4, + KB_NUM_5, KB_NUM_6, KB_NUM_7, KB_NUM_8, KB_NUM_9, + KB_BACKSP = 127 +}; + #ifdef __cplusplus extern "C" { diff -r fe94d9e986ae -r d94a69933a71 src/light.cc --- a/src/light.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/light.cc Sat Apr 12 23:28:24 2014 +0300 @@ -2,23 +2,13 @@ Light::Light() { + type = NODE_LIGHT; color.x = color.y = color.z = 1.0; atten.x = 1.0; atten.y = 0.0; atten.z = 0.0; } -void Light::set_position(const Vector3 &pos) -{ - this->pos = pos; -} - -const Vector3 &Light::get_position() const -{ - return pos; -} - - void Light::set_color(const Vector3 &color) { this->color = color; @@ -39,3 +29,12 @@ { return atten; } + +void Light::draw() const +{ +} + +bool Light::intersect(const Ray &ray, float *dist) +{ + return false; +} diff -r fe94d9e986ae -r d94a69933a71 src/light.h --- a/src/light.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/light.h Sat Apr 12 23:28:24 2014 +0300 @@ -2,24 +2,25 @@ #define LIGHT_H_ #include "vmath.h" +#include "xfnode.h" -class Light { +class Light : public SceneNode { private: - Vector3 pos; Vector3 color; Vector3 atten; public: Light(); - void set_position(const Vector3 &pos); - const Vector3 &get_position() const; - void set_color(const Vector3 &color); const Vector3 &get_color() const; void set_attenuation(const Vector3 &atten); const Vector3 &get_attenuation() const; + + void draw() const; + + bool intersect(const Ray &ray, float *dist); }; #endif // LIGHT_H_ diff -r fe94d9e986ae -r d94a69933a71 src/m3drast.c --- a/src/m3drast.c Thu Apr 10 08:42:33 2014 +0300 +++ b/src/m3drast.c Sat Apr 12 23:28:24 2014 +0300 @@ -43,7 +43,7 @@ crossz += a[0] * b[1] - a[1] * b[0]; } - if(crossz < 0) return; + if(crossz > 0) return; } last[0] = v[numv - 1]; diff -r fe94d9e986ae -r d94a69933a71 src/main.cc --- a/src/main.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/main.cc Sat Apr 12 23:28:24 2014 +0300 @@ -140,7 +140,11 @@ scene = new Scene; Sphere *sph = new Sphere; - scene->add_object(sph); + scene->add(sph); + + Box *box = new Box; + scene->add(box); + box->set_scaling(Vector3(2, 0.25, 2)); Modeller *modeller = new Modeller; if(!modeller->init()) { @@ -288,6 +292,13 @@ use_asm_swap = !use_asm_swap; break; + case 'q': + case 'x': + if(kb_isdown(KB_ALT)) { + quit_app(); + } + break; + default: break; } diff -r fe94d9e986ae -r d94a69933a71 src/modeller.cc --- a/src/modeller.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/modeller.cc Sat Apr 12 23:28:24 2014 +0300 @@ -1,9 +1,11 @@ +#include #include #include #include "modeller.h" #include "min3d.h" #include "rayzor.h" #include "scene.h" +#include "keyb.h" struct ModellerImpl { int mx, my; @@ -112,8 +114,25 @@ { if(press) { switch(key) { + case 'q': + quit_app(); + break; + case 27: - quit_app(); + scene->clear_selection(); + break; + + case '\t': + { + int s = scene->get_selection(); + if(s >= 0) { + s = (s + 1) % scene->get_node_count(); + scene->clear_selection(); + } else { + s = 0; + } + scene->select(s); + } break; default: @@ -136,15 +155,17 @@ mod->prev_x = x; mod->prev_y = y; - if(mod->bnstate[0]) { - mod->cam_theta += dx * 0.5; - mod->cam_phi += dy * 0.5; + if(kb_isdown(KB_ALT) || kb_isdown(KB_CTRL)) { + if(mod->bnstate[0]) { + mod->cam_theta += dx * 0.5; + mod->cam_phi += dy * 0.5; - if(mod->cam_phi < -90) mod->cam_phi = -90; - if(mod->cam_phi > 90) mod->cam_phi = 90; - } - if(mod->bnstate[1]) { - mod->cam_dist += dy * 0.1; - if(mod->cam_dist < 0) mod->cam_dist = 0; + if(mod->cam_phi < -90) mod->cam_phi = -90; + if(mod->cam_phi > 90) mod->cam_phi = 90; + } + if(mod->bnstate[1]) { + mod->cam_dist += dy * 0.1; + if(mod->cam_dist < 0) mod->cam_dist = 0; + } } } diff -r fe94d9e986ae -r d94a69933a71 src/object.cc --- a/src/object.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/object.cc Sat Apr 12 23:28:24 2014 +0300 @@ -5,12 +5,25 @@ Object::Object() { + type = NODE_OBJECT; } Object::~Object() { } +void Object::pre_draw() const +{ + m3d_matrix_mode(M3D_MODELVIEW); + m3d_push_matrix(); + m3d_mult_matrix(get_matrix()[0]); +} + +void Object::post_draw() const +{ + m3d_pop_matrix(); +} + // ---- sphere ---- Sphere::Sphere() { @@ -45,7 +58,7 @@ float theta = u * M_PI * 2.0; float x = sin(theta) * sin(phi); - float y = cos(phi); + float y = -cos(phi); float z = cos(theta) * sin(phi); *vptr++ = Vector3(x, y, z); @@ -71,9 +84,70 @@ printlog(" quads: %d (%d indices, %d usub, %d vsub)\n", USUB * VSUB, num_indices, USUB, VSUB); } - m3d_color(1, 1, 1); + pre_draw(); m3d_vertex_array(&varr->x); m3d_draw_indexed(M3D_QUADS, iarr, num_indices); m3d_vertex_array(0); + + post_draw(); } + +bool Sphere::intersect(const Ray &ray, float *dist) const +{ + return false; // TODO +} + +// ---- box ---- + +Box::Box() +{ +} + +Box::~Box() +{ +} + +/* + 3--------2 + /. .\ + 0------------1 + | 7--------6 | + |/ \| + 4------------5 + + */ +void Box::draw() const +{ + static const float verts[] = { + -1, 1, 1, + 1, 1, 1, + 1, 1, -1, + -1, 1, -1, + -1, -1, 1, + 1, -1, 1, + 1, -1, -1, + -1, -1, -1 + }; + static const unsigned int indices[] = { + 0, 1, 2, 3, // top + 4, 7, 6, 5, // bottom + 0, 4, 5, 1, // front + 5, 6, 2, 1, // right + 6, 7, 3, 2, // back + 7, 4, 0, 3 // left + }; + + pre_draw(); + + m3d_vertex_array(verts); + m3d_draw_indexed(M3D_QUADS, indices, sizeof indices / sizeof *indices); + m3d_vertex_array(0); + + post_draw(); +} + +bool Box::intersect(const Ray &ray, float *dist) const +{ + return false; // TODO +} diff -r fe94d9e986ae -r d94a69933a71 src/object.h --- a/src/object.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/object.h Sat Apr 12 23:28:24 2014 +0300 @@ -1,12 +1,17 @@ #ifndef OBJECT_H_ #define OBJECT_H_ -class Object { +#include "vmath.h" +#include "xfnode.h" + +class Object : public SceneNode { +protected: + void pre_draw() const; + void post_draw() const; + public: Object(); virtual ~Object(); - - virtual void draw() const = 0; }; class Sphere : public Object { @@ -15,6 +20,18 @@ ~Sphere(); void draw() const; + + bool intersect(const Ray &ray, float *dist = 0) const; +}; + +class Box : public Object { +public: + Box(); + ~Box(); + + void draw() const; + + bool intersect(const Ray &ray, float *dist = 0) const; }; #endif // OBJECT_H_ diff -r fe94d9e986ae -r d94a69933a71 src/scancode.h --- a/src/scancode.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/scancode.h Sat Apr 12 23:28:24 2014 +0300 @@ -2,29 +2,15 @@ #error "do not include scancode.h anywhere..." #endif -/* special keys */ -enum { - LALT, RALT, - LCTRL, RCTRL, - LSHIFT, RSHIFT, - F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, - CAPSLK, NUMLK, SCRLK, SYSRQ, - ESC = 27, - INSERT, DEL, HOME, END, PGUP, PGDN, LEFT, RIGHT, UP, DOWN, - NUM_DOT, NUM_ENTER, NUM_PLUS, NUM_MINUS, NUM_MUL, NUM_DIV, - NUM_0, NUM_1, NUM_2, NUM_3, NUM_4, NUM_5, NUM_6, NUM_7, NUM_8, NUM_9, - BACKSP = 127 -}; - /* table with rough translations from set 1 scancodes to ASCII-ish */ static int scantbl[] = { - 0, ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', /* 0 - e */ + 0, KB_ESC, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', /* 0 - e */ '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', /* f - 1c */ - LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', /* 1d - 29 */ - LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', RSHIFT, /* 2a - 36 */ - NUM_MUL, LALT, ' ', CAPSLK, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, /* 37 - 44 */ - NUMLK, SCRLK, NUM_7, NUM_8, NUM_9, NUM_MINUS, NUM_4, NUM_5, NUM_6, NUM_PLUS, /* 45 - 4e */ - NUM_1, NUM_2, NUM_3, NUM_0, NUM_DOT, SYSRQ, 0, 0, F11, F12, /* 4d - 58 */ + KB_LCTRL, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', /* 1d - 29 */ + KB_LSHIFT, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', KB_RSHIFT, /* 2a - 36 */ + KB_NUM_KB_MUL, KB_LALT, ' ', KB_CAPSLK, KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8, KB_F9, KB_F10, /* 37 - 44 */ + KB_NUMLK, KB_SCRLK, KB_NUM_7, KB_NUM_8, KB_NUM_9, KB_NUM_KB_MINUS, KB_NUM_4, KB_NUM_5, KB_NUM_6, KB_NUM_KB_PLUS, /* 45 - 4e */ + KB_NUM_1, KB_NUM_2, KB_NUM_3, KB_NUM_0, KB_NUM_KB_DOT, KB_SYSRQ, 0, 0, KB_F11, KB_F12, /* 4d - 58 */ 0, 0, 0, 0, 0, 0, 0, /* 59 - 5f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60 - 6f */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 70 - 7f */ diff -r fe94d9e986ae -r d94a69933a71 src/scene.cc --- a/src/scene.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/scene.cc Sat Apr 12 23:28:24 2014 +0300 @@ -1,5 +1,6 @@ #include #include "scene.h" +#include "min3d.h" Scene::Scene() { @@ -40,22 +41,35 @@ return name ? name : ""; } -void Scene::add_object(Object *obj) +void Scene::add(SceneNode *node) { - objects.push_back(obj); + nodes.push_back(node); + + switch(node->get_type()) { + case NODE_OBJECT: + objects.push_back((Object*)node); + break; + + case NODE_LIGHT: + lights.push_back((Light*)node); + break; + + case NODE_CAMERA: + cameras.push_back((Camera*)node); + break; + + default: + break; + } + + sel.clear(); } -void Scene::add_light(Light *lt) +int Scene::get_node_count() const { - lights.push_back(lt); + return (int)nodes.size(); } -void Scene::add_camera(Camera *cam) -{ - cameras.push_back(cam); -} - - int Scene::get_object_count() const { return (int)objects.size(); @@ -71,6 +85,15 @@ return (int)cameras.size(); } +SceneNode *Scene::get_node(int idx) +{ + return nodes[idx]; +} + +const SceneNode *Scene::get_node(int idx) const +{ + return nodes[idx]; +} Object *Scene::get_object(int idx) { @@ -108,8 +131,43 @@ // TODO } - int nobj = get_object_count(); - for(int i=0; idraw(); + int num_nodes = get_node_count(); + for(int i=0; idraw(); } } + +void Scene::select(int s) +{ + for(size_t i=0; i= 0 && idx < (int)sel.size() ? sel[idx] : -1; +} diff -r fe94d9e986ae -r d94a69933a71 src/scene.h --- a/src/scene.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/scene.h Sat Apr 12 23:28:24 2014 +0300 @@ -3,6 +3,7 @@ #include #include +#include "xfnode.h" #include "object.h" #include "light.h" #include "camera.h" @@ -10,11 +11,15 @@ class Scene { private: char *name; + vector nodes; + vector objects; vector lights; vector cameras; Camera *active_cam; + vector sel; + public: Scene(); ~Scene(); @@ -24,14 +29,15 @@ void set_name(const char *name); const char *get_name() const; - void add_object(Object *obj); - void add_light(Light *lt); - void add_camera(Camera *cam); + void add(SceneNode *node); + int get_node_count() const; int get_object_count() const; int get_light_count() const; int get_camera_count() const; + SceneNode *get_node(int idx); + const SceneNode *get_node(int idx) const; Object *get_object(int idx); const Object *get_object(int idx) const; Light *get_light(int idx); @@ -40,6 +46,11 @@ const Camera *get_camera(int idx) const; void draw() const; + + void select(int s); + void clear_selection(); + int get_selection_count() const; + int get_selection(int idx = 0) const; }; #endif // SCENE_H_ diff -r fe94d9e986ae -r d94a69933a71 src/stl/vector.h --- a/src/stl/vector.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/stl/vector.h Sat Apr 12 23:28:24 2014 +0300 @@ -2,6 +2,8 @@ #ifndef VECTOR_H_ #define VECTOR_H_ +#include + template class vector { private: diff -r fe94d9e986ae -r d94a69933a71 src/vmath.cc --- a/src/vmath.cc Thu Apr 10 08:42:33 2014 +0300 +++ b/src/vmath.cc Sat Apr 12 23:28:24 2014 +0300 @@ -16,3 +16,109 @@ translate(-pos.x, -pos.y, -pos.z); *this = *this * m; } + +void Matrix4x4::transpose() +{ + Matrix4x4 tmp = *this; + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + m[i][j] = tmp[j][i]; + } + } +} + +float Matrix4x4::determinant() const +{ + float det11 = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + + (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); + + float det12 = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); + + float det13 = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - + (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + float det14 = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - + (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + + (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + return m[0][0] * det11 - m[0][1] * det12 + m[0][2] * det13 - m[0][3] * det14; +} + + +Matrix4x4 Matrix4x4::adjoint() const +{ + Matrix4x4 coef; + + coef.m[0][0] = (m[1][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + + (m[1][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); + coef.m[0][1] = (m[1][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[1][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); + coef.m[0][2] = (m[1][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - + (m[1][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[1][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + coef.m[0][3] = (m[1][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - + (m[1][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + + (m[1][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + coef.m[1][0] = (m[0][1] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[0][2] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) + + (m[0][3] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])); + coef.m[1][1] = (m[0][0] * (m[2][2] * m[3][3] - m[3][2] * m[2][3])) - + (m[0][2] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[0][3] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])); + coef.m[1][2] = (m[0][0] * (m[2][1] * m[3][3] - m[3][1] * m[2][3])) - + (m[0][1] * (m[2][0] * m[3][3] - m[3][0] * m[2][3])) + + (m[0][3] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + coef.m[1][3] = (m[0][0] * (m[2][1] * m[3][2] - m[3][1] * m[2][2])) - + (m[0][1] * (m[2][0] * m[3][2] - m[3][0] * m[2][2])) + + (m[0][2] * (m[2][0] * m[3][1] - m[3][0] * m[2][1])); + + coef.m[2][0] = (m[0][1] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) - + (m[0][2] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) + + (m[0][3] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])); + coef.m[2][1] = (m[0][0] * (m[1][2] * m[3][3] - m[3][2] * m[1][3])) - + (m[0][2] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])); + coef.m[2][2] = (m[0][0] * (m[1][1] * m[3][3] - m[3][1] * m[1][3])) - + (m[0][1] * (m[1][0] * m[3][3] - m[3][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[3][1] - m[3][0] * m[1][1])); + coef.m[2][3] = (m[0][0] * (m[1][1] * m[3][2] - m[3][1] * m[1][2])) - + (m[0][1] * (m[1][0] * m[3][2] - m[3][0] * m[1][2])) + + (m[0][2] * (m[1][0] * m[3][1] - m[3][0] * m[1][1])); + + coef.m[3][0] = (m[0][1] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) - + (m[0][2] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) + + (m[0][3] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])); + coef.m[3][1] = (m[0][0] * (m[1][2] * m[2][3] - m[2][2] * m[1][3])) - + (m[0][2] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])); + coef.m[3][2] = (m[0][0] * (m[1][1] * m[2][3] - m[2][1] * m[1][3])) - + (m[0][1] * (m[1][0] * m[2][3] - m[2][0] * m[1][3])) + + (m[0][3] * (m[1][0] * m[2][1] - m[2][0] * m[1][1])); + coef.m[3][3] = (m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2])) - + (m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2])) + + (m[0][2] * (m[1][0] * m[2][1] - m[2][0] * m[1][1])); + + coef.transpose(); + + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + coef.m[i][j] = j % 2 ? -coef.m[i][j] : coef.m[i][j]; + if(i % 2) coef.m[i][j] = -coef.m[i][j]; + } + } + + return coef; +} + +Matrix4x4 Matrix4x4::inverse() const +{ + Matrix4x4 adj = adjoint(); + return adj * (1.0f / determinant()); +} diff -r fe94d9e986ae -r d94a69933a71 src/vmath.h --- a/src/vmath.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/vmath.h Sat Apr 12 23:28:24 2014 +0300 @@ -47,6 +47,11 @@ return Vector3(a.x - b.x, a.y - b.y, a.z - b.z); } +inline Vector3 operator *(const Vector3 &a, const Vector3 &b) +{ + return Vector3(a.x * b.x, a.y * b.y, a.z * b.z); +} + inline Vector3 operator *(const Vector3 &v, float s) { return Vector3(v.x * s, v.y * s, v.z * s); @@ -65,7 +70,7 @@ inline Vector3 cross(const Vector3 &a, const Vector3 &b) { return Vector3(a.y * b.z - a.z * b.y, - a.z * b.z - a.x * b.z, + a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x); } diff -r fe94d9e986ae -r d94a69933a71 src/vmathmat.h --- a/src/vmathmat.h Thu Apr 10 08:42:33 2014 +0300 +++ b/src/vmathmat.h Sat Apr 12 23:28:24 2014 +0300 @@ -38,6 +38,12 @@ float *operator [](int idx) { return m[idx]; } const float *operator [](int idx) const { return m[idx]; } + + void transpose(); + + float determinant() const; + Matrix4x4 adjoint() const; + Matrix4x4 inverse() const; }; inline Matrix4x4 operator *(const Matrix4x4 &a, const Matrix4x4 &b) @@ -52,6 +58,18 @@ return res; } +inline Matrix4x4 operator *(const Matrix4x4 &mat, float scalar) +{ + Matrix4x4 res; + + for(int i=0; i<4; i++) { + for(int j=0; j<4; j++) { + res.m[i][j] = mat.m[i][j] * scalar; + } + } + return res; +} + inline void Matrix4x4::set_identity() { m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0; @@ -112,5 +130,4 @@ *this = *this * m; } - #endif // VMATH_MATRIX_H_