diff options
author | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-02-03 02:20:19 +0100 |
---|---|---|
committer | Lizzy Fleckenstein <lizzy@vlhl.dev> | 2024-02-03 02:20:19 +0100 |
commit | 12829b9597fc7b11d1b475495fecc5d8acad7ac3 (patch) | |
tree | 7f36badc9443bd8ed2237cf483d3f8919d6159bf | |
parent | 81caa657050638938ad86744d557d22df2bd0f1b (diff) | |
download | juxtapos-12829b9597fc7b11d1b475495fecc5d8acad7ac3.tar.xz |
depth peeling
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | assets/fragment.glsl | 20 | ||||
-rw-r--r-- | assets/quad_fragment.glsl | 15 | ||||
-rw-r--r-- | assets/quad_vertex.glsl | 11 | ||||
-rw-r--r-- | assets/vertex.glsl | 26 | ||||
-rw-r--r-- | meson.build | 2 | ||||
-rw-r--r-- | src/main.c | 581 | ||||
m--------- | stb | 0 | ||||
-rw-r--r-- | textures/0.png | bin | 0 -> 2065 bytes | |||
-rw-r--r-- | textures/1.png | bin | 0 -> 987 bytes | |||
-rw-r--r-- | textures/10.png | bin | 0 -> 2370 bytes | |||
-rw-r--r-- | textures/11.png | bin | 0 -> 1306 bytes | |||
-rw-r--r-- | textures/12.png | bin | 0 -> 2281 bytes | |||
-rw-r--r-- | textures/13.png | bin | 0 -> 2519 bytes | |||
-rw-r--r-- | textures/14.png | bin | 0 -> 1686 bytes | |||
-rw-r--r-- | textures/15.png | bin | 0 -> 2304 bytes | |||
-rw-r--r-- | textures/16.png | bin | 0 -> 2708 bytes | |||
-rw-r--r-- | textures/17.png | bin | 0 -> 1709 bytes | |||
-rw-r--r-- | textures/18.png | bin | 0 -> 2728 bytes | |||
-rw-r--r-- | textures/19.png | bin | 0 -> 2743 bytes | |||
-rw-r--r-- | textures/2.png | bin | 0 -> 1872 bytes | |||
-rw-r--r-- | textures/20.png | bin | 0 -> 3164 bytes | |||
-rw-r--r-- | textures/21.png | bin | 0 -> 2260 bytes | |||
-rw-r--r-- | textures/22.png | bin | 0 -> 2779 bytes | |||
-rw-r--r-- | textures/23.png | bin | 0 -> 3117 bytes | |||
-rw-r--r-- | textures/3.png | bin | 0 -> 2204 bytes | |||
-rw-r--r-- | textures/4.png | bin | 0 -> 1364 bytes | |||
-rw-r--r-- | textures/5.png | bin | 0 -> 1962 bytes | |||
-rw-r--r-- | textures/6.png | bin | 0 -> 2381 bytes | |||
-rw-r--r-- | textures/7.png | bin | 0 -> 1432 bytes | |||
-rw-r--r-- | textures/8.png | bin | 0 -> 2335 bytes | |||
-rw-r--r-- | textures/9.png | bin | 0 -> 2376 bytes | |||
-rwxr-xr-x | textures/make.sh | 4 |
33 files changed, 541 insertions, 121 deletions
diff --git a/.gitmodules b/.gitmodules index 1d0fe9b..dbdadd0 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "linmath.h"] path = linmath.h url = https://github.com/datenwolf/linmath.h +[submodule "stb"] + path = stb + url = https://github.com/nothings/stb diff --git a/assets/fragment.glsl b/assets/fragment.glsl index a189112..3e67b83 100644 --- a/assets/fragment.glsl +++ b/assets/fragment.glsl @@ -1,12 +1,26 @@ #version 430 core -in vec2 fTexCoords; +in vec2 fTexCoord; +in vec3 fPos; +in noperspective vec2 fDepthTexCoord; out vec4 oColor; -uniform sampler2D texture0; +uniform sampler2D materialTexture; +//uniform sampler2DShadow prevDepth; +uniform sampler2DMS prevDepth; +//uniform sampler2D prevDepth; void main() { - oColor = texture(texture0, fTexCoords); + //if (texture(prevDepth, vec3(fDepthTexCoord, gl_FragCoord.z + 0.000005)) == 1) + //if (gl_FragCoord.z + 0.000005 < texelFetch(prevDepth, ivec2(gl_FragCoord.xy), gl_SampleID).r) + if (gl_FragCoord.z + 0.000005 < texelFetch(prevDepth, ivec2(gl_FragCoord.xy), gl_SampleID).r) + oColor = vec4(fPos, 0.5); + //oColor = texelFetch(prevDepth, ivec2(gl_FragCoord.xy), gl_SampleID); + else + discard; + //if (texture(prevDepth, vec3(fDepthTexCoord, )) < 1) + // discard; + } diff --git a/assets/quad_fragment.glsl b/assets/quad_fragment.glsl new file mode 100644 index 0000000..7cf31d4 --- /dev/null +++ b/assets/quad_fragment.glsl @@ -0,0 +1,15 @@ +#version 430 core + +in vec2 fTexCoord; + +out vec4 oColor; + +uniform sampler2DMS texture0; +//uniform sampler2D texture0; + +void main() +{ + // oColor = texture(texture0, fTexCoord); + // oColor = texelFetch(texture0, ivec2(gl_FragCoord.xy), 0); + oColor = texelFetch(texture0, ivec2(gl_FragCoord.xy), gl_SampleID); +} diff --git a/assets/quad_vertex.glsl b/assets/quad_vertex.glsl new file mode 100644 index 0000000..c5abaf5 --- /dev/null +++ b/assets/quad_vertex.glsl @@ -0,0 +1,11 @@ +#version 430 core + +layout(location = 0) in vec2 vPos; + +out vec2 fTexCoord; + +void main() +{ + gl_Position = vec4(vPos, 0.0, 1.0); + fTexCoord = vPos * 0.5 + 0.5; +} diff --git a/assets/vertex.glsl b/assets/vertex.glsl index ec8c0b6..b9a5162 100644 --- a/assets/vertex.glsl +++ b/assets/vertex.glsl @@ -6,7 +6,9 @@ layout(location = 1) in vec2 vTexCoord; //out vec3 fPos; //out vec3 fNormal; -out vec2 fTexCoords; +out vec2 fTexCoord; +out vec3 fPos; +out noperspective vec2 fDepthTexCoord; uniform mat4 model; uniform mat4 viewProj; @@ -15,13 +17,31 @@ uniform mat4 viewProj; uniform mat4 viewProj; uniform mat3 normalTransform;*/ +vec3 CMYKtoRGB (vec4 cmyk) { + float c = cmyk.x; + float m = cmyk.y; + float y = cmyk.z; + float k = cmyk.w; + + float invK = 1.0 - k; + float r = 1.0 - min(1.0, c * invK + k); + float g = 1.0 - min(1.0, m * invK + k); + float b = 1.0 - min(1.0, y * invK + k); + return clamp(vec3(r, g, b), 0.0, 1.0); +} + void main() { vec4 pos = vPos; pos = model * pos; - pos.w = pos.w + 2.0; + + pos.w = pos.w * 0.5 + 1.5; gl_Position = viewProj * (pos / pos.w); + fDepthTexCoord = (gl_Position.xy / gl_Position.w) * 0.5 + 0.5; // NDC to tex coords + + fPos = CMYKtoRGB(vPos * 0.4 + vec4(0.4)); + //gl_Position = viewProj * model * vec4(vPos.xyz, 1.0); - fTexCoords = vTexCoord; + fTexCoord = vTexCoord; } diff --git a/meson.build b/meson.build index a9e41c6..9ced251 100644 --- a/meson.build +++ b/meson.build @@ -24,6 +24,8 @@ juxtapos = executable('juxtapos', 'assets/wood.glsl', 'assets/vertex.glsl', 'assets/fragment.glsl', + 'assets/quad_vertex.glsl', + 'assets/quad_fragment.glsl', ]), ], dependencies: [glfw, glew, m], @@ -1,10 +1,13 @@ +#define _GNU_SOURCE #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <GL/glew.h> #include <GL/gl.h> #include <GLFW/glfw3.h> +#define STB_IMAGE_IMPLEMENTATION #include "../linmath.h/linmath.h" +#include "../stb/stb_image.h" static void opengl_error(GLenum err, const char *file, int line) { @@ -19,6 +22,106 @@ static void opengl_error(GLenum err, const char *file, int line) default: break; } } + + +void APIENTRY gl_debug_message_callback(GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, + const GLchar *msg, const void *data) +{ + char* _source; + char* _type; + char* _severity; + + switch (source) { + case GL_DEBUG_SOURCE_API: + _source = "API"; + break; + + case GL_DEBUG_SOURCE_WINDOW_SYSTEM: + _source = "WINDOW SYSTEM"; + break; + + case GL_DEBUG_SOURCE_SHADER_COMPILER: + _source = "SHADER COMPILER"; + break; + + case GL_DEBUG_SOURCE_THIRD_PARTY: + _source = "THIRD PARTY"; + break; + + case GL_DEBUG_SOURCE_APPLICATION: + _source = "APPLICATION"; + break; + + case GL_DEBUG_SOURCE_OTHER: + _source = "UNKNOWN"; + break; + + default: + _source = "UNKNOWN"; + break; + } + + switch (type) { + case GL_DEBUG_TYPE_ERROR: + _type = "ERROR"; + break; + + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + _type = "DEPRECATED BEHAVIOR"; + break; + + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + _type = "UDEFINED BEHAVIOR"; + break; + + case GL_DEBUG_TYPE_PORTABILITY: + _type = "PORTABILITY"; + break; + + case GL_DEBUG_TYPE_PERFORMANCE: + _type = "PERFORMANCE"; + break; + + case GL_DEBUG_TYPE_OTHER: + _type = "OTHER"; + break; + + case GL_DEBUG_TYPE_MARKER: + _type = "MARKER"; + break; + + default: + _type = "UNKNOWN"; + break; + } + + switch (severity) { + case GL_DEBUG_SEVERITY_HIGH: + _severity = "HIGH"; + break; + + case GL_DEBUG_SEVERITY_MEDIUM: + _severity = "MEDIUM"; + break; + + case GL_DEBUG_SEVERITY_LOW: + _severity = "LOW"; + break; + + case GL_DEBUG_SEVERITY_NOTIFICATION: + _severity = "NOTIFICATION"; + break; + + default: + _severity = "UNKNOWN"; + break; + } + + printf("%d: %s of %s severity, raised from %s: %s\n", + id, _type, _severity, _source, msg); +} + void opengl_debug(const char *file, int line) { GLenum err = glGetError(); @@ -35,13 +138,14 @@ struct shader { }; int win_width, win_height; +bool resized = false; static void framebuffer_size_callback(GLFWwindow *handle, int width, int height) { (void) handle; - glViewport(0, 0, width, height); win_width = width; win_height = height; + resized = true; } #define glUniform(loc, x) _Generic((x), \ @@ -50,6 +154,24 @@ static void framebuffer_size_callback(GLFWwindow *handle, int width, int height) GLuint: glUniform1ui \ )(loc, x) +#define GL_DEBUG opengl_debug(__FILE__, __LINE__); + +GLuint texture_upload(unsigned int width, unsigned int height, unsigned char *data, GLenum format) +{ + GLuint txo; + glGenTextures(1, &txo); GL_DEBUG + glBindTexture(GL_TEXTURE_2D, txo); GL_DEBUG + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GL_DEBUG + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GL_DEBUG + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); GL_DEBUG + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); GL_DEBUG + + glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); GL_DEBUG + glGenerateMipmap(GL_TEXTURE_2D); GL_DEBUG + return txo; +} + GLuint shader_program_create(const char *name, struct shader *shaders, size_t num_shaders) { GLuint prog_id = glCreateProgram(); @@ -68,7 +190,7 @@ GLuint shader_program_create(const char *name, struct shader *shaders, size_t nu if (!compile_success) { char error[BUFSIZ]; glGetShaderInfoLog(shader_ids[i], BUFSIZ, NULL, error); - fprintf(stderr, "[error] failed to compile %s shader: %s", shaders[i].name, error); + fprintf(stderr, "[error] failed to compile %s %s shader: %s", name, shaders[i].name, error); exit(EXIT_FAILURE); } @@ -92,7 +214,121 @@ GLuint shader_program_create(const char *name, struct shader *shaders, size_t nu return prog_id; } -#define GL_DEBUG opengl_debug(__FILE__, __LINE__); +#define COMPUTE_SHADER(name, compute_src) \ + GLuint name ## _shader = shader_program_create(#name, (struct shader []) { \ + { "compute", GL_COMPUTE_SHADER, compute_src, sizeof compute_src }, \ + }, 1); + +#define RENDER_SHADER(name, vertex_src, fragment_src) \ + GLuint name ## _shader = shader_program_create(#name, (struct shader []) { \ + { "vertex", GL_VERTEX_SHADER, vertex_src, sizeof vertex_src }, \ + { "fragment", GL_FRAGMENT_SHADER, fragment_src, sizeof fragment_src }, \ + }, 2); + +#define UNIFORM_LOC(shader, name) GLint loc_ ## name = glGetUniformLocation(shader, #name); + +#define EMBED(...) ((const char []) { __VA_ARGS__ }) + +#define BITS_FROM(x, i) ((x) & ((~0U) << (i))) // select all bits above and including bit i +#define BITS_TO(x, i) ((x) & ~((~0U) << (i))) // select all bits below bit i + +#define CUBE_VERTICES_POPULATE(array, DIM) \ + for (unsigned int j = 0; j < 1 << DIM; j++) \ + for (unsigned int k = 0; k < DIM; k++) \ + array[j].pos[k] = ((j >> k) & 1) * 2.0 - 1.0; + +#define CUBE_VERTICES(name, vertex_type, DIM) \ + vertex_type name[1 << DIM] = { 0 }; \ + CUBE_VERTICES_POPULATE(name, DIM) + +#define CUBE_LINE_INDICES(name, DIM) \ + GLuint name[DIM][1 << (DIM-1)][2]; \ + for (unsigned int i = 0; i < DIM; i++) \ + for (unsigned int j = 0; j < 1 << (DIM-1); j++) \ + name[i][j][1] = (indices[i][j][0] = ( \ + BITS_FROM(j, i) << 1 | BITS_TO(j, i) \ + )) | (1 << i); + +#define CUBE_FACE_INDICES(name, DIM) \ + GLuint name[(DIM * (DIM-1)) / 2][1 << (DIM-2)][6]; \ + { \ + unsigned int l = 0; \ + for (unsigned int i = 0; i < DIM; i++) \ + for (unsigned int j = i+1; j < DIM; j++) { \ + for (unsigned int k = 0; k < 1 << (DIM-2); k++) { \ + unsigned int quad[6] = { 0, 1, 2, 2, 3, 1 }; \ + for (unsigned int m = 0; m < 6; m++) { \ + name[l][k][m] = \ + BITS_TO(k, i) | \ + BITS_TO(BITS_FROM(k, i) << 1, j) | \ + BITS_FROM(k << 1, j) << 1 | \ + ((quad[m] >> 0) & 1) << i | \ + ((quad[m] >> 1) & 1) << j; \ + } \ + } \ + l++; \ + } \ + } + +/* +struct tri_sort_arg { + GLuint *indices; + struct vertex *vertices; + mat4x4 model; + mat4x4 view; +}; + +float get_depth(struct tri_sort_arg *arg, const GLuint *t) +{ + vec4 world_space = { 0.0 }; + + for (int i = 0; i < 3; i++) + vec4_add(world_space, world_space, arg->vertices[arg->indices[*t * 3 + i]].pos); + + vec4_scale(world_space, world_space, 1.0/4.0); + + vec4 model_space; + mat4x4_mul_vec4(model_space, arg->model, world_space); + model_space[4] = model_space[4] * 0.5 + 1.5; + vec4_scale(model_space, model_space, 1/model_space[4]); + + vec4 ndc_space; + mat4x4_mul_vec4(ndc_space, arg->view, model_space); + vec4_scale(ndc_space, ndc_space, 1/ndc_space[4]); + + return ndc_space[3]; +} + +int tri_sort_compare(const void *v_a, const void *v_b, void *v_arg) +{ + return (int) (get_depth(v_arg, v_b) - get_depth(v_arg, v_a)); +}*/ + +GLuint vertex_array_create(void *vertices, size_t size_vertices, void *indices, size_t size_indices, void configure()) +{ + GLuint vao; + glGenVertexArrays(1, &vao); + + GLuint buffers[2]; + glGenBuffers(2, buffers); + + glBindVertexArray(vao); + + glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); + glBufferData(GL_ARRAY_BUFFER, size_vertices, vertices, GL_STATIC_DRAW); + + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, size_indices, indices, GL_STATIC_DRAW); + + configure(); + + glBindVertexArray(0); + glDeleteBuffers(2, buffers); + + return vao; +} + +#define NUM_INDICES(array) (sizeof(array) / sizeof(GLuint)) #include "hacks.h" @@ -101,6 +337,37 @@ VERTEX_DEF(vertex, (T_FLOAT, texCoord, 2) ) +VERTEX_DEF(vertex_quad, + (T_FLOAT, pos, 2) +) + +void mat4x4_rotate_xw(mat4x4 out, float angle) +{ + mat4x4_identity(out); + out[0][0] = cos(angle); + out[0][3] = sin(angle); + out[3][0] = -sin(angle); + out[3][3] = cos(angle); +} + +void mat4x4_rotate_yw(mat4x4 out, float angle) +{ + mat4x4_identity(out); + out[1][1] = cos(angle); + out[1][3] = -sin(angle); + out[3][1] = sin(angle); + out[3][3] = cos(angle); +} + +void mat4x4_rotate_zw(mat4x4 out, float angle) +{ + mat4x4_identity(out); + out[2][2] = cos(angle); + out[2][3] = -sin(angle); + out[3][2] = sin(angle); + out[3][3] = cos(angle); +} + int main() { if (!glfwInit()) { @@ -108,15 +375,13 @@ int main() exit(EXIT_FAILURE); } - //FST(FLOAT) x = 0.0; - //MEOW(int) a = 0; - //int a = MEW(); - //int a = CALL(_1, PAIR(1,2)); - //int b = CALL(_2, FLOAT); - atexit(glfwTerminate); - glfwWindowHint(GLFW_SAMPLES, 8); +#define SAMPLES 2 + +#if SAMPLES > 1 + glfwWindowHint(GLFW_SAMPLES, SAMPLES); +#endif glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); @@ -137,9 +402,18 @@ int main() exit(EXIT_FAILURE); } + glEnable(GL_DEBUG_OUTPUT); + glDebugMessageCallback(gl_debug_message_callback, NULL); + glViewport(0, 0, win_width, win_height); glfwSetFramebufferSizeCallback(window, &framebuffer_size_callback); + const char wood_src[] = { +#include "wood.glsl.h" + }; + + COMPUTE_SHADER(wood, wood_src) + // dimensions of the image int tex_w = 480, tex_h = 480; GLuint wood_texture; @@ -166,160 +440,237 @@ int main() glGetIntegerv(GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS, &work_grp_inv); printf("max local work group invocations %i\n", work_grp_inv); */ - const char wood_shader_src[] = { -#include "wood.glsl.h" - }; - - GLuint wood_shader = shader_program_create("wood", (struct shader []) { - { "wood", GL_COMPUTE_SHADER, wood_shader_src, sizeof wood_shader_src }, - }, 1); - glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, wood_texture); glUseProgram(wood_shader); - glDispatchCompute(tex_w, tex_h, 1); + //glDispatchCompute(tex_w, tex_h, 1); + //glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); - const char fragment_shader_src[] = { -#include "fragment.glsl.h" - }; - const char vertex_shader_src[] = { + CUBE_VERTICES(tesseract_vertices, struct vertex, 4) + CUBE_FACE_INDICES(tesseract_indices, 4) + GLuint tesseract_vao = vertex_array_create( + tesseract_vertices, sizeof tesseract_vertices, + tesseract_indices, sizeof tesseract_indices, + vertex_configure_vao); + + const char tesseract_vert_src[] = { #include "vertex.glsl.h" }; + const char tesseract_frag_src[] = { +#include "fragment.glsl.h" + }; - GLuint main_shader = shader_program_create("main", (struct shader []) { - { "vertex", GL_VERTEX_SHADER, vertex_shader_src, sizeof vertex_shader_src }, - { "fragment", GL_FRAGMENT_SHADER, fragment_shader_src, sizeof fragment_shader_src }, - }, 2); - - GLint loc_model = glGetUniformLocation(main_shader, "model"); - GLint loc_viewProj = glGetUniformLocation(main_shader, "viewProj"); + RENDER_SHADER(tesseract, tesseract_vert_src, tesseract_frag_src) + UNIFORM_LOC(tesseract_shader, model) + UNIFORM_LOC(tesseract_shader, viewProj) + UNIFORM_LOC(tesseract_shader, materialTexture) + UNIFORM_LOC(tesseract_shader, prevDepth) - /*struct vertex cube[3][2][2][2]; - for (int i = 0; i < 3; i++) - // for those, i noticed i can replace them with a single loop and use bit manipulation - for (int a = 0; a <= 1; a++) - for (int b = 0; b <= 1; b++) - for (int c = 0; c <= 1; c++) { - struct vertex *v = &cube[i][a][b][c]; - // this simply turned into a loop over k - v->pos[(i+0)%3] = a * 2.0 - 1.0; - v->pos[(i+1)%3] = b * 2.0 - 1.0; - v->pos[(i+2)%3] = c * 2.0 - 1.0; - }*/ - -/*#define DIM 3 - struct vertex vertices[DIM][1 << DIM] = { 0 }; - for (int i = 0; i < DIM; i++) - for (int j = 0; j < 1 << DIM; j++) - for (int k = 0; k < DIM; k++) - vertices[i][j].pos[(i+k)%DIM] = ((j >> k) & 1) * 2.0 - 1.0; -*/ - - /*GLuint indices[DIM][1 << DIM][2]; - for (int i = 0; i < DIM; i++) - for (int j = 0; j < 1 << DIM; j++) - indices[i][j][1] = (indices[i][j][0] = j) ^ (1 << i);*/ - -#define DIM 0 - struct vertex vertices[1 << DIM] = { 0 }; - for (int j = 0; j < 1 << DIM; j++) - for (int k = 0; k < DIM; k++) - vertices[j].pos[k] = ((j >> k) & 1) * 2.0 - 1.0; - -#define BITS_FROM(x, i) ((x) & ((~0U) << (i))) // select all bits above and including bit -#define BITS_TO(x, i) ((x) & ~((~0U) << (i))) // select all bits below bit i - GLuint indices[DIM][1 << (DIM-1)][2]; - for (unsigned int i = 0; i < DIM; i++) - for (unsigned int j = 0; j < 1 << (DIM-1); j++) - indices[i][j][1] = (indices[i][j][0] = ( - BITS_FROM(j, i) << 1 | BITS_TO(j, i) - )) | (1 << i); + CUBE_VERTICES(quad_vertices, struct vertex_quad, 2) + CUBE_FACE_INDICES(quad_indices, 2) + GLuint quad_vao = vertex_array_create( + quad_vertices, sizeof quad_vertices, + quad_indices, sizeof quad_indices, + vertex_quad_configure_vao); - //for () + const char quad_vert_src[] = { +#include "quad_vertex.glsl.h" + }; + const char quad_frag_src[] = { +#include "quad_fragment.glsl.h" + }; - /*GLuint indices[ CUBE_LEN/4 ][8]; - for (int i = 0; i < CUBE_LEN/4; i++) { - for (int j = 0; j < 8; j++) - indices[i][j] = i*4 + (((j+1) % 8) / 2); - }*/ + RENDER_SHADER(quad, quad_vert_src, quad_frag_src) /* - GLuint indices[6][6]; - for (int i = 0; i < 6; i++) { - GLuint quad[6] = { 0, 1, 2, 2, 3, 1 }; - for (int j = 0; j < 6; j++) - indices[i][j] = i*4 + quad[j]; - }*/ + GLuint textures[24]; + for (int i = 0; i < 1; i++) { + char *file; + asprintf(&file, "../textures/%d.png", i); - /* - struct vertex_quad quad[4] = { - {{ -0.5, +0.5, +0.0 }}, - {{ -0.5, -0.5, +0.0 }}, - {{ +0.5, +0.5, +0.0 }}, - {{ +0.5, -0.5, +0.0 }}, - };*/ - - GLuint vao, vbo, ebo; - glGenVertexArrays(1, &vao); - glGenBuffers(1, &vbo); - glGenBuffers(1, &ebo); + int width, height, channels; - glBindVertexArray(vao); + unsigned char *data = stbi_load(file, &width, &height, &channels, 0); - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW); + if (!data) { + fprintf(stderr, "failed to load texture %s\n", file); + exit(EXIT_FAILURE); + } - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices, GL_STATIC_DRAW); + textures[i] = texture_upload(width, height, data, GL_RGBA); + stbi_image_free(data); - vertex_configure_vao(); + free(file); + }*/ + // config unsigned int skyblue = 0x87ceeb; glClearColor( ((skyblue >> 16) & 0xff) / 255.0, ((skyblue >> 8) & 0xff) / 255.0, - ((skyblue >> 0) & 0xff) / 255.0, 1.0); + ((skyblue >> 0) & 0xff) / 255.0, 0.0); glPointSize(5.0); - glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT); +#if SAMPLES > 1 + glEnable(GL_MULTISAMPLE); + glEnable(GL_SAMPLE_SHADING); + glMinSampleShading(1.0); + + GLfloat value; + glGetFloatv(GL_MIN_SAMPLE_SHADING_VALUE, &value); + printf("%f\n", value); + +#endif + + GLuint color_buffer; + glGenTextures(1, &color_buffer); + + GLuint depth_buffers[2]; + glGenTextures(2, depth_buffers); + + GLuint framebuffers[2]; + glGenFramebuffers(2, framebuffers); glfwSetTime(0.0); + float last = 0.0; + float angle = 0.0; + + resized = true; + while (!glfwWindowShouldClose(window)) { + float time = glfwGetTime(); + float delta = time-last; + last = time; + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_DEPTH_TEST); + if (resized) { + resized = false; + glViewport(0, 0, win_width, win_height); + +#if SAMPLES > 1 +#define TEXTURE_TARGET GL_TEXTURE_2D_MULTISAMPLE +#else +#define TEXTURE_TARGET GL_TEXTURE_2D +#endif + + glBindTexture(TEXTURE_TARGET, color_buffer); +#if SAMPLES > 1 + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, SAMPLES, GL_RGBA, win_width, win_height, GL_TRUE); +#else + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, win_width, win_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); +#endif + for (int i = 0; i < 2; i++) { + glBindTexture(TEXTURE_TARGET, depth_buffers[i]); +#if SAMPLES > 1 + glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, SAMPLES, GL_DEPTH_COMPONENT, win_width, win_height, GL_TRUE); +#else + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, win_width, win_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LESS); +#endif + + glBindFramebuffer(GL_FRAMEBUFFER, framebuffers[i]); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, TEXTURE_TARGET, color_buffer, 0); + glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, TEXTURE_TARGET, depth_buffers[i], 0); + } + } + + char *tit; + asprintf(&tit, "%d FPS", (int) (1 / delta)); + glfwSetWindowTitle(window, tit); + free(tit); // preview wood texture - glUseProgram(main_shader); mat4x4 proj; - mat4x4_perspective(proj, 90.0, (float) win_width / (float) win_height, 0.1, 100.0); + mat4x4_perspective(proj, 90.0, (float) win_width / (float) win_height, 1.0, 4.2); vec3 up; vec3_norm(up, (vec3) { 0.0, 1.0, -1.0 }); mat4x4 view; - mat4x4_look_at(view, (vec3) { 0.0, 2.0, 2.0 }, (vec3) { 0, 0, 0 }, up); + mat4x4_look_at(view, (vec3) { 0.0, 1.75, 1.75 }, (vec3) { 0, 0, 0 }, up); mat4x4 view_proj; mat4x4_mul(view_proj, proj, view); + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) + ; + else if (glfwGetKey(window, GLFW_KEY_BACKSPACE) == GLFW_PRESS) + angle -= delta * M_PI * 0.25; + else + angle += delta * M_PI * 0.25; + + /* mat4x4 id; mat4x4_identity(id); mat4x4 model; - mat4x4_rotate_Y(model, id, glfwGetTime() * M_PI * 0.1); + mat4x4_rotate_Z(model, id, angle); + */ - glUniformMatrix4fv(loc_model, 1, GL_FALSE, &model[0][0]); - glUniformMatrix4fv(loc_viewProj, 1, GL_FALSE, &view_proj[0][0]); + mat4x4 model; + mat4x4_identity(model); + + mat4x4 tmp; + + mat4x4_rotate_yw(tmp, angle); + mat4x4_mul(model, model, tmp); + + //mat4x4_rotate_yw(tmp, angle); + //mat4x4_rotate_Y(model, model, angle); + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClear(GL_COLOR_BUFFER_BIT); + + glBindFramebuffer(GL_FRAMEBUFFER, framebuffers[1]); + glClearDepth(1.0); + glClear(GL_DEPTH_BUFFER_BIT); + + for (int i = 0; i < 8; i++) { + glBindFramebuffer(GL_FRAMEBUFFER, framebuffers[i % 2]); + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_GREATER); + glDisable(GL_BLEND); + glClearDepth(0.0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glUseProgram(tesseract_shader); - glBindVertexArray(vao); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, wood_texture); - //glDrawArrays(GL_POINTS, 0, sizeof vertices / sizeof(struct vertex)); - glDrawElements(GL_LINES, sizeof indices / sizeof(GLuint), GL_UNSIGNED_INT, 0); + glUniformMatrix4fv(loc_model, 1, GL_FALSE, &model[0][0]); + glUniformMatrix4fv(loc_viewProj, 1, GL_FALSE, &view_proj[0][0]); + + //glUniform1i(loc_materialTexture, 0); + //glActiveTexture(GL_TEXTURE0); + //glBindTexture(GL_TEXTURE_2D, wood_texture); + + glUniform1i(loc_prevDepth, 1); + glActiveTexture(GL_TEXTURE0 + 1); + glBindTexture(TEXTURE_TARGET, depth_buffers[(i + 1) % 2]); + + glBindVertexArray(tesseract_vao); + glDrawElements(GL_TRIANGLES, NUM_INDICES(tesseract_indices), GL_UNSIGNED_INT, 0); + + // blend result + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glDisable(GL_DEPTH_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glUseProgram(quad_shader); + + glBindVertexArray(quad_vao); + glActiveTexture(GL_TEXTURE0); + glBindTexture(TEXTURE_TARGET, color_buffer); + glDrawElements(GL_TRIANGLES, NUM_INDICES(quad_indices), GL_UNSIGNED_INT, 0); + } glfwSwapBuffers(window); glfwPollEvents(); diff --git a/stb b/stb new file mode 160000 +Subproject f4a71b13373436a2866c5d68f8f80ac6f0bc1ff diff --git a/textures/0.png b/textures/0.png Binary files differnew file mode 100644 index 0000000..9178787 --- /dev/null +++ b/textures/0.png diff --git a/textures/1.png b/textures/1.png Binary files differnew file mode 100644 index 0000000..c623bb5 --- /dev/null +++ b/textures/1.png diff --git a/textures/10.png b/textures/10.png Binary files differnew file mode 100644 index 0000000..7464b09 --- /dev/null +++ b/textures/10.png diff --git a/textures/11.png b/textures/11.png Binary files differnew file mode 100644 index 0000000..f4d7eb5 --- /dev/null +++ b/textures/11.png diff --git a/textures/12.png b/textures/12.png Binary files differnew file mode 100644 index 0000000..18ab36a --- /dev/null +++ b/textures/12.png diff --git a/textures/13.png b/textures/13.png Binary files differnew file mode 100644 index 0000000..131aefc --- /dev/null +++ b/textures/13.png diff --git a/textures/14.png b/textures/14.png Binary files differnew file mode 100644 index 0000000..4307b08 --- /dev/null +++ b/textures/14.png diff --git a/textures/15.png b/textures/15.png Binary files differnew file mode 100644 index 0000000..b714ca5 --- /dev/null +++ b/textures/15.png diff --git a/textures/16.png b/textures/16.png Binary files differnew file mode 100644 index 0000000..31e97ea --- /dev/null +++ b/textures/16.png diff --git a/textures/17.png b/textures/17.png Binary files differnew file mode 100644 index 0000000..ee3bff7 --- /dev/null +++ b/textures/17.png diff --git a/textures/18.png b/textures/18.png Binary files differnew file mode 100644 index 0000000..24eaa74 --- /dev/null +++ b/textures/18.png diff --git a/textures/19.png b/textures/19.png Binary files differnew file mode 100644 index 0000000..699d045 --- /dev/null +++ b/textures/19.png diff --git a/textures/2.png b/textures/2.png Binary files differnew file mode 100644 index 0000000..cb5e014 --- /dev/null +++ b/textures/2.png diff --git a/textures/20.png b/textures/20.png Binary files differnew file mode 100644 index 0000000..6184b0c --- /dev/null +++ b/textures/20.png diff --git a/textures/21.png b/textures/21.png Binary files differnew file mode 100644 index 0000000..de15cad --- /dev/null +++ b/textures/21.png diff --git a/textures/22.png b/textures/22.png Binary files differnew file mode 100644 index 0000000..d9db9b8 --- /dev/null +++ b/textures/22.png diff --git a/textures/23.png b/textures/23.png Binary files differnew file mode 100644 index 0000000..4cf6037 --- /dev/null +++ b/textures/23.png diff --git a/textures/3.png b/textures/3.png Binary files differnew file mode 100644 index 0000000..e351b86 --- /dev/null +++ b/textures/3.png diff --git a/textures/4.png b/textures/4.png Binary files differnew file mode 100644 index 0000000..f2a9b8d --- /dev/null +++ b/textures/4.png diff --git a/textures/5.png b/textures/5.png Binary files differnew file mode 100644 index 0000000..31f5f92 --- /dev/null +++ b/textures/5.png diff --git a/textures/6.png b/textures/6.png Binary files differnew file mode 100644 index 0000000..9e568c3 --- /dev/null +++ b/textures/6.png diff --git a/textures/7.png b/textures/7.png Binary files differnew file mode 100644 index 0000000..3537ccb --- /dev/null +++ b/textures/7.png diff --git a/textures/8.png b/textures/8.png Binary files differnew file mode 100644 index 0000000..c01d815 --- /dev/null +++ b/textures/8.png diff --git a/textures/9.png b/textures/9.png Binary files differnew file mode 100644 index 0000000..43d65c1 --- /dev/null +++ b/textures/9.png diff --git a/textures/make.sh b/textures/make.sh new file mode 100755 index 0000000..3efd662 --- /dev/null +++ b/textures/make.sh @@ -0,0 +1,4 @@ +#!/bin/bash +for ((i = 0; i < 24; i++)); do + magick -size 100x100 -gravity center label:$i PNG32:$i.png +done |