bluegammon.gui
Class Context3D

java.lang.Object
  extended bybluegammon.gui.Context3D

public class Context3D
extends java.lang.Object

The 3d context used in this game. Consists of the scene which is a camera with one main headlight; and dice, string or piece objects. Also contains functionality for creating objects and defining materials. We only have one 3d context, thus the singleton pattern.

The dice and the piece is constructed mathematically based on the JSR184 specifications.

The piece is constructed from a number of predefined pie-slices. The greater amount of slices, the more circular piece - but also the amount of triangles per piece will increase, impacting the performance. Each piece consists of 4 * numberOfSlices triangles.





The dice is calculated using a somewhat more complicated model. Each dice consists of 6 * 6 faces + 8 corners = 44 triangles.

A graphical representation of how one face of the dice is calculated is depicted below:



Author:
Peter Andersson

Field Summary
static float FAR_CLIPPING_PLANE
          Z-axis far clipping plane
static float FOVY
          Field of view, degrees on horizontal axis
static float NEAR_CLIPPING_PLANE
          Z-axis near clipping plane
 
Constructor Summary
Context3D()
           
 
Method Summary
 javax.microedition.m3g.Graphics3D bindScene(javax.microedition.lcdui.Graphics g)
          Binds a graphics context and set up the common 3d environment used in this game.
static int[] createShadowRGB(int size, int sizeDim)
          Creates an rgb buffer rendered a shadow that is transparant.
 javax.microedition.m3g.Appearance getDiceAppearance(boolean white)
          Returns the appearance (texture and material) for a dice.
 javax.microedition.m3g.IndexBuffer getDiceIndexBuffer()
          Returns index buffer for a dice.
 javax.microedition.m3g.VertexBuffer getDiceVertexBuffer()
          Returns vertexbuffer for a dice.
static Context3D getInstance()
          Returns singleton instance.
 javax.microedition.m3g.Appearance getPieceAppearance(boolean white)
          Returns the appearance (texture and material) for a piece.
 javax.microedition.m3g.IndexBuffer getPieceIndexBuffer()
          Returns indexbuffer for a piece.
 javax.microedition.m3g.VertexBuffer getPieceVertexBuffer()
          Returns vertexbuffer for a piece.
 float getShadowProjectionDeltaX(float deltaZ)
          Returns delta x coordinate of shadow position given specified delta z between object and projection plane.
 javax.microedition.m3g.Texture2D getTexture(boolean white)
          Returns the textures used within this game.
 void init(int width, int height)
          Sets up our viewport, camera, lighting - everyting needed for the scene
 float to3DCoordinateX(float coordinateScreen, float z)
          Recalculates a screen x coordinate to a 3d x coordinate.
 float to3DCoordinateY(float coordinateScreen, float z)
          Recalculates a screen y coordinate to a 3d y coordinate.
 float toScreenCoordinateX(float coordinate3D, float z)
          Recalculates a x 3d coordinate to a screen x coordinate.
 float toScreenCoordinateY(float coordinate3D, float z)
          Recalculates a 3d y coordinate to a screen y coordinate.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

FOVY

public static final float FOVY
Field of view, degrees on horizontal axis

See Also:
Constant Field Values

NEAR_CLIPPING_PLANE

public static final float NEAR_CLIPPING_PLANE
Z-axis near clipping plane

See Also:
Constant Field Values

FAR_CLIPPING_PLANE

public static final float FAR_CLIPPING_PLANE
Z-axis far clipping plane

See Also:
Constant Field Values
Constructor Detail

Context3D

public Context3D()
Method Detail

getInstance

public static Context3D getInstance()
Returns singleton instance.

Returns:
The singleton instance.

init

public void init(int width,
                 int height)
Sets up our viewport, camera, lighting - everyting needed for the scene

Parameters:
width - The width of the viewport
height - The height of the viewport

toScreenCoordinateX

public float toScreenCoordinateX(float coordinate3D,
                                 float z)
Recalculates a x 3d coordinate to a screen x coordinate.

Parameters:
coordinate3D - The 3d x coordinate
z - The depth of the 3d coordinate
Returns:
The screen coordinate

to3DCoordinateX

public float to3DCoordinateX(float coordinateScreen,
                             float z)
Recalculates a screen x coordinate to a 3d x coordinate.

Parameters:
coordinateScreen - The screen x coordinate
z - The depth of the 3d coordinate
Returns:
The 3d x coordinate

toScreenCoordinateY

public float toScreenCoordinateY(float coordinate3D,
                                 float z)
Recalculates a 3d y coordinate to a screen y coordinate.

Parameters:
coordinate3D - The 3d y coordinate
z - The depth of the 3d coordinate
Returns:
The screen y coordinate

to3DCoordinateY

public float to3DCoordinateY(float coordinateScreen,
                             float z)
Recalculates a screen y coordinate to a 3d y coordinate.

Parameters:
coordinateScreen - The screen y coordinate
z - The depth of the 3d coordinate
Returns:
The 3d y coordinate

getShadowProjectionDeltaX

public float getShadowProjectionDeltaX(float deltaZ)
Returns delta x coordinate of shadow position given specified delta z between object and projection plane.

Parameters:
deltaZ - The difference in z between object and shadow projecton plane.
Returns:
Delta x coordinate in 3d units.

bindScene

public javax.microedition.m3g.Graphics3D bindScene(javax.microedition.lcdui.Graphics g)
Binds a graphics context and set up the common 3d environment used in this game.

Parameters:
g - The graphics environment to render upon.
Returns:
A Graphics3D object with common 3d environment settings.

createShadowRGB

public static int[] createShadowRGB(int size,
                                    int sizeDim)
Creates an rgb buffer rendered a shadow that is transparant.

Parameters:
size - Shadow size
sizeDim - Blank border.
Returns:
An rgb buffer containing a transparant shadow

getPieceAppearance

public javax.microedition.m3g.Appearance getPieceAppearance(boolean white)
Returns the appearance (texture and material) for a piece.

Parameters:
white - true for white appearance, false for black appearance.
Returns:
The piece appearance.

getDiceAppearance

public javax.microedition.m3g.Appearance getDiceAppearance(boolean white)
Returns the appearance (texture and material) for a dice.

Parameters:
white - true for white appearance, false for black appearance.
Returns:
The dice appearance.

getTexture

public javax.microedition.m3g.Texture2D getTexture(boolean white)
Returns the textures used within this game.

Parameters:
white - true for white textures, false for black textures.
Returns:
the texture.

getPieceVertexBuffer

public javax.microedition.m3g.VertexBuffer getPieceVertexBuffer()
Returns vertexbuffer for a piece.

Returns:
a piece vertex buffer

getPieceIndexBuffer

public javax.microedition.m3g.IndexBuffer getPieceIndexBuffer()
Returns indexbuffer for a piece.

Returns:
a piece index buffer

getDiceVertexBuffer

public javax.microedition.m3g.VertexBuffer getDiceVertexBuffer()
Returns vertexbuffer for a dice.

Returns:
a dice vertex buffer

getDiceIndexBuffer

public javax.microedition.m3g.IndexBuffer getDiceIndexBuffer()
Returns index buffer for a dice.

Returns:
a dice index buffer