Avatar billede fredand Forsker
27. februar 2005 - 20:25 Der er 1 løsning

J2ME - Where is my 3D cube?

Hello!

I have just started to develope some apps for J2ME. Before I have explored Java3D so perhaps I tries to merge these two technics and there for run into this problem.

My plan for my first bigger app would consist of 4 classes.

CubeGame - the midlet

CubeCanvas - the canvas to display and render 3D objects.

Cube - a class that holds everything for a 3D cube-object.

RefreshTask - a timer class that i hope will be replaced by letting CubeCanvas implemets Runnable instead.

I also use a image for the texture for the cube, that one can be found at:
http://developers.sun.com/techtopics/mobility/apis/articles/3dgraphics/texture.png

The problem is that the cube never get's displayed when I start this app. I just see a black screen with my time counter. Perhasps it is there somewhere but is out of sight I do not know??

So if any one could tell me whats wrong and how to get the cube in the display please tell me!

Below is the source code.

Best regards

Fredrik Andersson

--------------------------

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;

public class CubeGame extends MIDlet implements CommandListener
{
    static CubeGame instance;
    CubeCanvas cubeCanvas = new CubeCanvas();

    public CubeGame()
    {
        this.instance = this;
    }

    public void startApp()
    {
        Display.getDisplay(this).setCurrent(cubeCanvas);
        Command exitCommand = new Command("Exit", Command.EXIT, 0);
        cubeCanvas.addCommand(exitCommand);
        cubeCanvas.setCommandListener(this);
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
    }

    public static void quitApp()
    {
        instance.destroyApp(true);
        instance.notifyDestroyed();
        instance = null;
    }

    public void commandAction(Command c, Displayable s)
    {
        if (c.getCommandType() == Command.EXIT)
        {
            notifyDestroyed();
        }
    }
}

--------------------

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import java.util.*;
import javax.microedition.midlet.*;

public class CubeCanvas extends Canvas
{
    private Graphics3D          graphics3D;
    private Camera              camera = new Camera();
    private Light              light = new Light();
    private Transform          transform = new Transform();
    private Background          background = new Background();
    private World             world = new World();
    private byte             time = 100;
    private    Group             group = new Group();
    private Timer             timer = new Timer();
    private TimerTask         timerTask = null;
    private Cube             cube = new Cube();

    public CubeCanvas()
    {
        init();
    }

    public void init()
    {
        graphics3D = Graphics3D.getInstance();
        background.setColor(0x33ccff);

        world.addChild(group);
        group.setOrientation(15.0f, 1.0f, 0.0f, 0.0f);
        group.addChild(cube);

        world.addChild(camera);
        world.setActiveCamera(camera);

        camera.setParallel((1<<3)*1.5f, 1.0f, -(1<<3), (1<<3));

            camera.setPerspective( 60.0f,              // field of view
                    (float)getWidth()/ (float)getHeight(),  // aspectRatio
                    1.0f,      // near clipping plane
                    1000.0f ); // far clipping plane

            // create a light
            light.setColor(0xffffff);        // white light
            light.setIntensity(1.25f);          // overbright

        world.addChild(light);

    }

    public void rotateCube(short cubeId, short dircetion)
    {
    }

    public void connectCubes(short cubeId, short dircetion)
    {
    }

    public void paint(Graphics g)
    {
        if(this == null || world == null)
        {
            return;
        }

          if(timerTask != null)
            {
            timerTask.cancel();
            timerTask=null;
        }

        graphics3D.bindTarget(g);

        graphics3D.clear(background);

        Transform transform = new Transform();
            transform.postTranslate(0.0f, 0.0f, 10.0f);
            graphics3D.setCamera(camera, transform);

            // set up a "headlight": a directional light shining
            // from the direction of the camera
            graphics3D.resetLights();
            graphics3D.addLight(light, transform);

            try
            {
                graphics3D.render(world);
            }
        finally
        {
                graphics3D.releaseTarget();
        }



        g.setColor(0xFFFFFFFF);
        g.drawString(time + " sec", 10, 30, Graphics.LEFT | Graphics.TOP);


        timerTask = new RefreshTask(this);
            timer.schedule(timerTask, 1000);
        time--;

    }


}

-----------------------

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;

