diff --git a/opengl-test-two/main.cpp b/opengl-test-two/main.cpp index 1c4d57d..6a5e563 100644 --- a/opengl-test-two/main.cpp +++ b/opengl-test-two/main.cpp @@ -9,7 +9,7 @@ int main(int argc, char* args[]) { GLFWwindow* window; - GLuint texFG, texBG; + GLuint texBG; int textw, texth; int textw2, texth2; @@ -23,7 +23,6 @@ int main(int argc, char* args[]) { } /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); - glfwSetWindowSizeLimits(window, 640, 480, 640, 480); if (!window) { glfwTerminate(); return 1; @@ -51,6 +50,11 @@ int main(int argc, char* args[]) { } // INIT + + + + + // An array of 3 vectors which represents 3 vertices static const GLfloat g_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, // (bottom - left ) TRIANGLE 1 @@ -77,64 +81,66 @@ int main(int argc, char* args[]) { glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); - // Identify our color buffer + // Identify our texture coordinate buffer GLuint colorbuffer; glGenBuffers(1, &colorbuffer); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW); - // The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer. + // The framebuffer, which regroups one texture, and zero depth buffer. GLuint FramebufferName; glGenFramebuffers(1, &FramebufferName); glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); + // load foreground and background textures int lenTexFG = 10; GLint texFGs[lenTexFG]; char buffer[50]; - - texBG = LoadTexture("../img/backgrounds/sky.jpg",&textw2,&texth2); for (int i=0; i= lenTexFG) { loopNumber = 0; } - // Measure speed - double currentTime = glfwGetTime(); - nbFrames++; - if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago - // printf and reset timer - printf("%f ms/frame\n", 1000.0/double(nbFrames)); - nbFrames = 0; - lastTime += 1.0; - } + // Measure speed + double currentTime = glfwGetTime(); + nbFrames++; + if ( currentTime - lastTime >= 1.0 ){ + // If last prinf() was more than 1 sec ago + // printf and reset timer + printf("%f ms/frame\n", 1000.0/double(nbFrames)); + nbFrames = 0; + lastTime += 1.0; + } + + // Set current foreground texture glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texFGs[loopNumber]); + + + + + + /* RENDER TO FRAMEBUFFER */ + // Render to our framebuffer glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); - //glBindFramebuffer(GL_FRAMEBUFFER, 0); - //glViewport(0,0,textw,texth); // Render on the whole framebuffer, complete from the lower left corner to the upper right + + // Clear buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glUseProgram(programID); - glUniform1f(widthLoc, 1.0f/textw); - glUniform1f(heigthLoc, 1.0f/texth); - glUniform1i(textureLoc1, 0); + // Init trimap shader program + glUseProgram(progTrimapID); + glUniform1f(locTriPixWidth, 1.0f/textw); + glUniform1f(locTriPixHeight, 1.0f/texth); + glUniform1i(locTriTexForeground, 0); - // 1rst attribute buffer : vertices + // 1st attribute buffer: vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( @@ -199,6 +222,7 @@ int main(int argc, char* args[]) { 0, // stride (void*)0 // array buffer offset ); + // 2nd attribute buffer : colors glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); @@ -212,24 +236,33 @@ int main(int argc, char* args[]) { ); - // Draw the triangle ! + // Draw the triangles (and render trimap shaders) to framebuffer/texture glDrawArrays(GL_TRIANGLES, 0, 2*3); // 2*3 indices starting at 0 -> 2 triangles -> 1 square + glDisableVertexAttribArray(0); + glUseProgram(0); + + + + + + + + // Render to the screen - glUseProgram(0); glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0,0,textw, texth); // Render on the whole framebuffer, complete from the lower left corner to the upper right + // Init refinement shader program + glUseProgram(progRefineID); + glUniform1i(locRefTexForeground, 0); + glUniform1i(locRefTexBackground, 1); + glUniform1i(locRefTexTrimap, 2); + glUniform1f(locRefPixWidth, 1.0f/textw); + glUniform1f(locRefPixHeight, 1.0f/texth); - glUseProgram(programRefineID); - glUniform1i(textureLocTri, 2); - glUniform1i(textureLocBack, 1); - glUniform1i(textureLocFore, 0); - - - // 1rst attribute buffer : vertices + // 1st attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( @@ -240,7 +273,8 @@ int main(int argc, char* args[]) { 0, // stride (void*)0 // array buffer offset ); - // 2nd attribute buffer : colors + + // 2nd attribute buffer : texture coordinates glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); glVertexAttribPointer( @@ -253,7 +287,7 @@ int main(int argc, char* args[]) { ); - // Draw the triangle ! + // Draw the triangles (and render refinement shaders) to screen glDrawArrays(GL_TRIANGLES, 0, 2*3); // 2*3 indices starting at 0 -> 2 triangles -> 1 square glDisableVertexAttribArray(0); diff --git a/opengl-test-two/opengl-test-two.depend b/opengl-test-two/opengl-test-two.depend index abe2c93..55190a2 100644 --- a/opengl-test-two/opengl-test-two.depend +++ b/opengl-test-two/opengl-test-two.depend @@ -2,7 +2,7 @@ 1385320478 source:d:\owncloud\documents\programmierung\cpp\opengl-test-two\main.cpp -1473775715 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp +1473869767 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp diff --git a/opengl-test-two/refinement.fragmentshader b/opengl-test-two/refinement.fragmentshader index 09a7857..1f997d4 100644 --- a/opengl-test-two/refinement.fragmentshader +++ b/opengl-test-two/refinement.fragmentshader @@ -11,6 +11,9 @@ uniform sampler2D trimap; uniform sampler2D foreground; uniform sampler2D background; +// Relative width/height of a pixel +uniform float pixWidth; +uniform float pixHeight; int IS_BACKGROUND = 0; int IS_UNDEFINED = 1; diff --git a/opengl-test-two/trimap.fragmentshader b/opengl-test-two/trimap.fragmentshader index 75da72f..9cd1ada 100644 --- a/opengl-test-two/trimap.fragmentshader +++ b/opengl-test-two/trimap.fragmentshader @@ -8,7 +8,7 @@ layout(location = 0) out vec3 color; //out vec3 color; // Values that stay constant for the whole mesh. -uniform sampler2D myTextureSampler; +uniform sampler2D foreground; // Relative width/height of a pixel uniform float pixWidth; @@ -27,14 +27,10 @@ int radius = 10; vec3 getColor(vec2 uvCoord) { - return texture(myTextureSampler, uvCoord).rgb; + return texture(foreground, uvCoord).rgb; } bool isBackground(vec3 c) { - /*// 49,206, 11 - if (all( lessThanEqual(c, vec3(51.0f/255.0f, 208.0f/255.0f, 13.0f/255.0f))) && - all(greaterThanEqual(c, vec3(47.0f/255.0f, 204.0f/255.0f, 9.0f/255.0f)))*/ - // 0, 255, 0 if (all( lessThanEqual(c, greenValue+difference)) && all(greaterThanEqual(c, greenValue-difference)) ) { @@ -74,10 +70,10 @@ void main(){ } if (status == IS_BACKGROUND) { - color = vec3(0.0f); + color = vec3(0.0f); // black } else if (status == IS_FOREGROUND) { - color = vec3(1.0f); + color = vec3(1.0f); // white } else { - color = vec3(0.5f); + color = vec3(0.5f); // gray } }