Class GLApp

java.lang.Object
  |
  +--GLApp

public class GLApp
extends java.lang.Object

Collection of functions to init and run an OpenGL app using LWJGL.

Includes functions to handle:
Buffer allocation -- manage IntBuffer, ByteBuffer calls.
Setup display mode, keyboard, mouse, native cursor
Run main loop of application
OpenGL functions -- convert screen/world coords, set modes, lights, etc.
Utility functions -- load images, convert pixels, getTimeInMillis, etc.

Has a main() function to run as an application, though this class has only minimal placeholder functionality. It is meant to be subclassed, and the subclass will define more elaborate render() mouseMove() functions, etc.

GLApp initializes the LWJGL environment for OpenGL rendering, ie. creates a window, sets the display mode, inits mouse and keyboard, then runs a loop.

Uses GLImage to load and hold image pixels.

napier a t potatoland d o t org


Field Summary
static float aspectRatio
           
static java.nio.FloatBuffer bufferModelviewMatrix
           
static java.nio.FloatBuffer bufferProjectionMatrix
           
static java.nio.IntBuffer bufferSettingInt
           
static java.nio.IntBuffer bufferViewport
           
static java.nio.ByteBuffer bufferZdepth
           
static int cursorX
           
static int cursorY
           
static int depthBufferBits
           
static int displayColorBits
           
static int displayFrequency
           
static int displayHeight
           
static int displayWidth
           
static DisplayMode DM
           
static boolean finished
           
static int finishedKey
           
static int frameCount
           
static boolean fullScreen
           
static java.lang.String GLAPP_VERSION
           
static DisplayMode origDM
           
static long prevTime
           
static java.util.Properties settings
           
static int SIZE_BYTE
           
static int SIZE_DOUBLE
           
static int SIZE_FLOAT
           
static int SIZE_INT
           
static long ticksPerSecond
           
static float tileFactorHoriz
           
static float tileFactorVert
           
static int viewportH
           
static int viewportW
           
static int viewportX
           
static int viewportY
           
static java.lang.String windowTitle
           
 
Constructor Summary
GLApp()
           
 
Method Summary
static int allocateTexture()
          Allocate a texture (glGenTextures) and return the handle to it.
static java.nio.ByteBuffer allocBytes(byte[] bytearray)
           
static java.nio.ByteBuffer allocBytes(int howmany)
           
static java.nio.DoubleBuffer allocDoubles(int howmany)
           
static java.nio.FloatBuffer allocFloats(float[] floatarray)
           
static java.nio.FloatBuffer allocFloats(int howmany)
           
static java.nio.IntBuffer allocInts(int howmany)
           
static java.nio.IntBuffer allocInts(int[] intarray)
           
static void buildFont(int fontTxtrHandle, int fontWidth)
          Build the character set display list from the given texture.
static void buildFont(java.lang.String charSetImage, int fontWidth)
          Build a character set from the given texture image.
 void cleanup()
          Run() calls this right before exit.
static void destroyFont()
          Clean up the allocated display lists for the character set.
 void drawCursor(int cursorTextureHandle)
          Draw a cursor image textured onto a quad at cursor position.
static void drawImageInt(GLImage img, int x, int y, float scaleW, float scaleY)
          Draw an image at the given x,y.
static void drawImageQuad(int textureHandle, int x, int y, float w, float h)
          Draw an image at the given x,y.
static void drawQuad(int textureHandle, float x, float y, float z, float w, float h)
          Draw an image (loaded into a texture) onto a quad, in 3D model space.
static void frameDraw(int txtrHandle)
          Draw the screen-sized image over entire screen area.
static void frameSave(int txtrHandle)
          Save entire screen image to a texture.
static void frameSave(Pbuffer pbuff, int textureHandle)
          Copy the pbuffer contents to a texture.
static DisplayMode getDisplayMode(int w, int h, int colorBits, int freq)
          Retrieve a DisplayMode object with the given params
static void getFramePixels(int x, int y, GLImage img)
          Get pixels from frame buffer into an existing image.
static GLImage getFramePixels(int x, int y, int w, int h)
          Get pixels from frame buffer.
static java.io.InputStream getInputStream_OLD(java.lang.String filename)
           
static java.io.InputStream getInputStream(java.lang.String filename)
          Open a file InputStream and trap errors.
