Merge: roberto -> main
This commit is contained in:
108
src/main.c
108
src/main.c
@@ -3,9 +3,12 @@
|
||||
#include "data/shaders.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <cglm/vec3.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define CGLM_ALL_UNALIGNED
|
||||
#include <cglm/vec3.h>
|
||||
#include <cglm/vec4.h>
|
||||
|
||||
#define WIDTH 512
|
||||
#define HEIGHT 512
|
||||
|
||||
@@ -19,48 +22,100 @@ unsigned char palette[] =
|
||||
0x7A,0x1C,0xAC,0xff,
|
||||
};
|
||||
|
||||
void calc_normal(float* v1, float* v2, float* v3, float* normal)
|
||||
void calc_normal(float* p1, float* p2, float* p3,float* normal, unsigned char n)
|
||||
{
|
||||
vec3 lado1, lado2;
|
||||
float alpha;
|
||||
vec4 v1, v2, v3;
|
||||
vec4 u1, u2, u3;
|
||||
|
||||
switch (n)
|
||||
{
|
||||
case 3:
|
||||
|
||||
glm_vec3_sub(v2, v1, lado1);
|
||||
glm_vec3_sub(v3, v1, lado2);
|
||||
glm_vec3_sub(p2, p1, v1);
|
||||
glm_vec3_sub(p3, p1, v2);
|
||||
|
||||
glm_vec3_cross(lado1, lado2, normal);
|
||||
glm_vec3_normalize(normal);
|
||||
glm_vec3_cross(v1, v2, normal);
|
||||
glm_vec3_normalize(normal);
|
||||
return;
|
||||
|
||||
case 4:
|
||||
|
||||
/*
|
||||
In Grant-Shmidth we need 3 linearly independian vector that forms a basis,
|
||||
so we can have a ortonormal version of that basis, since, we must have
|
||||
v1 = p3 - p1
|
||||
v2 = p2 - p1
|
||||
Then v3 = p1, will most certantly be linerly independiant to v1 and v2.
|
||||
*/
|
||||
glm_vec4_sub(p2, p1, v1);
|
||||
glm_vec4_sub(p3, p1, v2);
|
||||
glm_vec4_copy(p1, v3);
|
||||
|
||||
/* Setup U1 */
|
||||
{
|
||||
glm_vec4_copy(v1, u1);
|
||||
}
|
||||
|
||||
/* Setup U2 */
|
||||
{
|
||||
vec4 proj;
|
||||
|
||||
alpha = glm_vec4_dot(v2, u1) / glm_vec4_dot(u1, u1);
|
||||
glm_vec4_scale(u1, alpha, proj);
|
||||
|
||||
glm_vec4_sub(v2, proj, u2);
|
||||
}
|
||||
|
||||
/* Setup U3 */
|
||||
{
|
||||
vec4 proj1, proj2;
|
||||
|
||||
alpha = glm_vec4_dot(v3, u1) / glm_vec4_dot(u1, u1);
|
||||
glm_vec4_scale(u1, alpha, proj1);
|
||||
|
||||
alpha = glm_vec4_dot(v3, u2) / glm_vec4_dot(u2, u2);
|
||||
glm_vec4_scale(u2, alpha, proj2);
|
||||
|
||||
glm_vec4_sub(v3, proj1, u3);
|
||||
glm_vec4_sub(u3, proj2, u3);
|
||||
}
|
||||
|
||||
glm_vec4_copy(u3, normal);
|
||||
glm_vec4_normalize(normal);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
float * fill_normal( float * d )
|
||||
float * fill_normal( float * d, unsigned char m )
|
||||
{
|
||||
float * n;
|
||||
|
||||
n = malloc((*d+1)*sizeof(float));
|
||||
n = malloc( (*d+1)*sizeof(float));
|
||||
*n = *d;
|
||||
|
||||
for (int i = 0; i < *d; i += 9)
|
||||
for (int i = 0; i < *d; i += 3*m)
|
||||
{
|
||||
|
||||
vec3 norm_vec;
|
||||
calc_normal((d+1)+i, (d+1)+i+3, (d+1)+i+6, norm_vec);
|
||||
vec4 norm_vec;
|
||||
|
||||
calc_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+3 );
|
||||
glm_vec3_copy( norm_vec, (n+1)+i+6 );
|
||||
glm_vec3_copy( norm_vec, (n+1)+i+m );
|
||||
glm_vec3_copy( norm_vec, (n+1)+i+2*m );
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
const char * wname = "manigraph: manifold grapher";
|
||||
float * generate_surface(unsigned int, unsigned char *m);
|
||||
|
||||
|
||||
void mlog(char * msg)
|
||||
void mlog( char * msg )
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printf(msg);
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(void)
|
||||
const char * wname = "manigraph: manifold grapher";
|
||||
float * generate_surface();
|
||||
|
||||
int main( void )
|
||||
{
|
||||
id_t shader, texture, shader_plain;
|
||||
mesh_t m_surface, m_axis;
|
||||
@@ -122,8 +177,8 @@ int main(void)
|
||||
unsigned char m;
|
||||
float * n_surface, *d_surface;
|
||||
d_surface = generate_surface(16,&m);
|
||||
n_surface = fill_normal(d_surface);
|
||||
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate)))
|
||||
n_surface = fill_normal(d_surface, m);
|
||||
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate, m)))
|
||||
{
|
||||
mlog("[MESH] Error al inicializar...\n");
|
||||
goto error_mesh_surface;
|
||||
@@ -135,8 +190,8 @@ int main(void)
|
||||
mlog("[MESH] Inicializando...\n");
|
||||
{
|
||||
float * n_axis;
|
||||
n_axis = fill_normal(d_axis);
|
||||
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate)))
|
||||
n_axis = fill_normal(d_axis, 3);
|
||||
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate, 3)))
|
||||
{
|
||||
mlog("[MESH] Error al inicializar...\n");
|
||||
goto error_mesh_axis;
|
||||
@@ -154,6 +209,7 @@ int main(void)
|
||||
load_rot_matrix(shader_plain, q);
|
||||
|
||||
clean_context();
|
||||
|
||||
#ifndef DEBUG
|
||||
load_mdl_matrix(shader_plain, 0, 0);
|
||||
draw_mesh( m_axis);
|
||||
|
||||
Reference in New Issue
Block a user