programming tools for Windows applications development
  Home  |   SwiftForth Archive  |   SwiftX Archive  |

Re: Ecartis command results: SUBSCRIBE

From: Charles Melice <mail_at_forthcad.com>
Date: Sat, 8 Nov 2003 12:04:30 +0100

----- Original Message -----
From: "Roger Levy" <trip_n_save_at_hotmail.com>
To: <sftalk_at_forth.com>
Sent: Friday, November 07, 2003 9:33 PM
Subject: [sftalk] Re: Ecartis command results: SUBSCRIBE

> hi, i dont know how active this list is but i will try this anyway.

I have posted 3 examples last years, in the FAQ. Here is a possible
starting kit.

Charles

{ ==============================================================
OpenGl 2D Animated Windows Charles Melice - 8nov2003

This sample show a simple method to use multiple rendering
contexts to draw in different windows/viewports.

Also expose a technic to exploit the OpenGl GLU tessellation
object to break polygons into triangles. (Basically, OpenGl can
only fill/paint triangles)

NB: This is a variant of a 27apr2002 version, using
    double-buffering for speed.

Exposed words:
--------------
    SET-WINDOW ( x y xsize ysize -- )
    SET-VIEWPORT ( x y xsize ysize -- )
    UPDATE-WINDOW ( -- )

    GLCONTEXT ( hdc -- hrc )
    END-GLCONTEXT ( hrc -- )

    gl-BeginTessPolygon ( -- )
    gl-EndTessPolygon ( -- )
    gl-TessVertex2f ( f: x y -- )
    gl-TessVertex2i ( x y -- )

    CONSOLE-HDC ( -- hdc )

================================================================}

REQUIRES FPMATH

PACKAGE OPENGL-2D

1 [IF] \ uncomment if needed
    100100 CONSTANT GLU_TESS_BEGIN
    100102 CONSTANT GLU_TESS_END
    100101 CONSTANT GLU_TESS_VERTEX
    16384 CONSTANT GL_COLOR_BUFFER_BIT
    3089 CONSTANT GL_SCISSOR_TEST
[THEN]

LIBRARY Glu32.dll
LIBRARY OpenGL32.dll

Function: ChoosePixelFormat ( hdc ppfd -- int )
Function: SetPixelFormat ( hdc iPix ppfd -- flag )
Function: glBegin ( mode -- )
Function: glClear ( mask -- )
Function: glColor3ub ( r b g -- )
Function: glEnable ( mode -- )
Function: glEnd ( -- )
Function: glFlush ( -- )
Function: glScissor ( x y cx cy -- )
Function: glVertex2i ( x y -- )
Function: glVertex3dv ( addr -- )
Function: glViewport ( x y cx cy -- )
Function: SwapBuffers ( hdc -- bool )
Function: gluBeginPolygon ( tess -- )
Function: gluDeleteTess ( tess -- )
Function: gluEndPolygon ( tess -- )
Function: gluNewTess ( -- tess )
Function: gluOrtho2D ( ll lh rl rh bl bh tl th -- )
Function: gluTessCallback ( tess which fn -- )
Function: gluTessVertex ( tess dbl[3] data -- )
Function: wglCreateContext ( hdc -- hrc )
Function: wglDeleteContext ( hrc -- bool )
Function: wglMakeCurrent ( hdc hglrc -- ok )
Function: wglGetCurrentDC ( -- hdc )

PFD_DOUBLEBUFFER
PFD_DRAW_TO_WINDOW OR
PFD_SUPPORT_OPENGL OR CONSTANT PIX-FLAGS

: (SetupPixelFormat) ( hdc -- bool )
>R \ save hdc
    HERE >R HERE 50 ERASE \ clear PIXELFORMATDESCRIPTOR
    50 H, 1 H, PIX-FLAGS , 0 C, 24 C, R> H ! \ 24 bit RGB color
    R@ HERE ChoosePixelFormat DUP -EXIT
    R> SWAP HERE SetPixelFormat ;

: (GLCONTEXT) ( hdc -- hrc )
    DUP (SetupPixelFormat) 0= ABORT" oups"
    DUP ( hdc) wglCreateContext ( hdc hrc)
    SWAP OVER wglMakeCurrent DROP ;

: (END-GLCONTEXT) ( hrc -- )
    0 0 wglMakeCurrent DROP
    wglDeleteContext DROP ;

: f>lh ( f: x -- )( s: -- xl xh )
    PAD DUP F! 2@ SWAP ;

PUBLIC

    : SET-WINDOW ( x y xsize ysize -- )
        THIRD + >R THIRD + SWAP R>
        S>F S>F S>F S>F f>lh f>lh f>lh f>lh gluOrtho2D ;

    : SET-VIEWPORT ( x y xsize ysize -- )
        2OVER 2OVER glViewport glScissor
        GL_SCISSOR_TEST glEnable ;

    : UPDATE-WINDOW ( -- )
        wglGetCurrentDC SwapBuffers DROP ;

PRIVATE

\ Tesselation : split polygon in triangles.

128 CONSTANT MAX-VERTEXES

CREATE TESS-BUFFER ( -- addr ) MAX-VERTEXES 3 * FLOATS ALLOT
TESS-BUFFER VALUE #tessaddr

