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

lots of dodgy example code

parent 7532ada8
......@@ -65,3 +65,4 @@ install:: ${TARGET}
-cp assets @prefix@/lib/jellyfish/ -r
distclean:: clean
-rm -rf config.status autom4te.cache config.log Makefile
......@@ -10,6 +10,7 @@
`(begin (set! frame-thunk (lambda () ,@args))))
(define (frame-hook)
;; (identity)
(set! flx_time (+ flx_time 1))
(if (not (null? frame-thunk))
(frame-thunk))
......@@ -550,3 +551,9 @@
(let ((c (compile-program 50 'triangles 1 code)))
(disassemble c))
code)
;;----------------------------------------------------------------
;; immediate mode (everyone loves immediate mode)
(define immediate-cube (with-state (hint-none) (build-cube)))
(define (draw-cube) (draw-instance immediate-cube))
This diff is collapsed.
(define (draw-row x y z)
(cond
((not (zero? x))
(translate (vector 1 0 0))
(with-state
(let* ((vec (vtransform (vector 0 0 0) (get-transform)))
(dist (vdist vec (vector 5 (* 5 (+ 1 (sin (* 0.1 (time))))) 5)))
(size (/ (max 0 (- 5 dist)) 5)))
(translate (vector -5 -2 -5))
(scale (vector size size size)))
(draw-cube))
(draw-row (- x 1) y z))))
(define (draw-flat-grid x y z)
(cond
((not (zero? y))
(translate (vector 0 1 0))
(with-state (draw-row x y z))
(draw-flat-grid x (- y 1) z))))
(define (draw-grid x y z)
(cond
((not (zero? z))
(translate (vector 0 0 1))
(with-state (draw-flat-grid x y z))
(draw-grid x y (- z 1)))))
(every-frame
(draw-grid 8 8 8))
(with-primitive
(build-jellyfish 4096)
(pdata-map! (lambda (c) (vector 1 1 1)) "c")
(hint-unlit)
(program-jelly
30000
prim-triangles
'(let ((vertex 0))
(write! reg-graphics (vector 4096 prim-triangles 2))
(set! vertex positions-start)
(loop (< vertex (+ positions-start 200))
(write! vertex (* (rndvec) 7))
(set! vertex (+ vertex 1)))
(forever
(set! vertex positions-start)
(loop (< vertex (+ positions-start 200))
(write! vertex
(+ (* (- (read (- vertex 1)) (read vertex)) 0.01)
(+ (* (read vertex) 0.999)
(* (rndvec) 0.01))))
(set! vertex (+ vertex 1)))))))
(define player
(list
(vector 0 6 0)
0
(with-state
(translate (vector 0 6 0))
(build-cube))))
(define (player-pos player) (list-ref player 0))
(define (player-modify-pos player v) (list-replace player 0 v))
(define (player-dir player) (list-ref player 1))
(define (player-modify-dir player v) (list-replace player 1 v))
(define (player-root player) (list-ref player 2))
(define (rot2d v a)
(vector (+ (* (vx v) (cos a))
(* (vz v) (sin a)))
(vy v)
(+ (* (vx v) (- (sin a)))
(* (vz v) (cos a)))))
(define (update-player player)
(with-primitive
(player-root player)
(identity)
(translate (player-pos player))
(rotate (vector 0 (- (player-dir player)) 0)))
(player-modify-pos
(player-modify-dir
player
(+ (player-dir player)
(cond
((key-pressed "a") -1)
((key-pressed "d") 1)
(else 0))))
(vadd
(player-pos player)
(vmul
(rot2d (vector 1 0 0) (player-dir player))
(cond
((key-pressed "w") 0.2)
((key-pressed "s") -0.2)
(else 0))))))
(define level
(with-state
(texture (load-texture "bg.png"))
(scale (vector 5 5 5))
(load-obj "assets/testlevel.obj")))
(with-primitive
level
(pdata-map!
(lambda (t) (vmul t 5)) "t"))
(lock-camera (player-root player))
(every-frame
(set! player (update-player player)))
; (with-primitive
; (player-root player)
; (rotate (vector 0 (if (key-pressed "a") 0.5 0) 0))))
(translate (vector -4 0 0))
(with-state
(build-cube))
(translate (vector 2 0 0))
(with-state
(hint-none)
(hint-wire)
(build-cube))
(translate (vector 2 0 0))
(with-state
(colour (vector 1 0 0))
(hint-none)
(hint-wire)
(build-cube))
(translate (vector 2 0 0))
(with-state
(colour (vector 1 0 0))
(build-cube))
(translate (vector 2 0 0))
(with-state
(colour (vector 1 0 0))
(hint-unlit)
(build-cube))
(identity)
(translate (vector 0 2 0))
(translate (vector -4 0 0))
(texture (load-texture "raspberrypi.png"))
(with-state
(build-cube))
(translate (vector 2 0 0))
(with-state
(hint-none)
(hint-wire)
(build-cube))
(translate (vector 2 0 0))
(with-state
(colour (vector 1 0 0))
(hint-none)
(hint-wire)
(build-cube))
(translate (vector 2 0 0))
(with-state
(colour (vector 1 0 0))
(build-cube))
(translate (vector 2 0 0))
(with-state
(colour (vector 1 0 0))
(hint-unlit)
(build-cube))
......@@ -238,7 +238,7 @@
)
(msg "hello")
(every-frame
(with-primitive
......
......@@ -275,6 +275,20 @@ void engine::setup_state(scenenode *n)
n->m_texture=state_top()->m_texture;
}
void engine::draw_instance(int id)
{
scenenode *node = m_sg->find(id);
if (node) {
// copy the primitive only
// (set to null after rendering so it don't get deleted)
// perhaps allocate a bunch of these at startup?
scenenode *n=new scenenode(node->m_primitive);
setup_state(n);
m_sg->add_immediate(n);
}
}
int engine::build_cube()
{
scenenode *n=new scenenode(new primitive());
......@@ -381,7 +395,7 @@ void engine::destroy(int id)
m_attached_prim = NULL;
m_attached_id = 0;
}
m_sg->remove(id);
}
......
......@@ -60,6 +60,7 @@ public:
vec3 *pdata_get(const char *name, int i);
void pdata_set(const char *name, int i, vec3 v);
void draw_instance(int id);
int build_cube();
int load_obj(char *fn);
int raw_obj(char *data);
......
......@@ -168,6 +168,8 @@ void scenegraph::render()
// render immediate mode
render_node_walk(m_immediate_root,1);
// protect the instances from being deleted...
clear_primitives_walk(m_immediate_root);
// clear immediate mode
delete m_immediate_root;
......@@ -175,6 +177,17 @@ void scenegraph::render()
m_immediate_root->m_id=9999;
}
void scenegraph::clear_primitives_walk(scenenode *node)
{
node->m_primitive=NULL;
scenenode *n=static_cast<scenenode*>(node->m_children.m_head);
while (n!=NULL)
{
clear_primitives_walk(n);
n=static_cast<scenenode*>(n->m_next);
}
}
void scenegraph::render_node_walk(scenenode *node, int depth)
{
#ifdef _EE
......@@ -214,7 +227,8 @@ void scenegraph::render_node_walk(scenenode *node, int depth)
}
else glDisable(GL_TEXTURE_2D);
if (node->m_primitive!=NULL)
// hint-none = hidden
if (node->m_primitive!=NULL && node->m_hints)
{
node->m_primitive->render(node->m_hints);
}
......
......@@ -28,7 +28,7 @@ public:
/// adds a node onto a parent node (0 is the root)
int add(int pid, scenenode *n);
/// adds immediate mode node referencing existing primitive,
/// adds immediate mode node referencing existing primitive,
/// will be removed after rendering
void add_immediate(scenenode *n);
/// delete a node and it's children
......@@ -41,14 +41,14 @@ public:
void clear();
/// is the child attached to the parent?
bool is_attached_to(scenenode *parent, scenenode *n) const;
scenenode *root() { return m_root; }
scenenode *root() { return m_root; }
mat44 get_global_transform(scenenode *n);
void render();
void dump() const;
texture_manager m_texture_manager;
// returns the first node intersecting with the line
u32 intersect_node_walk(scenenode *node, const vec3 &start, const vec3 &end);
......@@ -57,6 +57,7 @@ protected:
scenenode* find_node_walk(scenenode *n, int id) const;
void dump_walk(scenenode *n, int d) const;
void render_node_walk(scenenode *n, int depth);
void clear_primitives_walk(scenenode *node);
scenenode *m_root;
scenenode *m_immediate_root;
int m_current_id;
......
......@@ -29,10 +29,12 @@ scenenode::scenenode(primitive *p) :
scenenode::~scenenode()
{
// list destr deletes children
if (m_parent!=NULL)
{
m_parent->remove_child(m_id);
}
}
if (m_primitive!=NULL) delete m_primitive;
}
......@@ -45,7 +47,7 @@ scenenode *scenenode::find_child(int id)
if (n->m_id==id) return n;
n=static_cast<scenenode*>(n->m_next);
}
return NULL;
return NULL;
}
......@@ -54,7 +56,7 @@ void scenenode::remove_child(int id)
scenenode *n=find_child(id);
if (n!=NULL)
{
delete n;
m_children.remove(n);
}
}
......@@ -43,7 +43,7 @@
#define HINT_NOBLEND 0x00040000
#define HINT_NOZWRITE 0x00080000
class scenenode : public list::node
class scenenode : public list::node
{
public:
scenenode(primitive *p);
......@@ -51,7 +51,7 @@ public:
scenenode *find_child(int id);
void remove_child(int id);
u32 m_id;
primitive *m_primitive;
scenenode *m_parent;
......
#include "engine/importgl.h"
#include "linux/glut_graphics.h"
void glTranslatex(GLfixed x, GLfixed y, GLfixed z)
{
glTranslatef(x/65536.0,y/65536.0,z/65536.0);
}
void glFrustumx(GLfixed xmin, GLfixed xmax, GLfixed ymin, GLfixed ymax, GLfixed zNear, GLfixed zFar)
{
glFrustum(xmin/65536.0, xmax/65536.0,
ymin/65536.0, ymax/65536.0,
zNear/65536.0, zFar/65536.0);
}
void glClearColorx(GLfixed r, GLfixed g, GLfixed b, GLfixed a)
{
glClearColor(r/65536.0,g/65536.0,b/65536.0,a/65536.0);
}
void glMaterialx( GLenum face, GLenum pname, GLfixed param)
{
glMaterialf(face,pname,param/65536.0);
}
void glMaterialxv( GLenum face, GLenum pname, GLfixed * params)
{
float fparams[4];
fparams[0]=params[0]/65536.0;
fparams[1]=params[1]/65536.0;
fparams[2]=params[2]/65536.0;
fparams[3]=params[3]/65536.0;
glMaterialfv(face,pname,fparams);
}
void glLightxv( GLenum light, GLenum pname, GLfixed * params)
{
float fparams[4];
fparams[0]=params[0]/65536.0;
fparams[1]=params[1]/65536.0;
fparams[2]=params[2]/65536.0;
fparams[3]=params[3]/65536.0;
glLightfv(light,pname,fparams);
}
void glMultMatrixx( GLfixed * mat )
{
float m[16];
for (int i=0; i<16; i++)
{
m[i]=mat[i]/65536.0f;
}
glMultMatrixf(m);
}
#include "engine/importgl.h"
// fixed point versions for speeeed
void glTranslatex(GLfixed x, GLfixed y, GLfixed z);
void glFrustumx(GLfixed xmin, GLfixed xmax, GLfixed ymin, GLfixed ymax, GLfixed zNear, GLfixed zFar);
void glClearColorx(GLfixed r, GLfixed g, GLfixed b, GLfixed a);
void glMaterialx( GLenum face, GLenum pname, GLfixed param);
void glMaterialxv( GLenum face, GLenum pname, GLfixed * params);
void glLightxv( GLenum light, GLenum pname, GLfixed * params);
void glMultMatrixx( GLfixed * mat );
......@@ -191,6 +191,8 @@ int main(int argc, char *argv[])
glutCreateWindow(windowtitle);
glutDisplayFunc(DisplayCallback);
glutIdleFunc(IdleCallback);
glutKeyboardFunc(KeyboardCallback);
glutKeyboardUpFunc(KeyboardUpCallback);
#endif
appInit();
......@@ -221,8 +223,8 @@ int main(int argc, char *argv[])
render_mutex = new pthread_mutex_t;
pthread_mutex_init(render_mutex,NULL);
pthread_t *repl_thread = new pthread_t;
pthread_create(repl_thread,NULL,(void*(*)(void*))repl_loop,NULL);
setup_osc_repl();
//pthread_create(repl_thread,NULL,(void*(*)(void*))repl_loop,NULL);
//setup_osc_repl();
#ifdef FLX_RPI
// getMouse();
......
......@@ -225,6 +225,7 @@
_OP_DEF(opexe_6, "line-width", 1, 1, 0, OP_LINE_WIDTH )
_OP_DEF(opexe_6, "texture", 1, 1, 0, OP_TEXTURE )
_OP_DEF(opexe_6, "load-texture", 1, 1, 0, OP_LOAD_TEXTURE )
_OP_DEF(opexe_6, "draw-instance", 1, 1, 0, OP_DRAW_INSTANCE )
_OP_DEF(opexe_6, "build-cube", 0, 0, 0, OP_BUILD_CUBE )
_OP_DEF(opexe_6, "load-obj", 1, 1, 0, OP_LOAD_OBJ )
_OP_DEF(opexe_6, "raw-obj", 1, 1, 0, OP_RAW_OBJ )
......
......@@ -4676,6 +4676,9 @@ static pointer opexe_6(scheme *sc, enum scheme_opcodes op) {
s_return(sc,sc->F);
case OP_LOAD_TEXTURE:
s_return(sc,mk_integer(sc,engine::get()->get_texture(string_value(car(sc->args)))));
case OP_DRAW_INSTANCE:
engine::get()->draw_instance(ivalue(car(sc->args)));
s_return(sc,sc->F);
case OP_BUILD_CUBE:
s_return(sc,mk_integer(sc,engine::get()->build_cube()));
case OP_LOAD_OBJ:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment