diff --git a/img/tests/test.bmp b/img/tests/test.bmp index 84977fa..7f88582 100644 Binary files a/img/tests/test.bmp and b/img/tests/test.bmp differ diff --git a/img/tests/test.gif b/img/tests/test.gif index 701a519..064fe11 100644 Binary files a/img/tests/test.gif and b/img/tests/test.gif differ diff --git a/img/tests/test.png b/img/tests/test.png index 6e99f41..d459084 100644 Binary files a/img/tests/test.png and b/img/tests/test.png differ diff --git a/opengl-test-two/bin/Debug/opengl-test-two.exe b/opengl-test-two/bin/Debug/opengl-test-two.exe index 068eca7..8c77a22 100644 Binary files a/opengl-test-two/bin/Debug/opengl-test-two.exe and b/opengl-test-two/bin/Debug/opengl-test-two.exe differ diff --git a/opengl-test-two/main.cpp b/opengl-test-two/main.cpp index 22927f2..8716c01 100644 --- a/opengl-test-two/main.cpp +++ b/opengl-test-two/main.cpp @@ -44,8 +44,8 @@ GLuint LoadTexture(char *filename,int *textw,int *texth) { // this reads from the sdl surface and puts it into an opengl texture glTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels); // these affect how this texture is drawn later on...glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // clean up @@ -169,10 +169,6 @@ GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path int main(int argc, char* args[]) { GLFWwindow* window; GLuint tex_jpe; - GLuint tex_jpg; - GLuint tex_gif; - GLuint tex_png; - GLuint tex_bmp; int textw, texth; int timer = 0; @@ -213,15 +209,56 @@ int main(int argc, char* args[]) { } // INIT - tex_jpe=LoadTexture("../img/tests/test.jpeg",&textw,&texth); - //tex_jpg=LoadTexture("../img/tests/test.jpg",&textw,&texth); - //tex_png=LoadTexture("../img/tests/test.png",&textw,&texth); - //tex_bmp=LoadTexture("../img/tests/test.bmp",&textw,&texth); + + GLuint VertexArrayID; + glGenVertexArrays(1, &VertexArrayID); + glBindVertexArray(VertexArrayID); + // 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 + 1.0f, -1.0f, 0.0f, // (bottom - right) + -1.0f, 1.0f, 0.0f, // (top - left ) + 1.0f, 1.0f, 0.0f, // (top - right) TRIANGLE 2 + 1.0f, -1.0f, 0.0f, // (bottom - right) + -1.0f, 1.0f, 0.0f, // (top - left ) + }; + // One color for each vertex. They were generated randomly. + static const GLfloat g_uv_buffer_data[] = { + 0.0f, 1.0f-0.0f, // (bottom - left ) TRIANGLE 1 + 1.0f, 1.0f-0.0f, // (bottom - right) + 0.0f, 1.0f-1.0f, // (top - left ) + 1.0f, 1.0f-1.0f, // (top - right) TRIANGLE 2 + 1.0f, 1.0f-0.0f, // (bottom - right) + 0.0f, 1.0f-1.0f, // (top - left ) + }; + + + // Identify our vertex buffer + GLuint vertexbuffer; + glGenBuffers(1, &vertexbuffer); + 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 + 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); + + + + + + + + tex_jpe=LoadTexture("../img/tests/test.bmp",&textw,&texth); GLuint programID = LoadShaders("trimap.vertexshader", "trimap.fragmentshader"); printf("%d", programID); - GLint textureLocation = glGetUniformLocationARB(programID, "texture"); - glUniform1iARB(textureLocation, tex_jpe); + GLint textureLocation = glGetUniformLocationARB(programID, "myTextureSampler"); + glUniform1i(textureLocation, tex_jpe); + + // INIT END /* Loop until the user closes the window */ @@ -234,21 +271,35 @@ int main(int argc, char* args[]) { glUseProgram(programID); - // RENDER - /*if (timer<200) - DrawTexture(50,50,tex_jpe,textw,texth); - else if (timer<400) - DrawTexture(50,50,tex_jpg,textw,texth); - else if (timer<600) - DrawTexture(50,50,tex_png,textw,texth); - else - DrawTexture(50,50,tex_bmp,textw,texth);*/ - glBegin(GL_QUADS); - glTexCoord2d(0.0,0.0); glVertex2f(-1, -1); - glTexCoord2d(1.0,0.0); glVertex2f(1, -1); - glTexCoord2d(1.0,1.0); glVertex2f(1, 0); - glTexCoord2d(0.0,1.0); glVertex2f(-1, 0); - glEnd(); + // 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 END /* Swap front and back buffers */ diff --git a/opengl-test-two/opengl-test-two.depend b/opengl-test-two/opengl-test-two.depend index aa8e42d..a86399f 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 -1473070070 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp +1473097371 source:d:\owncloud\documents\programmierung\cpp\imanox-chroma-matting\opengl-test-two\main.cpp diff --git a/opengl-test-two/trimap.fragmentshader b/opengl-test-two/trimap.fragmentshader index fe22e4d..bdd956c 100644 --- a/opengl-test-two/trimap.fragmentshader +++ b/opengl-test-two/trimap.fragmentshader @@ -1,11 +1,17 @@ #version 330 core -uniform sampler2D texture; -in vec2 vTexCoord; -out vec4 color; +// Interpolated values from the vertex shaders +in vec2 UV; + +// Ouput data +out vec3 color; + +// Values that stay constant for the whole mesh. +uniform sampler2D myTextureSampler; void main(){ - //color = vec4(1f,0f,0f,1.0f); - vec2 tcoord=vec2(0.5f,0.5f); - color = vec4(texelFetch(texture, ivec2(vTexCoord), 0).rgb, 1); + + // Output color = color of the texture at the specified UV + color = texture( myTextureSampler, UV ).rgb; } + diff --git a/opengl-test-two/trimap.vertexshader b/opengl-test-two/trimap.vertexshader index 38e88de..59305f6 100644 --- a/opengl-test-two/trimap.vertexshader +++ b/opengl-test-two/trimap.vertexshader @@ -1,13 +1,17 @@ -#version 400 +#version 330 core -in vec2 aVertex; -in vec2 aTexCoord; +// Input vertex data, different for all executions of this shader. +layout(location = 0) in vec3 vertexPosition_modelspace; +layout(location = 1) in vec2 vertexUV; -out vec2 vTexCoord; -out vec3 vNormal; +// Output data ; will be interpolated for each fragment. +out vec2 UV; -void main() -{ - vTexCoord = aTexCoord; - gl_Position = vec3(aVertex, 1); +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; } +