Avatar billede fredand Forsker
22. marts 2005 - 11:10 Der er 1 løsning

Problems with a Polygon in J2ME (test source valid)

Hello!

I'm struggeling with geometry cordinates and texture cordinates.

I trying to create a polygon with 8 corners, just a plane surface. Something like this:
  __
/  \
|    |
\__/

I try to fill it with a texture but it will not be correct. Below you got a image that shows what I end up with:

http://www.dsv.su.se/~fr-ander/cubes.jpg

Acording to Andrew Davisons report at...

http://fivedots.coe.psu.ac.th/~ad/jg/objm3g/objM3G.pdf

...and the m3g API at TriangleStripArray the triangles will be created like:
"For example, the strip S = (2, 0, 1, 4) defines two triangles: (2, 0, 1) and (0, 1, 4)."

To me that looks like I should be able just to specify each corner of the polygon to be able to create a 8-corner polygon. That will not fill the total polygon but all the sides of it how ever.

At the image below i try to show how I guess this work if you try out my 8-corner polygon. The numbers are the indecies of the cordinates for the corners. The filled areas are the triangles that will be created if the above theory is correct and I have understand that correct.

http://www.dsv.su.se/~fr-ander/cubes.gif

But my code only generates the first image (http://www.dsv.su.se/~fr-ander/cubes.jpg) and I can't understand why?

So if you could help me out here please let me know!

Best regards
Fredrik

Below is the test sorce for this. All you need is a image with the power of 2.

//The polygon
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 PolygonSurface extends Group
{
    private Image image;
    private VertexBuffer    vertexBuffer;
    private IndexBuffer    indexBuffer;
    private Material        material = new Material();

    short[] vert = {-10,-9,0,    -10,9,0,    -9,10,0,    9,10,0,        10,9,0,        10,-9,0,    9,-10,0,    -9,-10,0,    -10,-9,0};//,            -10,9,0,    10,-9,0,    10,9,0,            -10,-9,0};
    short[] tex = {0, 18,        0, 2,        2, 0,        18, 0,        20, 2,        20, 18,        18, 20,        2, 20,        0, 18};//,                0, 2,         20, 18,        20, 2,            0,18,};
    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,    0,0,127};//,            0,0,127,    0,0,127,    0,0,127,        0,0,127};

    int[]    stripLen = {9};

    public PolygonSurface()
    {
        try
        {
            image = Image.createImage( "/images/up/1.png" );

            material.setColor(Material.DIFFUSE, 0xFFFFFFFF);
            material.setColor(Material.SPECULAR, 0xFFFFFFFF);
            material.setShininess(100.0f);

            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, 0.1f, null);
            vertexBuffer.setNormals(normArray);
            vertexBuffer.setTexCoords(0, texArray, 0.05f, null);

            indexBuffer = new TriangleStripArray( 0, stripLen );

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

            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 appearance = new Appearance();
            appearance.setTexture(0, texture);
            appearance.setMaterial(material);

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

            addChild(mesh);

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

//The Canvas
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 PolygonSurface polygonSurface = new PolygonSurface();

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

    public TestCanvas3D()
    {
        Transform transform4 = new Transform();
        transform4.postTranslate(0.0f, 0.0f, 0.0f);
        //transform4.postRotate(300f, 0.0f, 1.0f, 0.0f);
        polygonSurface.setTransform(transform4);

        world.addChild(polygonSurface);
        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, 3.0f);
        graphics3D.setCamera(camera, cameraTransform);
    }

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

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

//The Midlet
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();
            }
        }
      }
}
Avatar billede fredand Forsker
22. marts 2005 - 13:02 #1
It look like this solved the problem!

PolygonMode polygonMode = new PolygonMode ();
polygonMode.setCulling(PolygonMode.CULL_NONE);
polygonMode.setPerspectiveCorrectionEnable(true);

Appearance appearance = new Appearance();
appearance.setTexture(0, texture);
appearance.setMaterial(material);
appearance.setPolygonMode(polygonMode);

Best regards
Fredrik
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