Creating a Box with different parameters

Started by Baune, July 02, 2010, 03:43:04 PM

Previous topic - Next topic

Baune

Hello again

Yes I'm aware of the Primitives.getBox(float scale, float scaleHeight)

But is there a way to create it with these parameters  getBox(scale, x,y,z) where

x - the size of the box along the x axis, in both direction.
y - the size of the box along the y axis, in both directions
z - the size of the box along the z axis, in both directions

Thanks



paulscode

You could build the cube manually based on the parameters (only 12 polys and easy to visualize, so it's not too difficult).  Here is basically how you would do it (off the top of my head).  I don't have access to my PC at the moment, so check for typos.  You'll need to define the texture(s) to use for the faces ahead of time, or pass that information to the method.

public Object3D getBox( float scale, float x, float y, float z )
{
    Object3D box = new Object3D( 12 );
    float xoffset = x * scale / 2.0f;
    float yoffset = y * scale / 2.0f;
    float zoffset = z * scale / 2.0f;

    box.addTriangle( new SimpleVector( -xoffset, -yoffset, -zoffset ), 0, 0,
                     new SimpleVector( -xoffset, yoffset, -zoffset ), 0, 1,
                     new SimpleVector( xoffset, yoffset, -zoffset ), 1, 1,
                     TextureManager.getInstance().getTextureID(
                                                      "-Z Texture Here" ) );
    box.addTriangle( new SimpleVector( xoffset, yoffset, -zoffset ), 1, 1,
                     new SimpleVector( xoffset, -yoffset, -zoffset ), 1, 0,
                     new SimpleVector( -xoffset, -yoffset, -zoffset ), 0, 0,
                     TextureManager.getInstance().getTextureID(
                                                      "-Z Texture Here" ) );

    box.addTriangle( new SimpleVector( -xoffset, -yoffset, zoffset ), 0, 0,
                     new SimpleVector( -xoffset, yoffset, zoffset ), 0, 1,
                     new SimpleVector( xoffset, yoffset, zoffset ), 1, 1,
                     TextureManager.getInstance().getTextureID(
                                                      "+Z Texture Here" ) );
    box.addTriangle( new SimpleVector( xoffset, yoffset, zoffset ), 1, 1,
                     new SimpleVector( xoffset, -yoffset, zoffset ), 1, 0,
                     new SimpleVector( -xoffset, -yoffset, zoffset ), 0, 0,
                     TextureManager.getInstance().getTextureID(
                                                      "+Z Texture Here" ) );

    box.addTriangle( new SimpleVector( -xoffset, -yoffset, zoffset ), 0, 0,
                     new SimpleVector( -xoffset, -yoffset, -zoffset ), 0, 1,
                     new SimpleVector( xoffset, -yoffset, -zoffset ), 1, 1,
                     TextureManager.getInstance().getTextureID(
                                                      "-Y Texture Here" ) );
    box.addTriangle( new SimpleVector( xoffset, -yoffset, -zoffset ), 1, 1,
                     new SimpleVector( xoffset, -yoffset, zoffset ), 1, 0,
                     new SimpleVector( -xoffset, -yoffset, zoffset ), 0, 0,
                     TextureManager.getInstance().getTextureID(
                                                      "-Y Texture Here" ) );

    box.addTriangle( new SimpleVector( -xoffset, yoffset, -zoffset ), 0, 0,
                     new SimpleVector( -xoffset, yoffset, zoffset ), 0, 1,
                     new SimpleVector( xoffset, yoffset, zoffset ), 1, 1,
                     TextureManager.getInstance().getTextureID(
                                                      "+Y Texture Here" ) );
    box.addTriangle( new SimpleVector( xoffset, yoffset, zoffset ), 1, 1,
                     new SimpleVector( xoffset, yoffset, -zoffset ), 1, 0,
                     new SimpleVector( -xoffset, yoffset, -zoffset ), 0, 0,
                     TextureManager.getInstance().getTextureID(
                                                      "+Y Texture Here" ) );

    box.addTriangle( new SimpleVector( -xoffset, -yoffset, zoffset ), 0, 0,
                     new SimpleVector( -xoffset, yoffset, zoffset ), 0, 1,
                     new SimpleVector( -xoffset, yoffset, -zoffset ), 1, 1,
                     TextureManager.getInstance().getTextureID(
                                                      "-X Texture Here" ) );
    box.addTriangle( new SimpleVector( -xoffset, yoffset, -zoffset ), 1, 1,
                     new SimpleVector( -xoffset, -yoffset, -zoffset ), 1, 0,
                     new SimpleVector( -xoffset, -yoffset, zoffset ), 0, 0,
                     TextureManager.getInstance().getTextureID(
                                                      "-X Texture Here" ) );

    box.addTriangle( new SimpleVector( xoffset, -yoffset, -zoffset ), 0, 0,
                     new SimpleVector( xoffset, yoffset, -zoffset ), 0, 1,
                     new SimpleVector( xoffset, yoffset, zoffset ), 1, 1,
                     TextureManager.getInstance().getTextureID(
                                                      "+X Texture Here" ) );
    box.addTriangle( new SimpleVector( xoffset, yoffset, zoffset ), 1, 1,
                     new SimpleVector( xoffset, -yoffset, zoffset ), 1, 0,
                     new SimpleVector( xoffset, -yoffset, -zoffset ), 0, 0,
                     TextureManager.getInstance().getTextureID(
                                                      "+X Texture Here" ) );

    return box;
}

Baune


EgonOlsen

The boxes generated by Primitives are not very good anyway, because they lack texture coordinates. They are mainly meant for quick-and-dirty stuff.

Baune

Thanks again all..you've been very helpful

Baune

Ok just tried it and it seems like some surfaces are missing or ot showing...
I tried to make my own version from scratch, to see if there was something missing but with the same result:
Here's my version, acts just the same as the first one. Hope someone knows how to make a proper box.


box.addTriangle( v( -x, -y, -z ),v( -x, y, -z ),v( x, y, -z ));
   box.addTriangle( v( x, -y, -z ),v( -x, -y, -z ),v( x, y, -z ));//X,Y
     
   box.addTriangle( v( -x, -y, z ),v( -x, y, z ),v( x, y, z ));//X,Y
   box.addTriangle( v( x, -y, z ),v( -x, -y, z ),v( x, y, z ));
   
   box.addTriangle( v( -x, -y, -z ),v( -x, y, -z ),v( -x, y, z ));//Y,Z
   box.addTriangle( v( -x, -y, z ),v( -x, -y, -z ),v( -x, y, z ));
   
   box.addTriangle( v( x, -y, -z ),v( x, y, -z ),v( x, y, z ));//Y,Z
   box.addTriangle( v( x, -y, z ),v( x, -y, -z ),v( x, y, z ));
 
   box.addTriangle( v( -x, -y, -z ),v( -x, -y, z ),v( x, -y, z ));//X,Z
   box.addTriangle( v( x, -y, -z ),v( -x, -y, -z ),v( x, -y, z ));
   
   box.addTriangle( v( -x, y, -z ),v( -x, y, z ),v( x, y, z ));//X,Z
   box.addTriangle( v( x, y, -z ),v( -x, y, -z ),v( x, y, z ));

EgonOlsen

Most likely the polygin winding isn't correct, so that faces get culled that shouldn't. Try a call to Object3D.setCulling(false); to see if that helps. If it does, then that's not really the solution but a hint on what to change.

Baune


EgonOlsen

But it's an unefficient box then...try to define the box so that all polygons are defined in a counter clock wise manner, i.e.

Not:

1---2
|   /
|  /
| /
3

but

1---3
|   /
|  /
| /
2

...that way, backface culling is possible again.