Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Riven (posted 2014-07-14 18:02:53, viewed 66 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83  
public abstract class MultiDrawBuilder {
   public static final int FRAME_SPAN = 3;
   public final MultiDrawCommand cmd;
   public final DrawStrategy drawStrategy;
   private int frameCounter;
   private int firstIndex, baseVertex, baseInstance;
   private int lastCmd, drawCount;

   public MultiDrawBuilder(MultiDrawCommand cmd) {
      this.cmd = cmd;

      if(GLContext.getCapabilities().GL_ARB_multi_draw_indirect)
         drawStrategy = DrawStrategy.MULTI_DRAW_INDIRECT;
      else if(GLContext.getCapabilities().GL_ARB_base_instance)
         drawStrategy = DrawStrategy.DRAW_INSTANCED_BASE_VERTEX_BASE_INSTANCE;
      else
         throw new IllegalStateException();
   }

   private static enum DrawStrategy {
      MULTI_DRAW_INDIRECT, DRAW_INSTANCED_BASE_VERTEX_BASE_INSTANCE
   }

   public int beginFrame() {
      drawCount = 0;

      if(++frameCounter % FRAME_SPAN == 0) {
         lastCmd = 0;

         baseVertex = 0;
         firstIndex = 0;
         baseInstance = 0;

         cmd.resetStructView();

         this.onBufferReset();
      }

      return frameCounter - 1;
   }

   public abstract void onBufferReset();

   public abstract void beginDraw();

   public abstract void endDraw();

   protected void endDraw(int vertexCount, int indexCount, int instanceCount) {
      cmd.indexCount(indexCount);
      cmd.instanceCount(instanceCount);
      cmd.firstIndex(firstIndex);
      cmd.baseVertex(baseVertex);
      cmd.baseInstance(baseInstance);

      cmd.advanceStructView();

      firstIndex += indexCount;
      baseVertex += vertexCount;
      baseInstance += instanceCount;

      drawCount += 1;
      lastCmd += 1;
   }

   public void render() {
      if(drawStrategy == DrawStrategy.MULTI_DRAW_INDIRECT) {
         glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_SHORT, //
               (lastCmd - drawCount) * MultiDrawCommand.stride_bytes, // 
               drawCount, MultiDrawCommand.stride_bytes);
      }

      if(drawStrategy == DrawStrategy.DRAW_INSTANCED_BASE_VERTEX_BASE_INSTANCE) {
         cmd.setStructView(lastCmd - drawCount);
         for(int d = 0; d < drawCount; d++) {
            glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, //
                  cmd.indexCount(), GL_UNSIGNED_SHORT, 0L + (cmd.firstIndex() << 1), //
                  cmd.instanceCount(), cmd.baseVertex(), cmd.baseInstance());

            cmd.advanceStructView();
         }
      }
   }
}


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28  
         drawCmd = new MultiDrawCommand(multiDrawCommandBuf.buffer.asIntBuffer());

         builder = new MultiDrawBuilder(drawCmd) {
            @Override
            public void onBufferReset() {
               vertexBuf.buffer.position(0);
               indexBuf.buffer.position(0);
               idInstanceDrawBuf.buffer.position(0);
            }

            private int vb, ib, ii;

            @Override
            public void beginDraw() {
               vb = vertexBuf.buffer.position();
               ib = indexBuf.buffer.position();
               ii = idInstanceDrawBuf.buffer.position();
            }

            @Override
            public void endDraw() {
               int vertexCount = (vertexBuf.buffer.position() - vb) / vertexBufferStride;
               int indexCount = (indexBuf.buffer.position() - ib) >> 1;
               int instanceCount = (idInstanceDrawBuf.buffer.position() - ii) / idInstanceDrawStride;

               this.endDraw(vertexCount, indexCount, instanceCount);
            }
         };


1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53  
         int frame = builder.beginFrame();

         for(int drawID = 0; drawID < drawCount; drawID++) {

            int triangleCount = circleEdges[drawID];

            // draw a circle using N triangles

            builder.beginDraw();

            {
               final float scale = 1.0f / (20 + ((frame % 30)) * 0.05f);

               // vertex-attributes
               float x0 = 0.0f;
               float y0 = 0.0f;
               vertexBuf.buffer.putFloat(x0).putFloat(y0).putFloat(0.0f); // position
               vertexBuf.buffer.put((byte) 0xff).put((byte) 0xff).put((byte) 0x00).put((byte) 0xcc); // color

               for(int i = 0; i < triangleCount; i++) {
                  // vertex-attributes
                  float x1 = x0 + (float) Math.cos((float) (i + 0) / triangleCount * (Math.PI * 2.0)) * scale;
                  float y1 = y0 + (float) Math.sin((float) (i + 0) / triangleCount * (Math.PI * 2.0)) * scale;
                  vertexBuf.buffer.putFloat(x1).putFloat(y1).putFloat(0.0f); // position
                  vertexBuf.buffer.put((byte) 0xff).put((byte) 0x00).put((byte) 0x00).put((byte) 0xcc); // color

                  // vertex-attributes
                  float x2 = x0 + (float) Math.cos((float) (i + 1) / triangleCount * (Math.PI * 2.0)) * scale;
                  float y2 = y0 + (float) Math.sin((float) (i + 1) / triangleCount * (Math.PI * 2.0)) * scale;
                  vertexBuf.buffer.putFloat(x2).putFloat(y2).putFloat(0.0f); // position
                  vertexBuf.buffer.put((byte) 0x00).put((byte) 0xff).put((byte) 0x00).put((byte) 0xcc); // color

                  // indices
                  indexBuf.buffer.putShort((short) 0);
                  indexBuf.buffer.putShort((short) (1 + i * 2 + 0));
                  indexBuf.buffer.putShort((short) (1 + i * 2 + 1));
               }
            }

            int instanceCount = circleInstances[drawID];
            {
               // fake gl_InstanceID and gl_DrawID
               for(int instanceID = 0; instanceID < instanceCount; instanceID++) {
                  idInstanceDrawBuf.buffer.putInt(instanceID).putInt(instanceCount).putInt(drawID).putInt(drawCount);
               }
            }

            builder.endDraw();
         }

         glBindVertexArray(vaoId);
         builder.render();
         glBindVertexArray(0);





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (59 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!