public class Cube extends Group
{
    private short size;
    private Image[] images;
    private VertexBuffer    vertexBuffer;    // positions, normals, colors, texcoords
    private IndexBuffer    indexBuffer;    // indices to vertexBuffer, formingtriangle strips
    private Appearance      appearance; // material, texture, compositing, ...
    private Material        material = new Material();
    private Mesh        mesh;
    private Image iImage;


    short[] vert = {
        1, 1, 1,  -1, 1, 1,  1,-1, 1,  -1,-1, 1,  // front
      -1, 1,-1,  1, 1,-1,  -1,-1,-1,  1,-1,-1,  // back
      -1, 1, 1,  -1, 1,-1,  -1,-1, 1,  -1,-1,-1,  // left
        1, 1,-1,  1, 1, 1,  1,-1,-1,  1,-1, 1,  // right
        1, 1,-1,  -1, 1,-1,  1, 1, 1,  -1, 1, 1,  // top
        1,-1, 1,  -1,-1, 1,  1,-1,-1,  -1,-1,-1 }; // bottom

    byte[] norm = {
        0, 0, 127,    0, 0, 127,    0, 0, 127,    0, 0, 127,
        0, 0,-127,    0, 0,-127,    0, 0,-127,    0, 0,-127,
      -127, 0, 0,  -127, 0, 0,  -127, 0, 0,  -127, 0, 0,
        127, 0, 0,    127, 0, 0,    127, 0, 0,    127, 0, 0,
        0, 127, 0,    0, 127, 0,    0, 127, 0,    0, 127, 0,
        0,-127, 0,    0,-127, 0,    0,-127, 0,    0,-127, 0 };

    short[] tex = {
        1, 0,      0, 0,      1, 1,      0, 1,
        1, 0,      0, 0,      1, 1,      0, 1,
        1, 0,      0, 0,      1, 1,      0, 1,
        1, 0,      0, 0,      1, 1,      0, 1,
        1, 0,      0, 0,      1, 1,      0, 1,
        1, 0,      0, 0,      1, 1,      0, 1 };

    int[] stripLen = { 4, 4, 4, 4, 4, 4 };

    //public Cube(short s, Image[] i)
    public Cube()
    {
        try
        {
            //size = s;
            //images = i;

            VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
            vertArray.set(0, vert.length/3, vert);

            VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
            normArray.set(0, norm.length/3, norm);

            VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
            texArray.set(0, tex.length/2, tex);

            VertexBuffer vertexBuffer = new VertexBuffer();
            vertexBuffer.setPositions(vertArray, 1.0f, null);      // unit scale, zerobias
            vertexBuffer.setNormals(normArray);
            vertexBuffer.setTexCoords(0, texArray, 1.0f, null);    // unit scale, zerobias

            indexBuffer = new TriangleStripArray( 0, stripLen );

            iImage = Image.createImage( "/images/texture.png" );

            Image2D image2D = new Image2D( Image2D.RGB, iImage );

            Texture2D texture = new Texture2D( image2D );
            texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
            texture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
            texture.setBlending(Texture2D.FUNC_MODULATE);

            appearance = new Appearance();
            appearance.setTexture(0, texture);
            appearance.setMaterial(material);
            material.setColor(Material.DIFFUSE, 0xFFFFFFFF);  // white
            material.setColor(Material.SPECULAR, 0xFFFFFFFF);  // white
            material.setShininess(100.0f);

            mesh = new Mesh(vertexBuffer, indexBuffer, appearance);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public short getSize()
    {
        return size;
    }

    public Image[] getImages()
    {
        return images;
    }
}

-------------------------------------------


import java.util.*;

public class RefreshTask extends TimerTask
{
    CubeCanvas cubeCanvas;

    public RefreshTask(CubeCanvas cc)
    {
        cubeCanvas = cc;
    }

    public void run()
    {
        cubeCanvas.repaint();
    }
}
Avatar billede fredand Forsker
03. marts 2005 - 13:05 #1
Hello Mates!

(Greetings from a cold Sweden -20 deg. cel. this morning very unusually so late in the winter)

At last I solved it. Now I think I got hold of this.

I use immediate mode. I do not know if this is a good thing if we talk performance.

My intention was to be able to progamatically create different objects and add them to my world. I'm not so found of creating things in a 3D-program. I rather create them my self. Now I think I know how!

Perhaps you can advise me not to do this if you see that this would leak memory or something else. Perhaps it is better to use m3g-files, I do not know. But I do not like it.

Now my app contains these 4 classes:

TestMIDlet - just the start class for everything

TestCanvas3D - this is the interesting class that contains everything for the rendering and the world to attach objects into.

Cube - All data for a 3D-cube

Pyramid - All data for a Pyramid, I guess, I have copied you Mitch

This app displays these two objects. My intention is now to create a 3D-world progamatically. So please comment this if you in any way think this is stupid.

/Fredrik

The code is below:

Code:

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import java.util.*;
import javax.microedition.midlet.*;

public class TestMIDlet extends MIDlet implements CommandListener
{

  private Display display = null;
  private TestCanvas3D testCanvas3D = new TestCanvas3D();
  private Command exitCommand = new Command("Exit", Command.ITEM, 1);

  public TestMIDlet()
  {
      display = Display.getDisplay(this);
      testCanvas3D.setCommandListener(this);
      testCanvas3D.addCommand(exitCommand);
  }

  public void startApp() throws MIDletStateChangeException
  {
      try
      {
        testCanvas3D.setTitle("TestMIDlet");
        display.setCurrent(testCanvas3D);
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
  }

      public void pauseApp()
      {
      }

      public void destroyApp(boolean unconditional) throws MIDletStateChangeException
      {
      }

  public void commandAction(Command command, Displayable displayable)
      {
      if (command == exitCommand)
      {
        try
        {
            destroyApp(false);
            notifyDestroyed();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
      }
      }
}

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import java.util.*;
import javax.microedition.midlet.*;

public class TestCanvas3D extends Canvas
{
  private  Graphics3D graphics3D = Graphics3D.getInstance();

      private Camera camera = new Camera();
  private World world = new World();
  private Background background = new Background();
  private Light light = new Light();

  private Pyramid pyramid1 = new Pyramid(0xFFFFFF00);
  private Cube cube = new Cube();

  private Transform cameraTransform = new Transform();
  private Transform worldTransform = new Transform();
  private Transform lightTransform = new Transform();

  public TestCanvas3D()
  {
      Transform transform1 = new Transform();
      transform1.postTranslate(-0.5f, 0.0f, -8.0f);
      pyramid1.setTransform(transform1);

      Transform transform2 = new Transform();
      transform2.postTranslate(0.5f, 0.0f, -8.0f);
      cube.setTransform(transform2);

      world.addChild(pyramid1);
      world.addChild(cube);
      world.setActiveCamera(camera);
      worldTransform.postTranslate(0.0f, 0.0f, 0.0f);

      lightTransform.postTranslate(0.0f, 0.0f, 10.0f);
      graphics3D.resetLights();
      graphics3D.addLight(light, lightTransform);

      float aspect = (float) getWidth() / (float) getHeight();
      camera.setPerspective(60.0f, aspect, 1.0f, 1000.0f);
      cameraTransform.postTranslate(0.0f, 0.0f, 0.0f);
      graphics3D.setCamera(camera, cameraTransform);
  }

  protected void paint(Graphics g)
  {
      graphics3D.bindTarget(g);

      try
      {
        graphics3D.clear(null);
        graphics3D.render(world, worldTransform);
      }
      finally
      {
        graphics3D.releaseTarget();
      }
  }
}

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.*;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;

public class Cube extends Group
{
  private short size;
  private Image[] images;
  private VertexBuffer    vertexBuffer;    // positions, normals, colors, texcoords
  private IndexBuffer    indexBuffer;    // indices to vertexBuffer, formingtriangle strips
  private Appearance      appearance; // material, texture, compositing, ...
  private Material        material = new Material();
  private Mesh      mesh;
  private Image iImage;


  short[] vert = {
      1, 1, 1,  -1, 1, 1,  1,-1, 1,  -1,-1, 1,  // front
      -1, 1,-1,  1, 1,-1,  -1,-1,-1,  1,-1,-1,  // back
      -1, 1, 1,  -1, 1,-1,  -1,-1, 1,  -1,-1,-1,  // left
      1, 1,-1,  1, 1, 1,  1,-1,-1,  1,-1, 1,  // right
      1, 1,-1,  -1, 1,-1,  1, 1, 1,  -1, 1, 1,  // top
      1,-1, 1,  -1,-1, 1,  1,-1,-1,  -1,-1,-1 }; // bottom

  byte[] norm = {
      0, 0, 127,    0, 0, 127,    0, 0, 127,    0, 0, 127,
      0, 0,-127,    0, 0,-127,    0, 0,-127,    0, 0,-127,
      -127, 0, 0,  -127, 0, 0,  -127, 0, 0,  -127, 0, 0,
      127, 0, 0,    127, 0, 0,    127, 0, 0,    127, 0, 0,
      0, 127, 0,    0, 127, 0,    0, 127, 0,    0, 127, 0,
      0,-127, 0,    0,-127, 0,    0,-127, 0,    0,-127, 0 };

  short[] tex = {
      1, 0,      0, 0,      1, 1,      0, 1,
      1, 0,      0, 0,      1, 1,      0, 1,
      1, 0,      0, 0,      1, 1,      0, 1,
      1, 0,      0, 0,      1, 1,      0, 1,
      1, 0,      0, 0,      1, 1,      0, 1,
      1, 0,      0, 0,      1, 1,      0, 1 };

  int[] stripLen = { 4, 4, 4, 4, 4, 4 };

  //public Cube(short s, Image[] i)
  public Cube()
  {
      try
      {
        //size = s;
        //images = i;

        VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
        vertArray.set(0, vert.length/3, vert);

        VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
        normArray.set(0, norm.length/3, norm);

        VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
        texArray.set(0, tex.length/2, tex);

        VertexBuffer vertexBuffer = new VertexBuffer();
        vertexBuffer.setPositions(vertArray, 1.0f, null);      // unit scale, zerobias
        vertexBuffer.setNormals(normArray);
        vertexBuffer.setTexCoords(0, texArray, 1.0f, null);    // unit scale, zerobias

        indexBuffer = new TriangleStripArray( 0, stripLen );

        iImage = Image.createImage( "/images/texture.png" );

        Image2D image2D = new Image2D( Image2D.RGB, iImage );

        Texture2D texture = new Texture2D( image2D );
        texture.setFiltering(Texture2D.FILTER_NEAREST, Texture2D.FILTER_NEAREST);
        texture.setWrapping(Texture2D.WRAP_CLAMP, Texture2D.WRAP_CLAMP);
        texture.setBlending(Texture2D.FUNC_MODULATE);

        appearance = new Appearance();
        appearance.setTexture(0, texture);
        appearance.setMaterial(material);
        material.setColor(Material.DIFFUSE, 0xFFFFFFFF);  // white
        material.setColor(Material.SPECULAR, 0xFFFFFFFF);  // white
        material.setShininess(100.0f);

        mesh = new Mesh(vertexBuffer, indexBuffer, appearance);

        addChild(mesh);
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
  }

  public short getSize()
  {
      return size;
  }

  public Image[] getImages()
  {
      return images;
  }
}

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import java.util.*;
import javax.microedition.midlet.*;

public class Pyramid extends Group
{

  Appearance appearance = new Appearance();
  IndexBuffer triangles = null;
  VertexBuffer vertices = new VertexBuffer();
  Mesh mesh;

  public Pyramid(int color)
  {
      // Appearance and materials;
      Material material = new Material();
      appearance.setMaterial(material);
      material.setColor(Material.DIFFUSE, color ); // yellow

      // coordinates - These are the vertex positions
      short[] coordinates = { -1, -2, -1,    1, -2, -1,    0, 2, -1 };
      VertexArray vaCoordinates = new VertexArray( (coordinates.length/3), 3, 2);
      vaCoordinates.set(0, (coordinates.length/3), coordinates);
      vertices.setPositions(vaCoordinates, 1, null);

      // Normals - first two point back at the light, the last normal points down just as an experiment
      byte[] normals = { 0, 0, 127,    0, 0, 127,    0, -128, 0 };
      VertexArray vaNormals = new VertexArray( (normals.length/3), 3, 1);
      vaNormals.set(0, (normals.length/3), normals);
      vertices.setNormals(vaNormals);

      int[] coordIndex = {3};

      triangles = new TriangleStripArray(0, coordIndex);

      VertexBuffer vertexBuffer = new VertexBuffer();
      vertexBuffer.setPositions(vaCoordinates, 1.0f, null);      // unit scale, zerobias
      vertexBuffer.setNormals(vaNormals);

      mesh = new Mesh(vertexBuffer, triangles, appearance);

      addChild(mesh);
  }
}



Remeber that the cube uses the png mentioned above
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester