Paste: use render-set

Author: typemore
Mode: factor
Date: Wed, 18 Aug 2010 00:44:26
Plain Text |
! Copyright (C) 2010 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: colors game.loop game.worlds literals method-chains
       ui.gadgets.worlds arrays multiline prettyprint kernel
       math opengl opengl.gl opengl.glu opengl.demo-support
       ui ui.gadgets ui.render ui.pixel-formats accessors
       game.input game.input.scancodes sequences locals
       specialized-arrays.instances.alien.c-types.float
       gpu.buffers images gpu.shaders
       ;

IN: gfx

GLSL-SHADER: fixed-vertex-shader vertex-shader
    varying vec3 normal;
    varying vec3 vtl;
    uniform vec4 light_dir;
    varying float dt;

    void main()
    {
      gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
      normal = normalize(gl_NormalMatrix * gl_Normal);
      vtl = normalize(gl_ModelViewMatrix * light_dir);

      dt = clamp(dot(gl_Normal, light_dir), 0.0, 1.0);
      // float DiffuseTerm = clamp(dot(normal, vtl), 0.0, 1.0);
    }
;


GLSL-SHADER: fixed-fragment-shader fragment-shader
    varying vec3 normal;
    varying vec3 vtl;
    uniform vec4 dcolor;
    uniform vec4 acolor;
    uniform float light;
    varying float dt;

    uniform sampler2D colorMap;

    void main()
    {
      float DiffuseTerm = clamp(dot(normal, vtl), 0.0, 1.0);
      float dterm = light * dt + (1.0-light);
      gl_FragColor = acolor + dcolor * dterm;
      //vec4(1, 1, 1, 1) * dt;
    }
;


GLSL-PROGRAM: fixed-program fixed-vertex-shader fixed-fragment-shader ;


<PRIVATE

CONSTANT: width 640
CONSTANT: height 480

: gl_init ( -- )
    GL_DEPTH_TEST glEnable
    GL_LEQUAL glDepthFunc
    GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST glHint
    GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear 
    GL_SMOOTH glShadeModel ;

CONSTANT: square-vertexs
  float-array{
    -10.0 -10.0 0.0
    -10.0  10.0 0.0
     10.0  10.0 0.0
     10.0 -10.0 0.0
  }

: <square-vertex-buffers> ( -- buffer )
    square-vertexs
    static-upload draw-usage vertex-buffer
    byte-array>buffer ; inline

: <square-vertex-array> ( program-instance -- vertex-array )
  [ <square-vertex-buffers> ] dip { "vertex" float-components 3 f } <vertex-array*> ; inline

PRIVATE>


TUPLE: min-world < game-world
  { angle initial: 0 }
  { angle-diff initial: 0 }
  { width initial: 640 }
  { height initial: 480 } 
  { square-va }
  { fixed-program }
  ;

M:: min-world resize-world ( g -- )
    g dim>> first2 :> ( width height )
    0 0 width height glViewport
    GL_PROJECTION glMatrixMode
      glLoadIdentity
        0 width - width
        0 height - height
        -100.9 100.0 glOrtho
    GL_MODELVIEW glMatrixMode ;

M:: min-world draw-world* ( g -- )
    g g angle>> g angle-diff>> + >>angle drop
    GL_MODELVIEW glMatrixMode
      0 0 1   0 0 0   0 1 0 gluLookAt
      glLoadIdentity
      0.0 0.0 0.0 0.0 glClearColor
      1.0 glClearDepth
      GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT bitor glClear
      glLoadIdentity
      g angle>> 0.0 0.0 1.0 glRotatef
      GL_QUADS [
        -10.0   10.0  0.0 glVertex3f
        -10.0  -10.0  0.0 glVertex3f
         10.0  -10.0  0.0 glVertex3f
         10.0   10.0  0.0 glVertex3f
      ] do-state
    key-w read-keyboard keys>> nth
      [ g 0 >>angle-diff drop ]
      [ ] if
    key-e read-keyboard keys>> nth 
      [ g 1 >>angle-diff drop ]
      [ ] if
    ;




M:: min-world begin-game-world ( g -- )
    g 0.0 >>angle
      fixed-program <program-instance> >>fixed-program
      0.0 >>angle-diff
      g fixed-program>> <square-vertex-array> >>square-va
      drop
    gl_init
    0 0 g dim>> first2 glViewport
    GL_PROJECTION glMatrixMode
      glLoadIdentity
        0 width - width
        0 height - height
        -100.9 100.0 glOrtho
    open-game-input
    ;

M: min-world end-game-world ( gadget -- )
    close-game-input
    drop ;


GAME: run2 {
  { world-class min-world }
  { title "Minimal" }
  { pixel-format-attributes
    {
      windowed
      double-buffered
      T{ depth-bits { value 24 } }
    }
  }
  { pref-dim { $ width $ height } }
  { tick-interval-nanos $[ 60 fps ] }
} ;






New Annotation

Summary:
Author:
Mode:
Body: