diff --git a/Makefile b/Makefile index 72b4ef3..790cb3b 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,8 @@ cocoa: $(OBJ) $(CC) -framework OpenGL -o $(BIN) $(OBJ) ext/glfw.a -lGL -lglfw wasm: $(OBJ) - $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o mani.html $(OBJ) + $(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).js $(OBJ) + chmod -x $(BIN).wasm libglfw.so: $(CC) -fPIC -shared $(DFLAGS) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@ diff --git a/src/context.c b/src/context.c index 449e703..be286b5 100644 --- a/src/context.c +++ b/src/context.c @@ -14,7 +14,6 @@ void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b) { glEnable(GL_DEPTH_TEST); - glEnable(GL_FRAMEBUFFER_SRGB); glClearColor((float)r / 0xff, (float)g / 0xff, (float)b / 0xff, 1.0); } diff --git a/src/data/shaders.h b/src/data/shaders.h index 48294f5..1c25efc 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -40,8 +40,8 @@ const char * vs = " vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);" " vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);" - " aNormal[i] = (vec2(aNormal_w, aNormal[i]) * rotate2d(angle))[0];" - " aPos[i] = (vec2(aPos_w, aPos[i]) * rotate2d(angle))[0];" + " aNormal[i] = (vec2(aNormal[i], aNormal_w) * rotate2d(angle))[0];" + " aPos[i] = (vec2(aPos[i], aPos_w) * rotate2d(angle))[0];" " Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;" " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" @@ -67,7 +67,8 @@ const char * fs_plain = "void main()" "{" - " FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;" + " vec4 color = texture( palette, vec3(0,0,index)).rgba;" + " FragColor = vec4(pow(vec3(color),vec3(1.0/2.2)),color.a);" "}"; const char * fs = @@ -102,6 +103,6 @@ const char * fs = " float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n" " float diffuse = abs(dot(normalize(Normal), lightDir));\n" - " vec3 result = (0.5 + 1.5*diffuse + 4*specular) * color.rgb;\n" - " FragColor = vec4(result, color.a)*1/2.2;\n" + " vec3 result = pow((0.5 + 0.5*diffuse + 1.5*specular) * color.rgb, vec3(1.0/2.2));\n" + " FragColor = vec4(result, color.a);\n" "}"; diff --git a/src/input.c b/src/input.c index 865036b..319923a 100644 --- a/src/input.c +++ b/src/input.c @@ -73,7 +73,6 @@ void __key_callback_input( } - set_projection_mesh(projection); return; } diff --git a/src/main.c b/src/main.c index 1698f5e..b041fad 100644 --- a/src/main.c +++ b/src/main.c @@ -47,8 +47,8 @@ extern volatile unsigned char animate_index; #ifndef EMSCRIPTEN static inline #endif - void - main_loop(void) + +void main_loop(void) { quat_t q; @@ -60,23 +60,26 @@ static inline { static float angle = 0; - if (animate_index && m) - { - angle += 0.01; - - load_uint_to_shader(shader, "i", animate_index - 1); - load_uint_to_shader(shader_plain, "i", animate_index - 1); - load_float_to_shader(shader, "angle", angle); - load_float_to_shader(shader_plain, "angle", angle); - } - - if (angle > M_PI / 2 && angle) + if( angle > M_PI/2 ) { animate_index = 0; angle = 0; + + load_float_to_shader( shader, "angle", angle); + load_float_to_shader( shader_plain, "angle", angle); + set_projection_mesh( projection ); + } + + if( animate_index ) + { + load_uint_to_shader( shader, "i", animate_index-1 ); + load_uint_to_shader( shader_plain, "i", animate_index-1 ); + + angle+=0.01; + load_float_to_shader( shader, "angle", angle); + load_float_to_shader( shader_plain, "angle", angle); } } - clean_context(); #ifndef DEBUG @@ -116,7 +119,7 @@ int main(void) mlog("[CONTEXT] Error al inicializar...\n"); goto error_context; } - set_clean_color_context(0xFB, 0xEA, 0xEB); + set_clean_color_context(0xFF, 0xFF, 0xFF); } mlog("[TEXTURE] Inicializando...\n"); @@ -181,7 +184,8 @@ int main(void) mlog("[MAIN LOOP] Inicializando...\n"); #ifdef EMSCRIPTEN - emscripten_set_main_loop(&main_loop, 0, 1); + emscripten_set_main_loop(&main_loop, 60, 1); + return 0; #else while (is_open_window(window)) main_loop(); diff --git a/src/main.h b/src/main.h index 963ce6e..a14d487 100644 --- a/src/main.h +++ b/src/main.h @@ -154,3 +154,9 @@ void destroy_texture(id_t texture); id_t create_palette_texture(const unsigned char colors[][4], unsigned char n ); quat_t poll_input(window_t window); + +#ifdef EMSCRIPTEN +#ifdef GLAD +#error undefine GLAD on src/main.h please +#endif +#endif diff --git a/src/main.o b/src/main.o new file mode 100644 index 0000000..36f5d06 Binary files /dev/null and b/src/main.o differ diff --git a/src/matrix.c b/src/matrix.c index b5c48f9..5108268 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -30,7 +30,7 @@ void load_fix_matrix(id_t shader, float ratio) const int d = 7; glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m); - glm_perspective(CGLM_PI / 4, ratio, d - 3, d + 3, n); + glm_perspective(CGLM_PI / 6, ratio, d - 3, d + 3, n); glm_mat4_mul(n, m, m); load_mat4_to_shader(shader, "fix", (mat4_t)m); diff --git a/src/surface.c b/src/surface.c index 9f48186..12a99ab 100644 --- a/src/surface.c +++ b/src/surface.c @@ -1,6 +1,7 @@ #include #include #include +#include #define TEST @@ -106,7 +107,7 @@ float *generate_data_surface(unsigned char *s) int *cara; parm.f = cube; - parm.m = 5; + parm.m = 6; parm.n = parm.m; parm.grid = 1; @@ -183,79 +184,105 @@ float *generate_data_surface(unsigned char *s) } static void __calculate_normal( - float *p1, float *p2, float *p3, float *normal, unsigned char n) + float *p1, float *p2, float *p3, float *normal, unsigned char n) { - float alpha; - vec4 v1, v2, v3; - vec4 u1, u2, u3; + unsigned char i; + float alpha; + float *v1, *v2, *v3; + float *u1, *u2, *u3; - switch (n) - { - case 3: + v1 = malloc(n * sizeof(float)); + v2 = malloc(n * sizeof(float)); + v3 = malloc(n * sizeof(float)); + u1 = malloc(n * sizeof(float)); + u2 = malloc(n * sizeof(float)); + u3 = malloc(n * sizeof(float)); - glm_vec3_sub(p2, p1, v1); - glm_vec3_sub(p3, p1, v2); + /* + Calculate a normal vector of a plain using Gram-Schmidt process + */ + { + for (i = 0; i < n; ++i) { + v1[i] = p2[i] - p1[i]; + v2[i] = p3[i] - p1[i]; + v3[i] = p1[i]; + } - glm_vec3_cross(v1, v2, normal); - glm_vec3_normalize(normal); - return; + for (i = 0; i < n; ++i) { + u1[i] = v1[i]; + } - case 4: + { + float proj[n]; + float dot_v2_u1 = 0.0f, dot_u1_u1 = 0.0f; + for (i = 0; i < n; ++i) { + dot_v2_u1 += v2[i] * u1[i]; + dot_u1_u1 += u1[i] * u1[i]; + } + alpha = dot_v2_u1 / dot_u1_u1; - /* - In Grant-Shmidth we need 3 linearly independian vector that forms a - basis, so we can have a ortonormal version of that basis, since, we - must have v1 = p3 - p1 v2 = p2 - p1 Then v3 = p1, will most certantly - be linerly independiant to v1 and v2. - */ - glm_vec4_sub(p2, p1, v1); - glm_vec4_sub(p3, p1, v2); - glm_vec4_copy(p1, v3); + for (i = 0; i < n; ++i) { + proj[i] = u1[i] * alpha; + u2[i] = v2[i] - proj[i]; + } + } - /* Setup U1 */ - { - glm_vec4_copy(v1, u1); - } + { + float proj1[n], proj2[n]; + float dot_v3_u1 = 0.0f, dot_u1_u1 = 0.0f; + float dot_v3_u2 = 0.0f, dot_u2_u2 = 0.0f; - /* Setup U2 */ - { - vec4 proj; + for (i = 0; i < n; ++i) { + dot_v3_u1 += v3[i] * u1[i]; + dot_u1_u1 += u1[i] * u1[i]; + } + for (i = 0; i < n; ++i) { + proj1[i] = u1[i] * (dot_v3_u1 / dot_u1_u1); + } - alpha = glm_vec4_dot(v2, u1) / glm_vec4_dot(u1, u1); - glm_vec4_scale(u1, alpha, proj); + for (i = 0; i < n; ++i) { + dot_v3_u2 += v3[i] * u2[i]; + dot_u2_u2 += u2[i] * u2[i]; + } + for (i = 0; i < n; ++i) { + proj2[i] = u2[i] * (dot_v3_u2 / dot_u2_u2); + u3[i] = v3[i] - proj1[i] - proj2[i]; + } + } - glm_vec4_sub(v2, proj, u2); - } + float magnitude = 0.0f; + for (i = 0; i < n; ++i) { + magnitude += u3[i] * u3[i]; + } + magnitude = sqrtf(magnitude); - /* Setup U3 */ - { - vec4 proj1, proj2; + for (i = 0; i < n; ++i) { + normal[i] = u3[i] / magnitude; + } - alpha = glm_vec4_dot(v3, u1) / glm_vec4_dot(u1, u1); - glm_vec4_scale(u1, alpha, proj1); - - alpha = glm_vec4_dot(v3, u2) / glm_vec4_dot(u2, u2); - glm_vec4_scale(u2, alpha, proj2); - - glm_vec4_sub(v3, proj1, u3); - glm_vec4_sub(u3, proj2, u3); - } - - glm_vec4_copy(u3, normal); - glm_vec4_normalize(normal); - return; - } + free(v1); + free(v2); + free(v3); + free(u1); + free(u2); + free(u3); + return; + } } + float *generate_normals_surface(float *d, unsigned char m) { float *n; n = malloc((*d + 1) * sizeof(float)); *n = *d; + + float * norm_vec; + norm_vec=malloc(m*sizeof(float)); + for (int i = 0; i < *d; i += 3 * m) { - vec4 norm_vec; __calculate_normal( (d + 1) + i, (d + 1) + i + m, (d + 1) + i + 2 * m, norm_vec, m); @@ -263,5 +290,7 @@ float *generate_normals_surface(float *d, unsigned char m) glm_vec3_copy(norm_vec, (n + 1) + i + m); glm_vec3_copy(norm_vec, (n + 1) + i + 2 * m); } + + free(norm_vec); return n; }