From: Jos v.d.V. <jo...@us...> - 2015-12-04 19:08:45
|
Update of /cvsroot/win32forth/win32forth/demos/OpenGLdemos In directory sfp-cvs-1.v30.ch3.sourceforge.com:/tmp/cvs-serv12023 Added Files: OpenGlLessons.f Readme.txt opengllib-1.11.f tim.bmp Log Message: Jos: The last demo. --- NEW FILE: opengllib-1.11.f --- \ =================================================================== \ File: opengllib-1.11.fs \ Author: Bosco \ Linux Version: Ti Leggett \ gForth Version: Timothy Trussell, 07/25/2010 \ Description: Flag effect (waving texture) \ Forth System: gforth-0.7.0 \ Linux System: Ubuntu v10.04 LTS i386, kernel 2.6.31-23 \ C++ Compiler: gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) \ =================================================================== \ NeHe Productions \ http://nehe.gamedev.net/ \ =================================================================== \ OpenGL Tutorial Lesson 11 \ =================================================================== \ This code was created by Bosco \ ported to Linux/SDL by Ti Leggett \ March, 2013 adapted for Win32Forth by Jos v.d.Ven \ Visit Jeff at http://nehe.gamedev.net/ \ =================================================================== s" src\lib\OpenGl" "fpath+ \ For OpenGL support needs opengl.f \ The OpenGl wrapper and many tools menubar Openglmenu \ Define a menu for the OpenGL window popup "&File" menuitem "E&xit" bye ; endbar needs oglwin.f \ The OpenGL window needs Helpform.f \ For the help window needs OpenGlLessons.f \ To support this lesson vocabulary Lesson11 also Lesson11 definitions \ ---[ Variable Declarations ]--------------------------------------- -1e facos fconstant pi PI 2e F* fconstant 2PI \ common calc fvariable x-rot fvariable y-rot fvariable z-rot 0 value wiggle-count \ how fast the flag waves \ Need to create a [45][45][3] fp array here \ These can be fixed as 8-byte fp values, as they are sent to the \ OpenGL system via the gl-vertex-3f call, not by passing the address \ of the array. \ One way of thinking of this is as (3) (45x45) arrays \ So, x and y have a range of [0..44], and z has a range of [0..2] \ +-------+ |---x ---| \ | |-+ +--------+ -+- \ | | |-+ | | | \ | z=0| | | | | y \ +-------+1| | | | | \ +-------+2| +--------+ -+- \ +-------+ create points[] here 45 floats 45 * 3 * dup allot 0 fill \ ---[ Array Index Functions ]--------------------------------------- \ Index functions to access the arrays : points-ndx { _x _y _z -- *points[x][y][z] } points[] \ *points[] \ calculate row of the y coordinate _y 45 floats * \ *points[] yofs \ calculate column of the x coordinate _x floats + \ *points[] yofs+xofs \ calculate page of the z coordinate _z 45 floats 45 * * + \ *points[] yofs+xofs+zofs + \ *points[yofs+xofs+zofs] ; \ ---[ LoadGLTextures ]---------------------------------------------- : LoadGLTextures ( -- status ) \ create variables for storing surface pointers and return flag 1 MallocTextures \ MallocTextures allocates only when not done NumTextures texture[] gl-gen-textures \ create the textures \ Attempt to load the texture images by using a mapping s" tim.bmp" 0 ahndl LoadGLTexture \ ndx = 0 true \ exit -1=ok OR abort in LoadGLTexture ; \ ---[ HandleKeyPress ]---------------------------------------------- \ function to handle key press events: :long$ h11$ $| About lesson 11: $| $| Key-list for the available functions in this lesson: $| $| ESC exits the lesson $| w toggles between fullscreen and windowed modes ;long$ \ ---[ HandleKeyPress ]---------------------------------------------- \ function to handle key press events : HandleKeyPress ( VK_key -- ) ascii W = if start/end-fullscreen \ Starts of end the full screen else h11$ ShowHelp \ Show the help text for this lesson then ; \ ---[ Set the viewpoint ]------------------------------------------- : set-viewpoint ( -- ) \ the call to glViewport is done in Opengl.f GL_PROJECTION gl-matrix-mode \ Reset the matrix gl-load-identity \ Set our perspective - the F/ calcs the aspect ratio of w/h 45e widthViewport S>F heightViewport S>F F/ 0.1e 100e glu-perspective \ Make sure we are changing the model view and not the projection GL_MODELVIEW gl-matrix-mode ; \ ---[ InitGL ]------------------------------------------------------ \ general OpenGL initialization function : InitGL ( -- ) \ Load in the texture LoadGLTextures drop \ Enable texture mapping GL_TEXTURE_2D gl-enable \ Enable smooth shading GL_SMOOTH gl-shade-model \ Set the background black 0e 0e 0e 0.5e gl-clear-color \ Depth buffer setup 1e gl-clear-depth \ Enable depth testing GL_DEPTH_TEST gl-enable \ Type of depth test to do GL_LEQUAL gl-depth-func \ Really nice perspective calculations GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST gl-hint \ Fill the back with texture; the front will only be wireline GL_BACK GL_FILL gl-polygon-mode GL_FRONT GL_LINE gl-polygon-mode \ Apply the wave to our mesh array 45 0 do 45 0 do j S>F 5e F/ 4.5e F- j i 0 points-ndx F! i S>F 5e F/ 4.5e F- j i 1 points-ndx F! j S>F 5e F/ 40e F* 360e F/ 2PI F* FSIN j i 2 points-ndx F! loop loop ; \ ---[ DrawGLScene ]------------------------------------------------- \ Here goes our drawing code fvariable f-x \ use to break the flag into tiny quads fvariable f-y fvariable f-xb fvariable f-yb : DrawGLScene ( -- ) \ Clear the screen and the depth buffer GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT OR gl-clear gl-load-identity \ restore matrix \ Translate 17 units into the screen 0e 0e -17e gl-translate-f \ Rotate on the x/y/z axes x-rot F@ 1e 0e 0e gl-rotate-f y-rot F@ 0e 1e 0e gl-rotate-f z-rot F@ 0e 0e 1e gl-rotate-f \ Select our texture GL_TEXTURE_2D 0 texture-ndx @ gl-bind-texture \ Start drawing our quads GL_QUADS gl-begin 44 0 do 44 0 do j S>F 44e F/ f-x F! \ Create a fp x value i S>F 44e F/ f-y F! \ Create a fp y value j 1+ S>F 44e F/ f-xb F! \ Create x+0.0227e value i 1+ S>F 44e F/ f-yb F! \ Create y+0.0227e value \ Bottom Left texture coordinate f-x F@ f-y F@ gl-tex-coord-2f j i 0 points-ndx F@ j i 1 points-ndx F@ j i 2 points-ndx F@ gl-vertex-3f \ Top left texture coordinate f-x F@ f-yb F@ gl-tex-coord-2f j i 1+ 0 points-ndx F@ j i 1+ 1 points-ndx F@ j i 1+ 2 points-ndx F@ gl-vertex-3f \ Top Right texture coordinate f-xb F@ f-yb F@ gl-tex-coord-2f j 1+ i 1+ 0 points-ndx F@ j 1+ i 1+ 1 points-ndx F@ j 1+ i 1+ 2 points-ndx F@ gl-vertex-3f \ Bottom Right texture coordinate f-xb F@ f-y F@ gl-tex-coord-2f j 1+ i 0 points-ndx F@ j 1+ i 1 points-ndx F@ j 1+ i 2 points-ndx F@ gl-vertex-3f loop loop gl-end \ Used to slow down the wave (every 2nd frame only) wiggle-count 1 > if 45 0 do 0 i 2 points-ndx F@ 44 0 do i 1+ j 2 points-ndx F@ i j 2 points-ndx F! loop 44 i 2 points-ndx F! loop 0 to wiggle-count \ set back to zero then wiggle-count 1+ to wiggle-count \ Draw it to the screen sdl-gl-swap-buffers x-rot F@ 0.3e F+ x-rot F! \ increment x rotation y-rot F@ 0.2e F+ y-rot F! \ increment y rotation z-rot F@ 0.4e F+ z-rot F! \ increment z rotation ; : _exitLesson ( -- ) \ For a clean start in the next lesson DeleteTextures true to resizing? ; : ResetLesson ( -- ) \ For a clean start in this lesson ResetOpenGL \ Cleanup from a previous lesson InitGL \ Enable some features and load textures set-viewpoint \ Set the viewpoint false to resizing? ; also Forth definitions : DrawGLLesson11 ( -- ) \ Handles ONE frame LessonChanged? if false to LessonChanged? ['] HandleKeyPress is KeyboardAction \ Use the keystrokes for this lesson only ['] _ExitLesson is ExitLesson \ Specify ExitLesson to free allocated memory Reset-request-to-stop then resizing? if ResetLesson then DrawGLScene \ Redraw only the changes in the lesson ProcesKeyAndRelease \ HandleKeyPress only here ; : StartGLLesson11 ( -- ) Start: OpenGLWindow DrawGLLesson11 ['] DrawGLScene is painting begin DrawGLLesson11 request-to-stop until ; StartGLLesson11 \s --- NEW FILE: Readme.txt --- The following demos can be compiled: 1) OpenGl_Hello.f \ To show how a simple OpenGL drawing can be made in Win32Forth 2) UfoWithRobot.f \ To demonstrate the new 3D turtle. 3) opengllib-1.11.f \ A bit more complicated lesson from NeHe --- NEW FILE: tim.bmp --- (This appears to be a binary file; contents omitted.) --- NEW FILE: OpenGlLessons.f --- \ OpenGlLessons.f \ Adaptations for NeHeLessons aNew -OpenGlLessons.f synonym gl-shade-model glShadeModel synonym gl-clear-depth glClearDepth synonym gl-enable glEnable synonym gl-hint glHint synonym gl-depth-func glDepthFunc synonym gl-clear-color glClearColor synonym gl-clear glClear synonym gl-load-identity glLoadIdentity synonym gl-translate-f glTranslatef synonym gl-begin glBegin synonym gl-vertex-3f glVertex3f synonym gl-end glEnd synonym gl-color-3f glColor3f synonym gl-rotate-f glRotatef synonym gl-tex-image-2d glTexImage2D synonym gl-gen-textures glGenTextures synonym gl-bind-texture glBindTexture synonym gl-tex-parameter-i glTexParameteri synonym gl-tex-coord-2f glTexCoord2f synonym glu-build-2d-mipmaps gluBuild2DMipmaps synonym gl-light-fv glLightfv synonym gl-normal-3f glNormal3f synonym gl-color-4f glColor4f synonym gl-blend-func glBlendFunc synonym gl-color-4ub glColor4ub synonym gl-polygon-mode glPolygonMode synonym gl-gen-lists glGenLists synonym gl-new-list glNewList synonym gl-end-list glEndList synonym gl-color-3fv glColor3fv synonym gl-call-list glCallList synonym gl-delete-lists glDeleteLists synonym gl-fog-i glFogi synonym gl-fog-fv glFogfv synonym gl-fog-f glFogf synonym gl-vertex-2i glVertex2i synonym gl-translate-d glTranslated synonym gl-disable glDisable synonym gl-matrix-mode glMatrixMode synonym gl-push-matrix glPushMatrix synonym gl-ortho glOrtho synonym gl-list-base glListBase synonym gl-call-lists glCallLists synonym gl-pop-matrix glPopMatrix synonym gl-vertex-2f glVertex2f synonym glu-new-quadric gluNewQuadric synonym glu-quadric-normals gluQuadricNormals synonym glu-quadric-texture gluQuadricTexture synonym glu-cylinder gluCylinder synonym glu-disk gluDisk synonym glu-sphere gluSphere synonym glu-partial-disk gluPartialDisk synonym gl-depth-mask glDepthMask synonym gl-color-mask glColorMask synonym gl-stencil-func glStencilFunc synonym gl-front-face glFrontFace synonym gl-stencil-op glStencilOp synonym gl-clear-stencil glClearStencil synonym gl-material-fv glMaterialfv synonym gl-cull-face glCullFace synonym gl-get-float-v glGetFloatv synonym gl-flush glFlush synonym gl-scale-f glScalef synonym gl-color-3ub glColor3ub synonym gl-vertex-2d glVertex2d synonym gl-line-width glLineWidth synonym gl-get-string glGetString synonym gl-scissor glScissor synonym gl-get-integer-v glGetIntegerv synonym gl-tex-gen-i glTexGeni synonym gl-clip-plane glClipPlane synonym gl-pixel-transfer-f glpixeltransferf synonym gl-tex-env-f gltexenvf synonym gl-push-attrib glPushAttrib synonym gl-pop-attrib glPopAttrib synonym gl-raster-pos-2f glRasterPos2f synonym glu-perspective gluPerspective synonym gl-delete-textures glDeleteTextures synonym glu-delete-quadric gluDeleteQuadric defer ExitLesson ' noop is ExitLesson synonym sdl-gl-swap-buffers show-frame -1e facos fconstant pi synonym =: constant true value LessonChanged? : clrTitle$ ( - ) title$ 50 erase ; : NeHe>title ( - ) clrTitle$ s" NeHe lessons in Win32Forth. " title$ place ogl-hwnd retitle-window ; : StaticScene ( - ) NeHe>title true to static-scene ; : DynamicScene ( - ) clrTitle$ s" NeHe lessons in Win32Forth. Fps: " FpsStart$ place false to static-scene ; : SF, ( r -- ) here 1 sfloats allot SF! ; : F-! ( f: fval -- ) ( *fvar -- ) FNEGATE F+! ; : rnd ( -- rnd ) 0xfffffff random ; : set-viewpoint-wf32 ( -- ) \ Needed for the Win32Forth window GL_PROJECTION glMatrixMode \ set viewing to the projection matrix 85e .5e 0.1e 200.0e fref4PersP floatsf@+ gluPerspective 1.1e 2.0e 1.5e fref3ScaleS floatsf@+ glscalef \ To fit it into the window GL_MODELVIEW glMatrixMode \ set viewing to the model matrix \ 2 ; #define VK_NUMPAD+ 107 #define VK_NUMPAD- 109 \s |