6
\$\begingroup\$

I am a lightly experienced game developer and this is my first time trying 3D objects in Java for the first time. I have been recently creating and updating games using AWT, Swing, and Graphics, but I want to delve farther into Java.

I have looked into Java3D, but it's not what I want. I want to use Images and then crop the Image and place the respective textures in their respective places. I already know how to do the cropping and 2D Image editing, but how do I go 3D?

\$\endgroup\$
  • 1
    \$\begingroup\$ You could procedurally generate the geometry, if it's simple enough. You could also use voxels and design your world out of those voxels. You won't need any fancy 3D modelling software, but you'll probably need your own custom tool. Take a look at Fez game. It's built almost entirely out of voxels (or rather trixels). There's also Voxatron (lexaloffle.com/voxatron.php), very interesting. \$\endgroup\$ – sbenitezb Nov 5 '13 at 2:03
  • \$\begingroup\$ You are going to need to define all the the vertices by hand. It's a pretty tedious process but can be done for simple tests such as creating cubes. A few engines have simple primitives (cube, cylinder, sphere) already built in for prototyping but if you want to create a more complex model (like a person or animal) you will probably want to learn how to use some 3D modeling software. I'm also not exactly sure what you want to do in 3D. It sounds like billboarding (creating a flat 2D plane in a 3D world that always faces the player). You might want to provide more information. \$\endgroup\$ – Benjamin Danger Johnson Nov 5 '13 at 17:11
  • 2
    \$\begingroup\$ It is unclear what is being asked here \$\endgroup\$ – Kromster Nov 6 '13 at 5:20
0
\$\begingroup\$

I want to use Images and then crop the Image and place the respective textures in their respective places.

Using Java3D, as mentioned, and an your already cropped Bufferdimage/Image (e.g. a BufferedImage):

  1. Use [TextureLoader][1]

    import com.sun.j3d.utils.image.TextureLoader;

  2. Create a texture

    Texture texImage = new TextureLoader(myCroppedbufferedImage).getTexture();
    //Texture texImage = new TextureLoader(myCroppedImage, this).getTexture();
    
  3. Set the texture, where app is an appearance object for your textured primitive

    app.setTexture (texImage);

On how to programmatically create a primitive and texture it see this example (adapted from source):

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Texture;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader; //that is the relevant part!
import com.sun.j3d.utils.universe.SimpleUniverse;

public class TextureImage extends Applet {

  private java.net.URL texImage = null;

  private SimpleUniverse u = null;

  public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    // appearance object for textured cube
    Appearance app = new Appearance();
    Texture tex = new TextureLoader(texImage, this).getTexture();
    app.setTexture(tex);
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    app.setTextureAttributes(texAttr);

    // Create textured cube and add it to the scene graph.
    Box textureCube = new Box(0.4f, 0.4f, 0.4f,
        Box.GENERATE_TEXTURE_COORDS, app);
    objTrans.addChild(textureCube);

    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
        4000, 0, 0, 0, 0, 0);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
        objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

    // Have Java 3D perform optimizations on this scene graph.
    objRoot.compile();

    return objRoot;
  }

  public TextureImage() {
  }

  public TextureImage(java.net.URL url) {
    texImage = url;
  }

  public void init() {
    if (texImage == null) {
      // the path to the image for an applet
      try {
        texImage = new java.net.URL(getCodeBase().toString()
            + "/stone.jpg");
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    }
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();

    u.addBranchGraph(scene);
  }

  public void destroy() {
    u.cleanup();
  }

  //
  // The following allows TextureImage to be run as an application
  // as well as an applet
  //
  public static void main(String[] args) {
    java.net.URL url = null;
    if (args.length > 0) {
      try {
        url = new java.net.URL("file:" + args[0]);
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    } else {
      // the path to the image for an application
      try {
        url = new java.net.URL("file:stone.jpg");
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    }
    new MainFrame(new TextureImage(url), 256, 256);
  }

}
| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.