Clean up code; adding comments

This commit is contained in:
Caesar2011
2016-09-14 18:18:01 +02:00
parent 449060a463
commit 9cb2b0f8e8
4 changed files with 105 additions and 72 deletions

View File

@@ -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; i++) {
snprintf(buffer, sizeof(buffer), "../img/foregrounds/surfing/%d.jpg", i);
texFGs[i] = LoadTexture(buffer,&textw,&texth);
}
texFG = LoadTexture("../img/foregrounds/surfing/0.jpg",&textw,&texth);
glfwSetWindowSize(window, textw, texth);
glViewport(0, 0, textw, texth);
glfwSetWindowSizeLimits(window, textw, texth, textw, texth);
// The texture we're going to render to
// Generate texture for frame buffer
GLuint renderedTexture;
glGenTextures(1, &renderedTexture);
glBindTexture(GL_TEXTURE_2D, renderedTexture);
// Load texture and set filtering
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, textw, texth, 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);
// Set "renderedTexture" as our colour attachement #2
// Set "renderedTexture" as our color attachment #2 (render to location 2)
glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, renderedTexture, 0);
// Set the list of draw buffers.
// 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
// Always check that our frame buffer is ok
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
// Set window size to texture size (window size, opengl context size, resize constraints)
glfwSetWindowSize(window, textw, texth);
glViewport(0, 0, textw, texth);
glfwSetWindowSizeLimits(window, textw, texth, textw, texth);
GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader");
GLuint programRefineID = LoadShaders("refinement.vertexshader", "refinement.fragmentshader");
GLint textureLoc1 = glGetUniformLocation(programID, "myTextureSampler");
GLint widthLoc = glGetUniformLocation(programID, "pixWidth");
GLint heigthLoc = glGetUniformLocation(programID, "pixHeight");
// Load shader programs
GLuint progTrimapID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader");
GLuint progRefineID = LoadShaders("refinement.vertexshader", "refinement.fragmentshader");
// Get shader locations for trimap shader
GLint locTriTexForeground = glGetUniformLocation(progTrimapID, "foreground");
GLint locTriPixWidth = glGetUniformLocation(progTrimapID, "pixWidth");
GLint locTriPixHeight = glGetUniformLocation(progTrimapID, "pixHeight");
GLint textureLocTri = glGetUniformLocation(programRefineID, "trimap");
GLint textureLocFore = glGetUniformLocation(programRefineID, "foreground");
GLint textureLocBack = glGetUniformLocation(programRefineID, "background");
// Get shader locations for refinement shader
GLint locRefTexTrimap = glGetUniformLocation(progRefineID, "trimap");
GLint locRefTexForeground = glGetUniformLocation(progRefineID, "foreground");
GLint locRefTexBackground = glGetUniformLocation(progRefineID, "background");
GLint locRefPixWidth = glGetUniformLocation(progRefineID, "pixWidth");
GLint locRefPixHeight = glGetUniformLocation(progRefineID, "pixHeight");
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texBG);
@@ -142,53 +148,70 @@ int main(int argc, char* args[]) {
glBindTexture(GL_TEXTURE_2D, renderedTexture);
printf("Shader program ID: %d\n", programID);
printf("textureLoc1: %d\n", textureLoc1);
printf("textureLocTri: %d\n", textureLocTri);
printf("Shader program ID: %d\n", progTrimapID);
printf("textureLoc1: %d\n", locTriTexForeground);
printf("textureLocTri: %d\n", locRefTexTrimap);
//printf("textureLocFore: %d\n", textureLocFore);
printf("widthLoc: %d\n", widthLoc);
printf("heigthLoc: %d\n", heigthLoc);
printf("widthLoc: %d\n", locTriPixWidth);
printf("heigthLoc: %d\n", locTriPixHeight);
printf("Width: %f\n", 1.0f/textw);
printf("Height: %f\n", 1.0f/texth);
// INIT END
int loopNumber = -1;
double lastTime = glfwGetTime();
int nbFrames = 0;
// INIT END
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
/* Render here */
// Foreground texture counter
loopNumber++;
if (loopNumber >= lenTexFG) {
loopNumber = 0;
}
// Measure speed
double currentTime = glfwGetTime();
nbFrames++;
if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago
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);

View File

@@ -2,7 +2,7 @@
1385320478 source:d:\owncloud\documents\programmierung\cpp\opengl-test-two\main.cpp
<iostream>
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
<GL/glew.h>
<GLFW/glfw3.h>
<SDL2/SDL.h>

View File

@@ -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;

View File

@@ -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
}
}