Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. package com.starshock.gl;
  2.  
  3. import com.starshock.game.Game;
  4. import com.starshock.gl.address.types.*;
  5. import com.starshock.gl.helper.GLHelper;
  6.  
  7. import java.nio.FloatBuffer;
  8.  
  9. import static org.lwjgl.opengl.GL11.*;
  10. import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
  11. import static org.lwjgl.opengl.GL15.GL_STREAM_DRAW;
  12. import static org.lwjgl.opengl.GL15.glBindBuffer;
  13. import static org.lwjgl.opengl.GL20.*;
  14. import static org.lwjgl.opengl.GL30.*;
  15.  
  16. public class Renderer {
  17.     public TextureAddress masterTexture;
  18.     public FrameBufferAddress gameFramebuffer, processingFramebuffer;
  19.     public ProgramAddress gameProgram;
  20.     public Unsync buffer;
  21.  
  22.     public Renderer() {
  23.     }
  24.  
  25.     private void init() {
  26.         masterTexture = GLHelper.createTextureFromPath("/res/sheet.png");
  27.  
  28.         gameFramebuffer = GLHelper.createFrameBufferAddress(Game.PURE_WIDTH, Game.PURE_HEIGHT);
  29.         processingFramebuffer = GLHelper.createFrameBufferAddress(Game.PURE_WIDTH, Game.PURE_HEIGHT);
  30.  
  31.         ShaderAddress gameVertexShader = GLHelper.createShaderFromPath("/res/shader/game.vert", GL_VERTEX_SHADER);
  32.         ShaderAddress gameFragmentShader = GLHelper.createShaderFromPath("/res/shader/game.frag", GL_FRAGMENT_SHADER);
  33.  
  34.         gameProgram = GLHelper.createProgram(gameVertexShader, gameFragmentShader);
  35.  
  36.         masterTexture.bind();
  37.         gameProgram.use();
  38.  
  39.         int vao = glGenVertexArrays();
  40.         glBindVertexArray(vao);
  41.  
  42.         int posAttrib = glGetAttribLocation(gameProgram.getAddress(), "position");
  43.         glEnableVertexAttribArray(posAttrib);
  44.         glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, 32, 0L);
  45.  
  46.         int colAttrib = glGetAttribLocation(gameProgram.getAddress(), "color");
  47.         glEnableVertexAttribArray(colAttrib);
  48.         glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, 32, 12L);
  49.  
  50.         int texAttrib = glGetAttribLocation(gameProgram.getAddress(), "texCoord");
  51.         glEnableVertexAttribArray(texAttrib);
  52.         glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, 32, 24L);
  53.  
  54.         buffer = new Unsync(GL_ARRAY_BUFFER, GL_STREAM_DRAW);
  55.     }
  56.  
  57.     public void drawBuffer(Unsync buffer) {
  58.         glBindBuffer(GL_ARRAY_BUFFER, buffer.currentBufferHandle());
  59.         glDrawArrays(GL_TRIANGLES, 0, 6);
  60.     }
  61.  
  62.     public void render() {
  63.         glBindFramebuffer(GL_FRAMEBUFFER, 0);
  64.  
  65.         buffer.nextFrame();
  66.  
  67.         buffer.bind();
  68.  
  69.         buffer.ensureSize(6 * 32);
  70.  
  71.         FloatBuffer mapped = buffer.map().asFloatBuffer();
  72.  
  73.         mapped.put(new float[] {-1, 1, 0, 1, 0, 1, 0, 0});
  74.         mapped.put(new float[] {1, 1, 0, 0, 1, 1, 1, 0});
  75.         mapped.put(new float[] {1, -1, 0, 1, 1, 0, 1, 1});
  76.         mapped.put(new float[] {-1, 1, 0, 1, 0, 1, 0, 0});
  77.         mapped.put(new float[] {1, -1, 0, 0, 1, 1, 1, 1});
  78.         mapped.put(new float[] {-1, -1, 0, 1, 1, 0, 0, 1});
  79.  
  80.         buffer.unmap();
  81.  
  82.         drawBuffer(buffer);
  83.     }
  84.  
  85.     public static Renderer issueDefault() {
  86.         Renderer defaultHandler = new Renderer();
  87.  
  88.         defaultHandler.init();
  89.  
  90.         return defaultHandler;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement