Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
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: Phased (posted 2013-12-07 10:06:59, viewed 146 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   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130  
import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_COORD_ARRAY;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glVertexPointer;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import java.nio.FloatBuffer;

import static org.lwjgl.opengl.GL15.*;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;

public class VBO {
   private Texture texture;

   private int vertexID, textureID;

   private int size;
   private int toRender;

   private FloatBuffer vertices;
   ShaderProgram shader;

   // The amount of bytes an element has
   public static final int elementBytes = 4;
   
   // Elements per parameter
   public static final int positionElementCount = 3;
   public static final int colorElementCount = 4;
   public static final int textureElementCount = 2;
   
   // Bytes per parameter
   public static final int positionBytesCount = positionElementCount * elementBytes;
   public static final int colorByteCount = colorElementCount * elementBytes;
   public static final int textureByteCount = textureElementCount * elementBytes;
   
   // Byte offsets per parameter
   public static final int positionByteOffset = 0;
   public static final int colorByteOffset = positionByteOffset + positionBytesCount;
   public static final int textureByteOffset = colorByteOffset + colorByteCount;
   
   // The amount of elements that a vertex has
   public static final int elementCount = positionElementCount + 
         colorElementCount + textureElementCount;   
   // The size of a vertex in bytes, like in C/C++: sizeof(Vertex)
   public static final int stride = positionBytesCount + colorByteCount + 
         textureByteCount;
   
   public VBO(int size){
      this.size = size;

      vertices = BufferUtils.createFloatBuffer(8 * size);
      shader = new ShaderProgram("default", "default");
   }

   public void addRectangle(float x, float y, float width, float height, float u, float v, float u2, float v2){
      toRender += 1;
      if(toRender > size){
         System.out.println("To many sprites in the VBO");
         return;
      }
      
      vertices.put(new float[]
            { //x, y, z, r, g, b, a, u, v
               x, y, 0, 1, 1, 1, 1, u, v,
               x + width, y, 0, 1, 1, 1, 1, u, v2, 
               x + width, y + height, 0, 1, 1, 1, 1, u2, v2,
               x, y + height, 0, 1, 1, 1, 1, u2, v, 
            });

   }

   public void prepareForRendering(){
      vertices.rewind();

      vertexID = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vertexID);
      glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
      
      GL20.glVertexAttribPointer(0, positionElementCount, GL11.GL_FLOAT, 
            false, stride, positionByteOffset);
      // Put the color components in attribute list 1
      GL20.glVertexAttribPointer(1, colorElementCount, GL11.GL_FLOAT, 
            false, stride, colorByteOffset);
      // Put the texture coordinates in attribute list 2
      GL20.glVertexAttribPointer(2, textureElementCount, GL11.GL_FLOAT, 
            false, stride, textureByteOffset);
      
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }

   public void render(){
      shader.begin();
      
      GL20.glEnableVertexAttribArray(0);
      GL20.glEnableVertexAttribArray(1);
      GL20.glEnableVertexAttribArray(2);
      

      // Bind the vertex buffer
      glBindBuffer(GL_ARRAY_BUFFER, vertexID);
      glVertexPointer(2, GL_FLOAT, 0, 0);

      // Do rendering
      glDrawArrays(GL_QUADS, 0, toRender * 4);

      
      // Disable client states
      glDisableClientState(GL_VERTEX_ARRAY);
      
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      GL20.glDisableVertexAttribArray(0);
      GL20.glDisableVertexAttribArray(1);
      GL20.glDisableVertexAttribArray(2);
      shader.end();
      
      toRender = 0;
      vertices = BufferUtils.createFloatBuffer(8 * size);
   }
   
   public void destroy(){
      GL15.glDeleteBuffers(vertexID);
   }
}





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.

TehJavaDev (16 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

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

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

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

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

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

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

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

BurntPizza (34 views)
2014-08-08 02:01:56
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!