Guest User

Untitled

a guest
Mar 15th, 2014
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.84 KB | None | 0 0
  1. public void drawSpriteTransparent(Sprite sprite, int x, int y, float transparency) {
  2.         Texture t = sprite.getTexture();
  3.         double tx = sprite.getTexutreX();
  4.         double ty = sprite.getTextureY();
  5.         double tw = sprite.getWidthInTexture();
  6.         double th = sprite.getHeightInTexture();
  7.        
  8. //      System.out.println(vboiID);
  9. //      System.out.println(vaoID);
  10.        
  11.         gl.glBindTexture(GL2.GL_TEXTURE_2D, t.getTextureID());
  12.         gl.glUseProgram(spriteProgramID);
  13.        
  14.         gl.glUniform2i(this.positionLoc, x, y); // Update X and Y position of the Sprite's Location
  15.         gl.glUniform2i(this.sizeLoc, sprite.getWidth(), sprite.getHeight()); // Update Width and Height information of the Sprite's Dimension
  16.         gl.glUniform4f(this.colorLoc, 1.0f, 1.0f, 1.0f, 0.0f); // Setup the tint color (currently ignored)
  17.         gl.glUniform2f(this.texCoordLoc, (float) tx, (float) ty); // Update the X and Y of the Sprite within it's texture
  18.        
  19.         gl.glBindVertexArray(this.vaoID);
  20.         gl.glEnableVertexAttribArray(0);
  21.        
  22.         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, this.vboiID);
  23.         gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_BYTE, 0);
  24.        
  25. //      gl.glColor4f(1f, 0f, 0f, 1f - transparency);
  26. //      gl.glBegin(GL2.GL_QUADS);
  27. //      {
  28. //          gl.glTexCoord2d(tx, ty);
  29. //          gl.glVertex3d(x, y, 0);
  30. //          gl.glTexCoord2d(tx, ty + th);
  31. //          gl.glVertex3d(x, y + sprite.getHeight(), 0);
  32. //          gl.glTexCoord2d(tx + tw, ty + th);
  33. //          gl.glVertex3d(x + sprite.getWidth(), y + sprite.getHeight(), 0);
  34. //          gl.glTexCoord2d(tx + tw, ty);
  35. //          gl.glVertex3d(x + sprite.getWidth(), y, 0);
  36. //      }
  37. //      gl.glEnd();
  38.        
  39.         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
  40.        
  41.         gl.glDisableVertexAttribArray(0);
  42.         gl.glBindVertexArray(0);
  43.         gl.glBindTexture(GL2.GL_TEXTURE_2D, 0);
  44.         gl.glUseProgram(0);
  45.     }
  46.    
  47.     void init(GL2 gl){
  48.         this.gl = gl;
  49.         /* Load Shader */
  50.         System.out.println("Load Shader");
  51.         char sep = File.separatorChar;
  52.         this.spriteVertexShaderID = loadShader("res" + sep + "shader" + sep + "sprite_vs.glsl" , GL2.GL_VERTEX_SHADER, gl);
  53.         this.spriteHueFragmentShaderID = loadShader("res" + sep + "shader" + sep + "sprite_hue_fs.glsl", GL2.GL_FRAGMENT_SHADER, gl);
  54.        
  55.         this.spriteProgramID = gl.glCreateProgram();
  56.         gl.glAttachShader(this.spriteProgramID, this.spriteVertexShaderID);
  57.         gl.glAttachShader(this.spriteProgramID, this.spriteHueFragmentShaderID);
  58.        
  59.         gl.glLinkProgram(this.spriteProgramID);
  60.         gl.glValidateProgram(this.spriteProgramID);
  61.  
  62.         System.out.println("Error after validation: "+gl.glGetError());
  63.        
  64.         ByteBuffer cbuffer = ByteBuffer.allocate(512);
  65.         IntBuffer ibuffer = IntBuffer.allocate(16);
  66.         gl.glGetProgramInfoLog(this.spriteProgramID, 512, ibuffer, cbuffer);
  67.  
  68.         int c = 0;
  69.         boolean written = false;
  70.         for(int i = 0 ; i < 16 ; i++){
  71.             for(int j = ibuffer.get(i) ; j > 0 ; j--){
  72.                 char l = (char) cbuffer.get(c);
  73.                 c++;
  74.                 System.out.print(l);
  75.                 written = true;
  76.             }
  77.             if(written){
  78.                 System.out.println();  
  79.                 written = false;
  80.             }
  81.         }
  82.        
  83.         gl.glGetProgramiv(this.spriteProgramID, GL2.GL_VALIDATE_STATUS, VBO_VAO_ID_BUFFER);
  84.         System.out.println("Validate Status: " + VBO_VAO_ID_BUFFER.get(0));
  85.        
  86.         gl.glUseProgram(this.spriteProgramID);
  87.        
  88. //      System.out.println("Error: "+ gl.glGetError());
  89.         this.positionLoc = gl.glGetUniformLocation(spriteProgramID, "position");
  90.         this.sizeLoc = gl.glGetUniformLocation(spriteProgramID, "size");
  91.         this.colorLoc = gl.glGetUniformLocation(spriteProgramID, "color");
  92.         this.texCoordLoc = gl.glGetUniformLocation(spriteProgramID, "texture_coordinate");
  93.        
  94.         System.out.println("Error after getting Uniform Locations: "+ gl.glGetError());
  95.        
  96.         int vertexLoc = gl.glGetAttribLocation(this.spriteProgramID, "vertex");
  97.         System.out.println("Error after getting attrib location: "+ gl.glGetError());
  98.        
  99.         gl.glBindAttribLocation(this.spriteProgramID, 0, "vertex");
  100.        
  101.         System.out.println("Created Program");
  102.        
  103.         this.vaoID = createVertexArray(gl);
  104.         System.out.println("New VAO: " + this.vaoID);
  105.         this.vboID = createBufferObject(gl);
  106.         System.out.println("New VBO: " + this.vboID);
  107.        
  108.         float[] vertices = {
  109.                 0f, 0f,
  110.                 0f, 1f,
  111.                 1f, 1f,
  112.                 1f, 0f,
  113.                 };
  114. //      FloatBuffer vBuffer = ByteBuffer.allocate(vertices.length << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
  115.         FloatBuffer vBuffer = FloatBuffer.allocate(vertices.length);
  116.         vBuffer.put(vertices);
  117.         vBuffer.flip();
  118.        
  119.         byte[] indices = {
  120.                 0, 1, 2,
  121.                 2, 3, 0
  122.         };
  123.        
  124.         ByteBuffer iBuffer = ByteBuffer.allocate(indices.length).order(ByteOrder.nativeOrder());
  125.         iBuffer.put(indices);
  126.         iBuffer.flip();
  127.  
  128.        
  129.         gl.glBindVertexArray(this.vaoID);
  130.         gl.glEnableVertexAttribArray(vertexLoc);
  131.         gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, this.vboID);
  132.         gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.length * 4, vBuffer, GL2.GL_STATIC_DRAW);
  133.         gl.glVertexAttribPointer(vertexLoc, 2, GL2.GL_FLOAT, false, 0, 0L);
  134.         gl.glBindVertexArray(0);
  135.         System.out.println("Error after setting up Vertex Buffer Array: "+ gl.glGetError());
  136.        
  137.         this.vboiID = createBufferObject(gl);
  138.         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, vboiID);
  139.         gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.length, iBuffer, GL2.GL_STATIC_DRAW);
  140.         gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
  141.         System.out.println("Error after setting up Vertes Buffer Object for Indices: "+ gl.glGetError());  
  142.        
  143.         System.out.println("Renderer initialized!");
  144.     }
  145.    
  146.     private int loadShader(String file, int shaderType, GL2 gl){
  147.         String string = "";
  148.         StringBuilder builder = new StringBuilder();
  149.         int shaderID = 0;
  150.         try {
  151.             BufferedReader reader = new BufferedReader(new FileReader(SpritesUtility.loadFile(file)));
  152.             String line;
  153.             while((line = reader.readLine()) != null){
  154. //              System.out.println(line);
  155.                 string += line + "\n";
  156.             }
  157.         } catch (IOException e){
  158.             e.printStackTrace();
  159.             System.exit(-1);
  160.         }
  161.        
  162.         shaderID = gl.glCreateShader(shaderType);
  163.         System.out.println("New Shader ID: "+ shaderID);
  164.         gl.glShaderSource(shaderID, 1, new String[] {string}, null);
  165.         gl.glCompileShader(shaderID);
  166.        
  167.         ByteBuffer cbuffer = ByteBuffer.allocate(512);
  168.         IntBuffer ibuffer = IntBuffer.allocate(16);
  169.         gl.glGetShaderInfoLog(shaderID, 512, ibuffer, cbuffer);
  170.  
  171.         int c = 0;
  172.         boolean written = false;
  173.         for(int i = 0 ; i < 16 ; i++){
  174.             for(int j = ibuffer.get(i) ; j > 0 ; j--){
  175.                 char l = (char) cbuffer.get(c);
  176.                 c++;
  177.                 System.out.print(l);
  178.                 written = true;
  179.             }
  180.             if(written){
  181.                 System.out.println();  
  182.                 written = false;
  183.             }
  184.         }
  185.        
  186.         return shaderID;
  187.     }
  188.    
  189.     private int createVertexArray(GL2 gl){
  190.         gl.glGenVertexArrays(1, VBO_VAO_ID_BUFFER);
  191. //      System.out.println("Create Vertex Array Object: "+ gl.glGetError());
  192.         return VBO_VAO_ID_BUFFER.get(0);
  193.     }
  194.    
  195.     private int createBufferObject(GL2 gl){
  196.         gl.glGenBuffers(1, VBO_VAO_ID_BUFFER);
  197. //      System.out.println("Create Vertex Buffer Object: "+ gl.glGetError());
  198.         return VBO_VAO_ID_BUFFER.get(0);
  199.     }
Advertisement
Add Comment
Please, Sign In to add comment