Bladeren bron

fixed blending in framebuffer, post effect tests

Kajetan Johannes Hammerle 5 jaren geleden
bovenliggende
commit
41df625876
5 gewijzigde bestanden met toevoegingen van 61 en 18 verwijderingen
  1. 29 14
      engine/Wrapper.cpp
  2. 1 1
      engine/Wrapper.h
  3. 1 1
      shader/fragment.fs
  4. 29 1
      shader/postFragment.fs
  5. 1 1
      shader/vertex.vs

+ 29 - 14
engine/Wrapper.cpp

@@ -22,7 +22,7 @@ GLuint Engine::postProgram = 0;
 
 GLuint Engine::frameBuffer = 0;
 GLuint Engine::frameTexture = 0;
-GLuint Engine::renderBuffer = 0;
+GLuint Engine::depthTexture = 0;
 int Engine::scale = 1;
 int Engine::width = 0;
 int Engine::height = 0;
@@ -152,6 +152,7 @@ void Engine::start(IClient* client)
         }
 
         // draw scene in framebuffer
+        glActiveTexture(GL_TEXTURE0);
         glUseProgram(program);
         glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer);
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -159,14 +160,20 @@ void Engine::start(IClient* client)
         Engine::client->renderTick((float) lag / NANOS_PER_TICK);
         
         // draw textured framebuffer
+        glActiveTexture(GL_TEXTURE1);
+        glBindTexture(GL_TEXTURE_2D, frameTexture);
+        
+        glActiveTexture(GL_TEXTURE2);
+        glBindTexture(GL_TEXTURE_2D, depthTexture);
+        
         glUseProgram(postProgram);
         glBindFramebuffer(GL_FRAMEBUFFER, 0);
-        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+        glClear(GL_COLOR_BUFFER_BIT);
         glDisable(GL_DEPTH_TEST);
-        glBindTexture(GL_TEXTURE_2D, frameTexture);
         glBindVertexArray(postVba);
         glBindBuffer(GL_ARRAY_BUFFER, postVbo);
         glDrawArrays(GL_TRIANGLES, 0, 6);
+        
 
         glfwSwapBuffers(window);
         glfwPollEvents();
@@ -361,6 +368,15 @@ void Engine::onWindowResize(GLFWwindow* w, int width, int height)
     Engine::width = width;
     Engine::height = height;
     updateScale();
+    
+    glBindFramebuffer(GL_FRAMEBUFFER, frameBuffer); 
+    glBindTexture(GL_TEXTURE_2D, frameTexture);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+    glBindTexture(GL_TEXTURE_2D, 0);
+    glBindRenderbuffer(GL_RENDERBUFFER, depthTexture); 
+    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);  
+    glBindRenderbuffer(GL_RENDERBUFFER, 0);
+    glBindFramebuffer(GL_FRAMEBUFFER, 0);
 }
 
 void Engine::updateScale()
@@ -413,7 +429,7 @@ void Engine::printError()
     switch(error)
     {
         case GL_NO_ERROR: 
-            //cout << "> No error has been recorded." << endl;
+            cout << "> No error has been recorded." << endl;
             break;
         case GL_INVALID_ENUM: 
             cout << "> An unacceptable value is specified for an enumerated argument." << endl;
@@ -454,23 +470,22 @@ void Engine::generateFramebuffer()
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glBindTexture(GL_TEXTURE_2D, 0);
     
-    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, frameTexture, 0);  
+    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, frameTexture, 0);   
     
-    glGenRenderbuffers(1, &renderBuffer);
-    glBindRenderbuffer(GL_RENDERBUFFER, renderBuffer); 
-    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);  
-    glBindRenderbuffer(GL_RENDERBUFFER, 0);
+    glGenTextures(1, &depthTexture);
+    glBindTexture(GL_TEXTURE_2D, depthTexture);
     
-    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderBuffer);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glBindTexture(GL_TEXTURE_2D, 0);
     
+    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);   
+
     if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
     {
 	cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << endl;
     }
-    else
-    {
-        cout << "WORKS" << endl;
-    }
     glBindFramebuffer(GL_FRAMEBUFFER, 0);
 }
 

+ 1 - 1
engine/Wrapper.h

@@ -81,7 +81,7 @@ private:
     
     static GLuint frameBuffer;
     static GLuint frameTexture;
-    static GLuint renderBuffer;
+    static GLuint depthTexture;
     static int scale;
     static int width;
     static int height;

+ 1 - 1
shader/fragment.fs

@@ -1,6 +1,6 @@
 #version 430
 
-uniform sampler2D samp;
+layout (binding = 0) uniform sampler2D samp;
 
 uniform mat4 projMatrix;
 uniform mat4 viewMatrix;

+ 29 - 1
shader/postFragment.fs

@@ -1,11 +1,39 @@
 #version 430
 
-uniform sampler2D samp;
+layout (binding = 1) uniform sampler2D samp;
+layout (binding = 2) uniform sampler2D depth;
 
 in vec2 tc;
 out vec4 color;
 
+const float offset = 3.0 / 300.0;
+
 void main(void)
 {
+    /*vec2 offsets[8] = vec2[](
+        vec2(-offset,  offset), // top-left
+        vec2( 0.0f,    offset), // top-center
+        vec2( offset,  offset), // top-right
+        vec2(-offset,  0.0f),   // center-left
+        //vec2( 0.0f,    0.0f),   // center-center
+        vec2( offset,  0.0f),   // center-right
+        vec2(-offset, -offset), // bottom-left
+        vec2( 0.0f,   -offset), // bottom-center
+        vec2( offset, -offset)  // bottom-right    
+    );
+    
+    float d = texture(depth, tc).r;
+
+    float c = 0;
+    for(int i = 0; i < 8; i++)
+    {
+        //c += float(d < texture(depth, tc + offsets[i]).r);
+        c += d - texture(depth, tc + offsets[i]).r;
+    }
+
+    c *= 100;
+    
+    color = vec4(c, c, c, 1.0);*/
+    //color = texture(samp, tc) * c;
     color = texture(samp, tc);
 }

+ 1 - 1
shader/vertex.vs

@@ -23,7 +23,7 @@ out vec4 outColor;
 void main(void)
 { 
     tc = tex; 
-    outColor = color;
+    outColor = vec4(color.xyz, 1);
 
     if(useNormals)
     {