Procedural Texturing

From Croquet Consortium

Jump to: navigation, search

This is an open community project to procedurally build some cool textures for use in the Croquet environment. Plans for this project are still fluid. There are other active projects in the Croquet rendering area that may at some point present code conflicts with this project. We will attempt to make use of the best alternatives available.

The inspiration for the actual procedural textures is largely due to Jerzy Karczmarczuk's Clastic tutorial paper, and so this project is intended to be a library of general functions that can be combined in various ways, rather than a finished set of textures.

If you would like to investigate this project, please also refer to OpenGL in Croquet.

Contributors:

  • Tobias Germer, GLSL support for Croquet Jasmine
  • Joshua Gargus, NullTerminatedStringArray class
  • Bert Freudenberg, COpenGLPlayer class
  • Andreas Raab, COpenGLPanel class
  • Arne Reiners, GLSL noise code
  • Dave Faught, FramebufferObject support

Project gatekeeper: Dave Faught

The Plan

The current Croquet SDK code base will be used for the OpenGL support and Tweak 2D UI support. The framework will put the project strictly in the local processing arena, outside of Croquet's replication and collaboration model, at least for now.

The procedural code will be targetted to run on the Graphics Processor Unit (GPU) of the 3D graphics video card, using the OpenGL Shading Language (GLSL or glslang).

Current Status

The current code for this project is in the Jabberwocky PublicContribs repository in the Tweak-OpenGL package. The three OpenGL packages in the Jabberwocky Jabberwocky repository should be installed first, and it is necessary to manually do "OGLExtManager initialize" after loading them. This also depends on a fix for Mantis #6553, which can temporarily be a pretty trivial do-it-yourself kind of thing. Just change VectorColor#asVector4 so that it returns a Vector4 instance instead of a Vector3.

The current Tweak-OpenGL package contains:

  • Two, yes count them, two(2) different classes for a basic OpenGL display in Tweak. One is a subclass of CPlayer, and the other is a subclass of CPanel. The CPanel version can be launched directly from Morphic, and the CPlayer version needs to be opened in an already-open Tweak environment. There are a few other differences.
  • Two examples of the translated NeHe Tutorial #5 (the first one that has 3D objects), one for each of the basic Tweak OpenGL classes.
  • The beginnings of a GPUinfo query class to display the level of OpenGL support that the video card and drivers supply. Currently this is primarily used to verify GLSL and FramebufferObject support as required by the Wrinkle application.
  • Two versions of the Wrinkle application, the main product of this project. The first version, Wrinkle1, uses a single fragment(pixel) shader program at a time and displays the resulting texture dynamically. The second version, Wrinkle2, is more advanced through combining two dynamic procedural textures and allows saving a snapshot of the result to a .png or .jpg file by use of an OpenGL FramebufferObject.
  • A growing library of procedural textures which can be dynamically colored, scaled and rotated.

Here are a couple of short video clips:

The earlier Wrinkle1 version

The latest Wrinkle2 version

Procedural Code Framework

The simple method, used by Wrinkle1, is:

  • Compile the shaders that will generate the texture.
  • Every render cycle:
    • Enable the shaders.
    • Interactively pass parameters to the running shaders from the Tweak UI.
    • Render an OpenGL geometry with proper texture coordinates to dynamically display the generated texture.

The more advanced method, used by Wrinkle2, is:

  • Create an FBO with an attached texture.
  • Compile the shaders that will generate the texture.
  • Every render cycle:
    • Enable the shaders.
    • Interactively pass parameters to the running shaders from the Tweak UI.
    • Render the shaders into the FBO with texture coordinates.
    • Disable the shaders and FBO.
    • Render an OpenGL geometry to the display with texture coordinates mapped to the FBO's texture in the normal manner.
    • Allow a snapshot of the FBO texture to be saved to a bitmap file, such as png. To get the texture back from the GPU this step uses glReadPixels(), which is syncronous.

Resources and Background

Lumina is a very nice GLSL development IDE. The author has consented to allow the Lumina noise tutorial code to be released under the Croquet License for this project.

