Compare commits
8 Commits
pedro-klei
...
klein
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
65017c859c | ||
|
|
f514fc7ffe | ||
|
|
f8eb20c0cf | ||
|
|
33940b7bb6 | ||
|
|
b1897b500b | ||
|
|
fd598255f4 | ||
|
|
733ababde0 | ||
|
|
987ce54429 |
12
Makefile
12
Makefile
@@ -2,19 +2,23 @@ BIN = manigraph
|
||||
|
||||
OBJ = \
|
||||
ext/glad/glad.o \
|
||||
src/surface.o \
|
||||
src/context.o \
|
||||
src/window.o \
|
||||
src/load.o \
|
||||
src/shader.o \
|
||||
src/klein.o \
|
||||
src/input.o \
|
||||
src/load.o \
|
||||
src/mesh.o \
|
||||
src/main.o
|
||||
|
||||
EXAMPLES = \
|
||||
example/basic.o
|
||||
|
||||
CFLAGS = \
|
||||
-I./ext/cglm/include \
|
||||
-I./ext/glfw/include \
|
||||
-I./ext/glad \
|
||||
-I./include \
|
||||
-Wall -Wno-unused-function -std=c99 -D_GNU_SOURCE \
|
||||
|
||||
WAYLAND-LIB = \
|
||||
@@ -38,7 +42,7 @@ help:
|
||||
@echo "Clean"
|
||||
@echo " $(MAKE) clean"
|
||||
|
||||
src/main.o: src/data/axis.h src/data/shaders.h
|
||||
src/main.o: src/data/shaders.h
|
||||
|
||||
windows: $(OBJ)
|
||||
cd ext; $(MAKE) -f glfw.mk windows; cd -
|
||||
@@ -72,6 +76,8 @@ clean:
|
||||
cd ext; $(MAKE) -f glfw.mk clean; cd -
|
||||
|
||||
|
||||
examples: $(EXAMPLES)
|
||||
|
||||
.SUFFIXES: .c .o
|
||||
|
||||
.c.o:
|
||||
|
||||
92
example/basic.c
Normal file
92
example/basic.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define KLEIN_IMPLEMENT
|
||||
#include <klein/klein.h>
|
||||
#include <klein/norm.h>
|
||||
#include <klein/parm.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
void cube(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1;
|
||||
|
||||
if (4 == 2)
|
||||
d_surface[2] = 0;
|
||||
}
|
||||
|
||||
void mobius(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
const float width = 0.5;
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
|
||||
float v = (2 * width) * ((float)coord[1] / grid[1]) - width;
|
||||
|
||||
d_surface[0] = cos(u) + v * cos(u / 2) * cos(u);
|
||||
d_surface[1] = sin(u) + v * cos(u / 2) * sin(u);
|
||||
d_surface[2] = v * sin(u / 2);
|
||||
}
|
||||
|
||||
void torus(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
|
||||
float v = (2 * M_PI) * ((float)coord[1] / grid[1]);
|
||||
|
||||
d_surface[0] = (1 + 0.5 * cos(v)) * cos(u);
|
||||
d_surface[1] = (1 + 0.5 * cos(v)) * sin(u);
|
||||
d_surface[2] = 0.5 * sin(v);
|
||||
}
|
||||
|
||||
void klein(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
|
||||
float v = (2 * M_PI) * ((float)coord[1] / grid[1]);
|
||||
|
||||
d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u);
|
||||
d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u);
|
||||
d_surface[2] = sin(v) * cos(u / 2);
|
||||
d_surface[3] = sin(v) * sin(u / 2);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned char i = 0;
|
||||
const char *file_name[] = {"mobius.klein", "torus.klein", "klein.klein"};
|
||||
struct parm parametrization[] = {{
|
||||
.grid = (unsigned char[]){16, 4},
|
||||
.m = 2,
|
||||
.n = 3,
|
||||
.f = mobius,
|
||||
},
|
||||
{
|
||||
.grid = (unsigned char[]){16, 8},
|
||||
.m = 2,
|
||||
.n = 3,
|
||||
.f = torus,
|
||||
},
|
||||
{
|
||||
.grid = (unsigned char[]){16, 16},
|
||||
.m = 2,
|
||||
.n = 4,
|
||||
.f = klein,
|
||||
}};
|
||||
|
||||
for (i = 0; i < 3; ++i)
|
||||
{
|
||||
struct klein klein;
|
||||
printf("writing %s\n", file_name[i]);
|
||||
|
||||
klein_parametrize(&klein, parametrization[i]);
|
||||
klein_normalize(&klein);
|
||||
klein_export_file(klein, file_name[i]);
|
||||
|
||||
free(klein.vertex);
|
||||
free(klein.normals);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
50
example/n-cube.c
Normal file
50
example/n-cube.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define KLEIN_IMPLEMENT
|
||||
#include <klein/klein.h>
|
||||
#include <klein/norm.h>
|
||||
#include <klein/parm.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
unsigned char dim = 4;
|
||||
|
||||
void cube(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < dim; i++)
|
||||
d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1;
|
||||
|
||||
if (dim == 2)
|
||||
d_surface[2] = 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned char i;
|
||||
char file_name[0xff];
|
||||
struct klein klein;
|
||||
struct parm parametrization = {
|
||||
.m = dim,
|
||||
.n = dim,
|
||||
.f = cube,
|
||||
};
|
||||
|
||||
parametrization.grid = malloc(dim);
|
||||
|
||||
for (i = 0; i < dim; ++i)
|
||||
parametrization.grid[i] = 1 << i;
|
||||
|
||||
snprintf(file_name, 0xff, "%03d-cube.klein", dim);
|
||||
printf("writing %s\n", file_name);
|
||||
|
||||
klein_parametrize(&klein, parametrization);
|
||||
klein_normalize(&klein);
|
||||
klein_export_file(klein, file_name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
50
example/riemman.c
Normal file
50
example/riemman.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <complex.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define KLEIN_IMPLEMENT
|
||||
#include <klein/klein.h>
|
||||
#include <klein/norm.h>
|
||||
#include <klein/parm.h>
|
||||
|
||||
#ifndef CMPLX
|
||||
#define CMPLX(a, b) (a + I * b)
|
||||
#endif
|
||||
|
||||
complex float f(complex float z) { return csqrt(z); }
|
||||
|
||||
void riemman(float *d_surface, int *coords, unsigned char *grid)
|
||||
{
|
||||
complex float eq;
|
||||
float u = 2 * ((float)coords[0] / grid[0]) - 1;
|
||||
float v = 2 * ((float)coords[1] / grid[1]) - 1;
|
||||
|
||||
eq = f(CMPLX(u, v));
|
||||
|
||||
d_surface[0] = u;
|
||||
d_surface[1] = v;
|
||||
d_surface[2] = creal(eq);
|
||||
d_surface[3] = cimag(eq);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
const char *file_name = "riemman.klein";
|
||||
struct klein klein;
|
||||
struct parm parametrization = {
|
||||
.grid = (unsigned char[]){16, 4},
|
||||
.m = 2,
|
||||
.n = 4,
|
||||
.f = riemman,
|
||||
};
|
||||
|
||||
printf("writing %s\n", file_name);
|
||||
|
||||
klein_parametrize(&klein, parametrization);
|
||||
klein_normalize(&klein);
|
||||
klein_export_file(klein, file_name);
|
||||
|
||||
free(klein.vertex);
|
||||
free(klein.normals);
|
||||
return 0;
|
||||
}
|
||||
48
include/klein/klein.h
Normal file
48
include/klein/klein.h
Normal file
@@ -0,0 +1,48 @@
|
||||
#ifdef KLEIN_H
|
||||
#error file included twice
|
||||
#endif
|
||||
#define KLEIN_H
|
||||
|
||||
#ifdef KLEIN_IMPLEMENT
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
struct klein
|
||||
{
|
||||
unsigned long vertex_size;
|
||||
float *vertex, *normals;
|
||||
unsigned char dim;
|
||||
};
|
||||
|
||||
/*
|
||||
The klein format must have:
|
||||
5 bytes with klein.
|
||||
1 byte empty for expantions
|
||||
1 byte with the dimention of the surface
|
||||
|
||||
*/
|
||||
|
||||
static inline
|
||||
int klein_export_file(struct klein klein, const char * filename);
|
||||
|
||||
|
||||
#ifdef KLEIN_IMPLEMENT
|
||||
static inline
|
||||
int klein_export_file(struct klein klein, const char * filename)
|
||||
{
|
||||
FILE *file = fopen(filename, "wb");
|
||||
if (!file)
|
||||
return 1;
|
||||
|
||||
fwrite("KLEIN", 1, 5, file);
|
||||
fwrite("\0", 1, 1, file);
|
||||
fwrite(&klein.dim, 1, 1, file);
|
||||
fwrite(&klein.vertex_size, 8, 1, file);
|
||||
fwrite(klein.vertex, 16, klein.vertex_size * klein.dim, file);
|
||||
fwrite(klein.normals, 16, klein.vertex_size * klein.dim, file);
|
||||
|
||||
fclose(file);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
118
include/klein/norm.h
Normal file
118
include/klein/norm.h
Normal file
@@ -0,0 +1,118 @@
|
||||
static inline
|
||||
void __calculate_normal( float *p1, float *p2, float *p3, float *normal, unsigned char n)
|
||||
{ unsigned char i;
|
||||
float alpha;
|
||||
float *v1, *v2, *v3;
|
||||
float *u1, *u2, *u3;
|
||||
|
||||
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));
|
||||
|
||||
/*
|
||||
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];
|
||||
}
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
u1[i] = v1[i];
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
proj[i] = u1[i] * alpha;
|
||||
u2[i] = v2[i] - proj[i];
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
float magnitude = 0.0f;
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
magnitude += u3[i] * u3[i];
|
||||
}
|
||||
magnitude = sqrtf(magnitude);
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
normal[i] = u3[i] / magnitude;
|
||||
}
|
||||
|
||||
free(v1);
|
||||
free(v2);
|
||||
free(v3);
|
||||
free(u1);
|
||||
free(u2);
|
||||
free(u3);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void klein_normalize(struct klein * klein)
|
||||
{
|
||||
unsigned long i;
|
||||
unsigned char j;
|
||||
float *norm_vec;
|
||||
|
||||
klein->normals = malloc((klein->dim * klein->vertex_size) * sizeof(float));
|
||||
norm_vec = malloc(klein->dim * sizeof(float));
|
||||
|
||||
for (i = 0; i < klein->vertex_size; i += 3 * klein->dim)
|
||||
{
|
||||
__calculate_normal(klein->vertex + i, klein->vertex + i + klein->dim, klein->vertex + i + 2 * klein->dim, norm_vec, klein->dim);
|
||||
for (j = 0; j < klein->dim; ++j )
|
||||
{
|
||||
(klein->normals + i)[j]=norm_vec[j];
|
||||
(klein->normals + i + klein->dim)[j]=norm_vec[j];
|
||||
(klein->normals + i + 2*klein->dim)[j]=norm_vec[j];
|
||||
}
|
||||
}
|
||||
|
||||
free(norm_vec);
|
||||
}
|
||||
134
include/klein/parm.h
Normal file
134
include/klein/parm.h
Normal file
@@ -0,0 +1,134 @@
|
||||
#ifndef KLEIN_H
|
||||
#warning Please include klein/klein.h before klein/parm.h
|
||||
#endif
|
||||
|
||||
#ifdef KLEIN_PARM_H
|
||||
#error file included twice
|
||||
#endif
|
||||
#define KLEIN_PARM_H
|
||||
|
||||
|
||||
typedef void (*function_t)(float *, int *, unsigned char *);
|
||||
|
||||
struct parm
|
||||
{
|
||||
unsigned char *grid;
|
||||
unsigned char m, n;
|
||||
function_t f;
|
||||
};
|
||||
|
||||
|
||||
void klein_parametrize( struct klein * klein, struct parm parm );
|
||||
|
||||
#ifdef KLEIN_IMPLEMENT
|
||||
#ifdef TEST
|
||||
#include <assert.h>
|
||||
#endif
|
||||
|
||||
static inline int __factorial(int n)
|
||||
{
|
||||
if (n == 1)
|
||||
return 1;
|
||||
|
||||
return n * __factorial(n - 1);
|
||||
}
|
||||
|
||||
static inline int __face(int n)
|
||||
{
|
||||
if (n == 2)
|
||||
return 1;
|
||||
|
||||
return (1 << (n - 3)) * __factorial(n) / __factorial(n - 2);
|
||||
}
|
||||
|
||||
void klein_parametrize( struct klein * klein, struct parm parm)
|
||||
{
|
||||
unsigned int i, j, k, o, p, n;
|
||||
unsigned long size, q = 0;
|
||||
int *face;
|
||||
|
||||
#ifdef TEST
|
||||
assert(__face(2) == 1);
|
||||
assert(__face(3) == 6);
|
||||
assert(__face(4) == 24);
|
||||
#endif
|
||||
|
||||
klein->dim = parm.n;
|
||||
klein->vertex_size = 0;
|
||||
{
|
||||
unsigned char test = 0;
|
||||
for (o = 0; o < parm.m; o++)
|
||||
{
|
||||
for (p = 0; p < o; p++)
|
||||
{
|
||||
test += 1;
|
||||
klein->vertex_size += parm.grid[p] * parm.grid[o] * 6 * __face(parm.m);
|
||||
}
|
||||
}
|
||||
klein->vertex_size /= test;
|
||||
}
|
||||
|
||||
size = (klein->dim) * (klein->vertex_size);
|
||||
klein->vertex = malloc(size * sizeof(float));
|
||||
|
||||
face = malloc(parm.m * sizeof(int));
|
||||
|
||||
for (o = 0; o < parm.m; o++)
|
||||
{
|
||||
for (p = 0; p < o; p++)
|
||||
{
|
||||
for (k = 0; k < (1 << (parm.m - 2)); k++)
|
||||
{
|
||||
unsigned char skip = 0;
|
||||
for (n = 0; n < parm.m; n++)
|
||||
{
|
||||
if (n == o || n == p)
|
||||
skip++;
|
||||
|
||||
face[n] = (k & (1 << (n - skip))) ? parm.grid[n] : 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < parm.grid[p]; i++)
|
||||
{
|
||||
for (j = 0; j < parm.grid[o]; j++)
|
||||
{
|
||||
face[p] = i;
|
||||
face[o] = j;
|
||||
parm.f(&klein->vertex[q], face, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
face[p] = i + 1;
|
||||
face[o] = j;
|
||||
parm.f(&klein->vertex[q], face, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
face[p] = i + 1;
|
||||
face[o] = j + 1;
|
||||
parm.f(&klein->vertex[q], face, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
face[p] = i;
|
||||
face[o] = j;
|
||||
parm.f(&klein->vertex[q], face, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
face[p] = i;
|
||||
face[o] = j + 1;
|
||||
parm.f(&klein->vertex[q], face, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
face[p] = i + 1;
|
||||
face[o] = j + 1;
|
||||
parm.f(&klein->vertex[q], face, parm.grid);
|
||||
q += parm.n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
assert(q == size);
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -1,40 +0,0 @@
|
||||
#undef A
|
||||
#undef B
|
||||
#undef C
|
||||
#undef D
|
||||
#undef E
|
||||
#undef F
|
||||
#undef G
|
||||
#undef H
|
||||
|
||||
#define A -2.0,-0.05,-0.05,
|
||||
#define B -2.0,-0.05, 0.05,
|
||||
#define C -2.0, 0.05,-0.05,
|
||||
#define D -2.0, 0.05, 0.05,
|
||||
#define E 2.0,-0.05,-0.05,
|
||||
#define F 2.0,-0.05, 0.05,
|
||||
#define G 2.0, 0.05,-0.05,
|
||||
#define H 2.0, 0.05, 0.05,
|
||||
|
||||
float d_axis[] =
|
||||
{
|
||||
3*3*2*6,
|
||||
|
||||
A C E
|
||||
G E C
|
||||
|
||||
E G F
|
||||
H F G
|
||||
|
||||
F H B
|
||||
D B H
|
||||
|
||||
B D A
|
||||
C A D
|
||||
|
||||
C D G
|
||||
H G D
|
||||
|
||||
E B A
|
||||
B E F
|
||||
};
|
||||
37
src/input.c
37
src/input.c
@@ -9,7 +9,7 @@ unsigned char selected_axis = 0;
|
||||
int window_width;
|
||||
int window_height;
|
||||
|
||||
unsigned char animate_index=0;
|
||||
unsigned char animate_index = 0;
|
||||
|
||||
versor q = GLM_QUAT_IDENTITY_INIT;
|
||||
|
||||
@@ -56,23 +56,22 @@ void __key_callback_input(
|
||||
projection.w = projection.x;
|
||||
projection.x = tmp;
|
||||
|
||||
animate_index=1;
|
||||
animate_index = 1;
|
||||
break;
|
||||
case GLFW_KEY_O:
|
||||
tmp = projection.w;
|
||||
projection.w = projection.y;
|
||||
projection.y = tmp;
|
||||
animate_index=2;
|
||||
animate_index = 2;
|
||||
break;
|
||||
case GLFW_KEY_P:
|
||||
tmp = projection.w;
|
||||
projection.w = projection.z;
|
||||
projection.z = tmp;
|
||||
animate_index=3;
|
||||
animate_index = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -114,9 +113,9 @@ void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
|
||||
versor p = GLM_QUAT_IDENTITY_INIT;
|
||||
versor r = GLM_QUAT_IDENTITY_INIT;
|
||||
|
||||
//glm_quatv(p, yoffset * ANGLE, axis[selected_axis]);
|
||||
glm_quatv(p, yoffset * ANGLE * 2, (vec3){-1,0,0});
|
||||
glm_quatv(r, xoffset * ANGLE * 2, (vec3){0,1,0});
|
||||
// glm_quatv(p, yoffset * ANGLE, axis[selected_axis]);
|
||||
glm_quatv(p, yoffset * ANGLE * 2, (vec3){-1, 0, 0});
|
||||
glm_quatv(r, xoffset * ANGLE * 2, (vec3){0, 1, 0});
|
||||
glm_quat_mul(p, q, q);
|
||||
glm_quat_mul(r, q, q);
|
||||
|
||||
@@ -129,6 +128,23 @@ void __scroll_callback_input(GLFWwindow *window, double xoffset, double yoffset)
|
||||
glm_quat_rotatev(r, axis[2], axis[2]);
|
||||
}
|
||||
|
||||
void __drop_callback_input(GLFWwindow *window, int count, char **path)
|
||||
{
|
||||
struct surface surface;
|
||||
|
||||
if (create_surface_klein(*path, &surface))
|
||||
return;
|
||||
|
||||
if (!(projection.mesh = create_mesh(surface)))
|
||||
return;
|
||||
|
||||
projection.m = surface.dim;
|
||||
set_projection_mesh(projection);
|
||||
|
||||
free(surface.norm);
|
||||
free(surface.data);
|
||||
}
|
||||
|
||||
quat_t poll_input(window_t window)
|
||||
{
|
||||
versor p = GLM_QUAT_IDENTITY_INIT;
|
||||
@@ -172,10 +188,11 @@ end:
|
||||
glm_quat_normalize(q);
|
||||
|
||||
// LOG INFO
|
||||
if(0)
|
||||
if (0)
|
||||
{
|
||||
printf("QUAT: %2.5f %2.5f %2.5f %2.5f\n", q[0], q[1], q[2], q[3]);
|
||||
printf("PROY: %3d %3d %3d (%3d)\n", projection.x, projection.y, projection.z, projection.w );
|
||||
printf("PROY: %3d %3d %3d (%3d)\n", projection.x, projection.y,
|
||||
projection.z, projection.w);
|
||||
printf("\n");
|
||||
}
|
||||
return q;
|
||||
|
||||
44
src/klein.c
Normal file
44
src/klein.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "main.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
/*
|
||||
KLEIN Format:
|
||||
5 bytes with KLEIN
|
||||
1 byte empty for scaling
|
||||
1 byte with the dimention of the surface
|
||||
8 bytes interprated as a long with the number of vertex
|
||||
n bytes with the vertex data of the surface
|
||||
n bytes with the normal data of the surface
|
||||
|
||||
where n is the size of the vertex and normal data that could be
|
||||
calculated as the dimention of the surface time the number of vertes
|
||||
time the size of a 16 bytes float.
|
||||
*/
|
||||
|
||||
int create_surface_klein(unsigned char *path, struct surface *surface)
|
||||
{
|
||||
unsigned long size;
|
||||
char buffer[5];
|
||||
FILE *file = fopen(path, "rb");
|
||||
if (!file)
|
||||
return 1;
|
||||
|
||||
fread(buffer, 1, 5, file);
|
||||
|
||||
if (strncmp(buffer, "KLEIN", 5))
|
||||
return 1;
|
||||
|
||||
fread(buffer, 1, 1, file);
|
||||
fread(&surface->dim, 1, 1, file);
|
||||
fread(&surface->vertex, 8, 1, file);
|
||||
|
||||
size = surface->dim * surface->vertex;
|
||||
|
||||
surface->data = malloc(16 * size);
|
||||
fread(surface->data, 16, size, file);
|
||||
|
||||
surface->norm = malloc(16 * size);
|
||||
fread(surface->norm, 16, size, file);
|
||||
return 0;
|
||||
}
|
||||
41
src/main.c
41
src/main.c
@@ -15,10 +15,7 @@
|
||||
#define M_PI 3.14159
|
||||
#endif
|
||||
|
||||
float *generate_data_surface(unsigned char *, unsigned long *);
|
||||
float *generate_normals_surface(float *, unsigned char, unsigned long);
|
||||
|
||||
struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3};
|
||||
struct projection projection = {.x = 0, .y = 1, .z = 2, .w = 3, .mesh = NULL};
|
||||
|
||||
const char *wname = "manigraph: manifold grapher";
|
||||
|
||||
@@ -32,7 +29,6 @@ void mlog(char *msg)
|
||||
}
|
||||
|
||||
window_t window;
|
||||
mesh_t m_surface;
|
||||
id_t shader, shader_plain;
|
||||
|
||||
extern volatile unsigned char animate_index;
|
||||
@@ -78,8 +74,11 @@ static inline
|
||||
}
|
||||
clean_context();
|
||||
|
||||
draw_mesh(shader, m_surface);
|
||||
draw_mesh_lines(shader_plain, m_surface);
|
||||
if (!projection.mesh)
|
||||
return;
|
||||
|
||||
draw_mesh(shader, projection.mesh);
|
||||
draw_mesh_lines(shader_plain, projection.mesh);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -130,29 +129,6 @@ int main(void)
|
||||
fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT);
|
||||
}
|
||||
|
||||
mlog("[MESH] Inicializando...\n");
|
||||
{
|
||||
struct surface surface;
|
||||
|
||||
surface.data = generate_data_surface(&surface.dim, &surface.vertex);
|
||||
surface.norm =
|
||||
generate_normals_surface(surface.data, surface.dim, surface.vertex);
|
||||
|
||||
if (!(m_surface = create_mesh(surface)))
|
||||
{
|
||||
mlog("[MESH] Error al inicializar...\n");
|
||||
goto error_mesh_surface;
|
||||
}
|
||||
|
||||
projection.m = surface.dim;
|
||||
projection.mesh = m_surface;
|
||||
|
||||
set_projection_mesh(projection);
|
||||
|
||||
free(surface.norm);
|
||||
free(surface.data);
|
||||
}
|
||||
|
||||
mlog("[MAIN LOOP] Inicializando...\n");
|
||||
#ifdef EMSCRIPTEN
|
||||
emscripten_set_main_loop(&main_loop, 60, 1);
|
||||
@@ -164,7 +140,7 @@ int main(void)
|
||||
mlog("[MAIN LOOP] Terminando...\n");
|
||||
|
||||
mlog("[MESH] Destruyendo...\n");
|
||||
destroy_mesh(m_surface);
|
||||
destroy_mesh(projection.mesh);
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
destroy_shader(shader_plain);
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
@@ -174,9 +150,6 @@ int main(void)
|
||||
return 0;
|
||||
|
||||
error_context:
|
||||
mlog("[MESH] Destruyendo...\n");
|
||||
destroy_mesh(m_surface);
|
||||
error_mesh_surface:
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
destroy_shader(shader_plain);
|
||||
error_shader_plain:
|
||||
|
||||
@@ -36,6 +36,7 @@ struct projection
|
||||
vertex: the number of vertex
|
||||
dim: the dimentions of the surface
|
||||
*/
|
||||
|
||||
struct surface
|
||||
{
|
||||
float *data, *norm;
|
||||
@@ -50,6 +51,8 @@ struct surface
|
||||
name: Name of the window.
|
||||
*/
|
||||
|
||||
int create_surface_klein( unsigned char *, struct surface * );
|
||||
|
||||
window_t init_window(unsigned int w, unsigned int h, const char *name);
|
||||
|
||||
void use_window(window_t window);
|
||||
@@ -176,9 +179,3 @@ 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
|
||||
|
||||
400
src/surface.c
400
src/surface.c
@@ -1,400 +0,0 @@
|
||||
#include <complex.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define TEST
|
||||
|
||||
#define CGLM_ALL_UNALIGNED
|
||||
#include <cglm/vec3.h>
|
||||
#include <cglm/vec4.h>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
#ifndef CMPLX
|
||||
#define CMPLX(a, b) (a + I * b)
|
||||
#endif
|
||||
|
||||
#ifdef TEST
|
||||
#include <assert.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
|
||||
typedef void (*function_t)(float *, int *, unsigned char *);
|
||||
|
||||
struct parm
|
||||
{
|
||||
unsigned char *grid;
|
||||
unsigned char m, n;
|
||||
function_t f;
|
||||
} parm;
|
||||
|
||||
// Función para escribir el archivo .klein
|
||||
void write_klein_file(const char *filename, unsigned char dim, unsigned long vertex, float *vertices, float *normals)
|
||||
{
|
||||
FILE *file = fopen(filename, "wb");
|
||||
if (!file)
|
||||
{
|
||||
perror("Error al abrir el archivo");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Escribir encabezado
|
||||
fwrite("KLEIN", 1, 5, file); // Los primeros 5 bytes son "KLEIN"
|
||||
fputc(0, file); // Byte vacío
|
||||
fwrite(&dim, 1, 1, file); // Dimensión de la superficie
|
||||
fwrite(&vertex, sizeof(unsigned long), 1, file); // Número de vértices (8 bytes)
|
||||
|
||||
// Escribir dimensiones de la cuadrícula
|
||||
fwrite(parm.grid, sizeof(unsigned char), dim, file);
|
||||
// Debug info
|
||||
for (int i = 0; i < dim; i++) {
|
||||
printf("Grid[%d]: %u\n", i, parm.grid[i]);
|
||||
}
|
||||
|
||||
// Escribir vértices (en float)
|
||||
fwrite(vertices, sizeof(float), vertex * dim, file);
|
||||
// Debug info
|
||||
for (unsigned long i = 0; i < vertex * dim; i++) {
|
||||
printf("Vertices[%lu]: %f\n", i, vertices[i]);
|
||||
}
|
||||
|
||||
// Escribir normales (en float)
|
||||
fwrite(normals, sizeof(float), vertex * dim, file);
|
||||
// Debug info
|
||||
for (unsigned long i = 0; i < vertex * dim; i++) {
|
||||
printf("Normals[%lu]: %f\n", i, normals[i]);
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
printf("Archivo %s escrito correctamente.\n", filename);
|
||||
}
|
||||
|
||||
int factorial(int n)
|
||||
{
|
||||
if (n == 1)
|
||||
return 1;
|
||||
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
|
||||
int faces(int n)
|
||||
{
|
||||
if (n == 2)
|
||||
return 1;
|
||||
|
||||
return (1 << (n - 3)) * factorial(n) / factorial(n - 2);
|
||||
}
|
||||
|
||||
void riemman(float *d_surface, int *coords, unsigned char *grid)
|
||||
{
|
||||
complex double eq;
|
||||
float u = 2 * ((float)coords[0] / grid[0]) - 1;
|
||||
float v = 2 * ((float)coords[1] / grid[1]) - 1;
|
||||
|
||||
eq = csqrt(CMPLX(u, v));
|
||||
|
||||
d_surface[0] = u;
|
||||
d_surface[1] = v;
|
||||
d_surface[2] = creal(eq);
|
||||
d_surface[3] = cimag(eq);
|
||||
}
|
||||
|
||||
void cube(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < parm.m; i++)
|
||||
d_surface[i] = (2 * (float)coord[i] / grid[i]) - 1;
|
||||
|
||||
if (parm.m == 2)
|
||||
d_surface[2] = 0;
|
||||
}
|
||||
|
||||
void mobius(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
const float width = 0.5;
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
|
||||
float v = (2 * width) * ((float)coord[1] / grid[1]) - width;
|
||||
|
||||
d_surface[0] = cos(u) + v * cos(u / 2) * cos(u);
|
||||
d_surface[1] = sin(u) + v * cos(u / 2) * sin(u);
|
||||
d_surface[2] = v * sin(u / 2);
|
||||
}
|
||||
|
||||
void torus(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
|
||||
float v = (2 * M_PI) * ((float)coord[1] / grid[1]);
|
||||
|
||||
d_surface[0] = (1 + 0.5 * cos(v)) * cos(u);
|
||||
d_surface[1] = (1 + 0.5 * cos(v)) * sin(u);
|
||||
d_surface[2] = 0.5 * sin(v);
|
||||
}
|
||||
|
||||
void klein(float *d_surface, int *coord, unsigned char *grid)
|
||||
{
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid[0]);
|
||||
float v = (2 * M_PI) * ((float)coord[1] / grid[1]);
|
||||
|
||||
d_surface[0] = (0.5 * cos(v) + 0.5) * cos(u);
|
||||
d_surface[1] = (0.5 * cos(v) + 0.5) * sin(u);
|
||||
d_surface[2] = sin(v) * cos(u / 2);
|
||||
d_surface[3] = sin(v) * sin(u / 2);
|
||||
}
|
||||
|
||||
float *generate_data_surface(unsigned char *dim, unsigned long *vertex)
|
||||
{
|
||||
unsigned int i, j, k, o, p, n;
|
||||
unsigned long size, q = 0;
|
||||
float *d_surface;
|
||||
int *cara;
|
||||
|
||||
parm.f = cube;
|
||||
parm.m = 4;
|
||||
parm.n = 4;
|
||||
parm.grid = (char[]){16, 8, 4, 2, 1};
|
||||
|
||||
#ifdef TEST
|
||||
assert(faces(2) == 1);
|
||||
assert(faces(3) == 6);
|
||||
assert(faces(4) == 24);
|
||||
#endif
|
||||
|
||||
*dim = parm.n;
|
||||
*vertex = 0;
|
||||
|
||||
{
|
||||
unsigned char test = 0;
|
||||
for (o = 0; o < parm.m; o++)
|
||||
{
|
||||
for (p = 0; p < o; p++)
|
||||
{
|
||||
test += 1;
|
||||
*vertex += parm.grid[p] * parm.grid[o] * 6 * faces(parm.n);
|
||||
}
|
||||
}
|
||||
*vertex /= test;
|
||||
}
|
||||
|
||||
cara = malloc(parm.m * sizeof(int));
|
||||
size = (*dim) * (*vertex);
|
||||
d_surface = malloc(size * sizeof(float));
|
||||
|
||||
for (o = 0; o < parm.m; o++)
|
||||
{
|
||||
for (p = 0; p < o; p++)
|
||||
{
|
||||
for (k = 0; k < (1 << (parm.m - 2)); k++)
|
||||
{
|
||||
unsigned char skip = 0;
|
||||
for (n = 0; n < parm.m; n++)
|
||||
{
|
||||
if (n == o || n == p)
|
||||
skip++;
|
||||
|
||||
cara[n] = (k & (1 << (n - skip))) ? parm.grid[n] : 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < parm.grid[p]; i++)
|
||||
{
|
||||
for (j = 0; j < parm.grid[o]; j++)
|
||||
{
|
||||
cara[p] = i;
|
||||
cara[o] = j;
|
||||
parm.f(&d_surface[q], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p] = i + 1;
|
||||
cara[o] = j;
|
||||
parm.f(&d_surface[q], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p] = i + 1;
|
||||
cara[o] = j + 1;
|
||||
parm.f(&d_surface[q], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p] = i;
|
||||
cara[o] = j;
|
||||
parm.f(&d_surface[q], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p] = i;
|
||||
cara[o] = j + 1;
|
||||
parm.f(&d_surface[q], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p] = i + 1;
|
||||
cara[o] = j + 1;
|
||||
parm.f(&d_surface[q], cara, parm.grid);
|
||||
q += parm.n;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
assert(q == size);
|
||||
#endif
|
||||
return d_surface;
|
||||
}
|
||||
|
||||
static void __calculate_normal(
|
||||
float *p1, float *p2, float *p3, float *normal, unsigned char n)
|
||||
{
|
||||
unsigned char i;
|
||||
float alpha;
|
||||
float *v1, *v2, *v3;
|
||||
float *u1, *u2, *u3;
|
||||
|
||||
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));
|
||||
|
||||
/*
|
||||
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];
|
||||
}
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
u1[i] = v1[i];
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
proj[i] = u1[i] * alpha;
|
||||
u2[i] = v2[i] - proj[i];
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
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;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
float magnitude = 0.0f;
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
magnitude += u3[i] * u3[i];
|
||||
}
|
||||
magnitude = sqrtf(magnitude);
|
||||
|
||||
for (i = 0; i < n; ++i)
|
||||
{
|
||||
normal[i] = u3[i] / magnitude;
|
||||
}
|
||||
|
||||
free(v1);
|
||||
free(v2);
|
||||
free(v3);
|
||||
free(u1);
|
||||
free(u2);
|
||||
free(u3);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
float *generate_normals_surface(float *d, unsigned char m, unsigned long vertex)
|
||||
{
|
||||
float *n;
|
||||
float *norm_vec;
|
||||
|
||||
n = malloc((m * vertex) * sizeof(float));
|
||||
|
||||
norm_vec = malloc(m * sizeof(float));
|
||||
|
||||
for (int i = 0; i < *d; i += 3 * m)
|
||||
{
|
||||
|
||||
__calculate_normal(d + i, d + i + m, d + i + 2 * m, norm_vec, m);
|
||||
glm_vec3_copy(norm_vec, n + i);
|
||||
glm_vec3_copy(norm_vec, n + i + m);
|
||||
glm_vec3_copy(norm_vec, n + i + 2 * m);
|
||||
}
|
||||
|
||||
free(norm_vec);
|
||||
return n;
|
||||
}
|
||||
int main()
|
||||
{
|
||||
unsigned char dim;
|
||||
unsigned long vertex;
|
||||
float *vertices, *normals;
|
||||
|
||||
// Generar datos de la superficie
|
||||
vertices = generate_data_surface(&dim, &vertex);
|
||||
|
||||
// Verificar datos generados
|
||||
if (vertices == NULL) {
|
||||
printf("Error: vertices no generados.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Dim: %u, Vertex: %lu\n", dim, vertex);
|
||||
|
||||
// Generar normales
|
||||
normals = generate_normals_surface(vertices, dim, vertex);
|
||||
|
||||
// Verificar normales generadas
|
||||
if (normals == NULL) {
|
||||
printf("Error: normales no generadas.\n");
|
||||
free(vertices);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Escribir el archivo
|
||||
printf("Escribiendo archivo .klein\n");
|
||||
write_klein_file("kingtin.klein", dim, vertex, vertices, normals);
|
||||
|
||||
free(vertices);
|
||||
free(normals);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ void __window_callback_input(GLFWwindow *, int, int);
|
||||
void __mouse_callback_input(GLFWwindow *, int, int, int);
|
||||
void __scroll_callback_input(GLFWwindow *, double, double);
|
||||
void __key_callback_input(GLFWwindow *, int, int, int, int);
|
||||
void __drop_callback_input(GLFWwindow *, int, const char **);
|
||||
|
||||
window_t init_window(unsigned int w, unsigned int h, const char *name);
|
||||
|
||||
@@ -64,6 +65,7 @@ window_t init_window(unsigned int width, unsigned int height, const char *title)
|
||||
glfwSetMouseButtonCallback((GLFWwindow *)window, __mouse_callback_input);
|
||||
glfwSetScrollCallback((GLFWwindow *)window, __scroll_callback_input);
|
||||
glfwSetKeyCallback((GLFWwindow *)window, __key_callback_input);
|
||||
glfwSetDropCallback((GLFWwindow *)window, __drop_callback_input);
|
||||
|
||||
__window_callback_input((GLFWwindow *)window, width, height);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user