diff --git a/src/main.c b/src/main.c index 86a7d32..19e1b67 100644 --- a/src/main.c +++ b/src/main.c @@ -53,23 +53,32 @@ int main() glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + const int buf_width = 4; + const int buf_height = 4; + ////////////////////////////////////////////////////////////////////// // textures ////////////////////////////////////////////////////////////////////// - const char * data = "arst"; + const char * data = + "arst" + "qwfp" + "1234" + "6789"; + int input_width = 4; + int input_height = 4; uint texture_input = make_texture(data, GL_R8, // internalformat - 2, // width - 2, // height + input_width, + input_height, GL_RED, GL_UNSIGNED_BYTE); uint texture_framebuffer = make_texture(NULL, GL_RGBA32F, - 2, - 2, + buf_width, + buf_height, GL_RGBA, GL_UNSIGNED_BYTE); @@ -83,6 +92,8 @@ int main() src_shader_test_vs_glsl_size, src_shader_test_fs_glsl_start, src_shader_test_fs_glsl_size); + uint program_test__tex_sampler = glGetUniformLocation(program_test, "tex_sampler"); + uint program_test__tex_size = glGetUniformLocation(program_test, "tex_size"); ////////////////////////////////////////////////////////////////////// // buffers @@ -111,12 +122,56 @@ int main() if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - /* + // + glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); + glViewport(0, 0, buf_width, buf_height); + + glClearColor(0.78, 0.56, 0.34, 0.12); + glClear(GL_COLOR_BUFFER_BIT); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texture_input); uint draw_buffers[1] = {GL_COLOR_ATTACHMENT0}; glDrawBuffers(1, draw_buffers); - glViewport(0, 0, 2, 2); - */ + glUseProgram(program_test); + glUniform1i(program_test__tex_sampler, 0); + glUniform4f(program_test__tex_size, + input_width, input_height, + 0.5f / input_width, + 0.5f / input_height); + glBindVertexArray(vertex_array); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + float out[4 * buf_width * buf_height] = {}; + glReadPixels(0, 0, + buf_width, buf_height, + GL_RGBA, + GL_FLOAT, + out); + + for (int y = 0; y < buf_height; y++) { + for (int x = 0; x < buf_width; x++) { + printf("[% 2.3f % 2.3f % 2.3f % 2.3f ] ", + out[(y * buf_width + x) * 4 + 0], + out[(y * buf_width + x) * 4 + 1], + out[(y * buf_width + x) * 4 + 2], + out[(y * buf_width + x) * 4 + 3]); + } + printf("\n"); + } + int sum = 0; + for (int i = 0; i < buf_width * buf_height; i++) { + printf("%d ", data[i]); + if ((i % 4) == 3) + printf("\n"); + sum += data[i]; + } + printf("sum: %d\n", sum); + break; + + /* + glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, vp_width, vp_height); glClearColor(0.1, 0.2, 0.0, 0.4); @@ -125,24 +180,11 @@ int main() glUseProgram(program_test); glBindVertexArray(vertex_array); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + */ //glFlush(); //glFinish(); - /* - float out[4 * 2 * 2] = {0}; - glReadPixels(0, 0, - 2, 2, - GL_RGBA, - GL_FLOAT, - out); - - for (int i = 0; i < 4 * 2 * 2; i++) { - printf("%f\n", out[i]); - } - */ - //printf("swap\n"); - glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/src/shader/test.fs.glsl b/src/shader/test.fs.glsl index b4b046e..4a02ae7 100644 --- a/src/shader/test.fs.glsl +++ b/src/shader/test.fs.glsl @@ -2,9 +2,24 @@ out vec4 fragment_color; +uniform sampler2D tex_sampler; +uniform vec4 tex_size; + in vec2 f_position; void main() { - fragment_color = vec4(f_position, 0, 0); + vec4 p = texture(tex_sampler, f_position); + + float sum = 0; + for (float y = 0; y < tex_size.y; y++) { + for (float x = 0; x < tex_size.x; x++) { + vec2 coord = vec2((x * 2 + 1) * tex_size.z, + (y * 2 + 1) * tex_size.w); + + sum += texture(tex_sampler, coord).x * 255; + } + } + + fragment_color = vec4(p.x * 255, sum, f_position); } diff --git a/src/shader/test.vs.glsl b/src/shader/test.vs.glsl index e1b1518..37fea77 100644 --- a/src/shader/test.vs.glsl +++ b/src/shader/test.vs.glsl @@ -6,7 +6,7 @@ out vec2 f_position; void main() { - f_position = position; + f_position = position * 0.5 + 0.5; gl_Position = vec4(position, 0, 1); }