Sphere in JME3

hi,

I cannot create a Sphere in Jme3.

I tried this code

Sphere sp = new Sphere( 10, 10, 1f);

Geometry g=new Geometry();

g.setMesh(sp);

rootNode.attachChild(g);

and got this error

SEVERE Application 6:28:31 PM Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at com.jme3.renderer.queue.OpaqueComparator.compare(OpaqueComparator.java:58)

at com.jme3.renderer.queue.OpaqueComparator.compare(OpaqueComparator.java:9)

at java.util.Arrays.mergeSort(Arrays.java:1270)

at java.util.Arrays.mergeSort(Arrays.java:1281)

at java.util.Arrays.mergeSort(Arrays.java:1281)

at java.util.Arrays.mergeSort(Arrays.java:1281)

at java.util.Arrays.sort(Arrays.java:1210)

at com.jme3.renderer.queue.GeometryList.sort(GeometryList.java:72)

at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:93)

at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:142)

at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:332)

at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:460)

at com.jme3.renderer.RenderManager.render(RenderManager.java:474)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:165)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:112)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:162)

at java.lang.Thread.run(Thread.java:619)

AL lib: ALc.c:1352: exit(): closing 1 Device

AL lib: ALc.c:1329: alcCloseDevice(): destroying 1 Context

AL lib: alSource.c:2361: alcDestroyContext(): deleting 16 Source(s)





long ago I used to create Sphere like this

Sphere sp = new Sphere("My sphere", 10, 10, 1f);  // from jme2 tut

Spatial s=new Geometry("sphere",sp);

rootNode.attachChild(s);

but it doesnt work in jme3, Eclipse told me to remove argument to match (int,int,float)

so I tried this

Sphere sp = new Sphere( 10, 10, 1f);

Spatial s=new Geometry("sphere",sp); //Spatial s=new Geometry("",sp);Spatial s=new Geometry(null,sp);

rootNode.attachChild(s);



and got nullpoint error

java.lang.NullPointerException

at com.jme3.renderer.queue.OpaqueComparator.compare(OpaqueComparator.java:58)

at com.jme3.renderer.queue.OpaqueComparator.compare(OpaqueComparator.java:9)

at java.util.Arrays.mergeSort(Arrays.java:1270)

at java.util.Arrays.mergeSort(Arrays.java:1281)

at java.util.Arrays.mergeSort(Arrays.java:1281)

at java.util.Arrays.mergeSort(Arrays.java:1281)

at java.util.Arrays.sort(Arrays.java:1210)

at com.jme3.renderer.queue.GeometryList.sort(GeometryList.java:72)

at com.jme3.renderer.queue.RenderQueue.renderGeometryList(RenderQueue.java:93)

at com.jme3.renderer.queue.RenderQueue.renderQueue(RenderQueue.java:142)

at com.jme3.renderer.RenderManager.flushQueue(RenderManager.java:332)

at com.jme3.renderer.RenderManager.renderViewPort(RenderManager.java:460)

at com.jme3.renderer.RenderManager.render(RenderManager.java:474)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:165)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:112)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:162)

at java.lang.Thread.run(Thread.java:619)

AL lib: ALc.c:1352: exit(): closing 1 Device



in javadoc I still see parameter name, and the para isnt in the function.



Sphere

public Sphere(int zSamples,

              int radialSamples,

              float radius)

Constructs a sphere. All geometry data buffers are updated automatically. Both zSamples and radialSamples increase the quality of the generated sphere.



    Parameters:

        name - Name of the sphere.

        zSamples - The number of samples along the Z.

        radialSamples - The number of samples along the radial.

        radius - The radius of the sphere.



that was all information I managed to collect.

so what should I do to create a sphere in jme3?


You shouldn't use the empty constructor: Geometry(). Instead, use Geometry(String name) or Geometry(String name, Mesh mesh). This works for me:

       Sphere s = new Sphere(16, 16, 1);
        Box b = new Box(Vector3f.ZERO, 1, 1, 1);
        Geometry geom = new Geometry("Box", s);
        Material mat = new Material(assetManager, "Common/MatDefs/Misc/SolidColor.j3md");
        mat.setColor("m_Color", ColorRGBA.Blue);
        geom.setMaterial(mat);
        rootNode.attachChild(geom);

Yeah, and don't forget to specify a material, that's the most important part :slight_smile:

Momoko_Fan said:

Yeah, and don't forget to specify a material, that's the most important part :)

Forgetting to specify a material won't throw an exception though, right? You just wont be able to see the sphere  :P

it works for me now

thank you very much

and how silly I was ^^ sorry for troubling you guys ^^



I just read your post and throw the mat away and I got nullpoint error

so dont forget about material ^^

yeah and how did you put the code into that pretty box ?






xieu90 said:

it works for me now
thank you very much
and how silly I was ^^ sorry for troubling you guys ^^

I just read your post and throw the mat away and I got nullpoint error
so dont forget about material ^^
yeah and how did you put the code into that pretty box ?

use the [ code ] tags, it's the button with the # on it.

I guess if no Material is set, when it comes to the rendering and it tries to access the material, the null pointer exception is thrown. Wouldn't it make sense to put a check in the code, and just not render the object if it doesn't have a material?

Okay, I added a check in my local copy, that will show a more understandable error message.

Momoko_Fan said:

Okay, I added a check in my local copy, that will show a more understandable error message.

Cool. I had a quick look at RenderManager, and it seems quite easy to add a check at line 246 (you probably already know that though :P)

You're fast! But no, that part is actually called after the NullPointer happens (which is in the render queue), the proper location is right before geometry is queued, at line 306.

Momoko_Fan said:

You're fast! But no, that part is actually called after the NullPointer happens (which is in the render queue), the proper location is right before geometry is queued, at line 306.


Ah, yes. That makes more sense. So are you checking to make sure that the Geometry has a Material before adding it to the Render queue? (I hope I'm not sounding difficult, I just want to understand properly  :))
InfernoZeus said:

Momoko_Fan said:

You're fast! But no, that part is actually called after the NullPointer happens (which is in the render queue), the proper location is right before geometry is queued, at line 306.


Ah, yes. That makes more sense. So are you checking to make sure that the Geometry has a Material before adding it to the Render queue? (I hope I'm not sounding difficult, I just want to understand properly  :))

yeah