package com.starshock.gl; import com.starshock.game.Game; import com.starshock.gl.address.types.*; import com.starshock.gl.helper.GLHelper; import java.nio.FloatBuffer; import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER; import static org.lwjgl.opengl.GL15.GL_STREAM_DRAW; import static org.lwjgl.opengl.GL15.glBindBuffer; import static org.lwjgl.opengl.GL20.*; import static org.lwjgl.opengl.GL30.*; public class Renderer { public TextureAddress masterTexture; public FrameBufferAddress gameFramebuffer, processingFramebuffer; public ProgramAddress gameProgram; public Unsync buffer; public Renderer() { } private void init() { masterTexture = GLHelper.createTextureFromPath("/res/sheet.png"); gameFramebuffer = GLHelper.createFrameBufferAddress(Game.PURE_WIDTH, Game.PURE_HEIGHT); processingFramebuffer = GLHelper.createFrameBufferAddress(Game.PURE_WIDTH, Game.PURE_HEIGHT); ShaderAddress gameVertexShader = GLHelper.createShaderFromPath("/res/shader/game.vert", GL_VERTEX_SHADER); ShaderAddress gameFragmentShader = GLHelper.createShaderFromPath("/res/shader/game.frag", GL_FRAGMENT_SHADER); gameProgram = GLHelper.createProgram(gameVertexShader, gameFragmentShader); masterTexture.bind(); gameProgram.use(); int vao = glGenVertexArrays(); glBindVertexArray(vao); int posAttrib = glGetAttribLocation(gameProgram.getAddress(), "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, false, 32, 0L); int colAttrib = glGetAttribLocation(gameProgram.getAddress(), "color"); glEnableVertexAttribArray(colAttrib); glVertexAttribPointer(colAttrib, 3, GL_FLOAT, false, 32, 12L); int texAttrib = glGetAttribLocation(gameProgram.getAddress(), "texCoord"); glEnableVertexAttribArray(texAttrib); glVertexAttribPointer(texAttrib, 2, GL_FLOAT, false, 32, 24L); buffer = new Unsync(GL_ARRAY_BUFFER, GL_STREAM_DRAW); } public void drawBuffer(Unsync buffer) { glBindBuffer(GL_ARRAY_BUFFER, buffer.currentBufferHandle()); glDrawArrays(GL_TRIANGLES, 0, 6); } public void render() { glBindFramebuffer(GL_FRAMEBUFFER, 0); buffer.nextFrame(); buffer.bind(); buffer.ensureSize(6 * 32); FloatBuffer mapped = buffer.map().asFloatBuffer(); mapped.put(new float[] {-1, 1, 0, 1, 0, 1, 0, 0}); mapped.put(new float[] {1, 1, 0, 0, 1, 1, 1, 0}); mapped.put(new float[] {1, -1, 0, 1, 1, 0, 1, 1}); mapped.put(new float[] {-1, 1, 0, 1, 0, 1, 0, 0}); mapped.put(new float[] {1, -1, 0, 0, 1, 1, 1, 1}); mapped.put(new float[] {-1, -1, 0, 1, 1, 0, 0, 1}); buffer.unmap(); drawBuffer(buffer); } public static Renderer issueDefault() { Renderer defaultHandler = new Renderer(); defaultHandler.init(); return defaultHandler; } }