static float[][] getMatrixAsArray(java.nio.FloatBuffer fb)
          Convert a FloatBuffer matrix to a 4x4 float array.
static java.nio.FloatBuffer getModelviewMatrix()
           
static float[][] getModelviewMatrixA()
          Return the modelview matrix as a 4x4 float array
static int getPowerOfTwoBiggerThan(int dimension)
          Find a power of two big enough to hold the given dimension.
static java.nio.FloatBuffer getProjectionMatrix()
           
static float[][] getProjectionMatrixA()
          Return the projection matrix as a 4x4 float array
static java.lang.String getProperty(java.lang.String propertyName)
          Return a property from the properties file.
static int getSettingInt(int whichSetting)
           
static double getTimeInMillis()
           
static double getTimeInSeconds()
           
static java.nio.IntBuffer getViewport()
           
static int[] getViewportA()
          Return the Viewport data as array of 4 floats
static float[] getWorldCoordsAtScreen(int x, int y)
          For given screen xy, return the world xyz coords in a float array.
static float[] getWorldCoordsAtScreen(int x, int y, float z)
          For given screen xy and z depth, return the world xyz coords in a float array.
static float getZDepth(int x, int y)
          Return the Z depth of the single pixel at the given screen position.
static float getZDepthAtOrigin()
          Find the Z depth of the origin in the projected world view.
static void glPrint(int x, int y, int set, java.lang.String msg)
          Render a text string onto the screen, using the character set created by buildCharSet().
static void glText(float x, float y, float z, int set, float scale, java.lang.String msg)
          Render a text string in model space, using the character set created by buildCharSet().
 void init()
          The three init functions below must be called to get the display, mouse and OpenGL context ready for use.
 void initBuffers()
          For memory efficiency and performance, it's best to init NIO buffers once and reuse, particularly for calls that may be repeated often.
static boolean initDisplay()
          Initialize the Display mode, viewport size, and open a Window.
static void initInput()
          Initialize the Keyboard and Mouse.
 void initOpenGL()
          Called by GLApp.run() to initialize OpenGL rendering.
 void keyDown(int keycode)
           
 void keyUp(int keycode)
           
static GLImage loadImage(java.lang.String imgFilename)
          Load an image from the given file and return a GLImage object.
static java.nio.IntBuffer loadImageInt(java.lang.String imgFilename)
          Load an image from the given file and return an IntBuffer containing ARGB pixels.
Can be used to create Native Cursors.
static java.nio.ByteBuffer loadImagePixels(java.lang.String imgFilename)
          Load an image from the given file and return a ByteBuffer containing RGBA pixels.
Can be used to create textures.
 void loadSettings(java.lang.String configFilename)
          Load configuration settings from optional properties file.
static void main(java.lang.String[] args)
           
 void mainLoop()
          Called by the run() loop.
static GLImage makeImage(int w, int h)
          Make a blank image of the given size
static Cursor makeNativeCursor(java.lang.String imgFilename)
          Create a native cursor from the given image file.
My cursor motion doesn't work with native cursor since verion .95 I'll have to fix someday.
 Pbuffer makePbuffer(int width, int height)
          Create a Pbuffer for use as an offscreen buffer, with the given width and height.
static void makeSphere()
          Create a Display List of a sphere.
static int makeTexture(java.nio.ByteBuffer pixels, int w, int h)
          Create a texture from the given pixels in RGBA format.
static int makeTexture(GLImage textureImg)
          Create a texture from the given image.
static int makeTextureForScreen(int screenSize)
          Create a texture large enough to hold the screen image.
static int makeTextureMipMap(GLImage textureImg)
          Build Mipmaps for texture (builds different versions of the picture for distances - looks better)
 java.lang.String makeTimestamp()
          make a time stamp for filename
 void mouseDown(int x, int y)
           
 void mouseMove(int x, int y)
          Called by mainLoop() when mouse moves
 void mouseUp(int x, int y)
           
static void project(float x, float y, float z, float[] resultf)
          Return screen coordinates for a given point in world space.
 void render()
          Called by mainLoop() to render one frame.
static void renderCube()
          Render a unit cube, using current color, texture and material.
static void renderSphere()
          Render a unit sphere at the origin, using current color, texture and material.
 void run()
          Runs the application.
