Commit df08c096 authored by Dave Griffiths's avatar Dave Griffiths
Browse files

loads of fixes from scheme bricks test

parent 878fbc03
...@@ -243,3 +243,38 @@ sqr len dup drp cmp shf bld ret dbg ...@@ -243,3 +243,38 @@ sqr len dup drp cmp shf bld ret dbg
nrm mst mad msb swp rnd mull nrm mst mad msb swp rnd mull
jmr ldlv lensq noise lds sts mulv jmr ldlv lensq noise lds sts mulv
synth-crt synth-con synth-ply flr synth-crt synth-con synth-ply flr
### TinyScheme Licence follows ###
LICENSE TERMS
Copyright (c) 2000, Dimitrios Souflis
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of Dimitrios Souflis nor the names of the
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
...@@ -30,11 +30,31 @@ ...@@ -30,11 +30,31 @@
(define (hint-anti-alias) (hint 5)) (define (hint-anti-alias) (hint 5))
(define (hint-bound) (hint 6)) (define (hint-bound) (hint 6))
(define (hint-unlit) (hint 7)) (define (hint-unlit) (hint 7))
(define (hint-vertcols) (hint 8))
(define (hint-origin) (hint 9))
(define (hint-cast-shadow) (hint 10))
(define (hint-ignore-depth) (hint 11))
(define (hint-depth-sort) (hint 12))
(define (hint-lazy-parent) (hint 13))
(define (hint-cull-ccw) (hint 14))
(define (hint-wire-stippled) (hint 15))
(define (hint-spere-map) (hint 16))
(define (hint-frustum-cull) (hint 17))
(define (hint-normalise) (hint 18))
(define (hint-noblend) (hint 19))
(define (hint-nozwrite) (hint 20))
;------------------------------------------------------------ ;------------------------------------------------------------
(define _mouse-x 0) (define keys '())
(define _mouse-y 0) (define keys-this-frame '())
(define special-keys '())
(define special-keys-this-frame '())
(define mouse (vector 0 0))
(define mouse-buttons (vector #f #f #f #f))
(define mouse-wheel-v 0)
(define key-mods '())
;; this stuff for touchscreens
(define _mouse-b -1) (define _mouse-b -1)
(define _mouse-s 1) ; state - 0 down, 1 up (define _mouse-s 1) ; state - 0 down, 1 up
...@@ -46,25 +66,21 @@ ...@@ -46,25 +66,21 @@
(when (zero? _mouse-s) ; eh? (when (zero? _mouse-s) ; eh?
(set! _touching #t) (set! _touching #t)
(set! _touches (list (list 0 x y)))) (set! _touches (list (list 0 x y))))
(set! _mouse-x x) (vector-set! mouse 0 x)
(set! _mouse-y y)) (vector-set! mouse 1 y))
(define (mouse-x) (vector-ref mouse 0))
(define (mouse-y) (vector-ref mouse 1))
;;(define (mouse-button n)
;; (if _touching
;; #t
;; (if (zero? _mouse-s)
;; (eqv? _mouse-b n) #f)))
(define (mouse-x) _mouse-x)
(define (mouse-y) _mouse-y)
(define (mouse-button n) (define (mouse-button n)
(if _touching (vector-ref mouse-buttons (- n 1)))
#t
(if (zero? _mouse-s)
(eqv? _mouse-b n) #f)))
(define keys '())
(define keys-this-frame '())
(define special-keys '())
(define special-keys-this-frame '())
(define mouse (vector 0 0))
(define mouse-buttons (vector #f #f #f))
(define mouse-wheel-v 0)
(define key-mods '())
; utils funcs for using lists as sets ; utils funcs for using lists as sets
(define (set-remove a l) (define (set-remove a l)
...@@ -104,19 +120,19 @@ ...@@ -104,19 +120,19 @@
; (for/list ([bitmask (list 1 2 4)] ; (for/list ([bitmask (list 1 2 4)]
; [bitsym '(shift ctrl alt)] ; [bitsym '(shift ctrl alt)]
; #:when (> (bitwise-and mod bitmask) 0)) ; #:when (> (bitwise-and mod bitmask) 0))
; bitsym)) ; bitsym))
(cond ; mouse (cond ; mouse
((and (eq? key 0) (eq? special -1)) ((and (eqv? key 0) (eqv? special -1))
(when (eq? button 3) (set! mouse-wheel-v 1)) (when (eq? button 3) (set! mouse-wheel-v 1))
(when (eq? button 4) (set! mouse-wheel-v -1)) (when (eq? button 4) (set! mouse-wheel-v -1))
(when (and (eq? state 0) (when (and (eqv? state 0)
(< button (vector-length mouse-buttons))) (< button (vector-length mouse-buttons)))
(vector-set! mouse-buttons button #t)) (vector-set! mouse-buttons button #t))
(when (and (eq? state 1) (when (and (eqv? state 1)
(< button (vector-length mouse-buttons))) (< button (vector-length mouse-buttons)))
(vector-set! mouse-buttons button #f)) (vector-set! mouse-buttons button #f))))
(vector-set! mouse 0 x) (vector-set! mouse 0 x)
(vector-set! mouse 1 y)))) (vector-set! mouse 1 y))
(define (register-up key button special state x y mod) (define (register-up key button special state x y mod)
(when (not (eq? key -1)) (when (not (eq? key -1))
...@@ -289,6 +305,27 @@ ...@@ -289,6 +305,27 @@
(loop (+ n 1) total)))))) (loop (+ n 1) total))))))
(loop 0 (- (pdata-size) 1))))) (loop 0 (- (pdata-size) 1)))))
(define (pdata-range-map! start end . args)
(let ((proc (car args))
(pdata-write-name (cadr args))
(pdata-read-names (cddr args)))
(letrec
((loop (lambda (n total)
(cond ((not (> n total))
(pdata-set!
pdata-write-name n
(apply
proc
(cons
(pdata-ref pdata-write-name n)
(map
(lambda (read)
(pdata-ref read n))
pdata-read-names))))
(loop (+ n 1) total))))))
(loop start (min (pdata-size) end)))))
(define (pdata-index-map! . args) (define (pdata-index-map! . args)
(let ((proc (car args)) (let ((proc (car args))
(pdata-write-name (cadr args)) (pdata-write-name (cadr args))
...@@ -482,6 +519,13 @@ ...@@ -482,6 +519,13 @@
; (display code)(newline) ; (display code)(newline)
(eval code))) (eval code)))
;; detach and retain original transform
(define (detach-parent)
(let ((m (get-global-transform)))
(parent 1) ;; reparent to root
(identity)
(concat m)))
;;--------------------------------------------------------- ;;---------------------------------------------------------
;; jellyfish helpers ;; jellyfish helpers
......
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; maths hacks, move to fluxa
(define (all-numbers? l)
(cond
((null? l) #t)
((not (number? (car l))) #f)
(else (all-numbers? (cdr l)))))
(define (+ . args)
(if (all-numbers? args)
(apply + args)
(proc-list add args)))
(define (- . args)
(if (all-numbers? args)
(apply - args)
(proc-list sub args)))
(define (/ . args)
(if (all-numbers? args)
(apply / args)
(proc-list div args)))
(define (* . args)
(if (all-numbers? args)
(apply * args)
(proc-list mul args)))
(define (proc-list p l)
(cond
((eq? (length l) 1) (car l))
((eq? (length l) 2) (p (car l) (cadr l)))
(else (p (car l) (proc-list p (cdr l))))))
This diff is collapsed.
...@@ -66,3 +66,16 @@ ...@@ -66,3 +66,16 @@
(colour (vector 1 0 0)) (colour (vector 1 0 0))
(hint-unlit) (hint-unlit)
(build-cube)) (build-cube))
(translate (vector -4 2 0))
(with-state
(texture (load-texture "font.png"))
(build-text "hello"))
(translate (vector 2 -6 0))
(texture 0)
(define a (build-cube))
(define b (with-state (parent a) (build-cube)))
(define c (build-cube))
(with-primitive b (parent c))
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
#include "pdata.h" #include "pdata.h"
// returns the parametric distance along the line, or -1 for no intersection // returns the parametric distance along the line, or -1 for no intersection
flx_real intersect_line_triangle(const vec3 &start, const vec3 &end, flx_real intersect_line_triangle(const vec3 &start, const vec3 &end,
const vec3 &ta, const vec3 &tb, const vec3 &ta, const vec3 &tb,
const vec3 &tc, vec3 &bary) const vec3 &tc, vec3 &bary)
{ {
vec3 u = ta-tc; vec3 u = ta-tc;
...@@ -28,7 +28,7 @@ flx_real intersect_line_triangle(const vec3 &start, const vec3 &end, ...@@ -28,7 +28,7 @@ flx_real intersect_line_triangle(const vec3 &start, const vec3 &end,
vec3 n = v.cross(u); vec3 n = v.cross(u);
if (n.mag()==0.0f) return -1.0f; if (n.mag()==0.0f) return -1.0f;
vec3 ray = end-start; vec3 ray = end-start;
vec3 w0 = start-tc; vec3 w0 = start-tc;
flx_real a = -n.dot(w0); flx_real a = -n.dot(w0);
...@@ -60,6 +60,12 @@ flx_real intersect_line_triangle(const vec3 &start, const vec3 &end, ...@@ -60,6 +60,12 @@ flx_real intersect_line_triangle(const vec3 &start, const vec3 &end,
return r; return r;
} }
bool intersect_point_bb(const vec3 &p, const vec3 &min, const vec3 &max, flx_real threshold) {
return (p.x>min.x-threshold && p.x<max.x+threshold &&
p.y>min.y-threshold && p.y<max.y+threshold &&
p.z>min.z-threshold && p.z<max.z+threshold);
}
// need to delete the point // need to delete the point
intersect_point *interpolate_pdata(const list *pdata, float t, const vec3 &bary, u32 i1, u32 i2, u32 i3) intersect_point *interpolate_pdata(const list *pdata, float t, const vec3 &bary, u32 i1, u32 i2, u32 i3)
{ {
...@@ -79,4 +85,4 @@ intersect_point *interpolate_pdata(const list *pdata, float t, const vec3 &bary, ...@@ -79,4 +85,4 @@ intersect_point *interpolate_pdata(const list *pdata, float t, const vec3 &bary,
} }
return p; return p;
} }
...@@ -25,6 +25,8 @@ flx_real intersect_line_triangle(const vec3 &start, const vec3 &end, ...@@ -25,6 +25,8 @@ flx_real intersect_line_triangle(const vec3 &start, const vec3 &end,
const vec3 &ta, const vec3 &tb, const vec3 &tc, const vec3 &ta, const vec3 &tb, const vec3 &tc,
vec3 &bary); vec3 &bary);
bool intersect_point_bb(const vec3 &p, const vec3 &min, const vec3 &max, flx_real threshold);
class intersect_point : public list::node class intersect_point : public list::node
{ {
public: public:
......
...@@ -351,8 +351,8 @@ int engine::build_text(char *str) ...@@ -351,8 +351,8 @@ int engine::build_text(char *str)
{ {
// 16*16 grid of letters // 16*16 grid of letters
text_primitive *p = new text_primitive(strlen(str),16/256.0f,16/256.0f,16,0); text_primitive *p = new text_primitive(strlen(str),16/256.0f,16/256.0f,16,0);
p->set_text(str);
scenenode *n=new scenenode(p); scenenode *n=new scenenode(p);
p->set_text(str);
setup_state(n); setup_state(n);
return m_sg->add(state_top()->m_parent,n); return m_sg->add(state_top()->m_parent,n);
} }
...@@ -369,6 +369,7 @@ int engine::build_polygons(unsigned int size, int type) ...@@ -369,6 +369,7 @@ int engine::build_polygons(unsigned int size, int type)
scenenode *n=new scenenode( scenenode *n=new scenenode(
new primitive(size, new primitive(size,
static_cast<primitive::type>(type))); static_cast<primitive::type>(type)));
// todo: why not setup_state(n); ??
n->m_tx=state_top()->m_tx; n->m_tx=state_top()->m_tx;
n->m_hints=state_top()->m_hints; n->m_hints=state_top()->m_hints;
n->m_line_width=state_top()->m_line_width; n->m_line_width=state_top()->m_line_width;
...@@ -505,6 +506,33 @@ void engine::pdata_set(const char *name, int i, vec3 v) ...@@ -505,6 +506,33 @@ void engine::pdata_set(const char *name, int i, vec3 v)
} }
} }
void engine::recalc_bb()
{
if (grabbed())
{
scenenode *n = grabbed_node();
if (n && n->m_primitive!=NULL)
{
n->m_primitive->recalc_bb();
}
}
return;
}
bool engine::bb_point_intersect(const vec3 &p, flx_real threshold)
{
if (grabbed())
{
scenenode *n = grabbed_node();
if (n && n->m_primitive!=NULL)
{
vec3 pt=n->m_tx.transform(p);
return n->m_primitive->intersect_bb(pt,threshold);
}
}
return false;
}
bb::list *engine::geo_line_intersect(const vec3 &start, const vec3 &end) bb::list *engine::geo_line_intersect(const vec3 &start, const vec3 &end)
{ {
if (grabbed()) if (grabbed())
......
...@@ -83,6 +83,8 @@ public: ...@@ -83,6 +83,8 @@ public:
u32 get_texture(const char *filename); u32 get_texture(const char *filename);
list *geo_line_intersect(const vec3 &start, const vec3 &end); list *geo_line_intersect(const vec3 &start, const vec3 &end);
u32 get_line_intersect(const vec3 &start, const vec3 &end); u32 get_line_intersect(const vec3 &start, const vec3 &end);
void recalc_bb();
bool bb_point_intersect(const vec3 &p, flx_real threshold);
void render(); void render();
......
...@@ -168,6 +168,20 @@ void primitive::apply(const mat44 &m) ...@@ -168,6 +168,20 @@ void primitive::apply(const mat44 &m)
} }
} }
void primitive::recalc_bb()
{
for (int i=0; i<m_size; i++)
{
if (m_bbmin>m_positions[i]) m_bbmin=m_positions[i];
if (m_bbmax<m_positions[i]) m_bbmax=m_positions[i];
}
}
bool primitive::intersect_bb(const vec3 &p, flx_real threshold)
{
return intersect_point_bb(p, m_bbmin, m_bbmin, threshold);
}
list *primitive::intersect(const vec3 &start, const vec3 &end) list *primitive::intersect(const vec3 &start, const vec3 &end)
{ {
return intersect_tristrip(start,end); return intersect_tristrip(start,end);
......
...@@ -46,9 +46,13 @@ public: ...@@ -46,9 +46,13 @@ public:
void pdata_set(const char* name, int i, vec3 v); void pdata_set(const char* name, int i, vec3 v);
vec3 *pdata_get(const char* name, int i); vec3 *pdata_get(const char* name, int i);
void recalc_bb();
// delete my list please, see geometry.h:points // delete my list please, see geometry.h:points
list *intersect(const vec3 &start, const vec3 &end); list *intersect(const vec3 &start, const vec3 &end);
bool intersect_fast(const vec3 &start, const vec3 &end); bool intersect_fast(const vec3 &start, const vec3 &end);
// p is in object space
bool intersect_bb(const vec3 &p, flx_real threshold);
protected: protected:
...@@ -64,6 +68,10 @@ protected: ...@@ -64,6 +68,10 @@ protected:
u32 m_type; u32 m_type;
u32 m_size; u32 m_size;
list m_pdata; list m_pdata;
vec3 m_bbmin;
vec3 m_bbmax;
}; };
#endif #endif
...@@ -50,13 +50,13 @@ scenenode *scenenode::find_child(int id) ...@@ -50,13 +50,13 @@ scenenode *scenenode::find_child(int id)
return NULL; return NULL;
} }
// deleting the node is someone elses business - we may be reparenting
void scenenode::remove_child(int id) void scenenode::remove_child(int id)
{ {
scenenode *n=find_child(id); scenenode *n=find_child(id);
if (n!=NULL) if (n!=NULL)
{ {
delete n; //delete n;
m_children.remove(n); m_children.remove(n);
} }
} }
...@@ -39,7 +39,7 @@ void text_primitive::set_text(const char *str) ...@@ -39,7 +39,7 @@ void text_primitive::set_text(const char *str)
//20,-20,0.018 //20,-20,0.018
m_text_width=m_char_width*(float)length; m_text_width=m_char_width*(float)length;
m_text_height=m_char_height; m_text_height=m_char_height;
int wrapcount=0; int wrapcount=0;
u32 vp=0; u32 vp=0;
flx_real z=0; flx_real z=0;
flx_real zm=0.0001; flx_real zm=0.0001;
...@@ -55,7 +55,7 @@ void text_primitive::set_text(const char *str) ...@@ -55,7 +55,7 @@ void text_primitive::set_text(const char *str)
{ {
if (n<m_max_chars) if (n<m_max_chars)
{ {
u32 ascii_pos=(u32)str[n]; u32 ascii_pos=(u32)str[n];
flx_real s=(float)(ascii_pos%m_char_stride)*m_char_width+m_xoff; flx_real s=(float)(ascii_pos%m_char_stride)*m_char_width+m_xoff;
flx_real t=(float)(ascii_pos/m_char_stride)*m_char_height+m_yoff; flx_real t=(float)(ascii_pos/m_char_stride)*m_char_height+m_yoff;
vec3 min(s,t,0); vec3 min(s,t,0);
...@@ -74,7 +74,7 @@ void text_primitive::set_text(const char *str) ...@@ -74,7 +74,7 @@ void text_primitive::set_text(const char *str)
m_tex[vp].x=max.x; m_tex[vp].x=max.x;
m_tex[vp].y=min.y; m_tex[vp].y=min.y;
vp++; vp++;
m_positions[vp].x=x+w; m_positions[vp].x=x+w;
m_positions[vp].y=y+h; m_positions[vp].y=y+h;
m_positions[vp].z=z; m_positions[vp].z=z;
...@@ -106,9 +106,9 @@ void text_primitive::set_text(const char *str) ...@@ -106,9 +106,9 @@ void text_primitive::set_text(const char *str)
vp++; vp++;
z+=zm; z+=zm;
if (m_wrap_chars) wrapcount++; if (m_wrap_chars) wrapcount++;
if (str[n]=='\n' || (m_wrap_chars && wrapcount>m_wrap_chars)) if (str[n]=='\n' || (m_wrap_chars && wrapcount>m_wrap_chars))
{ {
y+=h; y+=h;
...@@ -139,5 +139,3 @@ void text_primitive::render(u32 hints) ...@@ -139,5 +139,3 @@ void text_primitive::render(u32 hints)
{ {
primitive::render(hints); primitive::render(hints);
} }
...@@ -138,6 +138,29 @@ void IdleCallback() ...@@ -138,6 +138,29 @@ void IdleCallback()
glutPostRedisplay(); glutPostRedisplay();
} }
void MouseCallback(int button, int state, int x, int y)
{
char code[256];
snprintf(code,256,"(%s %d %d %d %d %d %d %d)",INPUT_CALLBACK.c_str(),0,button,-1,state,x,y,0);
appEval(code);
cerr<<code<<endl;
}
void MotionCallback(int x, int y)
{
char code[256];
snprintf(code,256,"(%s %d %d %d %d %d %d %d)",INPUT_CALLBACK.c_str(),0,-1,-1,-1,x,y,0);
appEval(code);
}
void PassiveMotionCallback(int x, int y)
{
char code[256];
snprintf(code,256,"(%s %d %d %d %d %d %d %d)",INPUT_CALLBACK.c_str(),0,-1,-1,-1,x,y,0);
appEval(code);