Hi, I’m trying to create a SkyBox that will wrap all the gameworld. The SkyBox cube will have six different textures for all its sides, but I currently cannot get how to easily implement this.
I’m creating a cube mesh (VB,IB) and putting it around the game world, but how can I put different texture on each side ?
I finally managed to work it out. For those interested here’s the piece of code. The ‘Group’ class was the big thing here.
public SkyDome( Image2D[] img, int center_x, int center_y, int center_z,
int width, int height, int depth )
{
int half_width = (width / 2),
half_height = (height / 2),
half_depth = (depth / 2);
short posCX = (short)(center_x + half_width), negCX = (short)(center_x - half_width),
posCY = (short)(center_y + half_height), negCY =(short)(center_y - half_height),
posCZ = (short)(center_z + half_depth), negCZ = (short)(center_z - half_depth);
try
{
// load textures
Texture2D[] textures = new Texture2D[6];
for( int i = 0; i < 6; i++ )
{
textures[i] = new Texture2D( img[i] );
textures[i].setFiltering( Texture2D.FILTER_LINEAR, Texture2D.FILTER_LINEAR );
textures[i].setWrapping( Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP );
textures[i].setBlendColor( Texture2D.FUNC_MODULATE );
}
// front
short[] vSide1 = { posCX, posCY, posCZ, negCX, posCY, posCZ, posCX, negCY, posCZ, negCX, negCY, posCZ };
// back
short[] vSide2 = { negCX, posCY, negCZ, posCX, posCY, negCZ, negCX, negCY, negCZ, posCX, negCY, negCZ };
// left
short[] vSide3 = { negCX, posCY, posCZ, negCX, posCY, negCZ, negCX, negCY, posCZ, negCX, negCY, negCZ };
// right
short[] vSide4 = { posCX, posCY, negCZ, posCX, posCY, posCZ, posCX, negCY, negCZ, posCX, negCY, posCZ };
// top
short[] vSide5 = { posCX, posCY, negCZ, negCX, posCY, negCZ, posCX, posCY, posCZ, negCX, posCY, posCZ };
// bottom
short[] vSide6 = { posCX, negCY, posCZ, negCX, negCY, posCZ, posCX, negCY, negCZ, negCX, negCY, negCZ };
short[] tex = {
1, 0, 0, 0, 1, 1, 0, 1,
};
Mesh skymesh[] = new Mesh[6];
skymesh[0] = createFace( vSide1, textures[0], tex );
skymesh[1] = createFace( vSide2, textures[1], tex );
skymesh[2] = createFace( vSide3, textures[2], tex );
skymesh[3] = createFace( vSide4, textures[3], tex );
skymesh[4] = createFace( vSide5, textures[4], tex );
skymesh[5] = createFace( vSide6, textures[5], tex );
// compose skybox mesh as a group of submeshes
m_groupSkybox = new Group();
for( int i = 0; i < 6; i++)
m_groupSkybox.addChild( skymesh[i] );
}
catch( Exception e )
{
System.out.println( "*** SkyDome::SkyBox Construction Exception **** ");
e.printStackTrace();
}
}
private Mesh createFace( short[] vertices, Texture2D texture, short[] texcoords )
{
VertexArray v = new VertexArray( vertices.length / 3, 3, 2 );
v.set( 0, vertices.length / 3, vertices );
VertexArray t = new VertexArray( texcoords.length / 2, 2, 2 );
t.set( 0, texcoords.length / 2, texcoords );
VertexBuffer vb = new VertexBuffer();
vb.setPositions( v, 1.0f, null );
vb.setTexCoords( 0, t, 1.0f, null );
// create the appearance
Appearance app = new Appearance();
app.setTexture( 0, texture ) ;
PolygonMode poly = new PolygonMode();
poly.setCulling( PolygonMode.CULL_FRONT );
app.setPolygonMode( poly );
int strips[] = { 4 };
IndexBuffer ib = new TriangleStripArray( 0, strips );
Mesh meshFlat = new Mesh( vb, ib, app );
meshFlat.setAppearance( 0, app );
return meshFlat;
}
Gud luk