(SOLVED) adding a dynamically generated mesh during update crashes?

solved: there were two issues: one should not use null for TexCoords when creating a TriMesh, and one should call the rootNode.update… functions after adding something to them. Sorry to everyone for the inconvenciences.





Hi,



I am almost done with my project and would also like to add a view screenshots to the showcase, but first I need to fix one last but very annoying misunderstanding which I seem to have with jme2.



I generate a mesh during initialization phase of SimpleGame and show it and it works allright, as can be witnessed in the screenshot below. But then I'd like to regenerate it. So, according to the tutorials I do this during simplUpdate():



GameTaskQueueManager.getManager().update( new Callable<Void>()

         {

             public Void call() throws Exception

             {

                generateAndAddNewMesh();

            chaseCamera.setTarget(targetWord);  

          return null;

             }

         } );



But unfortunately, right after successfully finishing the generation of the mesh and even showing exactly one frame, this crashes with this error:



java.lang.NullPointerException

at com.jme.renderer.lwjgl.LWJGLRenderer.predrawGeometry(LWJGLRenderer.java:1479)

at com.jme.renderer.lwjgl.LWJGLRenderer.draw(LWJGLRenderer.java:999)

at com.jme.scene.TriMesh.draw(TriMesh.java:240)

at com.jme.scene.Spatial.onDraw(Spatial.java:429)

at com.jme.scene.Node.draw(Node.java:518)

at com.jme.scene.Spatial.onDraw(Spatial.java:429)

at com.jme.scene.Node.draw(Node.java:518)

at com.jme.scene.Spatial.onDraw(Spatial.java:429)

at com.jme.renderer.lwjgl.LWJGLRenderer.draw(LWJGLRenderer.java:1220)

at com.jme.app.SimpleGame.render(SimpleGame.java:81)

at com.jme.app.BaseGame.start(BaseGame.java:87)



I found a fix for this error somewhere to update LWJGLRenderer to read like this:



if (texC != null && texC.coords != null ) {

                   // make sure only the necessary texture coords are sent

                   // through on old cards.

                   oldLimit = texC.coords.limit();

                   texC.coords.limit(g.getVertexCount() * texC.perVert);

               }



this fixes the NullPointerException, but introduces a hard crash in the driver instead:



#

An unexpected error has been detected by Java Runtime Environment:

#

 EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x69295000, pid=5400, tid=5216

#

Java VM: Java HotSpot™ Client VM (10.0-b19 mixed mode windows-x86)

Problematic frame:

C  [atioglxx.dll+0x265000]

#

If you would like to submit a bug report, please visit:

  http://java.sun.com/webapps/bugreport/crash.jsp

The crash happened outside the Java Virtual Machine in native code.

See problematic frame for where to report the bug.

#


 T H R E A D  

Current thread (0x026d9000):  JavaThread "main" [_thread_in_native, id=5216, stack(0x00170000,0x001c0000)]

siginfo: ExceptionCode=0xc0000005, reading address 0x00000000

Registers:
EAX=0x11360010, EBX=0xeec9fff0, ECX=0x00000010, EDX=0x001bf640
ESP=0x001bf608, EBP=0x00000002, ESI=0x69c1a2c0, EDI=0x11360020
EIP=0x69295000, EFLAGS=0x00010287

...

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
J  org.lwjgl.opengl.GL11.nglDrawElements(IIILjava/nio/Buffer;IJ)V
J  org.lwjgl.opengl.GL11.glDrawElements(ILjava/nio/IntBuffer;)V
J  com.jme.renderer.lwjgl.LWJGLRenderer.draw(Lcom/jme/scene/TriMesh;)V
J  com.jme.scene.TriMesh.draw(Lcom/jme/renderer/Renderer;)V
J  com.jme.scene.Spatial.onDraw(Lcom/jme/renderer/Renderer;)V
J  com.jme.scene.Node.draw(Lcom/jme/renderer/Renderer;)V
J  com.jme.scene.Spatial.onDraw(Lcom/jme/renderer/Renderer;)V
J  com.jme.scene.Node.draw(Lcom/jme/renderer/Renderer;)V
J  com.jme.scene.Spatial.onDraw(Lcom/jme/renderer/Renderer;)V
J  com.jme.renderer.lwjgl.LWJGLRenderer.draw(Lcom/jme/scene/Spatial;)V
J  com.jme.app.SimpleGame.render(F)V
v  ~BufferBlob::Interpreter
v  ~BufferBlob::Interpreter
v  ~BufferBlob::StubRoutines (1)

Apparently I am doing something bad to some buffer somewhere. Does anyone have any idea what that could possibly be? I am kind of out of options currently... When generating the mesh I am messing around with FloatBuffers a lot. Do I have to clear them manually or something? Or am I completely offtrack?

Have you tried to replace the old meshdata ? instead of overwriting it

(buffer = new float[] … ect)

I tried both. either



vertices.clear();

vertices.limit(newSize);



or



vertices = BufferUtils.createVector3Buffer(3newPosMap.size());



but the first version only works if the new buffer is smaller than the old one.



I had a breakthrough by creating a dummy TexCoords object and filling it with a zeroed float buffer (though I am not positive about why it sometimes crashes with an IllegalArgumentException from Buffer.java from within LWJGLRenderer where it is trying to set it to a new limit. To which size does it need to be set? I am setting it 4
numOfTris and I set the perVert to 4)



With this dummy TexCoords it doesn't crash mostly anymore, but the new object is all messed up (see screenshot). It seems neither lighting affects it, nor are the normals correct (or being taken into account at all). also the colors are suddenly all very transparent. ?! At least the vertex colors themselves are okay. I don't understand it - the mesh generator class - I create an entirely new one, so it can't be garbage from an old instance. Apart from the mesh, there are only 4 lights, which also work for the first mesh, and that's all there is to it. Apparently I am having some kinds of side-effects but I don't know where to begin trying to track them down…

edit: I just tried not generating the first mesh upon initialization, but only after some user action (as were previously the second meshes). An now it also happens for the first mesh. So, it seems I am doing something differently when generating/adding the mesh during simpleInitGame() (here it works) and during simpleUpdate() (here it doesn't work, despite using the Callable). Does that ring some bell?

edit edit: even more interesting - if I do not remove the first mesh from the initphase, and add the new one anyway, then the new one is almost okay: only the text is missing (it's the 3DText from jme created with Text3D myText = myFont.createText(" my word"), 3f, 0); ) from the labels of the new one. Go figure… :frowning: