QuadBatch and texturing the quads

I'm trying to put texture on the quads of a quadbatch, but setting the TextureState to the QuadBatch doesnt work, or just partially. The color of the Quads are of one color and doesnt show the texture correctly. What should I do?



      QuadBatch[] qbStrips = new QuadBatch[10*10];
      Texture qtexture = TextureManager.loadTexture("./data/textures/low/"+"grass21.jpg",Texture.MM_LINEAR,
                Texture.FM_LINEAR);
      
      
      //qtexture.setWrap(Texture.WM_WRAP_S_WRAP_T);
      qtexture.setApply(Texture.AM_REPLACE);
      //float sc = -0.0001f;
      //qtexture.setScale(new Vector3f(sc,sc,sc));
      //qtexture.setTranslation(new Vector3f(1f,0,+1f));
      //qtexture.setRotation(J3DCore.qT);

      TextureState ts = getDisplay().getRenderer().createTextureState();
      ts.setTexture(qtexture);
      
        ts.setEnabled(true);

       int c = 0;
          for (int x=-5; x<5; x++) {
                for (int y=-5; y<5; y++) {
                QuadBatch qbQuads = new QuadBatch();
              qbQuads.setMode(QuadBatch.QUADS);
              qbQuads.setModelBound(new BoundingBox());
                      
               qbQuads.setRenderState(ts);

         
            qbQuads.setVertexBuffer(BufferUtils.createFloatBuffer(getVertsQuad(x, 0, y)));
          int[] buff = new int[QUAD_QUANTITY*QUAD_QUANTITY*4];
          for (int i=0; i<QUAD_QUANTITY*QUAD_QUANTITY*4; i++) buff[i] = i;
          qbQuads.setIndexBuffer(BufferUtils.createIntBuffer(buff));//new int[] {0, 1, 2, 3, 4, 5}));
          qbStrips[c++] = qbQuads;
                }
       }
          
       bmesh = new BatchMesh("batches", (GeomBatch[])qbStrips);
       bmesh.updateRenderState();
       cRootNode.attachChild(bmesh);



Any ideas? Anyone who did this successfully?

I've tried to mess with Texture's functions (rotation, scale, wrap), but couldnt find a working solution. Always single coloured Quads, instead of the texture...

Now, when I try to set TextureBuffer with coordinates I receive the following exception:





java.lang.IllegalArgumentException

at java.nio.Buffer.limit(Unknown Source)

at com.jme.renderer.lwjgl.LWJGLRenderer.predrawGeometry(Unknown Source)

at com.jme.renderer.lwjgl.LWJGLRenderer.draw(Unknown Source)

at com.jme.scene.batch.QuadBatch.draw(Unknown Source)

at com.jme.scene.batch.GeomBatch.onDraw(Unknown Source)

at com.jme.scene.BatchMesh.draw(Unknown Source)



The code:



           FloatBuffer tbuf = BufferUtils.createVector2Buffer(4);
             tbuf.put(0.0f).put(0.3f);
             tbuf.put(0.3f).put(0.0f);
             tbuf.put(0.3f).put(0.3f);
             tbuf.put(0.0f).put(0.0f);
             qbQuads.setTextureBuffer(tbuf,0);

you might need to rewind your buffer

Where exactly. Just adding after


           FloatBuffer tbuf = BufferUtils.createVector2Buffer(4);
             tbuf.put(0.0f).put(0.3f);
             tbuf.put(0.3f).put(0.0f);
             tbuf.put(0.3f).put(0.3f);
             tbuf.put(0.0f).put(0.0f);
             qbQuads.setTextureBuffer(tbuf,0);
            qbQuads.setRenderState(ts);
             tbuf.rewind();


or


           FloatBuffer tbuf = BufferUtils.createVector2Buffer(4);
             tbuf.put(0.0f).put(0.3f);
             tbuf.put(0.3f).put(0.0f);
             tbuf.put(0.3f).put(0.3f);
             tbuf.put(0.0f).put(0.0f);
             tbuf.rewind();
             qbQuads.setTextureBuffer(tbuf,0);
            qbQuads.setRenderState(ts);



didnt help. :(

Anyway, is it possible to texturize a QuadBatch? I couldn't find any example on the web...

Quads have not received a lot of attention, so yeah, it may well be a bug.  I'll look into it if I get some time this week.

Thanks for giving attention to it. It would be very nice to have a working example, to see, if my code is wrong, or is it really some kind of bug or an unimplemented feature?



Than let me ask a question if I can workaround the whole thing. So the thing is, I want batched (not dynamic, but static, unmoving in the first round) Grass in my JME project.  I've found that QuadBatches are very fast rendered in big quantity too, so I tried to texturize it with my texture (maybe later I want to use transparent textured bigger quads, not thin grass like quads) but that didnt work. So what do you advice, how to create grass in a JME app without great performance loss? (I've tried with blender plane meshes with transparent texture, but it was very slow in big quantity.)

You might find this thread of interest:



http://www.jmonkeyengine.com/jmeforum/index.php?topic=3362.0

renanse said:

You might find this thread of interest:

http://www.jmonkeyengine.com/jmeforum/index.php?topic=3362.0



Thanks! I've already stumbled into that thread... I will read it again with more focus on my project's need... I think it won't be easy to tune, but not impossible.

QuadBatch would be more easy to tune, so looking forward to any news on any example or success with texturizing it!

FYI:  MrCoder just informed me that he was able to get texturing to work on all mesh batch types in the mentioned Test by simply assigning proper tex coords and a texture.  I'll let him describe any steps or update the test though.

Updated the test(TestBatchMesh) and added texturing, so have a look at that. Nothing special, cause every batch type is textured exactly the same way, so there's nothing special with QuadBatches(only the indexing is different).

Thanks for the quick solution!! :slight_smile: