Author Topic: Creating a Box with different parameters  (Read 3381 times)

Offline Baune

  • byte
  • *
  • Posts: 7
    • View Profile
Creating a Box with different parameters
« on: July 02, 2010, 03:43:04 pm »
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



Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: Creating a Box with different parameters
« Reply #1 on: July 02, 2010, 04:55:29 pm »
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.

Code: [Select]
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;
}

Offline Baune

  • byte
  • *
  • Posts: 7
    • View Profile
Re: Creating a Box with different parameters
« Reply #2 on: July 02, 2010, 05:41:15 pm »
Thanks, I'll try that.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Creating a Box with different parameters
« Reply #3 on: July 02, 2010, 06:10:03 pm »
The boxes generated by Primitives are not very good anyway, because they lack texture coordinates. They are mainly meant for quick-and-dirty stuff.

Offline Baune

  • byte
  • *
  • Posts: 7
    • View Profile
Re: Creating a Box with different parameters
« Reply #4 on: July 03, 2010, 12:48:12 am »
Thanks again all..you've been very helpful

Offline Baune

  • byte
  • *
  • Posts: 7
    • View Profile
Re: Creating a Box with different parameters
« Reply #5 on: July 20, 2010, 06:48:13 pm »
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.

Code: [Select]
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 ));

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Creating a Box with different parameters
« Reply #6 on: July 20, 2010, 08:37:17 pm »
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.

Offline Baune

  • byte
  • *
  • Posts: 7
    • View Profile
Re: Creating a Box with different parameters
« Reply #7 on: July 20, 2010, 08:41:17 pm »
That helped, thanks !

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Creating a Box with different parameters
« Reply #8 on: July 20, 2010, 08:55:04 pm »
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.