Framebuffer working; Trimap renders to FB; Refinem. use Trimap to Screen

This commit is contained in:
Caesar2011
2016-09-12 18:18:32 +02:00
parent 7588c0d27b
commit a1c76d1e7f
8 changed files with 165 additions and 18 deletions

6
.gitignore vendored
View File

@@ -24,9 +24,9 @@
#*.lib
# Executables
#*.exe
#*.out
#*.app
*.exe
*.out
*.app
# ---> C
# Object files

View File

@@ -84,31 +84,62 @@ int main(int argc, char* args[]) {
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.
GLuint FramebufferName;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
tex1 = LoadTexture("../img/tests/flieger.jpg",&textw,&texth);
printf("Tex1: %d", tex1);
tex2 = LoadTexture("../img/tests/flieger2.jpg",&textw2,&texth2);
printf("Tex2: %d", tex2);
printf("Tex1: %d\n", tex1);
glfwSetWindowSize(window, textw, texth);
glViewport(0, 0, textw, texth);
glfwSetWindowSizeLimits(window, textw, texth, textw, texth);
// The texture we'<re going to render to
GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, renderedTexture);
// Give an empty image to OpenGL ( the last "0" )
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, textw, texth, 0,GL_RGB, GL_UNSIGNED_BYTE, 0);
// Poor filtering. Needed !
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// Set "renderedTexture" as our colour attachement #2
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, renderedTexture, 0);
// Set the list of draw buffers.
GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT2};
glDrawBuffers(1, DrawBuffers); // "1" is the size of DrawBuffers
// Always check that our framebuffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader");
GLuint programRefineID = LoadShaders("refinement.vertexshader", "refinement.fragmentshader");
GLint textureLoc1 = glGetUniformLocation(programID, "myTextureSampler");
GLint textureLoc2 = glGetUniformLocation(programID, "myTextureSampler2");
GLint widthLoc = glGetUniformLocation(programID, "pixWidth");
GLint heigthLoc = glGetUniformLocation(programID, "pixHeight");
GLint textureLocTri = glGetUniformLocation(programRefineID, "trimap");
GLint textureLocFore = glGetUniformLocation(programRefineID, "foreground");
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, tex1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex2);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
printf("Shader program ID: %d\n", programID);
printf("textureLoc1: %d\n", textureLoc1);
printf("textureLoc2: %d\n", textureLoc2);
printf("textureLocTri: %d\n", textureLocTri);
//printf("textureLocFore: %d\n", textureLocFore);
printf("widthLoc: %d\n", widthLoc);
printf("heigthLoc: %d\n", heigthLoc);
printf("Width: %f\n", 1.0f/textw);
@@ -120,13 +151,56 @@ int main(int argc, char* args[]) {
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
// 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
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glUniform1f(widthLoc, 1.0f/textw);
glUniform1f(heigthLoc, 1.0f/texth);
glUniform1i(textureLoc1, 0);
glUniform1i(textureLoc2, 1);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : colors
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 2*3); // 2*3 indices starting at 0 -> 2 triangles -> 1 square
glDisableVertexAttribArray(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
glUseProgram(programRefineID);
glUniform1i(textureLocTri, 2);
glUniform1i(textureLocFore, 0);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);

View File

@@ -742,3 +742,13 @@
<vector>
"loaders.h"
1473669189 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\src\loaders.cpp
<SDL2/SDL.h>
<SDL2/SDL_image.h>
<fstream>
<vector>
"loaders.h"
1473669189 d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\include\loaders.h
<GL/glew.h>

View File

@@ -0,0 +1,46 @@
#version 330 core
// Interpolated values from the vertex shaders
in vec2 UV;
// Ouput data
out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D trimap;
uniform sampler2D foreground;
int IS_BACKGROUND = 0;
int IS_UNDEFINED = 1;
int IS_FOREGROUND = 2;
int UNSET = -1;
vec3 getTriColor(vec2 uvCoord) {
return texture(trimap, uvCoord).rgb;
}
vec3 getForeColor(vec2 uvCoord) {
return texture(foreground, uvCoord).rgb;
}
int getState(vec3 c) {
if (all(lessThanEqual(c, vec3(0.2f)))) {
return IS_BACKGROUND;
} else if (all(greaterThanEqual(c, vec3(0.8f)))) {
return IS_FOREGROUND;
} else {
return IS_UNDEFINED;
}
}
void main(){
color = getTriColor(UV);
int status = getState(color);
if (status==IS_FOREGROUND) {
color = getForeColor(UV).rgb;
}
}

View File

@@ -0,0 +1,17 @@
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
void main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position = vec4(vertexPosition_modelspace, 1);
// UV of the vertex. No special space for this one.
UV = vertexUV;
}

View File

@@ -4,11 +4,11 @@
in vec2 UV;
// Ouput data
out vec3 color;
layout(location = 0) out vec3 color;
//out vec3 color;
// Values that stay constant for the whole mesh.
uniform sampler2D myTextureSampler;
uniform sampler2D myTextureSampler2;
// Relative width/height of a pixel
uniform float pixWidth;
@@ -23,7 +23,7 @@ int UNSET = -1;
vec3 getColor(vec2 uvCoord) {
return texture(myTextureSampler2, uvCoord).rgb;
return texture(myTextureSampler, uvCoord).rgb;
}
bool isBackground(vec3 c) {
@@ -70,10 +70,10 @@ void main(){
}
if (status == IS_BACKGROUND) {
color = vec3(1.0f, 0.0f, 0.0f);
color = vec3(0.0f);
} else if (status == IS_FOREGROUND) {
color = vec3(0.0f, 1.0f, 0.0f);
color = vec3(1.0f);
} else {
color = vec3(0.0f, 0.0f, 1.0f);
color = vec3(0.5f);
}
}

View File

@@ -9,7 +9,7 @@ out vec2 UV;
void main(){
// Output position of the vertex, in clip space : MVP * position
gl_Position = vec4(vertexPosition_modelspace, 1);
gl_Position = vec4(vertexPosition_modelspace.x, -1*vertexPosition_modelspace.y, vertexPosition_modelspace.z, 1);
// UV of the vertex. No special space for this one.
UV = vertexUV;