static void screenShot(int width, int height, java.lang.String saveFilename)
          Save the contents of the current render buffer to a PNG image.
 void selectDisplay()
          Make the Display the current context for OpenGL commands.
 Pbuffer selectPbuffer(Pbuffer pb)
          Make the pbuffer the current context for opengl commands.
static void setAmbientLight(float[] ambientLightColor)
          Set the color of the Global Ambient Light.
static void setFramePixels(GLImage img)
          Write pixels to frame buffer from an existing image.
static void setLight(int GLLightHandle, float[] diffuseLightColor, float[] ambientLightColor, float[] position)
          Set the color of a 'positional' light (a light that has a specific position within the scene).
static void setOrtho()
          Set OpenGL to render in flat 2D (no perspective).
static void setOrthoOff()
          Turn 2D mode off.
static void setOrthoOn()
          Set OpenGL to render in flat 2D (no perspective) on top of current scene.
static void setPerspective()
          Set OpenGL to render in 3D perspective.
static void setSpotLight(int GLLightHandle, float[] diffuseLightColor, float[] ambientLightColor, float[] position, float[] direction, float cutoffAngle)
           
static void setTextureTile(float horizontalTile, float verticalTile)
          How many times to repeat texture horizontally and vertically.
static void unProject(float x, float y, float z, float[] resultf)
          Return world coordinates for a given point on the screen.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

GLAPP_VERSION

public static final java.lang.String GLAPP_VERSION
See Also:
Constant Field Values

SIZE_DOUBLE

public static final int SIZE_DOUBLE
See Also:
Constant Field Values

SIZE_FLOAT

public static final int SIZE_FLOAT
See Also:
Constant Field Values

SIZE_INT

public static final int SIZE_INT
See Also:
Constant Field Values

SIZE_BYTE

public static final int SIZE_BYTE
See Also:
Constant Field Values

settings

public static java.util.Properties settings

finishedKey

public static int finishedKey

finished

public static boolean finished

cursorX

public static int cursorX

cursorY

public static int cursorY

prevTime

public static long prevTime

frameCount

public static int frameCount

ticksPerSecond

public static long ticksPerSecond

windowTitle

public static java.lang.String windowTitle

DM

public static DisplayMode DM

origDM

public static DisplayMode origDM

displayWidth

public static int displayWidth

displayHeight

public static int displayHeight

displayColorBits

public static int displayColorBits

displayFrequency

public static int displayFrequency

depthBufferBits

public static int depthBufferBits

fullScreen

public static boolean fullScreen

aspectRatio

public static float aspectRatio

viewportX

public static int viewportX

viewportY

public static int viewportY

viewportW

public static int viewportW

viewportH

public static int viewportH

tileFactorVert

public static float tileFactorVert

tileFactorHoriz

public static float tileFactorHoriz

bufferSettingInt

public static java.nio.IntBuffer bufferSettingInt

bufferViewport

public static java.nio.IntBuffer bufferViewport

bufferModelviewMatrix

public static java.nio.FloatBuffer bufferModelviewMatrix

bufferProjectionMatrix

public static java.nio.FloatBuffer bufferProjectionMatrix

bufferZdepth

public static java.nio.ByteBuffer bufferZdepth
Constructor Detail

GLApp

public GLApp()
Method Detail

main

public static void main(java.lang.String[] args)

run

public void run()
Runs the application. Calls init() functions to setup OpenGL, input and display. Runs the main loop of the application, which handles mouse and keyboard input.

Calls: init() and mainLoop().
mainLoop() calls: render(), mouseMove(), mouseDown(), mouseUp(), keyDown(), keyUp()


mainLoop

public void mainLoop()
Called by the run() loop. Handles animation and input for each frame.

Currently only works with Mouse.setGrabbed(true) (see initInput()) This means the native cursor is hidden and app has to draw its own.


loadSettings

public void loadSettings(java.lang.String configFilename)
Load configuration settings from optional properties file. File format is:
 # Comment
 displayWidth=1024
 displayHeight=768
 

Parameters:
configFilename -

getProperty

public static java.lang.String getProperty(java.lang.String propertyName)
Return a property from the properties file. This file is optional, so properties may be empty.

See Also:
loadSettings(String)

getInputStream

public static java.io.InputStream getInputStream(java.lang.String filename)
Open a file InputStream and trap errors.

Returns:
InputStream

