|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Object | +--GLApp
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 |
public static final java.lang.String GLAPP_VERSION
public static final int SIZE_DOUBLE
public static final int SIZE_FLOAT
public static final int SIZE_INT
public static final int SIZE_BYTE
public static java.util.Properties settings
public static int finishedKey
public static boolean finished
public static int cursorX
public static int cursorY
public static long prevTime
public static int frameCount
public static long ticksPerSecond
public static java.lang.String windowTitle
public static DisplayMode DM
public static DisplayMode origDM
public static int displayWidth
public static int displayHeight
public static int displayColorBits
public static int displayFrequency
public static int depthBufferBits
public static boolean fullScreen
public static float aspectRatio
public static int viewportX
public static int viewportY
public static int viewportW
public static int viewportH
public static float tileFactorVert
public static float tileFactorHoriz
public static java.nio.IntBuffer bufferSettingInt
public static java.nio.IntBuffer bufferViewport
public static java.nio.FloatBuffer bufferModelviewMatrix
public static java.nio.FloatBuffer bufferProjectionMatrix
public static java.nio.ByteBuffer bufferZdepth
| Constructor Detail |
public GLApp()
| Method Detail |
public static void main(java.lang.String[] args)
public void run()
Calls: init() and mainLoop().
mainLoop() calls: render(), mouseMove(), mouseDown(), mouseUp(), keyDown(), keyUp()
public void mainLoop()
Currently only works with Mouse.setGrabbed(true) (see initInput()) This means the native cursor is hidden and app has to draw its own.
public void loadSettings(java.lang.String configFilename)
# Comment displayWidth=1024 displayHeight=768
configFilename - public static java.lang.String getProperty(java.lang.String propertyName)
loadSettings(String)public static java.io.InputStream getInputStream(java.lang.String filename)
public static java.io.InputStream getInputStream_OLD(java.lang.String filename)
public void init()
public void initBuffers()
public void initOpenGL()
java.lang.Exceptionpublic void render()
public void mouseMove(int x,
int y)
public void mouseDown(int x,
int y)
public void mouseUp(int x,
int y)
public void keyDown(int keycode)
public void keyUp(int keycode)
public void cleanup()
public static boolean initDisplay()
public static void initInput()
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.
for mouse motion tracking,
to draw our own cursor on screen
public static DisplayMode getDisplayMode(int w,
int h,
int colorBits,
int freq)
public static int getSettingInt(int whichSetting)
public static java.nio.FloatBuffer getModelviewMatrix()
public static java.nio.FloatBuffer getProjectionMatrix()
public static java.nio.IntBuffer getViewport()
public static float[][] getMatrixAsArray(java.nio.FloatBuffer fb)
fb - FloatBuffer containing 16 values of 4x4 matrix
public static float[][] getModelviewMatrixA()
public static float[][] getProjectionMatrixA()
public static int[] getViewportA()
public static float getZDepth(int x,
int y)
public static float getZDepthAtOrigin()
public static void project(float x,
float y,
float z,
float[] resultf)
x - world coordinatesy - z - resultf - the screen coordinate as an array of 3 floats
public static void unProject(float x,
float y,
float z,
float[] resultf)
x - screen x positiony - screen y positionz - z-buffer depth positionresultf - the world coordinate as an array of 3 floatsgetWorldCoordsAtScreen(int,int)
public static float[] getWorldCoordsAtScreen(int x,
int y)
public static float[] getWorldCoordsAtScreen(int x,
int y,
float z)
public static int allocateTexture()
public static int makeTexture(GLImage textureImg)
public static void setTextureTile(float horizontalTile,
float verticalTile)
public static int makeTexture(java.nio.ByteBuffer pixels,
int w,
int h)
public static int makeTextureMipMap(GLImage textureImg)
textureImg - the texture image
public static int makeTextureForScreen(int screenSize)
frameSave(int),
frameDraw(int)public static int getPowerOfTwoBiggerThan(int dimension)
dimension -
makeTextureForScreen(int)public static void setPerspective()
public static void setOrtho()
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
public static void setOrthoOn()
setOrthoOff()public static void setOrthoOff()
setOrthoOn()
public static void setLight(int GLLightHandle,
float[] diffuseLightColor,
float[] ambientLightColor,
float[] position)
GLLightHandle - diffuseLightColor - ambientLightColor - position -
public static void setSpotLight(int GLLightHandle,
float[] diffuseLightColor,
float[] ambientLightColor,
float[] position,
float[] direction,
float cutoffAngle)
public static void setAmbientLight(float[] ambientLightColor)
public static double getTimeInSeconds()
public static double getTimeInMillis()
public static GLImage loadImage(java.lang.String imgFilename)
imgFilename -
public static java.nio.ByteBuffer loadImagePixels(java.lang.String imgFilename)
imgFilename -
public static java.nio.IntBuffer loadImageInt(java.lang.String imgFilename)
imgFilename -
public static void drawImageInt(GLImage img,
int x,
int y,
float scaleW,
float scaleY)
public void drawCursor(int cursorTextureHandle)
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);
}
cursorTextureHandle - handle to texture containing 32x32 cursor image
public static void drawImageQuad(int textureHandle,
int x,
int y,
float w,
float h)
public static void drawQuad(int textureHandle,
float x,
float y,
float z,
float w,
float h)
public static Cursor makeNativeCursor(java.lang.String imgFilename)
imgFilename -
drawCursor(int)
public static GLImage getFramePixels(int x,
int y,
int w,
int h)
public static void getFramePixels(int x,
int y,
GLImage img)
public static void setFramePixels(GLImage img)
public static void frameSave(int txtrHandle)
txtrHandle - texture where screen image will be storedframeDraw(int),
makeTextureForScreen(int)public static void frameDraw(int txtrHandle)
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.
txtrHandle -
public static GLImage makeImage(int w,
int h)
public static void renderCube()
public static void renderSphere()
public static void makeSphere()
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.
to draw the display list
public static void buildFont(java.lang.String charSetImage,
int fontWidth)
charSetImage - texture image containing 256 characters in a 16x16 gridfontWidth - how many pixels to allow per character on screendestroyFont()
public static void buildFont(int fontTxtrHandle,
int fontWidth)
fontWidth - how many pixels to allow per character on screendestroyFont()public static void destroyFont()
public static void glPrint(int x,
int y,
int set,
java.lang.String msg)
public static void glText(float x,
float y,
float z,
int set,
float scale,
java.lang.String msg)
public Pbuffer makePbuffer(int width,
int height)
width - height -
selectPbuffer(Pbuffer),
selectDisplay()public Pbuffer selectPbuffer(Pbuffer pb)
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.
pb - pbuffer to make current
selectDisplay(),
makePbuffer(int,int)public void selectDisplay()
selectPbuffer(Pbuffer)
public static void frameSave(Pbuffer pbuff,
int textureHandle)
public static void screenShot(int width,
int height,
java.lang.String saveFilename)
WARNING: this function hogs memory! Call java with more memory (java -Xms360m -Xmx360)
public static java.nio.ByteBuffer allocBytes(int howmany)
public static java.nio.IntBuffer allocInts(int howmany)
public static java.nio.FloatBuffer allocFloats(int howmany)
public static java.nio.DoubleBuffer allocDoubles(int howmany)
public static java.nio.ByteBuffer allocBytes(byte[] bytearray)
public static java.nio.IntBuffer allocInts(int[] intarray)
public static java.nio.FloatBuffer allocFloats(float[] floatarray)
public java.lang.String makeTimestamp()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||