: !TESSi ( coor -- ) S>F #tessaddr F! 1 FLOATS +TO #tessaddr ;
: !TESSf ( f: coor -- ) #tessaddr F! 1 FLOATS +TO #tessaddr ;

0 VALUE #tess
0 VALUE #contextcount

: OPEN-TESSELATOR ( -- )
    #contextcount 1 +TO #contextcount ?EXIT
    gluNewTess TO #tess
    [ IMPORTS +ORDER ]
    #tess GLU_TESS_BEGIN ['] glBegin >BODY @ gluTessCallback
    #tess GLU_TESS_END ['] glEnd >BODY @ gluTessCallback
    #tess GLU_TESS_VERTEX ['] glVertex3dv >BODY @ gluTessCallback
    [ IMPORTS -ORDER ] ;

: CLOSE-TESSELATOR ( -- )
    -1 +TO #contextcount #contextcount ?EXIT
    #tess gluDeleteTess
    0 TO #tess ;

PUBLIC

    : gl-TessVertex2i ( x y -- )
        SWAP !TESSi !TESSi 0 !TESSi
        #tess #tessaddr 3 FLOATS - DUP gluTessVertex ;

    : gl-TessVertex2f ( f: x y -- )
        FSWAP !TESSf !TESSf 0e !TESSf
        #tess #tessaddr 3 FLOATS - DUP gluTessVertex ;

    : gl-BeginTessPolygon ( -- )
        TESS-BUFFER TO #tessaddr
        #tess gluBeginPolygon ;

    : gl-EndTessPolygon ( -- )
        #tess gluEndPolygon ;

    : GLCONTEXT ( hdc -- hrc )
        (GLCONTEXT) OPEN-TESSELATOR ;

    : END-GLCONTEXT ( hrc -- )
        CLOSE-TESSELATOR (END-GLCONTEXT) ;

    \ experimentation tool
    : CONSOLE-HDC ( -- hdc )
        HWND Z" TTY" GetProp GetDC ;

END-PACKAGE

\ -------------------TEST-------------------

REQUIRES RND

0 VALUE hrc1
0 VALUE hrc2

\ TEST use 2 contexts

: DRAWING-LOOP ( hdc -- )
>R
    0
    BEGIN KEY? 0= WHILE
        \ swap context based on counter parity
        1+ DUP 1 AND IF hrc1 ELSE hrc2 THEN
        R@ SWAP wglMakeCurrent DROP
        \ fill 100 tesselated/clipped random polygons
        255 RND 255 RND 255 RND glColor3ub
        GL_COLOR_BUFFER_BIT glClear
        100 0 DO
            gl-BeginTessPolygon
                255 RND 255 RND 255 RND glColor3ub
                5 0 DO 150 RND 150 RND gl-TessVertex2i LOOP
            gl-EndTessPolygon
        LOOP
        UPDATE-WINDOW 250 MS
    REPEAT
    R> 2DROP ;

: TEST ( -- )
    \ create 2 contexts
    CONSOLE-HDC DUP GLCONTEXT TO hrc1
        20 20 300 300 SET-VIEWPORT
        10 10 100 100 SET-WINDOW
        ( hdc) DUP GLCONTEXT TO hrc2
            100 100 300 300 SET-VIEWPORT
            10 10 100 100 SET-WINDOW
            ( hdc) DRAWING-LOOP
        hrc1 END-GLCONTEXT
    hrc2 END-GLCONTEXT ;

\ TEST1 use 1 context

: DRAWING-LOOP1 ( hdc -- )
    hrc1 wglMakeCurrent DROP
    BEGIN KEY? 0= WHILE
        \ fill 100 tesselated/clipped random polygons
        255 RND 255 RND 255 RND glColor3ub
        GL_COLOR_BUFFER_BIT glClear
        100 0 DO
            gl-BeginTessPolygon
                255 RND 255 RND 255 RND glColor3ub
                5 0 DO 150 RND 150 RND gl-TessVertex2i LOOP
            gl-EndTessPolygon
        LOOP
        UPDATE-WINDOW 250 MS
    REPEAT ;

: TEST1 ( -- )
    \ create 1 context
    CONSOLE-HDC dup GLCONTEXT TO hrc1
        20 20 300 300 SET-VIEWPORT
        10 10 100 100 SET-WINDOW
        ( hdc) DRAWING-LOOP1
    hrc1 END-GLCONTEXT ;

.( Type TEST or TEST1, <ESC> to stop animation.
)

\\ eof

----------------------------------------------------------------------
sftalk_at_forth.com The SwiftForth programming discussion email list
To unsubscribe, send subject "unsubscribe" to sftalk-request_at_forth.com
For list command help, send subject "help" to sftalk-request_at_forth.com
Message archives are located at http://www.forth.com/archive/sftalk
----------------------------------------------------------------------
This list is a forum for SwiftForth users. For product support and bug
reports, please send email to support_at_forth.com
----------------------------------------------------------------------
Received on Sat Nov 08 2003 - 03:12:08 PST

This archive was generated by hypermail 2.2.0 : Tue Dec 02 2008 - 03:04:34 PST