getInputStream_OLD

public static java.io.InputStream getInputStream_OLD(java.lang.String filename)

init

public void init()
The three init functions below must be called to get the display, mouse and OpenGL context ready for use. Override init() in a subclass to customize behavior.


initBuffers

public void initBuffers()
For memory efficiency and performance, it's best to init NIO buffers once and reuse, particularly for calls that may be repeated often. Allocate NIO buffers here, use them in getSetingInt(), getModelviewMatrix(), etc.


initOpenGL

public void initOpenGL()
Called by GLApp.run() to initialize OpenGL rendering.

Throws:
java.lang.Exception

render

public void render()
Called by mainLoop() to render one frame. Subclass will override this.


mouseMove

public void mouseMove(int x,
                      int y)
Called by mainLoop() when mouse moves


mouseDown

public void mouseDown(int x,
                      int y)

mouseUp

public void mouseUp(int x,
                    int y)

keyDown

public void keyDown(int keycode)

keyUp

public void keyUp(int keycode)

cleanup

public void cleanup()
Run() calls this right before exit.


initDisplay

public static boolean initDisplay()
Initialize the Display mode, viewport size, and open a Window. By default the window is fullscreen, the viewport is the same dimensions as the window.


initInput

public static void initInput()
Initialize the Keyboard and Mouse.

Set Mouse.setGrabbed(true). This hides the native cursor and enables the Mouse.getDX() and Mouse.getDY() functions so we can track mouse motion ourselves.

See Also:
for mouse motion tracking, to draw our own cursor on screen

getDisplayMode

public static DisplayMode getDisplayMode(int w,
                                         int h,
                                         int colorBits,
                                         int freq)
Retrieve a DisplayMode object with the given params


getSettingInt

public static int getSettingInt(int whichSetting)

getModelviewMatrix

public static java.nio.FloatBuffer getModelviewMatrix()

getProjectionMatrix

public static java.nio.FloatBuffer getProjectionMatrix()

getViewport

public static java.nio.IntBuffer getViewport()

getMatrixAsArray

public static float[][] getMatrixAsArray(java.nio.FloatBuffer fb)
Convert a FloatBuffer matrix to a 4x4 float array.

Parameters:
fb - FloatBuffer containing 16 values of 4x4 matrix
Returns:
2 dimensional float array

getModelviewMatrixA

public static float[][] getModelviewMatrixA()
Return the modelview matrix as a 4x4 float array


getProjectionMatrixA

public static float[][] getProjectionMatrixA()
Return the projection matrix as a 4x4 float array


getViewportA

public static int[] getViewportA()
Return the Viewport data as array of 4 floats


getZDepth

public static float getZDepth(int x,
                              int y)
Return the Z depth of the single pixel at the given screen position.


getZDepthAtOrigin

public static float getZDepthAtOrigin()
Find the Z depth of the origin in the projected world view. Used by getWorldCoordsAtScreen() Projection matrix must be active for this to return correct results (GL.glMatrixMode(GL.GL_PROJECTION)). For some reason I have to chop this to four decimals or I get bizarre results when I use the value in project().


project

public static void project(float x,
                           float y,
                           float z,
                           float[] resultf)
Return screen coordinates for a given point in world space. The world point xyz is 'projected' into screen coordinates using the current model and projection matrices, and the current viewport settings.

Parameters:
x - world coordinates
y -
z -
resultf - the screen coordinate as an array of 3 floats

unProject

public static void unProject(float x,
                             float y,
                             float z,
                             float[] resultf)
Return world coordinates for a given point on the screen. The screen point xyz is 'un-projected' back into world coordinates using the current model and projection matrices, and the current viewport settings.

Parameters:
x - screen x position
y - screen y position
z - z-buffer depth position
resultf - the world coordinate as an array of 3 floats
See Also:
getWorldCoordsAtScreen(int,int)

getWorldCoordsAtScreen

public static float[] getWorldCoordsAtScreen(int x,
                                             int y)
For given screen xy, return the world xyz coords in a float array. Assume Z position is 0.


getWorldCoordsAtScreen

public static float[] getWorldCoordsAtScreen(int x,
                                             int y,
                                             float z)
For given screen xy and z depth, return the world xyz coords in a float array.


allocateTexture

public static int allocateTexture()
Allocate a texture (glGenTextures) and return the handle to it.


