Advertisement
Guest User

Untitled

a guest
Feb 26th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.21 KB | None | 0 0
  1. public interface Drawable {
  2.     public void compileProgram();
  3.  
  4.     public Program getProgram();
  5.  
  6.     default public boolean isTessellated() {
  7.         return false;
  8.     }
  9.  
  10.     default public boolean isInstanced() {
  11.         return false;
  12.     }
  13.  
  14.     default public int getInstancesCount() {
  15.         return 0;
  16.     }
  17.  
  18.     public int getDataSize();
  19.  
  20.     public FloatBuffer putData(final FloatBuffer dataBuffer);
  21.  
  22.     public int getDataMode();
  23.  
  24.     public boolean isShadowReceiver();
  25.  
  26.     public boolean isShadowCaster();    //TODO use for AABB calculations
  27.  
  28.     default public void drawDepthPass(final int offset, final Program depthNormalProgram, final Program depthTessellationProgram) {
  29.         Program depthProgram = (isTessellated()) ? depthTessellationProgram : depthNormalProgram;
  30.         if (isInstanced()) {
  31.             depthProgram.use().drawArraysInstanced(getDataMode(), offset, getDataSize(), getInstancesCount());
  32.         }
  33.         else {
  34.             depthProgram.use().drawArrays(getDataMode(), offset, getDataSize());
  35.         }
  36.     }
  37.  
  38.     default public void draw(final int offset) {
  39.         if (isInstanced()) {
  40.             getProgram().use().drawArraysInstanced(getDataMode(), offset, getDataSize(), getInstancesCount());
  41.         }
  42.         else {
  43.             getProgram().use().drawArrays(getDataMode(), offset, getDataSize());
  44.         }
  45.     }
  46.  
  47.     default public void delete() {
  48.         getProgram().delete();
  49.     }
  50.  
  51.     public static int countDataSize(final Collection<Drawable> drawables) {
  52.         return drawables.stream()
  53.                 .mapToInt(Drawable::getDataSize)
  54.                 .sum();
  55.     }
  56.  
  57.     public static FloatBuffer putAllData(final List<Drawable> drawables) {
  58.         FloatBuffer dataBuffer = BufferUtils.createFloatBuffer(countDataSize(drawables) * 3);
  59.         drawables.stream().forEachOrdered(drawable -> drawable.putData(dataBuffer));
  60.         return (FloatBuffer)dataBuffer.clear();
  61.     }
  62.  
  63.     public static void drawAllDepthPass(final List<Drawable> drawables, final Program depthNormalProgram, final Program depthTessellationProgram) {
  64.         int offset = 0;
  65.         for (Drawable drawable : drawables) {
  66.             if (drawable.isShadowReceiver()) {
  67.                 drawable.drawDepthPass(offset, depthNormalProgram, depthTessellationProgram);
  68.             }
  69.             offset += drawable.getDataSize();   //TODO count offset only if not shadow receiver?
  70.         }
  71.     }
  72.  
  73.     public static void drawAll(final List<Drawable> drawables) {
  74.         int offset = 0;
  75.         for (Drawable drawable : drawables) {
  76.             drawable.draw(offset);
  77.             offset += drawable.getDataSize();
  78.         }
  79.     }
  80.  
  81.     public static void deleteAll(final List<Drawable> drawables) {
  82.         drawables.stream().forEach(Drawable::delete);
  83.     }
  84. }
  85.  
  86. //---
  87.  
  88. public interface TessellatedDrawable extends Drawable {
  89.     @Override
  90.     default public boolean isTessellated() {
  91.         return true;
  92.     }
  93. }
  94.  
  95. //---
  96.  
  97. public interface InstancedDrawable extends Drawable {
  98.     @Override
  99.     default public boolean isInstanced() {
  100.         return true;
  101.     }
  102.    
  103.     @Override
  104.     public int getInstancesCount();
  105. }
  106.  
  107. //---
  108.  
  109. public class Box implements TessellatedDrawable, InstancedDrawable {
  110.     //<editor-fold defaultstate="collapsed" desc="keep-imports">
  111.     static {
  112.         int KEEP_LWJGL_IMPORTS = GL_2_BYTES | GL_ALIASED_LINE_WIDTH_RANGE | GL_ACTIVE_TEXTURE | GL_BLEND_COLOR | GL_ARRAY_BUFFER | GL_ACTIVE_ATTRIBUTE_MAX_LENGTH | GL_COMPRESSED_SLUMINANCE | GL_ALPHA_INTEGER | GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH | GL_ALREADY_SIGNALED | GL_ANY_SAMPLES_PASSED | GL_ACTIVE_SUBROUTINE_UNIFORM_MAX_LENGTH | GL_ACTIVE_PROGRAM | GL_ACTIVE_ATOMIC_COUNTER_BUFFERS | GL_ACTIVE_RESOURCES | GL_BUFFER_IMMUTABLE_STORAGE;
  113.         int KEEP_OWN_IMPORTS = UNIFORM_PROJECTION_MATRIX.getLocation() | VS_POSITION.getLocation();
  114.     }
  115. //</editor-fold>
  116.     private FloatBuffer data;
  117.     private Program program;
  118.  
  119.     private final float width, height, depth;
  120.  
  121.     public Box(final float width, final float height, final float depth) {
  122.         this.width = width;
  123.         this.height = height;
  124.         this.depth = depth;
  125.         data = generateBox();
  126.         data.clear();
  127.     }
  128.  
  129.     @Override
  130.     public void compileProgram() {
  131.         program = new Program(
  132.                 new VertexShader("data/shaders/box.vs.glsl").compile(),
  133.                 new FragmentShader("data/shaders/box.fs.glsl").compile()
  134.         ).compile().usingUniforms(
  135.                         UNIFORM_MODEL_MATRIX,
  136.                         UNIFORM_VIEW_MATRIX,
  137.                         UNIFORM_PROJECTION_MATRIX,
  138.                         UNIFORM_SHADOW_MATRIX
  139.                         );
  140.     }
  141.    
  142.     @Override
  143.     public int getInstancesCount() {
  144.         return 100;
  145.     }
  146.  
  147.     @Override
  148.     public Program getProgram() {
  149.         return program;
  150.     }
  151.  
  152.     @Override
  153.     public int getDataSize() {
  154.         return 6 * 6;
  155.     }
  156.  
  157.     @Override
  158.     public FloatBuffer putData(final FloatBuffer dataBuffer) {
  159.         FloatBuffer returnData = dataBuffer.put(data);
  160.         data.clear();   //clear to reset data state
  161.         return returnData;
  162.     }
  163.  
  164.     @Override
  165.     public int getDataMode() {
  166.         return GL_TRIANGLES;
  167.     }
  168.  
  169.     @Override
  170.     public boolean isShadowReceiver() {
  171.         return true;
  172.     }
  173.  
  174.     @Override
  175.     public boolean isShadowCaster() {
  176.         return true;
  177.     }
  178.  
  179.     private FloatBuffer generateBox() {
  180.         FloatBuffer boxData = BufferUtils.createFloatBuffer(6 * 6 * 3);
  181.  
  182.         float halfWidth = width / 2;
  183.         float halfHeight = height / 2;
  184.         float halfDepth = depth / 2;
  185.  
  186.         //calculate nodes
  187.         float topN1X = -halfWidth;
  188.         float topN1Y = halfHeight;
  189.         float topN1Z = -halfDepth;
  190.  
  191.         float topN2X = -halfWidth;
  192.         float topN2Y = halfHeight;
  193.         float topN2Z = halfDepth;
  194.  
  195.         float topN3X = halfWidth;
  196.         float topN3Y = halfHeight;
  197.         float topN3Z = halfDepth;
  198.  
  199.         float topN4X = halfWidth;
  200.         float topN4Y = halfHeight;
  201.         float topN4Z = -halfDepth;
  202.  
  203.         float bottomN1X = -halfWidth;
  204.         float bottomN1Y = -halfHeight;
  205.         float bottomN1Z = -halfDepth;
  206.  
  207.         float bottomN2X = -halfWidth;
  208.         float bottomN2Y = -halfHeight;
  209.         float bottomN2Z = halfDepth;
  210.  
  211.         float bottomN3X = halfWidth;
  212.         float bottomN3Y = -halfHeight;
  213.         float bottomN3Z = halfDepth;
  214.  
  215.         float bottomN4X = halfWidth;
  216.         float bottomN4Y = -halfHeight;
  217.         float bottomN4Z = -halfDepth;
  218.  
  219.         //top face
  220.         boxData
  221.                 .put(topN4X).put(topN4Y).put(topN4Z)    //first triangle
  222.                 .put(topN1X).put(topN1Y).put(topN1Z)
  223.                 .put(topN2X).put(topN2Y).put(topN2Z)
  224.                 .put(topN2X).put(topN2Y).put(topN2Z)    //second triangle
  225.                 .put(topN3X).put(topN3Y).put(topN3Z)
  226.                 .put(topN4X).put(topN4Y).put(topN4Z);
  227.  
  228.         //bottom face
  229.         boxData
  230.                 .put(bottomN4X).put(bottomN4Y).put(bottomN4Z)    //first triangle
  231.                 .put(bottomN1X).put(bottomN1Y).put(bottomN1Z)
  232.                 .put(bottomN2X).put(bottomN2Y).put(bottomN2Z)
  233.                 .put(bottomN2X).put(bottomN2Y).put(bottomN2Z)   //second triangle
  234.                 .put(bottomN3X).put(bottomN3Y).put(bottomN3Z)
  235.                 .put(bottomN4X).put(bottomN4Y).put(bottomN4Z);
  236.  
  237.         //face from N1 to N2
  238.         boxData
  239.                 .put(topN1X).put(topN1Y).put(topN1Z)            //first triangle
  240.                 .put(bottomN1X).put(bottomN1Y).put(bottomN1Z)
  241.                 .put(bottomN2X).put(bottomN2Y).put(bottomN2Z)
  242.                 .put(bottomN2X).put(bottomN2Y).put(bottomN2Z)   //second triangle
  243.                 .put(topN2X).put(topN2Y).put(topN2Z)
  244.                 .put(topN1X).put(topN1Y).put(topN1Z);
  245.  
  246.         //face from N2 to N3
  247.         boxData
  248.                 .put(topN2X).put(topN2Y).put(topN2Z)            //first triangle
  249.                 .put(bottomN2X).put(bottomN2Y).put(bottomN2Z)
  250.                 .put(bottomN3X).put(bottomN3Y).put(bottomN3Z)
  251.                 .put(bottomN3X).put(bottomN3Y).put(bottomN3Z)   //second triangle
  252.                 .put(topN3X).put(topN3Y).put(topN3Z)
  253.                 .put(topN2X).put(topN2Y).put(topN2Z);
  254.  
  255.         //face from N3 to N4
  256.         boxData
  257.                 .put(topN3X).put(topN3Y).put(topN3Z)            //first triangle
  258.                 .put(bottomN3X).put(bottomN3Y).put(bottomN3Z)
  259.                 .put(bottomN4X).put(bottomN4Y).put(bottomN4Z)
  260.                 .put(bottomN4X).put(bottomN4Y).put(bottomN4Z)   //second triangle
  261.                 .put(topN4X).put(topN4Y).put(topN4Z)
  262.                 .put(topN3X).put(topN3Y).put(topN3Z);
  263.  
  264.         //face from N4 to N1
  265.         boxData
  266.                 .put(topN4X).put(topN4Y).put(topN4Z)            //first triangle
  267.                 .put(bottomN4X).put(bottomN4Y).put(bottomN4Z)
  268.                 .put(bottomN1X).put(bottomN1Y).put(bottomN1Z)
  269.                 .put(bottomN1X).put(bottomN1Y).put(bottomN1Z)   //second triangle
  270.                 .put(topN1X).put(topN1Y).put(topN1Z)
  271.                 .put(topN4X).put(topN4Y).put(topN4Z);
  272.  
  273.         return (FloatBuffer)boxData.clear();
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement