de.rico.engine.util
Class Tools

java.lang.Object
  extended by de.rico.engine.util.Tools

public class Tools
extends java.lang.Object

This class provides some usefull methods for calculations or image processing, for example

Author:
Frank Bruns

Constructor Summary
Tools()
           
 
Method Summary
static void beginOrthoMode(javax.media.opengl.GL gl)
          Enables the orthographic projection mode.
static void billboardCylindricalBegin(javax.media.opengl.GL gl)
          Applies the cylindrical billboarding technique by performing some undo calculations on the current modelview matrix.
static void billboardEnd(javax.media.opengl.GL gl)
          Finishes the application of the current billboarding technique
static void billboardSphericalBegin(javax.media.opengl.GL gl)
          Applies the spherical billboarding technique by performing some undo calculations on the current modelview matrix.
static float cos(float value)
          Returns the cosine value for the specified value
static com.sun.opengl.util.texture.Texture createCubeMapTexture(java.lang.String posX, java.lang.String negX, java.lang.String posY, java.lang.String negY, java.lang.String posZ, java.lang.String negZ)
          Creates a texture with six images that can be used for cube maps.
static Line3D createLineFromScreenCoords(int screenX, int screenY)
          Creates a line from the specified screen coordinates into the world.
static com.sun.opengl.util.texture.Texture createTexture(java.lang.String path, boolean mipmap)
          Returns the texture object that is created by the file which is specified by the method parameter.
static com.sun.opengl.util.texture.Texture createTexture(java.lang.String texPath, boolean mipmap, boolean rotate, boolean flip)
          Returns the texture object that is created by the file which is specified by the method parameter.
static float cut(float value, float min, float max)
          Cuts the specified value to stay within the min and max boundaries.
static void endOrthoMode(javax.media.opengl.GL gl)
          Disables the orthographic projection mode.
static float findMaximum(float... values)
          A method that accepts an arbitrary number of float values and returns the maximum value of these
static float findMinimum(float... values)
          A method that accepts an arbitrary number of float values and returns the minimum value of these
static java.awt.image.BufferedImage getHorizontallyFlippedImage(java.awt.image.BufferedImage img)
          Returns a horizontally flipped version of the specified image
static int getLastTextureUnit(javax.media.opengl.GL gl)
          Returns the OpenGL constant for the last texture unit of the graphics card.
static int getNearestPowerOfTwo(int number)
          This method returns the neares power of two according to the specified number.
static Point3D getNormalVector(Point3D p0, Point3D p1, Point3D p2)
          Calculates and returns the normal by using the three specified vertices The returned normal vector is normalized.
static Point3D getNormalVector(Triangle trig)
          Calculates and returns the normal by using the specified triangle The returned normal vector is normalized.
static Point3D getNormalVectorNotNormalized(Point3D p0, Point3D p1, Point3D p2)
          Calculates and returns the normal by using the three specified vertices.
static Point3D getNormalVectorNotNormalized(Triangle trig)
          Calculates and returns the normal by using the specified triangle The returned normal vector is not normalized.
static Point3D getProjectedScreenCoords(Point3D worldCoords)
          Returns the screen coordinates as a Point3D object, that can be projected from the specified point in world coordinates to the screen.
static Point3D getProjectedWorldCoords(int screenX, int screenY)
          Returns the world coordinates of the point that is under the specified screen coordinates.
static java.awt.image.BufferedImage getRotatedAndHorizFlippedImage(java.awt.image.BufferedImage image)
          Returns the rotated and horizontally flipped version of the specified image
static java.awt.image.BufferedImage getRotatedImage(java.awt.image.BufferedImage img, float degrees)
          Returns a rotated version of the specified image
static float[] getRow(int row, javax.vecmath.Matrix4f mat)
          This method returns the desired row from the specified matrix.
static int getTextureUnit(int number)
          Returns the OpenGL texture unit constant for the specified number.
static java.awt.image.BufferedImage getVerticallyFlippedImage(java.awt.image.BufferedImage img)
          Returns a vertically flipped version of the specified image
static boolean isEqual(float a, float b, float tolerance)
          Compares to float values for equality by using the specified tolerance.
static float[] matrixToFloatArray(javax.vecmath.Matrix4f matrix4f)
          Returns the float array representation of the specified Matrix.
static float[] removeRotations(javax.media.opengl.GL gl)
          This method returns a float array with 16 values, that represents the modelview matrix with all rotation operations eliminated.
static float[] removeTransformations(javax.media.opengl.GL gl)
          This method returns a float array with 16 values, that represents the modelview matrix with all translate and rotate operations eliminated.
static float[] removeTranslations(javax.media.opengl.GL gl)
          This method returns a float array with 16 values, that represents the modelview matrix with all translate operations eliminated.
static float round(float value, int decimal)
          Returns a rounded version of the specified value.
static void setGlobalAmbientLight(javax.media.opengl.GL gl, float red, float green, float blue, float alpha)
          Sets the color of the global ambient background light.
static void setUpBlendFunction(javax.media.opengl.GL gl, BlendSourceFactor srcFact, BlendDestFactor destFact)
          Sets the blend function to the specified values.
static float sin(float value)
          Returns the sine value for the specified value
static float tan(float value)
          Returns the tangent value for the specified value
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Tools

public Tools()
Method Detail

sin

public static float sin(float value)
Returns the sine value for the specified value

Parameters:
value - value in degrees, not radians
Returns:
sinus

cos

public static float cos(float value)
Returns the cosine value for the specified value

Parameters:
value - value in degrees, not radians
Returns:
cosine

tan

public static float tan(float value)
Returns the tangent value for the specified value

Parameters:
value - value in degrees, not radians
Returns:
tangent

cut

public static float cut(float value,
                        float min,
                        float max)
Cuts the specified value to stay within the min and max boundaries.

Parameters:
value - value to cut
min - min value boundary
max - max value boundary
Returns:
a value that lies between the specified boundaries

round

public static float round(float value,
                          int decimal)
Returns a rounded version of the specified value. The second parameters defines the number of decimal places to round to.

Parameters:
decimal - decimal place (position after decimal point)
Returns:
returns a rounded version of the specified value

getNearestPowerOfTwo

public static int getNearestPowerOfTwo(int number)
This method returns the neares power of two according to the specified number. It works by the means of bit shifting. Don't use 0 as a parameter.

Parameters:
number - number to find the neares power of two for
Returns:
neares power of two to the specified number

isEqual

public static boolean isEqual(float a,
                              float b,
                              float tolerance)
Compares to float values for equality by using the specified tolerance.

Parameters:
a - first value
b - second value
tolerance - tolerance while comparing
Returns:
are the values equal? --> true=yes, false=no

getNormalVector

public static Point3D getNormalVector(Point3D p0,
                                      Point3D p1,
                                      Point3D p2)
Calculates and returns the normal by using the three specified vertices The returned normal vector is normalized.

Parameters:
p0 - vertex 1
p1 - vertex 2
p2 - vertex 3
Returns:
normal vector

getNormalVector

public static Point3D getNormalVector(Triangle trig)
Calculates and returns the normal by using the specified triangle The returned normal vector is normalized.

Parameters:
trig - triangle to calculate a normal vector for
Returns:
normal vector for the triangle

getNormalVectorNotNormalized

public static Point3D getNormalVectorNotNormalized(Point3D p0,
                                                   Point3D p1,
                                                   Point3D p2)
Calculates and returns the normal by using the three specified vertices. The returned normal vector is not normalized.

Parameters:
p0 - vertex 1
p1 - vertex 2
p2 - vertex 3
Returns:
normal vector

getNormalVectorNotNormalized

public static Point3D getNormalVectorNotNormalized(Triangle trig)
Calculates and returns the normal by using the specified triangle The returned normal vector is not normalized.

Parameters:
trig - triangle to calculate a normal vector for
Returns:
normal vector for the triangle

matrixToFloatArray

public static float[] matrixToFloatArray(javax.vecmath.Matrix4f matrix4f)
Returns the float array representation of the specified Matrix. The values are ordered by column in the array, i.e. the first four values are column 1, the seconf four values are column 2 and so on.

Parameters:
matrix4f - 4x4 matrix to convert to a float array
Returns:
float array representation of the specified matrix

getRow

public static float[] getRow(int row,
                             javax.vecmath.Matrix4f mat)
This method returns the desired row from the specified matrix.

Parameters:
row - row to retrieve (1-4)
mat - matrix to rterieve the row from
Returns:
desired row

findMaximum

public static float findMaximum(float... values)
A method that accepts an arbitrary number of float values and returns the maximum value of these

Parameters:
values - float values
Returns:
maximum value

findMinimum

public static float findMinimum(float... values)
A method that accepts an arbitrary number of float values and returns the minimum value of these

Parameters:
values - float values
Returns:
minimum value

billboardSphericalBegin

public static void billboardSphericalBegin(javax.media.opengl.GL gl)
Applies the spherical billboarding technique by performing some undo calculations on the current modelview matrix. The effect of this is that the objects which are drawn between the billboardSphericalBegin()- and billboardEnd()-methods always face the camera. They even face the camera if you look up or down, which is the difference to the spherical billboarding technique.

Parameters:
gl -

billboardCylindricalBegin

public static void billboardCylindricalBegin(javax.media.opengl.GL gl)
Applies the cylindrical billboarding technique by performing some undo calculations on the current modelview matrix. The effect of this is that the objects which are drawn between the billboardCylindricalBegin()- and billboardEnd()-methods always face the camera. But if you look up with the camera you will still get a perspective effect, which is the difference to the spherical billboarding technique.

Parameters:
gl -

billboardEnd

public static void billboardEnd(javax.media.opengl.GL gl)
Finishes the application of the current billboarding technique


removeTransformations

public static float[] removeTransformations(javax.media.opengl.GL gl)
This method returns a float array with 16 values, that represents the modelview matrix with all translate and rotate operations eliminated. You can load the matrix with the gl.glLoadMatrixf()-method.

Parameters:
gl -
Returns:
modelview matrix without its translations and rotations

removeTranslations

public static float[] removeTranslations(javax.media.opengl.GL gl)
This method returns a float array with 16 values, that represents the modelview matrix with all translate operations eliminated. You can load the matrix with the gl.glLoadMatrixf()-method.

Parameters:
gl -
Returns:
modelview matrix without its translations

removeRotations

public static float[] removeRotations(javax.media.opengl.GL gl)
This method returns a float array with 16 values, that represents the modelview matrix with all rotation operations eliminated. You can load the matrix with the gl.glLoadMatrixf()-method.

Parameters:
gl -
Returns:
modelview matrix without its rotations

getRotatedImage

public static java.awt.image.BufferedImage getRotatedImage(java.awt.image.BufferedImage img,
                                                           float degrees)
Returns a rotated version of the specified image

Parameters:
img - image to be used for rotation
degrees - number of degrees to rotate the image
Returns:
rotated version of the specified image

getHorizontallyFlippedImage

public static java.awt.image.BufferedImage getHorizontallyFlippedImage(java.awt.image.BufferedImage img)
Returns a horizontally flipped version of the specified image

Parameters:
img - image to be used for rotation
Returns:
horizontally flipped version of the specified image

getVerticallyFlippedImage

public static java.awt.image.BufferedImage getVerticallyFlippedImage(java.awt.image.BufferedImage img)
Returns a vertically flipped version of the specified image

Parameters:
img - image to be used for rotation
Returns:
vertically flipped version of the specified image

getRotatedAndHorizFlippedImage

public static java.awt.image.BufferedImage getRotatedAndHorizFlippedImage(java.awt.image.BufferedImage image)
Returns the rotated and horizontally flipped version of the specified image

Parameters:
image - original image
Returns:
rotated and flipped version of the specified image

createTexture

public static com.sun.opengl.util.texture.Texture createTexture(java.lang.String texPath,
                                                                boolean mipmap,
                                                                boolean rotate,
                                                                boolean flip)
Returns the texture object that is created by the file which is specified by the method parameter. The texture can get rotated and flipped to show correct appearance, if neccessay.

Parameters:
texPath - path to texture file
mipmap - create mipmaps? --> true = yes, false = no
rotate - rotate texture 180° clockwise? --> true = yes, false = no
flip - flip texture? --> true = yes, false = no
Returns:
Texture object that is created from the specified texture file

createTexture