makeTexture

public static int makeTexture(GLImage textureImg)
Create a texture from the given image.


setTextureTile

public static void setTextureTile(float horizontalTile,
                                  float verticalTile)
How many times to repeat texture horizontally and vertically.


makeTexture

public static int makeTexture(java.nio.ByteBuffer pixels,
                              int w,
                              int h)
Create a texture from the given pixels in RGBA format. Set the texture to repeat in both directions and use LINEAR for magnification.

Returns:
the texture handle

makeTextureMipMap

public static int makeTextureMipMap(GLImage textureImg)
Build Mipmaps for texture (builds different versions of the picture for distances - looks better)

Parameters:
textureImg - the texture image
Returns:
error code of buildMipMap call

makeTextureForScreen

public static int makeTextureForScreen(int screenSize)
Create a texture large enough to hold the screen image. Use RGBA format to insure colors are copied exactly. Use GL_NEAREST for magnification to prevent slight blurring of image when screen is drawn back.

See Also:
frameSave(int), frameDraw(int)

getPowerOfTwoBiggerThan

public static int getPowerOfTwoBiggerThan(int dimension)
Find a power of two big enough to hold the given dimension. Ie. to make a texture big enough to hold a screen image for an 800x600 screen getPowerOfTwoBiggerThan(800) will return 1024.

Parameters:
dimension -
Returns:
a power of two bigger than the given dimension
See Also:
makeTextureForScreen(int)

setPerspective

public static void setPerspective()
Set OpenGL to render in 3D perspective.


setOrtho

public static void setOrtho()
Set OpenGL to render in flat 2D (no perspective). This creates a one-to-one relation between screen pixel and rendered pixel, ie. if you draw a 10x10 quad at 100,100 it will appear as a 10x10 pixel square on screen at pixel position 100,100.

ABOUT Ortho and Viewport:
--------------------------
Let's say we're drawing in 2D and want to have a cinema proportioned viewport (16x9), and want to bound our 2D rendering into that area ie.

          ___________1024,576
         |           |
         |  Scene    |      Set the bounds on the scene geometry
         |___________|      to the viewport size and shape
      0,0

          ___________1024,576
         |           |
         |  Ortho    |      Set the projection to cover the same
         |___________|      area as the scene
      0,0

          ___________ 1024,768
         |___________|
         |           |1024,672
         |  Viewport |      Set the viewport to the same shape
     0,96|___________|      as scene and ortho, but centered on
         |___________|      screen.
      0,0
     


setOrthoOn

public static void setOrthoOn()
Set OpenGL to render in flat 2D (no perspective) on top of current scene. Preserve current projection and model views, and disable depth testing. Once Ortho is On, glTranslate() will take pixel coords as arguments, with the lower left corner 0,0 and the upper right corner 1024,768 (or whatever your screen size is).

See Also:
setOrthoOff()

setOrthoOff

public static void setOrthoOff()
Turn 2D mode off. Return the projection and model views to their preserved state that was saved when setOrthoOn() was called, and enable depth testing.

See Also:
setOrthoOn()

setLight

public static void setLight(int GLLightHandle,
                            float[] diffuseLightColor,
                            float[] ambientLightColor,
                            float[] position)
Set the color of a 'positional' light (a light that has a specific position within the scene).
Pass in an OpenGL light number (GL11.GL_LIGHT1), the 'Diffuse' and 'Ambient' colors (direct light and reflected light), and the position.

Parameters:
GLLightHandle -
diffuseLightColor -
ambientLightColor -
position -

setSpotLight

public static void setSpotLight(int GLLightHandle,
                                float[] diffuseLightColor,
                                float[] ambientLightColor,
                                float[] position,
                                float[] direction,
                                float cutoffAngle)

setAmbientLight

public static void setAmbientLight(float[] ambientLightColor)
Set the color of the Global Ambient Light. Affects all objects in scene regardless of their placement.


getTimeInSeconds

public static double getTimeInSeconds()

getTimeInMillis

public static double getTimeInMillis()

loadImage

public static GLImage loadImage(java.lang.String imgFilename)
Load an image from the given file and return a GLImage object.

Parameters:
imgFilename -
Returns:
GLImage of the given image file

loadImagePixels

