Merge: roberto -> main
This commit is contained in:
@@ -4,6 +4,9 @@
|
|||||||
void set_clean_color_context( unsigned char r, unsigned char g, unsigned char b )
|
void set_clean_color_context( unsigned char r, unsigned char g, unsigned char b )
|
||||||
{
|
{
|
||||||
glEnable( GL_DEPTH_TEST );
|
glEnable( GL_DEPTH_TEST );
|
||||||
|
glEnable( GL_CULL_FACE );
|
||||||
|
glCullFace( GL_BACK );
|
||||||
|
|
||||||
glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 );
|
glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,6 +45,7 @@ const char * fs_plain =
|
|||||||
" FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;"
|
" FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;"
|
||||||
"}";
|
"}";
|
||||||
|
|
||||||
|
|
||||||
const char * fs =
|
const char * fs =
|
||||||
"#version 330 core\n"
|
"#version 330 core\n"
|
||||||
|
|
||||||
@@ -62,11 +63,7 @@ const char * fs =
|
|||||||
" vec3 lightPos = vec3(0,0,-15);"
|
" vec3 lightPos = vec3(0,0,-15);"
|
||||||
" vec3 lightDir = normalize(lightPos - FragPos);"
|
" vec3 lightDir = normalize(lightPos - FragPos);"
|
||||||
|
|
||||||
/*
|
" float diffuse = max(dot(Normal, lightDir),0.0); "
|
||||||
We use the absoulute value, insted of the traditional "max(...,0.0);"
|
|
||||||
so we can ignore the orientation of the triangles in openGL.
|
|
||||||
*/
|
|
||||||
" float diffuse = abs(dot(Normal, lightDir)); "
|
|
||||||
|
|
||||||
" float ambient = 0.5;"
|
" float ambient = 0.5;"
|
||||||
" FragColor = (ambient + diffuse)*color;"
|
" FragColor = (ambient + diffuse)*color;"
|
||||||
|
|||||||
106
src/main.c
106
src/main.c
@@ -3,9 +3,12 @@
|
|||||||
#include "data/shaders.h"
|
#include "data/shaders.h"
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <cglm/vec3.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#define CGLM_ALL_UNALIGNED
|
||||||
|
#include <cglm/vec3.h>
|
||||||
|
#include <cglm/vec4.h>
|
||||||
|
|
||||||
#define WIDTH 512
|
#define WIDTH 512
|
||||||
#define HEIGHT 512
|
#define HEIGHT 512
|
||||||
|
|
||||||
@@ -19,48 +22,100 @@ unsigned char palette[] =
|
|||||||
0x7A,0x1C,0xAC,0xff,
|
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;
|
||||||
|
|
||||||
glm_vec3_sub(v2, v1, lado1);
|
switch (n)
|
||||||
glm_vec3_sub(v3, v1, lado2);
|
{
|
||||||
|
case 3:
|
||||||
|
|
||||||
glm_vec3_cross(lado1, lado2, normal);
|
glm_vec3_sub(p2, p1, v1);
|
||||||
|
glm_vec3_sub(p3, p1, v2);
|
||||||
|
|
||||||
|
glm_vec3_cross(v1, v2, normal);
|
||||||
glm_vec3_normalize(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;
|
float * n;
|
||||||
|
n = malloc( (*d+1)*sizeof(float));
|
||||||
n = malloc((*d+1)*sizeof(float));
|
|
||||||
*n = *d;
|
*n = *d;
|
||||||
|
for (int i = 0; i < *d; i += 3*m)
|
||||||
for (int i = 0; i < *d; i += 9)
|
|
||||||
{
|
{
|
||||||
|
|
||||||
vec3 norm_vec;
|
vec4 norm_vec;
|
||||||
calc_normal((d+1)+i, (d+1)+i+3, (d+1)+i+6, 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 );
|
||||||
glm_vec3_copy( norm_vec, (n+1)+i+3 );
|
glm_vec3_copy( norm_vec, (n+1)+i+m );
|
||||||
glm_vec3_copy( norm_vec, (n+1)+i+6 );
|
glm_vec3_copy( norm_vec, (n+1)+i+2*m );
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char * wname = "manigraph: manifold grapher";
|
void mlog( char * msg )
|
||||||
float * generate_surface(unsigned int, unsigned char *m);
|
|
||||||
|
|
||||||
|
|
||||||
void mlog(char * msg)
|
|
||||||
{
|
{
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
printf(msg);
|
printf(msg);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
const char * wname = "manigraph: manifold grapher";
|
||||||
|
float * generate_surface();
|
||||||
|
|
||||||
|
int main( void )
|
||||||
{
|
{
|
||||||
id_t shader, texture, shader_plain;
|
id_t shader, texture, shader_plain;
|
||||||
mesh_t m_surface, m_axis;
|
mesh_t m_surface, m_axis;
|
||||||
@@ -122,8 +177,8 @@ int main(void)
|
|||||||
unsigned char m;
|
unsigned char m;
|
||||||
float * n_surface, *d_surface;
|
float * n_surface, *d_surface;
|
||||||
d_surface = generate_surface(16,&m);
|
d_surface = generate_surface(16,&m);
|
||||||
n_surface = fill_normal(d_surface);
|
n_surface = fill_normal(d_surface, m);
|
||||||
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate)))
|
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate, m)))
|
||||||
{
|
{
|
||||||
mlog("[MESH] Error al inicializar...\n");
|
mlog("[MESH] Error al inicializar...\n");
|
||||||
goto error_mesh_surface;
|
goto error_mesh_surface;
|
||||||
@@ -135,8 +190,8 @@ int main(void)
|
|||||||
mlog("[MESH] Inicializando...\n");
|
mlog("[MESH] Inicializando...\n");
|
||||||
{
|
{
|
||||||
float * n_axis;
|
float * n_axis;
|
||||||
n_axis = fill_normal(d_axis);
|
n_axis = fill_normal(d_axis, 3);
|
||||||
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate)))
|
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate, 3)))
|
||||||
{
|
{
|
||||||
mlog("[MESH] Error al inicializar...\n");
|
mlog("[MESH] Error al inicializar...\n");
|
||||||
goto error_mesh_axis;
|
goto error_mesh_axis;
|
||||||
@@ -154,6 +209,7 @@ int main(void)
|
|||||||
load_rot_matrix(shader_plain, q);
|
load_rot_matrix(shader_plain, q);
|
||||||
|
|
||||||
clean_context();
|
clean_context();
|
||||||
|
|
||||||
#ifndef DEBUG
|
#ifndef DEBUG
|
||||||
load_mdl_matrix(shader_plain, 0, 0);
|
load_mdl_matrix(shader_plain, 0, 0);
|
||||||
draw_mesh( m_axis);
|
draw_mesh( m_axis);
|
||||||
|
|||||||
@@ -32,9 +32,10 @@ void close_window(window_t window);
|
|||||||
d: array of floats with the vertex data.
|
d: array of floats with the vertex data.
|
||||||
n: array of floats with the normal data.
|
n: array of floats with the normal data.
|
||||||
coordanate: the number of the 3 axis that we are displaying.
|
coordanate: the number of the 3 axis that we are displaying.
|
||||||
|
m: Dimention of mesh
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate );
|
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m );
|
||||||
|
|
||||||
void destroy_mesh(mesh_t p);
|
void destroy_mesh(mesh_t p);
|
||||||
|
|
||||||
|
|||||||
12
src/mesh.c
12
src/mesh.c
@@ -13,38 +13,38 @@ struct obj
|
|||||||
to display using the layout location in GLSL.
|
to display using the layout location in GLSL.
|
||||||
This trick can be done with glVertexAttribPointer.
|
This trick can be done with glVertexAttribPointer.
|
||||||
*/
|
*/
|
||||||
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate )
|
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m )
|
||||||
{
|
{
|
||||||
unsigned char i;
|
unsigned char i;
|
||||||
struct obj * p;
|
struct obj * p;
|
||||||
|
|
||||||
p=malloc(sizeof(struct obj));
|
p=malloc(sizeof(struct obj));
|
||||||
|
|
||||||
p->vertex=(*d)/3;
|
p->vertex=(*d)/m;
|
||||||
|
|
||||||
glGenVertexArrays( 1, &p->vao );
|
glGenVertexArrays( 1, &p->vao );
|
||||||
|
|
||||||
glGenBuffers( 1, &p->d_vbo );
|
glGenBuffers( 1, &p->d_vbo );
|
||||||
glBindVertexArray( p->vao );
|
glBindVertexArray( p->vao );
|
||||||
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
|
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
|
||||||
glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), d+1,
|
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1,
|
||||||
GL_STATIC_DRAW );
|
GL_STATIC_DRAW );
|
||||||
|
|
||||||
for( i=0; i<4; ++i )
|
for( i=0; i<4; ++i )
|
||||||
{
|
{
|
||||||
glVertexAttribPointer( i,1,GL_FLOAT, 0, 3*sizeof(float),
|
glVertexAttribPointer( i,1,GL_FLOAT, 0, m*sizeof(float),
|
||||||
(float*)(coordanate[i]*sizeof(float)) );
|
(float*)(coordanate[i]*sizeof(float)) );
|
||||||
glEnableVertexAttribArray(i);
|
glEnableVertexAttribArray(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
glGenBuffers( 1, &p->n_vbo );
|
glGenBuffers( 1, &p->n_vbo );
|
||||||
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
|
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
|
||||||
glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), n+1,
|
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
|
||||||
GL_STATIC_DRAW );
|
GL_STATIC_DRAW );
|
||||||
|
|
||||||
for( i=0; i<4; ++i )
|
for( i=0; i<4; ++i )
|
||||||
{
|
{
|
||||||
glVertexAttribPointer( i+4,1,GL_FLOAT, 0, 3*sizeof(float),
|
glVertexAttribPointer( i+4,1,GL_FLOAT, 0, m*sizeof(float),
|
||||||
(float*)(coordanate[i]*sizeof(float)) );
|
(float*)(coordanate[i]*sizeof(float)) );
|
||||||
glEnableVertexAttribArray(i+4);
|
glEnableVertexAttribArray(i+4);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,28 +37,28 @@ void klein(float *d_surface, int i, int j, int grid_size)
|
|||||||
d_surface[0] = (0.5*cos(v) + 0.5) * cos(u);
|
d_surface[0] = (0.5*cos(v) + 0.5) * cos(u);
|
||||||
d_surface[1] = (0.5*cos(v) + 0.5) * sin(u);
|
d_surface[1] = (0.5*cos(v) + 0.5) * sin(u);
|
||||||
d_surface[2] = sin(v) * cos(u/2);
|
d_surface[2] = sin(v) * cos(u/2);
|
||||||
//d_surface[2] = sin(v)*sin(u/2);
|
d_surface[3] = sin(v)*sin(u/2);
|
||||||
}
|
}
|
||||||
|
|
||||||
float * generate_surface(int grid_size, unsigned char * m)
|
float * generate_surface(int grid_size, unsigned char *m )
|
||||||
{
|
{
|
||||||
float * d_surface;
|
unsigned int i,j,k=0;
|
||||||
function_t f;
|
|
||||||
int i,j,k=0;
|
|
||||||
long size;
|
long size;
|
||||||
|
function_t f;
|
||||||
|
float * d_surface;
|
||||||
|
|
||||||
*m = 3;
|
|
||||||
f = klein;
|
f = klein;
|
||||||
size = grid_size*grid_size*2*3*(*m);
|
*m = 4;
|
||||||
|
size = grid_size*grid_size*12*(*m);
|
||||||
d_surface = malloc((size+1)*sizeof(float));
|
d_surface = malloc((size+1)*sizeof(float));
|
||||||
d_surface[0] = size;
|
d_surface[0] = size;
|
||||||
|
|
||||||
|
|
||||||
for( i = 0; i < grid_size; i++)
|
for( i = 0; i < grid_size; i++)
|
||||||
{
|
{
|
||||||
for( j = 0; j < grid_size; j++)
|
for( j = 0; j < grid_size; j++)
|
||||||
{
|
{
|
||||||
// triangle 1
|
// triangle 1, Front
|
||||||
f(&d_surface[k + 1], i, j, grid_size);
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
k+=*m;
|
k+=*m;
|
||||||
f(&d_surface[k + 1], i + 1, j, grid_size);
|
f(&d_surface[k + 1], i + 1, j, grid_size);
|
||||||
@@ -66,13 +66,29 @@ float * generate_surface(int grid_size, unsigned char * m)
|
|||||||
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
k+=*m;
|
k+=*m;
|
||||||
|
|
||||||
// triangle 2
|
// triangle 1, Back
|
||||||
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
|
k+=*m;
|
||||||
|
f(&d_surface[k + 1], i + 1, j, grid_size);
|
||||||
|
k+=*m;
|
||||||
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
|
k+=*m;
|
||||||
|
|
||||||
|
// triangle 2, Frontt
|
||||||
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
k+=*m;
|
k+=*m;
|
||||||
f(&d_surface[k + 1], i, j + 1, grid_size);
|
f(&d_surface[k + 1], i, j + 1, grid_size);
|
||||||
k+=*m;
|
k+=*m;
|
||||||
f(&d_surface[k + 1], i, j, grid_size);
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
k+=*m;
|
k+=*m;
|
||||||
|
|
||||||
|
// triangle 2, Back
|
||||||
|
f(&d_surface[k + 1], i, j, grid_size);
|
||||||
|
k+=*m;
|
||||||
|
f(&d_surface[k + 1], i, j + 1, grid_size);
|
||||||
|
k+=*m;
|
||||||
|
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
|
||||||
|
k+=*m;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user