Merge branch 'main' into experimental
This commit is contained in:
29 files changed
+828
-941
No files matched your search
@@ -2,20 +2,23 @@ BIN = manigraph
|
||||
|
||||
OBJ = \
|
||||
ext/glad/glad.o \
|
||||
src/surface.o \
|
||||
src/context.o \
|
||||
src/texture.o \
|
||||
src/window.o \
|
||||
src/matrix.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 = \
|
||||
@@ -30,17 +33,16 @@ WAYLAND-LIB = \
|
||||
wayland
|
||||
|
||||
help:
|
||||
@echo "Para compilar el proyecto a tu sistema operativo"
|
||||
@echo "porfavor usa uno de los siguientes comandos:"
|
||||
@echo "Usage:"
|
||||
@echo " $(MAKE) windows"
|
||||
@echo " $(MAKE) linux-x11"
|
||||
@echo " $(MAKE) linux-wayland"
|
||||
@echo " $(MAKE) cocoa"
|
||||
@echo " $(MAKE) CC=emcc wasm"
|
||||
@echo "Para limpiar los archivos compilados se puede usar"
|
||||
@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 -
|
||||
@@ -63,7 +65,7 @@ cocoa: $(OBJ)
|
||||
$(CC) -framework OpenGL -o $(BIN) $(OBJ) ext/glfw.a -lGL -lglfw
|
||||
|
||||
wasm: $(OBJ)
|
||||
$(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).js $(OBJ)
|
||||
$(CC) -sUSE_WEBGL2=1 -sUSE_GLFW=3 -o $(BIN).html $(OBJ)
|
||||
chmod -x $(BIN).wasm
|
||||
|
||||
libglfw.so:
|
||||
@@ -74,7 +76,9 @@ clean:
|
||||
cd ext; $(MAKE) -f glfw.mk clean; cd -
|
||||
|
||||
|
||||
examples: $(EXAMPLES)
|
||||
|
||||
.SUFFIXES: .c .o
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
$(CC) -Wno-implicit-function-declaration $(CFLAGS) -c -o $@ $<
|
||||
@@ -1,92 +1,58 @@
|
||||
# Manigraph: Graficadora de variedades
|
||||
Diaz Camacho Pedro Emilio
|
||||
# Manigraph
|
||||
Manigraph is a cutting-edge tool for multidimentional surface visualization,
|
||||
capable of handling over a hundred dimensions. It allows users to interactively
|
||||
explore each dimension in an intuitive and dynamic way.
|
||||
Import your .klein files, which represents multidimentional surface.
|
||||
|
||||
# Resumen
|
||||
manigraph es un graficador interactiva de variedades que lee archivos binarios, con la información de
|
||||
una variedad multidimensional y grafica esta variedad en una proyección tridimensional.
|
||||
# Building
|
||||
Manigraph is written in C99, and uses OpenGL 2.0 for the rendering. So you just
|
||||
need a C compiler to build this project.
|
||||
|
||||
# Dependencias
|
||||
Para poder compilar el proyecto hace falta lo siguiente.
|
||||
## Programas
|
||||
- `cc`: Cualquier compilador de C.
|
||||
- `git`: Gestor de dependencias. (Opcional)
|
||||
- `make`: Herramienta para compilar automáticamente. (Opcional)
|
||||
|
||||
## Librerías
|
||||
- `openGL`: A cross-platform API for rendering 2D and 3D graphics. (no incluida)
|
||||
- `glew`: The OpenGL Extension Wrangler Library (no incluida)
|
||||
- `glfw`: A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input.
|
||||
- `cglm`: Highly Optimized 2D / 3D Graphics Math (glm) for C.
|
||||
### Linux
|
||||
Para compilar `glfw` en linux hacen falta las siguientes librerías. (no incluidas)
|
||||
- `x11`: libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel
|
||||
- `wayland`: libwayland-dev libxkbcommon-dev wayland-protocols
|
||||
|
||||
# Descargas
|
||||
- [git](https://git-scm.com/downloads/win)
|
||||
- [MinGW](https://github.com/niXman/mingw-builds-binaries/releases)
|
||||
- [glew](https://glew.sourceforge.net/)
|
||||
|
||||
# Clonar el código fuente
|
||||
## Source code
|
||||
```
|
||||
git clone https://gitea.adles.top/software/manigraph.git
|
||||
git clone https://gitea.axiolutions.top/software/manigraph.git
|
||||
cd manigraph
|
||||
git submodule update --init --recursive
|
||||
```
|
||||
|
||||
# Compilación
|
||||
Para compilar manualmente el proyecto se puede usar `make` o ejecutar los comandos manualmente
|
||||
en el código fuente de `manigraph`.
|
||||
## Dependecies
|
||||
You may need system specific dependecies to build *Manigraph*.
|
||||
[Reference](https://www.glfw.org/docs/3.3/compile.html#compile_deps)
|
||||
|
||||
## Make
|
||||
Los siguientes comandos sirven para compilar `manigraph` deacuerdo al sistema operativo.
|
||||
### Linux
|
||||
- `x11`: libXcursor-devel libXi-devel libXinerama-devel libXrandr-devel
|
||||
- `wayland`: libwayland-dev libxkbcommon-dev wayland-protocols
|
||||
|
||||
## Commands
|
||||
A C compiler is needed for building *Manigraph*. and any of those commands
|
||||
|
||||
```
|
||||
make windows
|
||||
./compile.bat
|
||||
make linux-x11
|
||||
make linux-wayland
|
||||
make cocoa
|
||||
make CC=emcc wasm
|
||||
```
|
||||
|
||||
## Manualmente
|
||||
se pueden compilar el proyecto manualmente sin necesidad del programa `make`.
|
||||
# Design
|
||||
Manigraph is a program designed with sustainability in mind. It is built to be
|
||||
portable, resource-efficient, and easy to maintain and scale. The program
|
||||
follows a Data-Oriented Programming (DOP) paradigm, where each file is
|
||||
responsible for a specific data type and manages its own dependencies. Each
|
||||
function is named after the file it resides in, ensuring a clear organization
|
||||
and structure.
|
||||
|
||||
### GLFW
|
||||
Si tienes problemas compilando `GLFW` puedes checar su página web oficial
|
||||
explicando el tema: [Compiling GLFW](https://www.glfw.org/docs/3.3/compile.html).
|
||||
The independence of data types allows for efficient scaling and maintenance.
|
||||
The main file is a special case in this design: it is responsible for combining
|
||||
all the data types in a comprehensive manner, actng as the workflow of the program.
|
||||
|
||||
#### Windows
|
||||
```
|
||||
cc -fPIC -shared -D_GLFW_WIN32 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o glfw.dll -lgdi32
|
||||
```
|
||||
# Contributing
|
||||
Before commiting use `clang-format`, for coding style consistency. Your
|
||||
contribution must be whitin the design principles and the concept of Manigraph.
|
||||
|
||||
#### Linux
|
||||
```
|
||||
cc -fPIC -shared -D_GLFW_X11 -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o libglfw.so
|
||||
cc -fPIC -shared -D_GLFW_WAYLAND -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o libglfw.so
|
||||
```
|
||||
The following diagram illustrates the relationships between files, showing how
|
||||
they are connected through dependencies. It also highlights the scenarios where
|
||||
files are not independent of one another,
|
||||
|
||||
#### Mac
|
||||
```
|
||||
cc -fPIC -shared -D_GLFW_COCOA -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o libglfw.so
|
||||
```
|
||||
|
||||
### Manigraph
|
||||
#### Windows
|
||||
```
|
||||
cc -I ext/cglm/include/ -I ext/glfw/include/ src/*.c -o manigraph -L. -lglfw -lopengl32 -lglew32
|
||||
```
|
||||
#### Linux
|
||||
```
|
||||
cc -I ext/cglm/include/ -I ext/glfw/include/ src/*.c -o manigraph -L. -lglfw -lGL -lGLEW
|
||||
```
|
||||
#### Mac
|
||||
```
|
||||
cc -framework GL -I ext/cglm/include/ -I ext/glfw/include/ src/*.c -o manigraph -L. -lglfw -lGLEW
|
||||
```
|
||||
|
||||
# Ejecutar
|
||||
En linux se puede ejecutar el programa sin instalar la libreria de glfw usando:
|
||||
```
|
||||
LD_LIBRARY_PATH=. ./manigraph
|
||||
```
|
||||

|
||||
@@ -1,80 +0,0 @@
|
||||
# Estructura de archivos
|
||||
Díaz Camacho Pedro Emilio
|
||||
|
||||
# Headers
|
||||
## main.h
|
||||
Este archivo incluye los prototipos internos, que va a usar el programa
|
||||
funge como api interna para los desarrolladores y debe estar bien documentada.
|
||||
|
||||
# codigo fuente
|
||||
|
||||
## main.c
|
||||
Este archivo debe configurar el programa antes de iniciarse, avisar de los problemas
|
||||
que eviten que el programa se ejecute correctamente, iniciar y terminar los objetos que use el
|
||||
programa, así como ejecutar la función principal
|
||||
### Depende de
|
||||
- <GL/gl.h>
|
||||
|
||||
## context.c
|
||||
En este archivo se escoge un color para limpiar la pantalla y tiene una función para limpiar
|
||||
tanto lo dibujado en pantalla, como distintos buffers gráficos.
|
||||
### Depende de
|
||||
- <GL/gl.h>
|
||||
|
||||
## input.c
|
||||
Incluye la función `poll_input`, que se ejecuta en cada ciclo del programa y devuelve un
|
||||
cuaternion de tipo `float *` que representa la rotación total del cubo, después de interactuar
|
||||
con el programa.
|
||||
### Depende de
|
||||
- <GLFW/glfw3.h>
|
||||
- <cglm/quat.h>
|
||||
|
||||
## matrix.c
|
||||
Incluye las funciónes para configurar y cargar en la GPU las 3 matrices principales.
|
||||
- `fix_matrix` esta matriz no cambia y se usa para configurar la perspectiva y la vista que
|
||||
va a usar el programa en todo momento.
|
||||
- `mdl_matrix` esta matriz depende de cada mesh y describe la rotación única de cada objeto,
|
||||
se usa para rotar los ejes ortogonamente con un indice.
|
||||
- `rot_matrix` esta matriz describe la rotación total de todos los objetos y se configura con
|
||||
un cuaternio
|
||||
### Depende de
|
||||
- <cglm/mat4.h>
|
||||
- <cglm/cam.h>
|
||||
- <cglm/quat.h>
|
||||
|
||||
## mesh.c
|
||||
Este archivo se usa para crear, destruir y dibujar objetos de tipo `mesh`, un `mesh` es una
|
||||
colección de triángulos que describen un objeto multidimensional.
|
||||
### Depende de
|
||||
- <GL/gl.h>
|
||||
- <stdlib.h>
|
||||
|
||||
## shader.c
|
||||
Debe crear, destruir y usar los shaders, estos shaders son programas para la gpu escritos en glsl para
|
||||
además aquí se carga memoria en gpu con las funciónes `gload_`
|
||||
### Depende de
|
||||
- <GL/gl.h>
|
||||
|
||||
## texture.c
|
||||
Debe cargar, usar y destruir las texturas, que va a usar la gpu, también crea una textura
|
||||
para una paleta de colores definida, esto para manipular fácilmente los colores que usará el programa.
|
||||
### Depende de
|
||||
- <GL/gl.h>
|
||||
|
||||
## window.c
|
||||
Crea, usa, limpie y cierra una ventana, una ventana tiene un contexto de OpenGL asociado para poder
|
||||
dibujar objetos en ella, tiene una función especial para avisar si la ventana está abierta para
|
||||
dibujar.
|
||||
### Depende de
|
||||
- <GLFW/glfw3.h>
|
||||
|
||||
# Datos
|
||||
## data/cube.h
|
||||
Aquí esta la información del mesh de un cubo, este se va a borrar cuando podamos
|
||||
importar meshes para graficarlos.
|
||||
|
||||
## data/axis.h
|
||||
Aquí está el mesh de un solo eje, este se va a rotar para hacer los otros 2 ejes.
|
||||
|
||||
## data/shaders.h
|
||||
Aquí estan los shaders en glsl para dibujar en pantalla
|
||||
@@ -1,43 +0,0 @@
|
||||
# Tipos de datos del proyecto
|
||||
Díaz Camacho Pedro Emilio
|
||||
|
||||
# Introducción
|
||||
En este archivo vamos a detallar los tipos de datos únicos del programa, estos
|
||||
tipos de datos son estructuras que representan objetos computacionales y son
|
||||
necesarios para integrar las distintas partes del programa.
|
||||
|
||||
## window_t
|
||||
En `window.c` tenemos una estructura que es la ventana y esta se representa por `window_t`,
|
||||
este tipo de dato es un pointer que es manejado por `glfw`.
|
||||
|
||||
## id_t
|
||||
`id_t` es un tipo de dato que representa objetos de `OpenGL`, estos datos
|
||||
normalmente vienen enumerados, y en `manigraph` se usan para representar `shaders`,
|
||||
`programs` y `texture`.
|
||||
|
||||
## mesh_t
|
||||
Este es un tipo de dato usado para interactuar con `mesh.c`, este representa la triangulación de un
|
||||
objeto gráfico y funciona ocultando las variables que `OpenGL` necesita en un `void *`.
|
||||
|
||||
## mat4_t
|
||||
`mat4_t` se usará para representar las matrices, se usará este tipo de dato exclusivamente
|
||||
en `matrix.c` y funcionará con el tipo de dato `mat4` de `cglm`.
|
||||
|
||||
## quat_t
|
||||
Este tipo de dato sirve para representar un quaternio, este quaternio representa la rotación
|
||||
del objeto gráficado en la proyección tridimensional, y se usará para crear una matriz que
|
||||
pueda entender `glsl`.
|
||||
|
||||
## narray
|
||||
`narray` es un tipo de dato que funciona como un `array` enumerado, esto significa que el primer
|
||||
elemento del `array` dice cuantos elementos tiene este, sin contar el primer elemento, y los demas elementos
|
||||
funcionan como un `array` normal.
|
||||
|
||||
esta estructura nos ayuda a trabajar con arrays de tamaños arbitrario de forma óptima y sin tener que marcar el
|
||||
último elemento de forma especial.
|
||||
|
||||
### narray_u8_t
|
||||
es un `narray` para `char` y `unsigned char`.
|
||||
### narray_float_t
|
||||
es un `narray` para `float`.
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
# Manual de Diseño
|
||||
Diaz Camacho Pedro Emilio
|
||||
|
||||
# Objetivos
|
||||
## Prioridad alta
|
||||
- [ ] El programa debe actualizarse cuando interactuan con el.
|
||||
- [ ] El usuario quiere:
|
||||
- [X] Ver 3 ejes en todo momento.
|
||||
- [ ] Ver que ejes esta viendo.
|
||||
- [ ] Ver cuantos ejes hay.
|
||||
- [X] El usuario quiere:
|
||||
- [X] Rotar estos ejes,
|
||||
- [X] Ver donde quedan los ejes despues de la rotados.
|
||||
- [ ] El usuario experimentado quiere ejecutar el programa y pasar la variedad:
|
||||
- [ ] Como argumento del programa.
|
||||
- [ ] Desde la entrada estandar del programa.
|
||||
|
||||
## Prioridad media
|
||||
|
||||
- [ ] El usuario quiere:
|
||||
- [ ] Cambiar los ejes que se ven por pares.
|
||||
- [ ] Cambiar los ejes donde esta la informacion de los ejes.
|
||||
- [ ] El usuario quiere interactuar con el programa:
|
||||
- [ ] Usando solo el mouse.
|
||||
- [ ] Usando solo el teclado.
|
||||
|
||||
## Prioridad baja
|
||||
- [ ] El usuario quiere guardar las transformaciones como un archivo GIF.
|
||||
|
||||
# Camino
|
||||
|
||||
0. Graficadora de un cubo.
|
||||
1. Se hace un programa que muestre un cubo.
|
||||
1. Se muestran los 3 ejes del cubo
|
||||
1. Se rota el cubo en sus 3 ejes usando el teclado.
|
||||
1. Se rota el cubo en sus 3 ejes usando el mouse.
|
||||
1. Se muestran la etiqueta de los ejes.
|
||||
1. Se hace el formato de objetos multidimensionales.
|
||||
0. Programa ejemplo
|
||||
1. Se hace un programa que divida un cuadrado en triangulos.
|
||||
1. Se mapea cada vertice del triangulo en un punto de la cinta de
|
||||
mobius usando una parametrizacion
|
||||
1. Se mapea cada vertice del triangulo en un punto de la botella de
|
||||
klein usando una parametrizacion
|
||||
1. Se divide cada lado de un cubo de n dimensiones, en triangulos.
|
||||
|
||||
0. Graficadora de variedades
|
||||
1. Se generaliza el programa para usar este formato.
|
||||
1. Se muestran la informacion de los ejes mostrados y cuantos hay.
|
||||
1. Se pueden cambiar los ejes por pares
|
||||
1. Usando el mouse.
|
||||
1. Usando el teclado.
|
||||
0. Detalles.
|
||||
1. Se anima el cambio de eje.
|
||||
1. Se hace el generador de GIF.
|
||||
1. Se puede cambiar el shader
|
||||
1. Documentacion
|
||||
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
# Problemas esperados al desarrollar
|
||||
Diaz Camacho Pedro Emilio
|
||||
|
||||
## OpenGL esperan objetos tridimensionales, no de n dimensiones.
|
||||
|
||||
Usando glVertexAttribPointer podemos pasar las coordenadas que
|
||||
queramos de los puntos de las variedades, y con ello usar objetos 3D en
|
||||
el shader.
|
||||
|
||||
## Rotar ejes por pares en una animacion continua.
|
||||
- Con glVertexAttribPointer podemos pasar las coordenadas del eje
|
||||
seleccionado, al shader.
|
||||
- Con la funcion mix del shader y bloqueando el input, podemos hacer
|
||||
una animacion pasando una variable con la cpu.
|
||||
- Cuando la animacion termine intercambiar los indices del layout,
|
||||
usando glVertexAttribPointer otra vez.
|
||||
|
||||
## Volumen de la variedad.
|
||||
Dado que la funcion parametrizadora es continua, mandamos la frontera
|
||||
de un n-cubo a la frontera de una variedad, por lo que solo basta
|
||||
graficar la frontera de la variedad.
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
# Manual de usario para Manigraph
|
||||
Manigraph es un graficador interactivo de variedades que lee archivos
|
||||
binarios de una variedad multidimensional y los proyecta a 3 dimensiones.
|
||||
|
||||
# Glosario
|
||||
Llamaremos a los 3 ejes de la proyección tridimensional `X`, `Y` y `Z`,
|
||||
este sistema de coordenadas es derecho con el eje `Z` apuntando afuera de la pantalla
|
||||
|
||||
# Controles
|
||||
## Teclado
|
||||
- `Q` Rota el eje `X` de forma horaria.
|
||||
- `W` Rota el eje `X` de forma antihoraria.
|
||||
- `A` Rota el eje `Y` de forma horaria.
|
||||
- `S` Rota el eje `Y` de forma antihoraria.
|
||||
- `Z` Rota el eje `Z` de forma horaria.
|
||||
- `X` Rota el eje `Z` de forma antihoraria.
|
||||
## Mouse
|
||||
- Hacer `click` en un eje lo selecciona.
|
||||
- El `scroll` del mouse hace girar al eje seleccionado.
|
||||
@@ -0,0 +1,10 @@
|
||||
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024.1997862712624 780.4323630001875" width="2048.3995725425248" height="1560.864726000375" filter="invert(93%) hue-rotate(180deg)">
|
||||
<!-- svg-source:excalidraw -->
|
||||
|
||||
<defs>
|
||||
<style class="style-fonts">
|
||||
@font-face { font-family: Excalifont; src: url(data:font/woff2;base64,d09GMgABAAAAABpsAA4AAAAALpAAABoXAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhwbiygcgSwGYACBBBEICsYgs3ELUgABNgIkA4EgBCAFgxgHIBseJCMDdYvSKkb2lwfcofLFNUBDpYNih53Ilwl1K/MjFI3wbLCLBb91s1eJhEeHi0FWw5OG5+fW+7H/V/U36DWMGKNHL5oag5EijExBMPJIE7HrSvsiMbkosNEL9MKzC/5hLvb2N0u3hcNQ7FdERDwwGaqhRj1c26gvDVU5CaURSWmS0eGBawIJxHE8njb9+BjF8F4C08Ak4+3+T6cltZr2/xlBwpIMC+hNFoANB5Xv3pVbVIdpk1YyYECyOcnbmLIAdJLmf3VW9m6XtLTEuT2mCjGTa+/1V/RyK7X/69tW4iQTQ2g4MMCLJEv/u9+v/mE+ksaPRAvNLBISpezh9wwxr6JpiFezEDlUERl4FE0iEhKxUKmVXCit4udwfhX2/VGS3MQIvVCxze+LFQIQAOgAADYIyoxCBIDce0OlpoVul7Qc4HXrbmsAXo+2ynrgdbhnNAEvKACAeGR63duaALkWBNLh9RlshcgZZPC8yPyc0H1RAUEITCmQ2PJgeZs5GMEi1TxjcWPAZYEIDnExDgnU3K9eKJUm9q13cF8I7RQHhlOUt76oEDCVtZXZwZMPSIroyA5sivxThdpM0f39wZ4Ctnfup1t5aii4pXT88FXmu4RaXIm9BL39nhmUyWmPgXh8fdglFFR0DEwsbBxcfAQBT978+BORkYsKBDCBT960VBLWpULQAA0C3KSTELAVcCxJEJ0fR8bAxcbEwwIAAUIilZsMCAB6Q4GmZA20QG/Qr0gAAJ+yF5TTR1dQ3oGiTLFKg61H1Qo08aCXdlVD6m0SAMZDpCIhsCUJpoEwBsEwFEMYGMKhSHvhJITBFEKM8sMkYoyTe5FrcjFA9wGgGC6o/uC/RqIdGSHNXZSHOZ7VbsB/t1U0HQyAUgQyA4DQELSHgEDCZiwHqrz5lBulgPDrky/mRWTEjIQIOL5gtTwAYGg5YyUScoPlAyCVugLtIrwC65AJjVdLBhgBKl2z88eRnjONzjOwrRT91uMr+thJRwDATymkMaol3QMb4p0sjI5FJieXPLXuvB1gu6YEqJQYGEAqMOgG9aANzPw6nReee2LXti2rVixbCgLwhg5yJACBCXKIokzQDgDAGAZAkuwLTqJgWeULkzesGJe7Ztgi9Jhx5S7CbvBrT3e6EiqKfZXNOa2RfIUwOpGp5GfnGrx80uRpBk+/IFa0tig1s6hY4E9YXLsVe/23CT9MGPVcs2ENsUGz37U6aK33wauGwkDeuk2bhN4n+8OAfaEbzvPXfnQ+8Pz58znrD30kazQ6OJW7K3KNeqqUKrGnxxKnBXj0COATcDHa07hlGsQ3NrY0r0pHoapoyv/Acg66y56ARLi2bSNaAPN2QF/Acj4H8JsY1tXG6qx61V0Y4MHYA7DTPDixHUwYtrwT+yjosFiRiixTUEE8Eud8aUYrklaimZDKVg14z2lYHdCYI6LgoYyI14j/QX/7YOLd4Q6qD0ktwVotIxVO5XohEN2IT8g2q1SM/p5RRGA5wfyPeS1PQq6RyFmj4wW3pex+803DMj1lJJ6JgidfGyogBr6cY+4LZAalekoB4qhUGw3imisrYjRJ9TqbMjGsf2T0dlik2EdlxKLRAYfKJ2AAvFGppEMZKWacFziT9XFLDnQYSHclxB7Mjw6EOV/KlljGiUXMA8DE0+HVKy/0aH1IpSjFk29HV+d87hFjfUgEiscy5gAmZVID0JMASg4bF/gtEsYds7YwkjKAB7Gn8xEUbpR0zOnqcA8yBRpNakm39dzwBr9pmUjt+6SrrJ67fb4R95yR88j6BBq7/vkpn7MmLIPAPyxoUQiHrotIkCMN8UFKA2x24D/IYP00TH4tRwBTHeibs1mPT/ZSjY0QzWUrMoz5Y02zeUQ8SI3u2seIJdvKYP5TePToGTyv/pEcx+ePy7h9AgFwbJw0D0DvTT99Mbm4x3Kba95ChrbZnAhFRzseQK4MGEuICNkDZl3bRXRtW3B8/X+o0XR9lYaBKrWnRxNK/qYH/YPbSbl+n76vwQCkky5aGUj3h9roLPENq6n0cIjApKmgjeU6Q+JsIyWLbSZcLGMT0cfgPyVHfI7YSHA12kMLjKpimB+tpTy0PUd2k6Cdx5Y/jiuI+dO/JdWG2uJVy7w1rRfKnsvMA2bBJR1terAajk3ZKqz3cguhk1sGP1eWtQW/8yBp2JpHMeMG+sYDhawrnGNxLUHTrXP4bPTw68uITS8HR2YSPwYNIXZG8i5AzsohTgePm5HBPU63zwyD6FMKtbDIKLYXE2+H89DovvmQAo4ijmU7L2hEH1tj/iM6UReTlsIUteusuq6pj8YA7+QEfoQkR/S7DuaukQEh7nngDDDxjTJ4hXBzoLByDiDND5n5fjLimseZnHBTHbErw2apBODlusrSYyMhgtGV70dt488UhxRLUqpXEcOj8wh2AVb+k5m3E9rY1kOARBytSLF6Nkp6E5EoJ1z11KBdJ3C6pQRYRlalPTaiNMWu5oNGwWkKTkBIW9mzMdlLffBBRWhelp0lIgxUGDZ3rsrjuPaC1zmU/TCYkqbRynDmjJeBf/qbeS0nEQdRmSkYrgXW5k+/IHZARAJdG8oJ4kHKu4RlsCA+JDFtgVbCpJYkQo8WtxJe9e54tJ86b/Z3DdK3yZBJNBSfpWyWERHjTU/WOJq7Z4vLU38s2GyENdmMemGmEr8GFoL9n41fH++4vLc4oGa9nGq/PHnAY9l2MxLkZR+4k6ERlQOYfNvJSnkMLid+qIFNgc1PEjwqaRhorjU49ZXqxcZFWitWmeKiCuE2BtP/sLjvyc7p94KujwGAxM395GFWH29T8zERzDLUJLPVvMjPWq5meEelut65WOJXbvhC1784vVrLLmaKs3vwmu5rMpwQtN2lJZI91dSVG3zUCJ+qywO3CWo8kppE2DaozxaiVPvu6CgNWX2K18Y9Yfkc+KZs0fQgd+jMi+tUeeobD0rY0EHiAZ1JXHdOOU1fYZbzx0NKay01Co2IQHzevC5YtsWZ4tpti1/YDiZ6/OgOEacZMIlTeSEddcIAYB1Iz8R51PxrxA6Kya5ypeXJGpqbO8c6pLm5HKgmH39qaBjPg8R3S9ErLGRZm68atRfMXpXZbNa2SQ2LyIH5U5YzIiJ7/nQeZRaG/xVqH2ha0dRDA7MtCXoIPI4l+DeODqlckmx4FUOFlfMryEo5IcDwDg/Rw2ydiIAuiNxRwmMvWp8u5YihDAfx/iFDskU9qQCe3NQn6vIkFYsiVcSTUcDDZGT7mO+HAqGOiY+1E1EprIo9PrnIZxQREWZjHYWO54nAL7cUBEhMVFkQXUVEE0/707cSI3XHSHY5I66thKaRtYRfLqt4cGhaKk8lsyqGEjTAB6UvS9CwIK5RKboFCRog3VgaNWlZoYiWxLdvkSf75teu4sxTpei1USiBwEoCbWwwarfZaI7crq3kos7KQgDgJO7+ttcnGnKB7aR2iPBP500N0HPepATnCVkoZFrUWBxigAZsypArC9jF5GewPlyGNOQFC7Zqtd7/V6fvNuX4AprehUlwosA8QWMg938VeywN35zbjAeHt2LJrxtHDy7eGw0EBn2J12hhEn5tDNCBjoJ5K2cBSPyJuhAlI02FyEW0BZZoUJHdmWG/774lIkRFmgb0hIxSoNHR3y0Ld+lYb10rzF9ZE8cByOGWH8kFi9/W41l6FpX3K5rJqImQxsB79F6xZFllX8wvVt1RbKCgj9ezt/WPPpqO9cU0gh8s3c652C50QwRgfE5lkK2c7sC48ekS9NK8VqOykDlKfhpWXK3MExquFrhIoitnqc2oTwZKyg4tdHahaxOxw/LFtvxsDyHbxcEYg+Z//ngrDAnuLGbocH+4uqqe8QqiV8gsZE/WdjbRcYJxJNTeYFW1xdRFYefzeVxSUKmNXvIIHaCTlHXZYOyDdIMj6SlpBHNZc+koXSWuyUKLHodd2kFE6LCb6Bd5SZV4iNrZOtRDZPMLLG9BX9cAWEcDXRsDYLp0apP915WLUtx4BPAN4g9ajT9v6/aReF+J6JIHMyzjUrw09uITc8jUVSLifBGHiH0XB7I/D7UBLUCvlxJc9S7RjMfgICLJJZgSK1+4AxLdMQplsbtfziTS1WOzcNf7p6LRGS8Lv35vWLOlwebB7/gipHosCYNV7l2p/NwZhLrL/1UMZ9JKSk6B0L1Hv/Eo/qgP3TZ9CPYygqXfFPhGc6xtj4YP0LJeXByQ5wXFd5gHG05qCn9TR5CBZyTj+O0ZNvqYD3WtC1pt7Bds4NPoN7CILyoq9rH3S3mOc9PznnXseLI11GR4yZ8GLYKaWw7xvwR4LUvqh0EQXOa1gQlzh071ySEa9P6/6sh0jo1EzuFt/H4P9hRrEenbLx2rUKtAnuql2HBNXdCyBRjAGiixJ3DsM71rK1FbtEpfqPOpbhdhcCcbhW+ITS+a5gfM/S4T/tarxsBhA+jcMVNNzpLiMQ512p37z0yF6zMcUbomn2GAOLHVCI9pdnV7dmUtSPUANvFD8ue9FeePwIccrc6jRzESp+NVYTXVDFOF5JMWymkISNOEjoAjcb9703a9MIj6Fcz+LJfZaZZqPy+5Yvlr/Erpmes6/wrOUpZspvnu7G3UquQIrtmFk7LC7XnlvUVNPrgx5itIlwwVonqercmuoGR/BbbT+VNodmPLkvACsoGMF7hKIq+TtEYF5RO2ZYz4f/+x4ZMHer22y2PWrlv/CykldnRnuPxwM2oW7jYIjr2txKtbqpQhyHQfI+sQ9dCI4dTP1TETznnopQ5hprfcKf1soRU9727Vwv4OjF3RgZEykBzLine truncated
|
||||
</style>
|
||||
|
||||
</defs>
|
||||
<rect x="0" y="0" width="1024.1997862712624" height="780.4323630001875" fill="#ffffff"></rect><g stroke-linecap="round" transform="translate(851.1997862712624 354.71618150009374) rotate(0 81.5 35.5)"><path d="M17.75 0 C49.51 1.43, 85.67 -0.41, 145.25 0 C159.08 0.81, 160.78 5.85, 163 17.75 C162.63 23.54, 166.27 34.26, 163 53.25 C166.58 66.67, 153.59 67.99, 145.25 71 C107.15 67.76, 73.81 70.82, 17.75 71 C8.55 72.47, -2.15 63.47, 0 53.25 C1.24 39.07, -0.05 22.89, 0 17.75 C2.11 2.57, 3.08 3.01, 17.75 0" stroke="none" stroke-width="0" fill="#ffffff"></path><path d="M17.75 0 C49.22 1.7, 83.5 -0.37, 145.25 0 M17.75 0 C54.18 0.04, 91.42 -0.76, 145.25 0 M145.25 0 C157.12 0.2, 161.01 5.48, 163 17.75 M145.25 0 C156.48 0.96, 164.2 6.91, 163 17.75 M163 17.75 C164 29.04, 163.85 36.77, 163 53.25 M163 17.75 C163.54 26, 162.8 35.19, 163 53.25 M163 53.25 C162.69 65.27, 155.39 71, 145.25 71 M163 53.25 C163.03 66.53, 158.99 70.56, 145.25 71 M145.25 71 C116.76 70.51, 85.82 68.97, 17.75 71 M145.25 71 C101.35 71.52, 56.26 70.4, 17.75 71 M17.75 71 C6.29 70.71, -0.25 64.65, 0 53.25 M17.75 71 C8.13 72.54, 2.15 66.81, 0 53.25 M0 53.25 C-1.01 42.76, -0.36 35.76, 0 17.75 M0 53.25 C0.37 40.1, -0.06 27, 0 17.75 M0 17.75 C0.89 5.79, 5.76 -0.31, 17.75 0 M0 17.75 C-0.77 6.86, 4.23 -1.64, 17.75 0" stroke="#1e1e1e" stroke-width="2" fill="none"></path></g><g transform="translate(896.0997862712625 377.71618150009374) rotate(0 36.60000000000002 12.5)"><text x="36.6" y="17.619999999999997" font-family="Excalifont, Xiaolai, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">example</text></g><g stroke-linecap="round" transform="translate(481.08350232307953 354.71618150009374) rotate(0 81.5 35.5)"><path d="M17.75 0 C56.58 2.76, 97.59 1.04, 145.25 0 C153.95 0.88, 163.61 5.02, 163 17.75 C160.41 28.34, 164.42 32.48, 163 53.25 C159.99 63.69, 155.86 70.32, 145.25 71 C114.79 71.92, 92.44 70.77, 17.75 71 C3.41 72.84, -2.11 63.25, 0 53.25 C1.88 43.01, -1.31 34.17, 0 17.75 C-1.62 6.74, 7.44 -0.85, 17.75 0" stroke="none" stroke-width="0" fill="#ffffff"></path><path d="M17.75 0 C46.23 0.38, 71.83 -0.86, 145.25 0 M17.75 0 C62.89 0.55, 108 0.12, 145.25 0 M145.25 0 C158.02 1.95, 162.01 6.82, 163 17.75 M145.25 0 C159.22 0.93, 161.15 5.65, 163 17.75 M163 17.75 C163.16 28.37, 163.55 38.32, 163 53.25 M163 17.75 C163.32 31.15, 162.83 46.58, 163 53.25 M163 53.25 C161.99 64.94, 155.64 72.94, 145.25 71 M163 53.25 C164.28 66.34, 157.55 70.54, 145.25 71 M145.25 71 C114.62 69.8, 87.29 72.13, 17.75 71 M145.25 71 C101.71 70.18, 59.13 68.95, 17.75 71 M17.75 71 C7.76 72.64, -1.95 65.56, 0 53.25 M17.75 71 C4.1 71.64, -0.58 63.2, 0 53.25 M0 53.25 C2.09 43.15, -0.39 32.18, 0 17.75 M0 53.25 C-0.91 45.47, 0.17 37.32, 0 17.75 M0 17.75 C-0.21 4.5, 4.03 -1.53, 17.75 0 M0 17.75 C-1.49 8.05, 4.66 1.61, 17.75 0" stroke="#1e1e1e" stroke-width="2" fill="none"></path></g><g transform="translate(534.2501689897463 377.71618150009374) rotate(0 28.33333333333337 12.5)"><text x="28.333333333333332" y="17.619999999999997" font-family="Excalifont, Xiaolai, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">main.c</text></g><g mask="url(#mask-rhsh--Nqw0UhZ4cChM8e3)" stroke-linecap="round"><g transform="translate(848.5039775457833 393.07535597041465) rotate(0 -98.92848442705531 -2.8591744703209088)"><path d="M-1.06 0.86 C-33.93 -0.09, -165.18 -3.82, -198.12 -4.97 M0.58 0.26 C-32.38 -1.08, -166.4 -5.84, -199.21 -6.7" stroke="#1e1e1e" stroke-width="2" fill="none"></path></g><g transform="translate(848.5039775457833 393.07535597041465) rotate(0 -98.92848442705531 -2.8591744703209088)"><path d="M-175.46 -14.5 C-179.26 -13.42, -184.73 -12.64, -199.21 -6.7 M-175.46 -14.5 C-181.5 -12.75, -188.18 -9.51, -199.21 -6.7" stroke="#1e1e1e" stroke-width="2" fill="none"></path></g><g transform="translate(848.5039775457833 393.07535597041465) rotate(0 -98.92848442705531 -2.8591744703209088)"><path d="M-176 2.59 C-179.78 -0.12, -185.13 -3.14, -199.21 -6.7 M-176 2.59 C-182.06 -0.32, -188.6 -1.76, -199.21 -6.7" stroke="#1e1e1e" stroke-width="2" fill="none"></path></g></g><mask id="mask-rhsh--Nqw0UhZ4cChM8e3"><rect x="0" y="0" fill="#fff" width="1146.360946399894" height="498.79370491105647"></rect><rect x="698.242159785395" y="377.7161815000936" fill="#000" width="102.66666666666667" height="25" opacity="1"></rect></mask><g transform="translate(698.2421597853945 377.71618150009374) rotate(0 50.948908750995315 12.438354021268879)"><text x="51.333333333333336" y="17.619999999999997" font-family="Excalifont, Xiaolai, Segoe UI Emoji" font-size="20px" fill="#1e1e1e" text-anchor="middle" style="white-space: pre;" direction="ltr" dominant-baseline="alphabetic">figure.klein</text></g><g stroke-opacity="0.3" fill-opacity="0.3" stroke-linecap="round" transform="translate(10 10) rotate(0 221.5 117.92569168297996)"><path d="M1.26 1.69 L442.36 1.8 L4Line truncated
|
||||
|
After Width: | Height: | Size: 39 KiB |
@@ -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;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define KLEIN_IMPLEMENT
|
||||
#include <klein/klein.h>
|
||||
#include <klein/norm.h>
|
||||
#include <klein/parm.h>
|
||||
|
||||
int p = 37;
|
||||
int q = 1;
|
||||
|
||||
void lens(float *d_surface, int *coord, unsigned char * grid)
|
||||
{
|
||||
|
||||
float norm = 0;
|
||||
float sphere[4];
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
sphere[i] = ((float)coord[i] / grid[i]) - 0.5;
|
||||
|
||||
for(int i = 0; i < 4; i++)
|
||||
norm += sphere[i] * sphere[i];
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
sphere[i] = sphere[i] / sqrt(norm);
|
||||
|
||||
d_surface[0] = (sphere[0] * cos(2 * M_PI / p)) - (sphere[1] * sin(2 * M_PI / p));
|
||||
d_surface[1] = (sphere[0] * sin(2 * M_PI / p)) + (sphere[1] * cos(2 * M_PI / p));
|
||||
d_surface[2] = (sphere[2] * cos(2 * M_PI * q / p)) - (sphere[3] * sin(2 * M_PI * q / p));
|
||||
d_surface[3] = (sphere[2] * sin(2 * M_PI * q / p)) + (sphere[3] * cos(2 * M_PI * q / p));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char file_name[0xff];
|
||||
struct klein klein;
|
||||
struct parm parametrization = {
|
||||
.grid = (unsigned char[]){8, 8,8,8},
|
||||
.m = 4,
|
||||
.n = 4,
|
||||
.f = lens,
|
||||
};
|
||||
|
||||
snprintf(file_name, 0xff, "%03d-%03d-cube.klein", p, q);
|
||||
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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -0,0 +1,132 @@
|
||||
#ifdef KLEIN_NORM_H
|
||||
#error file included twice
|
||||
#endif
|
||||
#define KLEIN_NORM_H
|
||||
|
||||
|
||||
#ifndef KLEIN_H
|
||||
#warning Please include klein/klein.h
|
||||
#endif
|
||||
|
||||
void klein_normalize(struct klein * klein);
|
||||
|
||||
#ifdef KLEIN_IMPLEMENT
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
@@ -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
|
||||
@@ -3,12 +3,8 @@
|
||||
#ifdef EMSCRIPTEN
|
||||
#include <GL/gl.h>
|
||||
#else
|
||||
#ifdef GLAD
|
||||
#include <glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
void set_clean_color_context(unsigned char r, unsigned char g, unsigned char b)
|
||||
@@ -22,12 +18,7 @@ int init_context(void)
|
||||
#ifdef EMSCRIPTEN
|
||||
return 1;
|
||||
#else
|
||||
#ifdef GLAD
|
||||
|
||||
return gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
|
||||
#else
|
||||
return glewInit();
|
||||
#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
|
||||
};
|
||||
+16
-26
@@ -15,18 +15,16 @@ const char * vs =
|
||||
"layout (location = 6) in float aNormal_z;"
|
||||
"layout (location = 7) in float aNormal_w;"
|
||||
|
||||
"uniform uint idx;"
|
||||
|
||||
"uniform uint i;"
|
||||
"uniform float angle;"
|
||||
"uniform float i;"
|
||||
"uniform vec4 color;"
|
||||
|
||||
"uniform mat4 fix;"
|
||||
"uniform mat4 rot;"
|
||||
"uniform mat4 mdl;"
|
||||
|
||||
"flat out uint index;"
|
||||
"out vec3 Normal;"
|
||||
"out vec3 FragPos;"
|
||||
"out vec4 Color;"
|
||||
|
||||
"mat2 rotate2d( float angle )"
|
||||
"{"
|
||||
@@ -35,62 +33,53 @@ const char * vs =
|
||||
|
||||
"void main()"
|
||||
"{"
|
||||
" index=idx;"
|
||||
" Color=color;"
|
||||
|
||||
" vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);"
|
||||
" vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);"
|
||||
|
||||
" aNormal[i] = (vec2(aNormal[i], aNormal_w) * rotate2d(angle))[0];"
|
||||
" aPos[i] = (vec2(aPos[i], aPos_w) * rotate2d(angle))[0];"
|
||||
" aNormal[int(i)] = (vec2(aNormal[int(i)], aNormal_w) * "
|
||||
"rotate2d(angle))[0];"
|
||||
" aPos[int(i)] = (vec2(aPos[int(i)], aPos_w) * rotate2d(angle))[0];"
|
||||
|
||||
" Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;"
|
||||
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
|
||||
" FragPos = vec3( rot * mdl * vec4(aPos, 1.0));"
|
||||
" Normal = mat3(transpose(inverse(rot))) * aNormal;"
|
||||
" gl_Position = fix * rot * vec4( aPos, 1.0 );\n"
|
||||
" FragPos = vec3( rot * vec4(aPos, 1.0));"
|
||||
"}";
|
||||
|
||||
|
||||
const char *fs_plain =
|
||||
#ifdef EMSCRIPTEN
|
||||
"#version 300 es\n"
|
||||
|
||||
"precision highp float;"
|
||||
"precision highp sampler2DArray;"
|
||||
#else
|
||||
"#version 330 core\n"
|
||||
#endif
|
||||
"uniform sampler2DArray palette;"
|
||||
|
||||
"flat in uint index;"
|
||||
"out vec4 FragColor;"
|
||||
"in vec3 Normal;"
|
||||
"in vec3 FragPos;"
|
||||
"in vec4 Color;"
|
||||
|
||||
"void main()"
|
||||
"{"
|
||||
" vec4 color = texture( palette, vec3(0,0,index)).rgba;"
|
||||
" FragColor = vec4(pow(vec3(color),vec3(1.0/2.2)),color.a);"
|
||||
" FragColor = vec4(pow(vec3(Color),vec3(1.0/2.2)),Color.a);"
|
||||
"}";
|
||||
|
||||
const char *fs =
|
||||
#ifdef EMSCRIPTEN
|
||||
"#version 300 es\n"
|
||||
"precision highp float;"
|
||||
"precision highp sampler2DArray;"
|
||||
#else
|
||||
"#version 330 core\n"
|
||||
#endif
|
||||
|
||||
"uniform sampler2DArray palette;"
|
||||
|
||||
"flat in uint index;"
|
||||
"in vec3 Normal;"
|
||||
"in vec3 FragPos;"
|
||||
"in vec4 Color;"
|
||||
|
||||
"out vec4 FragColor;"
|
||||
|
||||
"void main()"
|
||||
"{"
|
||||
" vec4 color = texture(palette, vec3(0, 0, index));\n"
|
||||
|
||||
" vec3 viewPos = vec3(0, 0, -15);\n"
|
||||
" vec3 viewDir = normalize(viewPos - FragPos);\n"
|
||||
@@ -103,6 +92,7 @@ const char * fs =
|
||||
" float specular = pow(abs(dot(normalize(Normal), halfwayDir)), 32.0);\n"
|
||||
" float diffuse = abs(dot(normalize(Normal), lightDir));\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"
|
||||
" 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"
|
||||
"}";
|
||||
+19
-2
@@ -72,7 +72,6 @@ void __key_callback_input(
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
@@ -175,7 +191,8 @@ end:
|
||||
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
@@ -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;
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
#include "main.h"
|
||||
#include <cglm/cam.h>
|
||||
#include <cglm/mat4.h>
|
||||
#include <cglm/quat.h>
|
||||
|
||||
void fix_matrix_load(id_t shader, float ratio)
|
||||
{
|
||||
mat4 m, n;
|
||||
const int d = 7;
|
||||
|
||||
glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m);
|
||||
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);
|
||||
}
|
||||
|
||||
void rot_matrix_load(id_t shader, quat_t q)
|
||||
{
|
||||
mat4 m;
|
||||
glm_quat_mat4(q, m);
|
||||
load_mat4_to_shader(shader, "rot", (mat4_t)m);
|
||||
}
|
||||
|
||||
void color_load(id_t shader, unsigned char color[4])
|
||||
{
|
||||
float res[4];
|
||||
|
||||
res[0] = (float)color[0] / 0xff;
|
||||
res[1] = (float)color[1] / 0xff;
|
||||
res[2] = (float)color[2] / 0xff;
|
||||
res[3] = (float)color[3] / 0xff;
|
||||
load_float4_to_shader(shader, "color", res);
|
||||
}
|
||||
+16
-82
@@ -1,5 +1,4 @@
|
||||
#include "main.h"
|
||||
#include "data/axis.h"
|
||||
#include "data/shaders.h"
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -16,19 +15,11 @@
|
||||
#define M_PI 3.14159
|
||||
#endif
|
||||
|
||||
float *generate_data_surface(unsigned char *);
|
||||
float *generate_normals_surface(float *, unsigned char);
|
||||
|
||||
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";
|
||||
|
||||
unsigned char palette[][4] = {
|
||||
{0xEB, 0xD3, 0xF8, 0xff},
|
||||
{0xEB, 0xD4, 0xF8, 0xff},
|
||||
{0xEB, 0xD5, 0xF8, 0xff},
|
||||
{0x2F, 0x3C, 0x7E, 0xff},
|
||||
};
|
||||
unsigned char color[4] = {0x2F, 0x3C, 0x7E, 0xff};
|
||||
|
||||
void mlog(char *msg)
|
||||
{
|
||||
@@ -38,9 +29,7 @@ void mlog(char *msg)
|
||||
}
|
||||
|
||||
window_t window;
|
||||
mesh_t m_surface, m_axis;
|
||||
id_t shader, shader_plain;
|
||||
unsigned char m;
|
||||
|
||||
extern volatile unsigned char animate_index;
|
||||
|
||||
@@ -55,8 +44,10 @@ static inline
|
||||
|
||||
q = poll_input(window);
|
||||
|
||||
load_rot_matrix(shader, q);
|
||||
load_rot_matrix(shader_plain, q);
|
||||
rot_matrix_load(shader, q);
|
||||
rot_matrix_load(shader_plain, q);
|
||||
color_load(shader, color);
|
||||
color_load(shader_plain, color);
|
||||
|
||||
{
|
||||
static float angle = 0;
|
||||
@@ -73,8 +64,8 @@ static inline
|
||||
|
||||
if (animate_index)
|
||||
{
|
||||
load_uint_to_shader(shader, "i", animate_index - 1);
|
||||
load_uint_to_shader(shader_plain, "i", animate_index - 1);
|
||||
load_float_to_shader(shader, "i", animate_index - 1);
|
||||
load_float_to_shader(shader_plain, "i", animate_index - 1);
|
||||
|
||||
angle += 0.01;
|
||||
load_float_to_shader(shader, "angle", angle);
|
||||
@@ -83,24 +74,15 @@ static inline
|
||||
}
|
||||
clean_context();
|
||||
|
||||
#ifndef DEBUG
|
||||
load_mdl_matrix(shader_plain, 0, 0);
|
||||
draw_mesh(m_axis);
|
||||
load_mdl_matrix(shader_plain, 1, 1);
|
||||
draw_mesh(m_axis);
|
||||
load_mdl_matrix(shader_plain, 2, 2);
|
||||
draw_mesh(m_axis);
|
||||
#endif
|
||||
load_mdl_matrix(shader, 0, 3);
|
||||
draw_mesh(m_surface);
|
||||
if (!projection.mesh)
|
||||
return;
|
||||
|
||||
load_mdl_matrix(shader_plain, 0, 3);
|
||||
draw_mesh_lines(m_surface);
|
||||
draw_mesh(shader, projection.mesh);
|
||||
draw_mesh_lines(shader_plain, projection.mesh);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
id_t texture;
|
||||
|
||||
mlog("[VENTANA] Inicializando...\n");
|
||||
{
|
||||
@@ -123,12 +105,6 @@ int main(void)
|
||||
set_clean_color_context(0xFF, 0xFF, 0xFF);
|
||||
}
|
||||
|
||||
mlog("[TEXTURE] Inicializando...\n");
|
||||
{
|
||||
texture = create_palette_texture(palette, 4);
|
||||
use_texture(texture);
|
||||
}
|
||||
|
||||
mlog("[SHADER] Inicializando...\n");
|
||||
{
|
||||
if (!(shader = create_shader()))
|
||||
@@ -138,7 +114,7 @@ int main(void)
|
||||
}
|
||||
load_program_to_shader(shader, vs, VERTEX);
|
||||
load_program_to_shader(shader, fs, FRAGMENT);
|
||||
load_fix_matrix(shader, (float)WIDTH / HEIGHT);
|
||||
fix_matrix_load(shader, (float)WIDTH / HEIGHT);
|
||||
}
|
||||
|
||||
mlog("[SHADER] Inicializando...\n");
|
||||
@@ -150,37 +126,7 @@ int main(void)
|
||||
}
|
||||
load_program_to_shader(shader_plain, vs, VERTEX);
|
||||
load_program_to_shader(shader_plain, fs_plain, FRAGMENT);
|
||||
load_fix_matrix(shader_plain, (float)WIDTH / HEIGHT);
|
||||
}
|
||||
|
||||
mlog("[MESH] Inicializando...\n");
|
||||
{
|
||||
float *n_surface, *d_surface;
|
||||
d_surface = generate_data_surface(&m);
|
||||
n_surface = generate_normals_surface(d_surface, m);
|
||||
|
||||
if (!(m_surface = create_mesh(d_surface, n_surface, m)))
|
||||
{
|
||||
mlog("[MESH] Error al inicializar...\n");
|
||||
goto error_mesh_surface;
|
||||
}
|
||||
|
||||
projection.m = m;
|
||||
projection.mesh = m_surface;
|
||||
|
||||
set_projection_mesh(projection);
|
||||
|
||||
free(n_surface);
|
||||
free(d_surface);
|
||||
}
|
||||
|
||||
mlog("[MESH] Inicializando...\n");
|
||||
{
|
||||
if (!(m_axis = create_mesh(d_axis, NULL, 3)))
|
||||
{
|
||||
mlog("[MESH] Error al inicializar...\n");
|
||||
goto error_mesh_axis;
|
||||
}
|
||||
fix_matrix_load(shader_plain, (float)WIDTH / HEIGHT);
|
||||
}
|
||||
|
||||
mlog("[MAIN LOOP] Inicializando...\n");
|
||||
@@ -194,34 +140,22 @@ int main(void)
|
||||
mlog("[MAIN LOOP] Terminando...\n");
|
||||
|
||||
mlog("[MESH] Destruyendo...\n");
|
||||
destroy_mesh(m_axis);
|
||||
mlog("[MESH] Destruyendo...\n");
|
||||
destroy_mesh(m_surface);
|
||||
destroy_mesh(projection.mesh);
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
destroy_shader(shader_plain);
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
destroy_shader(shader);
|
||||
mlog("[TEXTURE] Destruyendo...\n");
|
||||
destroy_texture(texture);
|
||||
mlog("[WINDOW] Destruyendo...\n");
|
||||
close_window(window);
|
||||
return 0;
|
||||
|
||||
mlog("[MESH] Destruyendo...\n");
|
||||
destroy_mesh(m_axis);
|
||||
error_mesh_axis:
|
||||
mlog("[MESH] Destruyendo...\n");
|
||||
destroy_mesh(m_surface);
|
||||
error_mesh_surface:
|
||||
error_context:
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
destroy_shader(shader_plain);
|
||||
error_shader_plain:
|
||||
mlog("[SHADER] Destruyendo...\n");
|
||||
destroy_shader(shader);
|
||||
error_shader:
|
||||
mlog("[TEXTURE] Destruyendo...\n");
|
||||
destroy_texture(texture);
|
||||
error_context:
|
||||
mlog("[WINDOW] Destruyendo...\n");
|
||||
close_window(window);
|
||||
error_window:
|
||||
|
||||
+41
-24
@@ -4,8 +4,7 @@
|
||||
error of the shaders.
|
||||
*/
|
||||
|
||||
/* #define DEBUG */
|
||||
#define GLAD
|
||||
#define DEBUG
|
||||
|
||||
typedef const void *window_t;
|
||||
typedef unsigned int id_t;
|
||||
@@ -29,6 +28,22 @@ struct projection
|
||||
unsigned char m, x, y, z, w;
|
||||
};
|
||||
|
||||
/*
|
||||
this structure has all the information to generate
|
||||
a mesh, where:
|
||||
data: the buffer with the vertex coords
|
||||
norm: the buffer with the norm coords
|
||||
vertex: the number of vertex
|
||||
dim: the dimentions of the surface
|
||||
*/
|
||||
|
||||
struct surface
|
||||
{
|
||||
float *data, *norm;
|
||||
unsigned long vertex;
|
||||
unsigned char dim;
|
||||
};
|
||||
|
||||
/*
|
||||
Init window:
|
||||
w: default width;
|
||||
@@ -36,6 +51,8 @@ struct projection
|
||||
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);
|
||||
@@ -51,15 +68,15 @@ void close_window(window_t window);
|
||||
m: Dimention of mesh
|
||||
*/
|
||||
|
||||
mesh_t create_mesh( float * d, float * n, unsigned char m );
|
||||
mesh_t create_mesh(struct surface);
|
||||
|
||||
void set_projection_mesh(struct projection);
|
||||
|
||||
void destroy_mesh(mesh_t p);
|
||||
|
||||
void draw_mesh(mesh_t p);
|
||||
void draw_mesh(id_t, mesh_t p);
|
||||
|
||||
void draw_mesh_lines(mesh_t p);
|
||||
void draw_mesh_lines(id_t, mesh_t p);
|
||||
|
||||
/*
|
||||
Set background color:
|
||||
@@ -82,7 +99,8 @@ void use_shader(id_t shader);
|
||||
|
||||
enum
|
||||
{
|
||||
VERTEX, FRAGMENT
|
||||
VERTEX,
|
||||
FRAGMENT
|
||||
};
|
||||
|
||||
/*
|
||||
@@ -91,7 +109,8 @@ enum
|
||||
type: VERTEX or FRAGMENT
|
||||
*/
|
||||
|
||||
unsigned char load_program_to_shader(id_t shader, const char * src, unsigned int type);
|
||||
unsigned char load_program_to_shader(
|
||||
id_t shader, const char *src, unsigned int type);
|
||||
|
||||
/*
|
||||
load float to shader:
|
||||
@@ -109,6 +128,14 @@ void load_float_to_shader(id_t shader, char * var, float f);
|
||||
|
||||
void load_uint_to_shader(id_t shader, char *var, unsigned int u);
|
||||
|
||||
/*
|
||||
load float[4] to shader:
|
||||
var: name of glsl variable.
|
||||
f: float[4] to load
|
||||
*/
|
||||
|
||||
void load_float4_to_shader(id_t shader, char *var, float f[4]);
|
||||
|
||||
/*
|
||||
load matrix 4 to shader:
|
||||
var: name of glsl variable.
|
||||
@@ -124,23 +151,19 @@ void load_mat4_to_shader(id_t shader, char * var, mat4_t m);
|
||||
|
||||
ratio: default ratio of window.
|
||||
*/
|
||||
void load_fix_matrix(id_t shader, float ratio);
|
||||
|
||||
/*
|
||||
Generate and load model matrix, it also sets the color
|
||||
to draw.
|
||||
i: From {0,1,2} select one of 3 ortogonal rotations,
|
||||
One for each axis.
|
||||
c: Color index of the pallete.
|
||||
*/
|
||||
void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c);
|
||||
void fix_matrix_load(id_t shader, float ratio);
|
||||
|
||||
/*
|
||||
Generate and load rotation matrix.
|
||||
q: quaterinon describing the rotation.
|
||||
*/
|
||||
|
||||
void load_rot_matrix(id_t shader, quat_t q);
|
||||
void rot_matrix_load(id_t shader, quat_t q);
|
||||
|
||||
/*
|
||||
*/
|
||||
|
||||
void color_load(id_t shader, unsigned char color[4]);
|
||||
|
||||
id_t config_texture(unsigned short type);
|
||||
|
||||
@@ -156,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
|
||||
@@ -1,50 +0,0 @@
|
||||
#include "main.h"
|
||||
#include <cglm/cam.h>
|
||||
#include <cglm/mat4.h>
|
||||
#include <cglm/quat.h>
|
||||
|
||||
mat4 ortho[] = {
|
||||
{
|
||||
{1, 0, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
},
|
||||
{
|
||||
{0, 1, 0, 0},
|
||||
{-1, 0, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1},
|
||||
},
|
||||
{
|
||||
{0, 0, 1, 0},
|
||||
{0, 1, 0, 0},
|
||||
{-1, 0, 0, 0},
|
||||
{0, 0, 0, 1},
|
||||
},
|
||||
};
|
||||
|
||||
void load_fix_matrix(id_t shader, float ratio)
|
||||
{
|
||||
mat4 m, n;
|
||||
const int d = 7;
|
||||
|
||||
glm_lookat((vec3){0, 0, -d}, (vec3){0, 0, 0}, (vec3){0, 1, 0}, m);
|
||||
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);
|
||||
}
|
||||
|
||||
void load_mdl_matrix(id_t shader, unsigned char i, unsigned char c)
|
||||
{
|
||||
load_uint_to_shader(shader, "idx", c);
|
||||
load_mat4_to_shader(shader, "mdl", (mat4_t)ortho[i]);
|
||||
}
|
||||
|
||||
void load_rot_matrix(id_t shader, quat_t q)
|
||||
{
|
||||
mat4 m;
|
||||
glm_quat_mat4(q, m);
|
||||
load_mat4_to_shader(shader, "rot", (mat4_t)m);
|
||||
}
|
||||
+24
-16
@@ -1,9 +1,11 @@
|
||||
#include "main.h"
|
||||
#ifdef GLAD
|
||||
#include <glad.h>
|
||||
|
||||
#ifdef EMSCRIPTEN
|
||||
#include <GL/gl.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#include <glad.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -47,36 +49,34 @@ void set_projection_mesh(struct projection projection)
|
||||
This trick can be done with glVertexAttribPointer.
|
||||
*/
|
||||
|
||||
mesh_t create_mesh(float *d, float *n, unsigned char m)
|
||||
mesh_t create_mesh(struct surface surface)
|
||||
{
|
||||
unsigned char i;
|
||||
struct obj *p;
|
||||
|
||||
p = malloc(sizeof(struct obj));
|
||||
|
||||
p->vertex = (*d) / m;
|
||||
p->vertex = surface.vertex;
|
||||
|
||||
glGenVertexArrays(1, &p->vao);
|
||||
glBindVertexArray(p->vao);
|
||||
|
||||
glGenBuffers(1, &p->d_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, p->d_vbo);
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER, p->vertex * m * sizeof(float), d + 1, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, p->vertex * surface.dim * sizeof(float),
|
||||
surface.data, GL_STATIC_DRAW);
|
||||
|
||||
if (n)
|
||||
if (surface.norm)
|
||||
{
|
||||
glGenBuffers(1, &p->n_vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, p->n_vbo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, p->vertex * m * sizeof(float),
|
||||
n + 1, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER,
|
||||
p->vertex * surface.dim * sizeof(float), surface.norm,
|
||||
GL_STATIC_DRAW);
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; ++i)
|
||||
{
|
||||
for (i = 0; i < 8; ++i)
|
||||
glEnableVertexAttribArray(i);
|
||||
glEnableVertexAttribArray(i + 4);
|
||||
}
|
||||
|
||||
return p;
|
||||
}
|
||||
@@ -91,19 +91,27 @@ void destroy_mesh(mesh_t p)
|
||||
free(p);
|
||||
}
|
||||
|
||||
void draw_mesh(mesh_t p)
|
||||
void draw_mesh(id_t shader, mesh_t p)
|
||||
{
|
||||
struct obj *obj = p;
|
||||
|
||||
glUseProgram(shader);
|
||||
glBindVertexArray(obj->vao);
|
||||
#ifndef EMSCRIPTEN
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
#endif
|
||||
glDrawArrays(GL_TRIANGLES, 0, obj->vertex);
|
||||
}
|
||||
void draw_mesh_lines(mesh_t p)
|
||||
void draw_mesh_lines(id_t shader, mesh_t p)
|
||||
{
|
||||
struct obj *obj = p;
|
||||
|
||||
glUseProgram(shader);
|
||||
glBindVertexArray(obj->vao);
|
||||
#ifndef EMSCRIPTEN
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||
glDrawArrays(GL_TRIANGLES, 0, obj->vertex);
|
||||
#else
|
||||
glDrawArrays(GL_LINES, 0, obj->vertex);
|
||||
#endif
|
||||
}
|
||||
+11
-10
@@ -1,20 +1,20 @@
|
||||
#include "main.h"
|
||||
|
||||
#ifdef GLAD
|
||||
#ifndef EMSCRIPTEN
|
||||
#include <glad.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#include <GL/gl.h>
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG
|
||||
#include <stdio.h>
|
||||
#endif
|
||||
|
||||
void destroy_shader(unsigned int shader) { return glDeleteProgram(shader); }
|
||||
void destroy_shader(unsigned int shader) { glDeleteProgram(shader); }
|
||||
|
||||
unsigned int create_shader(void) { return glCreateProgram(); }
|
||||
|
||||
void use_shader(unsigned int program) { return glUseProgram(program); }
|
||||
void use_shader(unsigned int program) { glUseProgram(program); }
|
||||
|
||||
unsigned char load_program_to_shader(
|
||||
unsigned int program, const char *src, unsigned int i)
|
||||
@@ -54,14 +54,15 @@ void load_float_to_shader(unsigned int program, char *var, float f)
|
||||
glUniform1f(glGetUniformLocation(program, var), f);
|
||||
}
|
||||
|
||||
void load_uint_to_shader(unsigned int program, char *var, unsigned int u)
|
||||
{
|
||||
glUseProgram(program);
|
||||
glUniform1ui(glGetUniformLocation(program, var), u);
|
||||
}
|
||||
|
||||
void load_mat4_to_shader(unsigned int program, char *var, float *mat)
|
||||
{
|
||||
glUseProgram(program);
|
||||
glUniformMatrix4fv(glGetUniformLocation(program, var), 1, 0, mat);
|
||||
}
|
||||
|
||||
void load_float4_to_shader(unsigned int program, char *var, float float4[4])
|
||||
{
|
||||
glUseProgram(program);
|
||||
glUniform4f(glGetUniformLocation(program, var), float4[0], float4[1],
|
||||
float4[2], float4[3]);
|
||||
}
|
||||
-340
@@ -1,340 +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 *, int);
|
||||
|
||||
struct parm
|
||||
{
|
||||
function_t f;
|
||||
unsigned char m, n;
|
||||
unsigned int grid;
|
||||
} parm;
|
||||
|
||||
int factorial(int n)
|
||||
{
|
||||
if (n == 1)
|
||||
return 1;
|
||||
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
|
||||
int numero_caras(int n)
|
||||
{
|
||||
if (n == 2)
|
||||
return 1;
|
||||
|
||||
return (1 << (n - 3)) * factorial(n) / factorial(n - 2);
|
||||
}
|
||||
|
||||
void riemman(float *d_surface, int *coords, int grid)
|
||||
{
|
||||
complex double eq;
|
||||
float u = 2 * ((float)coords[0] / grid) - 1;
|
||||
float v = 2 * ((float)coords[1] / grid) - 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, int grid)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < parm.m; i++)
|
||||
d_surface[i] = (2 * (float)coord[i] / grid) - 1;
|
||||
|
||||
if (parm.m == 2)
|
||||
d_surface[2] = 0;
|
||||
}
|
||||
|
||||
void sphere(float *d_surface, int *coord, int grid)
|
||||
{
|
||||
for (int i = 0; i < parm.m; i++)
|
||||
d_surface[i] = ((float)coord[i] / grid) - 0.5;
|
||||
|
||||
float norm = 0;
|
||||
for(int i = 0; i < parm.m; i++)
|
||||
norm += d_surface[i] * d_surface[i];
|
||||
|
||||
for (int i = 0; i < parm.m; i++)
|
||||
d_surface[i] = d_surface[i] / sqrt(norm);
|
||||
}
|
||||
|
||||
void lens(float *d_surface, int *coord, int grid)
|
||||
{
|
||||
int p = 7;
|
||||
int q = 1;
|
||||
|
||||
float sphere[4];
|
||||
for (int i = 0; i < 4; i++)
|
||||
sphere[i] = ((float)coord[i] / grid) - 0.5;
|
||||
|
||||
float norm = 0;
|
||||
for(int i = 0; i < 4; i++)
|
||||
norm += sphere[i] * sphere[i];
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
sphere[i] = sphere[i] / sqrt(norm);
|
||||
|
||||
d_surface[0] = (sphere[0] * cos(2 * M_PI / p)) - (sphere[1] * sin(2 * M_PI / p));
|
||||
d_surface[1] = (sphere[0] * sin(2 * M_PI / p)) + (sphere[1] * cos(2 * M_PI / p));
|
||||
d_surface[2] = (sphere[2] * cos(2 * M_PI * q / p)) - (sphere[3] * sin(2 * M_PI * q / p));
|
||||
d_surface[3] = (sphere[2] * sin(2 * M_PI * q / p)) + (sphere[3] * cos(2 * M_PI * q / p));
|
||||
}
|
||||
|
||||
void mobius(float *d_surface, int *coord, int grid)
|
||||
{
|
||||
const float width = 0.5;
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid);
|
||||
float v = (2 * width) * ((float)coord[1] / grid) - 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, int grid)
|
||||
{
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid);
|
||||
float v = (2 * M_PI) * ((float)coord[1] / grid);
|
||||
|
||||
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, int grid)
|
||||
{
|
||||
float u = (2 * M_PI) * ((float)coord[0] / grid);
|
||||
float v = (2 * M_PI) * ((float)coord[1] / grid);
|
||||
|
||||
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 *s)
|
||||
{
|
||||
unsigned int i, j, k, o, p, n;
|
||||
long size, q = 0;
|
||||
float *d_surface;
|
||||
int *cara;
|
||||
|
||||
parm.f = lens;
|
||||
parm.m = 4;
|
||||
parm.n = 4;
|
||||
parm.grid = 5;
|
||||
|
||||
#ifdef TEST
|
||||
assert(numero_caras(2) == 1);
|
||||
assert(numero_caras(3) == 6);
|
||||
assert(numero_caras(4) == 24);
|
||||
#endif
|
||||
|
||||
*s = parm.n;
|
||||
|
||||
cara = malloc(parm.m * sizeof(int));
|
||||
size = parm.grid * parm.grid * 6 * (parm.n) * numero_caras(parm.m);
|
||||
d_surface = malloc((size + 1) * sizeof(float));
|
||||
d_surface[0] = size;
|
||||
|
||||
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 : 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < parm.grid; i++)
|
||||
{
|
||||
for (j = 0; j < parm.grid; j++)
|
||||
{
|
||||
cara[p] = i;
|
||||
cara[o] = j;
|
||||
parm.f(&d_surface[q + 1], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p]++;
|
||||
parm.f(&d_surface[q + 1], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[o]++;
|
||||
parm.f(&d_surface[q + 1], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p] = i;
|
||||
cara[o] = j;
|
||||
parm.f(&d_surface[q + 1], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[o]++;
|
||||
parm.f(&d_surface[q + 1], cara, parm.grid);
|
||||
q += parm.n;
|
||||
|
||||
cara[p]++;
|
||||
parm.f(&d_surface[q + 1], 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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
|
||||
__calculate_normal(
|
||||
(d + 1) + i, (d + 1) + i + m, (d + 1) + i + 2 * m, norm_vec, m);
|
||||
glm_vec3_copy(norm_vec, (n + 1) + i);
|
||||
glm_vec3_copy(norm_vec, (n + 1) + i + m);
|
||||
glm_vec3_copy(norm_vec, (n + 1) + i + 2 * m);
|
||||
}
|
||||
|
||||
free(norm_vec);
|
||||
return n;
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
#include "main.h"
|
||||
#ifdef GLAD
|
||||
#include <glad.h>
|
||||
#else
|
||||
#include <GL/glew.h>
|
||||
#endif
|
||||
|
||||
#define TYPE GL_TEXTURE_2D_ARRAY
|
||||
|
||||
static id_t __config_texture(unsigned short type)
|
||||
{
|
||||
id_t texture;
|
||||
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(TYPE, texture);
|
||||
|
||||
{
|
||||
glTexParameteri(TYPE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(TYPE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
}
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
void use_texture(id_t texture) { return glBindTexture(TYPE, texture); }
|
||||
|
||||
void destroy_texture(unsigned int texture)
|
||||
{
|
||||
return glDeleteTextures(1, &texture);
|
||||
}
|
||||
|
||||
id_t create_palette_texture(const unsigned char colors[][4], unsigned char n)
|
||||
{
|
||||
id_t texture = __config_texture(TYPE);
|
||||
glTexImage3D(
|
||||
TYPE, 0, GL_RGBA, 1, 1, n, 0, GL_RGBA, GL_UNSIGNED_BYTE, colors);
|
||||
return texture;
|
||||
}
|
||||
@@ -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