public static com.sun.opengl.util.texture.Texture createTexture(java.lang.String path,
                                                                boolean mipmap)
Returns the texture object that is created by the file which is specified by the method parameter. The texture will be rotated and flipped. It's usually neccessary to do so. I fyou need more freedom of manipulation you can use the other createTexture() method.

Parameters:
path - path to texture file
mipmap - create mipmaps? --> true = yes, false = no
Returns:
(rotated and flipped) texture object that is created from the specified texture file path

createCubeMapTexture

public static com.sun.opengl.util.texture.Texture createCubeMapTexture(java.lang.String posX,
                                                                       java.lang.String negX,
                                                                       java.lang.String posY,
                                                                       java.lang.String negY,
                                                                       java.lang.String posZ,
                                                                       java.lang.String negZ)
Creates a texture with six images that can be used for cube maps.

Parameters:
posX - path to posX texture
negX - path to negX texture
posY - path to posY texture
negY - path to negY texture
posZ - path to posZ texture
negZ - path to negZ texture
Returns:
cube map texture

getLastTextureUnit

public static int getLastTextureUnit(javax.media.opengl.GL gl)
Returns the OpenGL constant for the last texture unit of the graphics card.

Parameters:
gl -
Returns:
OpenGL constant for the last texture unit

getTextureUnit

public static int getTextureUnit(int number)
Returns the OpenGL texture unit constant for the specified number. Please only specfiy values between 0 and 31. Otherwise this method will return the value for texture unit 0 by default.

Returns:
OpenGL constant for the texture unit number

setGlobalAmbientLight

public static void setGlobalAmbientLight(javax.media.opengl.GL gl,
                                         float red,
                                         float green,
                                         float blue,
                                         float alpha)
Sets the color of the global ambient background light. Set all parameters to 0.0f, if you would like to archieve perfect darkness in your scene, while all other light sources must be disabled, too.

Parameters:
red - amount of red
green - amount of green
blue - amount of blue
alpha - alpha-value

setUpBlendFunction

public static void setUpBlendFunction(javax.media.opengl.GL gl,
                                      BlendSourceFactor srcFact,
                                      BlendDestFactor destFact)
Sets the blend function to the specified values.

Parameters:
gl -
srcFact - value from the BlendSourceFactor enumeration
destFact - value from the BlendDestFactor enumeration

getProjectedWorldCoords

public static Point3D getProjectedWorldCoords(int screenX,
                                              int screenY)
Returns the world coordinates of the point that is under the specified screen coordinates. This only works in a reasonable way, if there actually is something to click on (opaque meshes).

Note: This method doesn't return correct values if the specified screen coordinates are being projected to a transparent mesh, but they can be projected to meshes that are (partially) covered by transparent meshes.
Another important thing to mention is, that you can't use this method while using instances of the Monitor or Mirror class. Instances of those classes need to clear the color buffer and the depth buffer, which doesn't allow this method to return a proper value, because it depends on the content of the color and depth buffer. The only way to circumvent this is to call this method in the game class' draw() method after all the objects have been drawn instead of calling it in the update() method, which would be the common way.

Parameters:
screenX - x coordinate on the screen
screenY - y coordinate on the screen
Returns:
the point in 3D space that lies under the screen coordinates

getProjectedScreenCoords

public static Point3D getProjectedScreenCoords(Point3D worldCoords)
Returns the screen coordinates as a Point3D object, that can be projected from the specified point in world coordinates to the screen.

Parameters:
worldCoords - point in 3D world space
Returns:
point that represents the projected screen coordinates (ignore the z component!)

createLineFromScreenCoords

public static Line3D createLineFromScreenCoords(int screenX,
                                                int screenY)
Creates a line from the specified screen coordinates into the world. The line reaches from the camera's current near plane to the current far plane.

Parameters:
screenX - x coordinate on the screen
screenY - y coordinate on the screen
Returns:
a line from the camera's near plane to its far plane

beginOrthoMode

public static void beginOrthoMode(javax.media.opengl.GL gl)
Enables the orthographic projection mode.

Parameters:
gl -

endOrthoMode

public static void endOrthoMode(javax.media.opengl.GL gl)
Disables the orthographic projection mode.

Parameters:
gl -