Here are a few resources on general procedural textures:

"Texturing and Modeling, A Procedural Approach" by Ebert, et al. is an excellent book on this subject.

Also, check out the procedural texture editor in the Open Source 3D modeler Art Of Illusion. You can use the VRML export feature of AOI and select "Create Image Files for Textures" to export 2D procedural textures that can be imported to Croquet with the VRMLImporter. Take a look at the AOI tutorial, Procedural Texture and Material Editor, Chapter 9, "Example Textures" for an idea what the GUI looks like. Only AOI's textures can be exported to VRML, and end up being both textures and materials in Croquet. In AOI, materials are more like OpenGL shader programs, and cannot be expressed in VRML.

Besides Clastic and the ArtOfIllusion Texture Editor, a couple of other free tools for creating procedural textures are .werkkzeug 3 TE, and mental mill Artist Edition that comes with the NVidia's FX Composer 2 beta 4.

In order to have the GPU run the code for doing procedural textures, a higher level of GPU functionality is required than that needed to run normal Croquet. A recommended minimum GPU chipset is the NVidia GeforceFX series, or the ATI Radeon 9500.

More recent NVidia (Forceware 60 or newer) and ATI (Catalyst 4.5 or newer) drivers include the GLSL compiler within the drivers for Windows and Linux. Apple takes a much larger role in writing drivers for OS X (the Windows and Linux drivers are substantially the same code base, at least for NVIDIA), but finally did finish their glslang support.

The OpenGL FrameBuffer object is a new extension that is platform-independent and is more flexible than PBuffers, but just became generally available in the NVidia Forceware 75 drivers released in June 2005. Not sure exactly which release of ATI drivers contains FrameBuffer object support, but there is support in the October 2005 ATI Radeon SDK.

Michael Kleiber has done some nice extensions to Croquet Jasmine to use OGL shader programs.

Other shader languages that might be considered at some point are Stanford's BrookGPU or UW's Sh.

Some related code

Here is some simple Croquet/Squeak code to generate a one-shot texture and display it. Based on example 1 in Clastic (see above). Does not require any special graphics support beyond running Squeak. This gives an idea of where we're headed, but this is not the method we will use.

  • Define the class AFirstTry as a subclass of Form, and then enter this method in AFirstTry:
 plot1
 	"plot a function"
 	" (AFirstTry extent: 128@128 depth: 32) plot1 asMorph openInWorld "
 	| xmin xmax xincr ymin ymax yincr x y z scaled |
 	xmin := -1.
 	xmax := 1.
 	xincr := (xmax - xmin) / (self width).
 	ymin := -1.
 	ymax := 1.
 	yincr := (ymax - ymin) / (self height).
 	scaled := 0.1.
 
 	1 to: self width do: [ :xpixel |
 		x := (xmin + (xincr * xpixel)) / scaled.
 		1 to: self height do: [ :ypixel |
 			y := (ymin + (yincr * ypixel)) / scaled.
 			z := ((x sin + y) sin + x) sin - (((y sin + x) sin + y) sin).	 " <------<<  the function "
 			self colorAt: xpixel@ypixel put: (Color r: z g: 0 b: (1-z)).
 		].
 	].
 
 "xfun1 (V2 x y) = sin(x+sin(y+sin x)) - sin(y+sin(x+sin y))
  xfun2 (V2 x y) = floor(sin(x+sin(y+sin x))) - 
                  floor(sin(y+sin(x+sin y)))
 "
  • To execute the code, highlight the code in the comment and DoIt.
  • This code generates the following image:

Image:TextureExample.png


A Personal Note From DAF

Using the GPU to do the processing for procedural textures through vertex and pixel/fragment shaders is a current and exciting area of experimentation for many developers inside and outside of the Croquet community. While I am very interested in exploring this area, I am also a bit disappointed that I have to code the actual shader programs in a variant of C, instead of Smalltalk. Maybe someday this can be remedied by an industrious Croquet developer as an embedded language extension. Look at PyGPU, Vertigo or UW's Sh in the links above for ideas.

Views
Personal tools