public static java.nio.ByteBuffer loadImagePixels(java.lang.String imgFilename)
Load an image from the given file and return a ByteBuffer containing RGBA pixels.
Can be used to create textures.

Parameters:
imgFilename -
Returns:
ByteBuffer containing pixels from the given image

loadImageInt

public static java.nio.IntBuffer loadImageInt(java.lang.String imgFilename)
Load an image from the given file and return an IntBuffer containing ARGB pixels.
Can be used to create Native Cursors.

Parameters:
imgFilename -
Returns:
IntBuffer

drawImageInt

public static void drawImageInt(GLImage img,
                                int x,
                                int y,
                                float scaleW,
                                float scaleY)
Draw an image at the given x,y. If scale values are not 1, then scale the image. See loadImage().


drawCursor

public void drawCursor(int cursorTextureHandle)
Draw a cursor image textured onto a quad at cursor position. The cursor image must be loaded into a texture, then this function can be called after scene is drawn. Uses glPushAttrib() to preserve the current drawing state. glPushAttrib() may slow performance down, so in your app you may want to set the states yourself before calling drawCursor() and take the push/pop out of here.

See mainLoop() for cursorX cursorY and mouse motion handling.

Example:

    GLImage cursorImg = loadImage("images/cursorCrosshair32.gif");  // cursor image must be 32x32
    int cursorCrosshairTxtr = makeTexture(cursorImg);

    public void render() {
        // render scene
        ...
        drawCursor(cursorCrosshairTxtr);
    }
 

Parameters:
cursorTextureHandle - handle to texture containing 32x32 cursor image

drawImageQuad

public static void drawImageQuad(int textureHandle,
                                 int x,
                                 int y,
                                 float w,
                                 float h)
Draw an image at the given x,y. Scale the image to the given w,h. The image must be loaded into the texture pointed to by textureHandle. It will be drawn in 2D on top of the current scene.


drawQuad

public static void drawQuad(int textureHandle,
                            float x,
                            float y,
                            float z,
                            float w,
                            float h)
Draw an image (loaded into a texture) onto a quad, in 3D model space.


makeNativeCursor

public static Cursor makeNativeCursor(java.lang.String imgFilename)
Create a native cursor from the given image file.
My cursor motion doesn't work with native cursor since verion .95 I'll have to fix someday. I prefer drawing cursor as a quad anyway, so this is low priority.

Parameters:
imgFilename -
Returns:
Cursor
See Also:
drawCursor(int)

getFramePixels

public static GLImage getFramePixels(int x,
                                     int y,
                                     int w,
                                     int h)
Get pixels from frame buffer.


getFramePixels

public static void getFramePixels(int x,
                                  int y,
                                  GLImage img)
Get pixels from frame buffer into an existing image.


setFramePixels

public static void setFramePixels(GLImage img)
Write pixels to frame buffer from an existing image.
Need to switch Projection matrix to ortho (2D) since Raster Position is transformed in space just like a vertex. Steps: switch to 2D, set the draw position to lower left, draw the image into Back and Front buffers.
Draw into both buffers so they both have same image (otherwise you may get flicker as front and back buffers have different images). This is an issue only when you're not clearing the screen before drawing each frame, ie. when leaving trails as objects move.


frameSave

public static void frameSave(int txtrHandle)
Save entire screen image to a texture. Will copy entire screen even if a viewport is in use. Texture param must be large enough to hold screen image (see makeTextureForScreen()).

Parameters:
txtrHandle - texture where screen image will be stored
See Also:
frameDraw(int), makeTextureForScreen(int)

frameDraw

public static void frameDraw(int txtrHandle)
Draw the screen-sized image over entire screen area. The screen image is stored in the given texture at 0,0 (see frameSave()) and has the same dimensions as the current display mode (DM.getWidth(), DM.getHeight()).

Reset the viewport and ortho mode to full screen (viewport may be different proportion than screen if custom aspectRatio is set). Draw the quad the same size as texture so no stretching or compression of image.

Parameters:
txtrHandle -

makeImage

public static GLImage makeImage(int w,
                                int h)
Make a blank image of the given size


renderCube

public static void renderCube()
Render a unit cube, using current color, texture and material.


renderSphere

public static void renderSphere()
Render a unit sphere at the origin, using current color, texture and material. This function calls makeSphere() to create the sphere in a display list.


makeSphere

