diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 81ebd96..8b4c716 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,18 +2,13 @@ - - - + + + - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -375,6 +269,38 @@ - + @@ -620,13 +546,14 @@ - + + @@ -638,7 +565,6 @@ - @@ -653,18 +579,6 @@ - - - - - - - - - - - - @@ -709,10 +623,6 @@ - - - - @@ -820,7 +730,7 @@ - + @@ -886,28 +796,6 @@ - - - - - - - - - - - - - - - - - - - - - - @@ -972,24 +860,6 @@ - - - - - - - - - - - - - - - - - - @@ -998,14 +868,6 @@ - - - - - - - - @@ -1014,40 +876,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -1058,14 +886,116 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/res/shaders/fbo.frag b/res/shaders/fbo.frag new file mode 100644 index 0000000..a1db2af --- /dev/null +++ b/res/shaders/fbo.frag @@ -0,0 +1,12 @@ +#version 150 +//Il n’y a pas de layout(location=i) dans OpenGL < 3.3, mais tu peux utiliser glFragData[i] = myvalue à la place. +uniform sampler2D materialTex; + +in vec2 fragTexCoord; +in vec3 fragVert; + +out vec4 finalColor; + +void main() { + finalColor = texture(materialTex, fragTexCoord); +} \ No newline at end of file diff --git a/res/shaders/fbo.vert b/res/shaders/fbo.vert new file mode 100644 index 0000000..50af87c --- /dev/null +++ b/res/shaders/fbo.vert @@ -0,0 +1,18 @@ +#version 150 + +uniform mat4 projection; + +in vec3 vert; +in vec2 vertTexCoord; + +out vec3 fragVert; +out vec2 fragTexCoord; + +void main() { + // Pass some variables to the fragment shader + fragTexCoord = vertTexCoord; + fragVert = vert; + + // Apply all matrix transformations to vert + gl_Position = projection * vec4(vert, 1); +} \ No newline at end of file diff --git a/res/shaders/main.frag b/res/shaders/main.frag index fd264ac..f3ebea8 100644 --- a/res/shaders/main.frag +++ b/res/shaders/main.frag @@ -1,12 +1,12 @@ -#version 150 - +#version 330 +//Il n’y a pas de layout(location=i) dans OpenGL < 3.3, mais tu peux utiliser glFragData[i] = myvalue à la place. uniform sampler2D materialTex; uniform vec4 color; in vec2 fragTexCoord; in vec3 fragVert; -out vec4 finalColor; +layout(location = 0) out vec4 finalColor; void main() { finalColor = texture(materialTex, fragTexCoord) * color; diff --git a/src/globalgamejam/game/MainGame.java b/src/globalgamejam/game/MainGame.java index 616131e..672e276 100644 --- a/src/globalgamejam/game/MainGame.java +++ b/src/globalgamejam/game/MainGame.java @@ -3,7 +3,6 @@ package globalgamejam.game; import globalgamejam.render.*; import globalgamejam.tiles.TestTile; import globalgamejam.tiles.Tile; -import org.lwjgl.opengl.GL11; import java.util.ArrayList; @@ -13,9 +12,11 @@ import java.util.ArrayList; public class MainGame extends Game{ private ArrayList tiles; + private FrameBufferObject fbo; @Override public void init() { + fbo = new FrameBufferObject(); tiles = new ArrayList(); TestTile t = new TestTile(); t.getTransform().translate(100,100,0); @@ -31,7 +32,11 @@ public class MainGame extends Game{ @Override public void render2D() { + fbo.startRenderToFBO(); for(Tile t : tiles)t.render(); + fbo.stopRenderToFBO(); + + fbo.renderFBO(); } @@ -42,6 +47,7 @@ public class MainGame extends Game{ @Override public void destroy() { + fbo.destroy(); tiles.clear(); } diff --git a/src/globalgamejam/render/FrameBufferObject.java b/src/globalgamejam/render/FrameBufferObject.java new file mode 100644 index 0000000..d08f475 --- /dev/null +++ b/src/globalgamejam/render/FrameBufferObject.java @@ -0,0 +1,110 @@ +package globalgamejam.render; + +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL30.*; +import static org.lwjgl.opengl.GL32.*; + +import globalgamejam.Main; +import globalgamejam.math.Matrix4f; +import globalgamejam.math.Vector2f; +import org.lwjgl.BufferUtils; +import org.lwjgl.opengl.GL11; +import org.lwjgl.opengl.GL13; +import org.lwjgl.opengl.GL15; +import org.lwjgl.opengl.GL20; + +import java.nio.FloatBuffer; +import java.nio.IntBuffer; + +/** + * Created by MrDev023 on 14/01/2017. + */ +public class FrameBufferObject { + + private int fbo,fboTexID,renderID,vbo,size; + + public FrameBufferObject(){ + int width = Main.WIDTH; + int height = Main.HEIGHT; + this.fbo = glGenFramebuffers(); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + + //Creation de la texture qui va contenir la sortie RGB du shader + int renderedTexture = glGenTextures(); + glBindTexture(GL_TEXTURE_2D, renderedTexture); + glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0,GL_RGB, GL_UNSIGNED_BYTE, 0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + + //Creation du tampon de profondeur + int depthrenderbuffer = glGenRenderbuffers(); + glBindRenderbuffer(GL_RENDERBUFFER, depthrenderbuffer); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthrenderbuffer); + + //Definir le render Texture + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D, renderedTexture, 0); + IntBuffer drawBuffs = BufferUtils.createIntBuffer(1); + drawBuffs.put(0, GL_COLOR_ATTACHMENT0); + GL20.glDrawBuffers(drawBuffs); + if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + throw new IllegalStateException("FBO not loaded !"); + fboTexID = renderedTexture; + renderID = depthrenderbuffer; + + + this.vbo = GL15.glGenBuffers(); + float[] a = new float[]{ + 0,0, 0.0f,0.0f, + 1,0, 1.0f,0.0f, + 1,1, 1.0f,1.0f, + 0,1, 0.0f,1.0f + }; + FloatBuffer buffer = BufferUtils.createFloatBuffer(a.length); + buffer.put(a).flip(); + size = a.length/(2+2); + + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); + + GL20.glEnableVertexAttribArray(Shaders.MAIN_SHADERS.getAttribLocation("vert")); + GL20.glVertexAttribPointer(Shaders.MAIN_SHADERS.getAttribLocation("vert"), 2, GL11.GL_FLOAT, false, (2+2)*4, 0); + + GL20.glEnableVertexAttribArray(Shaders.MAIN_SHADERS.getAttribLocation("vertTexCoord")); + GL20.glVertexAttribPointer(Shaders.MAIN_SHADERS.getAttribLocation("vertTexCoord"), 2, GL11.GL_FLOAT, true, (2+2)*4, 2*4); + + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); + } + + public void startRenderToFBO(){ + glBindFramebuffer(GL_FRAMEBUFFER, fbo); + glViewport(0,0,Main.WIDTH,Main.HEIGHT); + } + + public void stopRenderToFBO(){ + glBindFramebuffer(GL_FRAMEBUFFER, 0); + } + + public void renderFBO(){ + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glViewport(0,0,Main.WIDTH,Main.HEIGHT); + + Shaders.MAIN_FBO.bind(); + Shaders.MAIN_FBO.uniform("projection", (new Matrix4f()).Ortho2D(0,1,0,1,-1,1).mul(new Matrix4f().translate(.5f,.5f,0))); + GL13.glActiveTexture(GL13.GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, fboTexID); + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); + GL11.glDrawArrays(GL11.GL_QUADS, 0, size); + GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + Shaders.MAIN_SHADERS.unbind(); + } + + public void destroy(){ + glDeleteTextures(fboTexID); + glDeleteRenderbuffers(renderID); + glDeleteFramebuffers(fbo); + } + +} diff --git a/src/globalgamejam/render/Shaders.java b/src/globalgamejam/render/Shaders.java index 336d747..71f83a6 100644 --- a/src/globalgamejam/render/Shaders.java +++ b/src/globalgamejam/render/Shaders.java @@ -12,11 +12,12 @@ public class Shaders { public int program; - public static Shaders MAIN_SHADERS; + public static Shaders MAIN_SHADERS,MAIN_FBO; static{ try{ MAIN_SHADERS = new Shaders("res/shaders/main.vert","res/shaders/main.frag"); + MAIN_FBO = new Shaders("res/shaders/fbo.vert","res/shaders/fbo.frag"); }catch(Exception e){ e.printStackTrace(); }