--
--	g3dcogs.occ -- cog test for graphics3d
--	Copyright (C) 2009 Fred Barnes <frmb@kent.ac.uk>
--
--	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
--	the Free Software Foundation; either version 2 of the License, or
--	(at your option) any later version.
--
--	This program is distributed in the hope that it will be useful,
--	but WITHOUT ANY WARRANTY; without even the implied warranty of
--	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
--	GNU General Public License for more details.
--
--	You should have received a copy of the GNU General Public License
--	along with this program; if not, write to the Free Software
--	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
--

#INCLUDE "course.module"
#INCLUDE "sdlraster.module"
#INCLUDE "graphics3d.module"
#INCLUDE "maths.module"


PROC cog.network (G3D.RENDER.ENGINE? rlink.in, VAL INT width, height, nteeth, colour, VAL REAL32 i.r, o.r, i.a, o.a, c.h, tx, ty, tz, velocity, VAL INT rmode, VAL BOOL wireframe) --{{{
  CHAN INT trigger:
  CHAN MOBILE []G3D.POINT3 gp1, gp2:
  CHAN MOBILE []G3D.POINT2.3 gc1:
  CHAN MOBILE []G3D.POLY pc1:
  CHAN G3D.GEN.CONTROL gen.ctl:
  CHAN G3D.ROT.CONTROL rot.ctl:
  CHAN G3D.PRJ.CONTROL prj.ctl:
  CHAN G3D.PGN.CONTROL pgn.ctl:
  CHAN G3D.PSC.CONTROL psc.ctl:
  CHAN G3D.RDR.CONTROL rdr.ctl:
  PAR
    cogpoints.generator (trigger?, nteeth, i.r, o.r, i.a, o.a, gp1!, gen.ctl?)
    points3d.rotate (gp1?, gp2!, rot.ctl?)
    points.3d.to.2d3 (gp2?, gc1!, prj.ctl?, width, height)
    cogify.points (gc1?, nteeth, pc1!, pgn.ctl?)
    IF
      wireframe
        render.polyframe.trigger (pc1?, rlink.in?, rdr.ctl?, trigger!, width, height)
      TRUE
        CHAN MOBILE []G3D.POLY.SCAN ps1:
        PAR
          poly.scanlines (pc1?, ps1!, psc.ctl?, width, height)
          render.polyscans.trigger (ps1?, rlink.in?, rdr.ctl?, trigger!, width, height)

    SEQ
      gen.ctl ! set.scale; [1.0, c.h, 1.0]
      prj.ctl ! set.3dshift; [tx, ty, tz]
      rdr.ctl ! set.mode; rmode
      IF
        ((rmode /\ G3D.RENDER.MODE.GLENZ) <> 0) OR wireframe
          pgn.ctl ! set.backfaces; TRUE
        TRUE
          pgn.ctl ! set.backfaces; FALSE
      pgn.ctl ! set.face.colours; [colour,colour,colour,colour,colour,colour,colour,colour,colour,colour]
      IF
        NOT wireframe
          psc.ctl ! set.zcolour; TRUE
        TRUE
          SKIP
      rot.ctl ! set.mode; G3D.ROTATE.MODE.Y
      rot.ctl ! add.increment; velocity
:
--}}}
PROC cog.network.spawn (VAL INT width, height, CHAN G3D.RENDER.ENGINE! new.link!, VAL INT nteeth, colour, VAL REAL32 i.r, o.r, i.a, o.a,
                        VAL REAL32 c.h, tx, ty, tz, velocity, VAL INT rmode, VAL BOOL wireframe) --{{{
  G3D.RENDER.ENGINE! rlink:
  G3D.RENDER.ENGINE? rsvr:
  SEQ
    rsvr, rlink := MOBILE G3D.RENDER.ENGINE
    FORK cog.network (rsvr?, width, height, nteeth, colour, i.r, o.r, i.a, o.a, c.h, tx, ty, tz, velocity, rmode, wireframe)
    new.link ! rlink
:

--}}}

PROC g3dcogs (CHAN BYTE kyb?, scr!) --{{{  
  CHAN RASTER rc1, rc2, rc3:
  SHARED! CHAN G3D.RENDER.ENGINE! rre:
  CHAN G3D.TMR.CONTROL tmr.ctl:
  CHAN BOOL go:
  PAR
    raster.timed.buffer (rc1?, rc2!, 60, tmr.ctl?)
    raster.display.simple ("Cog test", G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, 3, rc2?, rc3!)
    raster.chain (rc3?, rc1!, rre?, FALSE)

    CLAIM rre!
      SEQ
        tmr.ctl ! set.fps; 5
        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 8, #000000FF, 2.0, 3.0,
                            (PI / 64.0) * 10.0, (PI / 64.0) * 3.0, 1.0, 2.0, 3.0, -1.0, 0.03,
                            G3D.RENDER.MODE.NONE, TRUE)

        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 6, #0000C0C0, 1.0, 2.0,
                            (PI / 64.0) * 12.0, (PI / 64.0) * 5.0, 3.0, -2.0, 2.7, 0.5, -0.04,
                            G3D.RENDER.MODE.NONE, TRUE)
        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 12, #0000FF00, 3.0, 4.0,
                            (PI / 64.0) * 8.0, (PI / 64.0) * 2.0, 0.8, -2.0, 1.0, 0.5, -0.04,
                            G3D.RENDER.MODE.NONE, FALSE)

        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 6, #00C0C000, 1.0, 1.7,
                            (PI / 64.0) * 12.0, (PI / 64.0) * 5.0, 3.0, 3.1, 0.5, 1.0, 0.08,
                            G3D.RENDER.MODE.NONE, FALSE)
        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 12, #00FF0000, 3.0, 4.0,
                            (PI / 64.0) * 7.0, (PI / 64.0) * 2.0, 1.0, 3.1, -1.7, 1.0, 0.08,
                            G3D.RENDER.MODE.NONE, FALSE)

        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 6, #00C000C0, 1.0, 2.0,
                            (PI / 64.0) * 12.0, (PI / 64.0) * 5.0, 3.0, -2.0, -2.2, 0.5, -0.16,
                            G3D.RENDER.MODE.NONE, FALSE)
        cog.network.spawn (G3D.DEFAULT.WIDTH, G3D.DEFAULT.HEIGHT, rre!, 12, #000000FF, 3.0, 4.0,
                            (PI / 64.0) * 8.0, (PI / 64.0) * 2.0, 0.8, -2.0, -4.3, 0.5, -0.16,
                            G3D.RENDER.MODE.NONE, FALSE)

        tmr.ctl ! set.fps; 60
:
--}}}