public static void makeSphere()
Create a Display List of a sphere. Sphere will be at origin, will have radius of 1 unit, and will have 24 segments x 24 slices.

The LWJGL sphere has it's poles aligned horizontally. I rotate 90 degrees around the X axis to get the poles of the sphere vertical. Just a preference, not critical.

Display Lists store opengl rendering commands. In this example the opengl commands to render a sphere are stored into a display list for future drawing. This approach is often used to increase performance.

See Also:
to draw the display list

buildFont

public static void buildFont(java.lang.String charSetImage,
                             int fontWidth)
Build a character set from the given texture image.

Parameters:
charSetImage - texture image containing 256 characters in a 16x16 grid
fontWidth - how many pixels to allow per character on screen
See Also:
destroyFont()

buildFont

public static void buildFont(int fontTxtrHandle,
                             int fontWidth)
Build the character set display list from the given texture. Creates one quad for each character, with one letter textured onto each quad. Assumes the texture is a 256x256 image containing every character of the charset arranged in a 16x16 grid. Each character is 16x16 pixels. Call destroyFont() to release the display list memory. Should be in ORTHO (2D) mode to render text (see setOrtho()). Special thanks to NeHe and Giuseppe D'Agata for the "2D Texture Font" tutorial (http://nehe.gamedev.net).

Parameters:
fontWidth - how many pixels to allow per character on screen
See Also:
destroyFont()

destroyFont

public static void destroyFont()
Clean up the allocated display lists for the character set.


glPrint

public static void glPrint(int x,
                           int y,
                           int set,
                           java.lang.String msg)
Render a text string onto the screen, using the character set created by buildCharSet().


glText

public static void glText(float x,
                          float y,
                          float z,
                          int set,
                          float scale,
                          java.lang.String msg)
Render a text string in model space, using the character set created by buildCharSet().


makePbuffer

public Pbuffer makePbuffer(int width,
                           int height)
Create a Pbuffer for use as an offscreen buffer, with the given width and height. Use selectPbuffer() to make the pbuffer the context for all subsequent opengl commands. Use selectDisplay() to make the Display the context for opengl commands.

Parameters:
width -
height -
Returns:
Pbuffer
See Also:
selectPbuffer(Pbuffer), selectDisplay()

selectPbuffer

public Pbuffer selectPbuffer(Pbuffer pb)
Make the pbuffer the current context for opengl commands. All following gl functions will operate on this buffer instead of the display.

NOTE: the Pbuffer may be recreated if it was lost since last used. It's a good idea to use:

         pbuff = selectPbuffer(pbuff);
 
to hold onto the new Pbuffer reference if Pbuffer was recreated.

Parameters:
pb - pbuffer to make current
Returns:
Pbuffer
See Also:
selectDisplay(), makePbuffer(int,int)

selectDisplay

public void selectDisplay()
Make the Display the current context for OpenGL commands. Subsequent gl functions will operate on the Display.

See Also:
selectPbuffer(Pbuffer)

frameSave

public static void frameSave(Pbuffer pbuff,
                             int textureHandle)
Copy the pbuffer contents to a texture. (Should this use glCopyTexSubImage2D()? Is RGB the fastest format?)


screenShot

public static void screenShot(int width,
                              int height,
                              java.lang.String saveFilename)
Save the contents of the current render buffer to a PNG image. If the current buffer is the framebuffer then this will work as a screen capture. Can also be used with the PBuffer class to copy large images or textures that have been rendered into the offscreen pbuffer.

WARNING: this function hogs memory! Call java with more memory (java -Xms360m -Xmx360)

See Also:

allocBytes

public static java.nio.ByteBuffer allocBytes(int howmany)

allocInts

public static java.nio.IntBuffer allocInts(int howmany)

allocFloats

public static java.nio.FloatBuffer allocFloats(int howmany)

allocDoubles

public static java.nio.DoubleBuffer allocDoubles(int howmany)

allocBytes

public static java.nio.ByteBuffer allocBytes(byte[] bytearray)

allocInts

public static java.nio.IntBuffer allocInts(int[] intarray)

allocFloats

public static java.nio.FloatBuffer allocFloats(float[] floatarray)

makeTimestamp

public java.lang.String makeTimestamp()
make a time stamp for filename

Returns:
String containing date-time YYYYMMDD-HHMMSS