187 lines
3.9 KiB
C
Executable File
187 lines
3.9 KiB
C
Executable File
#include "main.h"
|
|
#include "data/axis.h"
|
|
#include "data/shaders.h"
|
|
#include <stdlib.h>
|
|
#include <GL/glew.h>
|
|
#include <cglm/vec3.h>
|
|
#include <cglm/vec4.h>
|
|
|
|
#define WIDTH 512
|
|
#define HEIGHT 512
|
|
|
|
unsigned char coordanate[4] = {0,1,2,3};
|
|
|
|
unsigned char palette[] =
|
|
{
|
|
16,
|
|
0xEB,0xD3,0xF8,0xff,
|
|
0xEB,0xD4,0xF8,0xff,
|
|
0xEB,0xD5,0xF8,0xff,
|
|
0x7A,0x1C,0xAC,0xff,
|
|
};
|
|
|
|
void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n)
|
|
{
|
|
vec4 lado1, lado2, lado3;
|
|
vec4 u1, u2, u3;
|
|
|
|
switch (n)
|
|
{
|
|
case 3:
|
|
|
|
glm_vec3_sub(v2, v1, lado1);
|
|
glm_vec3_sub(v3, v1, lado2);
|
|
|
|
glm_vec3_cross(lado1, lado2, normal);
|
|
glm_vec3_normalize(normal);
|
|
return;
|
|
|
|
case 4:
|
|
|
|
glm_vec4_sub(v2, v1, lado1);
|
|
glm_vec4_sub(v3, v1, lado2);
|
|
glm_vec4_sub(v2, v1, lado3);
|
|
|
|
glm_vec4_copy(lado1, u1);
|
|
|
|
float alpha = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1);
|
|
vec4 proj_u1;
|
|
glm_vec4_scale(u1, alpha, proj_u1);
|
|
glm_vec4_sub(lado2, proj_u1, u2);
|
|
|
|
glm_vec4_normalize(u2);
|
|
|
|
float beta = glm_vec4_dot(lado3, u1) / glm_vec4_dot(u1, u1);
|
|
vec4 proj_u1_lado3;
|
|
glm_vec4_scale(u1, beta, proj_u1_lado3);
|
|
|
|
float gamma = glm_vec4_dot(lado3, u2) / glm_vec4_dot(u2, u2);
|
|
vec4 proj_u2;
|
|
glm_vec4_scale(u2, gamma, proj_u2);
|
|
|
|
glm_vec4_sub(lado3, proj_u1_lado3, u3);
|
|
glm_vec4_sub(u3, proj_u2, u3);
|
|
|
|
glm_vec4_normalize(u3);
|
|
|
|
glm_vec4_copy(u3, normal);
|
|
return;
|
|
}
|
|
}
|
|
|
|
float * fill_normal( float * d, unsigned char m )
|
|
{
|
|
float * n;
|
|
n = malloc( (*d+1)*sizeof(float));
|
|
*n = *d;
|
|
for (int i = 0; i < *d; i += 3*m)
|
|
{
|
|
|
|
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+m );
|
|
glm_vec3_copy( norm_vec, (n+1)+i+2*m );
|
|
}
|
|
return n;
|
|
}
|
|
|
|
const char * wname = "manigraph: manifold grapher";
|
|
float * generate_surface();
|
|
|
|
int main( void )
|
|
{
|
|
id_t shader, texture, shader_plain;
|
|
mesh_t m_cube, m_axis;
|
|
window_t window;
|
|
|
|
if( !( window = init_window( WIDTH, HEIGHT, wname ) ) )
|
|
goto error_window;
|
|
|
|
use_window( window );
|
|
set_clean_color_context( 0x2E, 0x07, 0x3F );
|
|
|
|
glewInit();
|
|
|
|
texture=create_palette_texture( palette );
|
|
use_texture( texture );
|
|
|
|
if( !( shader = create_shader() ) )
|
|
goto error_shader;
|
|
gload_program( shader, vs, VERTEX );
|
|
gload_program( shader, fs, FRAGMENT );
|
|
|
|
if( !( shader_plain = create_shader() ) )
|
|
goto error_shader_plain;
|
|
gload_program( shader_plain, vs, VERTEX );
|
|
gload_program( shader_plain, fs_plain, FRAGMENT );
|
|
|
|
load_fix_matrix( shader, (float)WIDTH/HEIGHT );
|
|
load_fix_matrix( shader_plain, (float)WIDTH/HEIGHT );
|
|
|
|
/* Fill m_cube */
|
|
{
|
|
float * n_cube, *d_cube;
|
|
d_cube = generate_surface(16);
|
|
n_cube = fill_normal( d_cube, 4 );
|
|
if( !( m_cube = create_mesh( d_cube, n_cube, coordanate, 4 ) ) )
|
|
goto error_mesh_cube;
|
|
free( n_cube );
|
|
free( d_cube);
|
|
}
|
|
|
|
/* Fill m_axis */
|
|
{
|
|
float * n_axis;
|
|
n_axis = fill_normal( d_axis, 3 );
|
|
if( !( m_axis = create_mesh( d_axis, n_axis, coordanate, 3 ) ) )
|
|
goto error_mesh_axis;
|
|
free( n_axis );
|
|
}
|
|
|
|
while( is_open_window( window ) )
|
|
{
|
|
quat_t q;
|
|
|
|
q=poll_input( window );
|
|
load_rot_matrix( shader, q );
|
|
load_rot_matrix( shader_plain, q );
|
|
|
|
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_cube );
|
|
|
|
}
|
|
|
|
destroy_mesh( m_axis );
|
|
destroy_mesh( m_cube );
|
|
destroy_shader( shader_plain );
|
|
destroy_shader( shader );
|
|
destroy_texture( texture );
|
|
close_window( window );
|
|
return 0;
|
|
|
|
error_mesh_axis:
|
|
destroy_mesh( m_cube );
|
|
error_mesh_cube:
|
|
destroy_shader( shader_plain );
|
|
error_shader_plain:
|
|
destroy_shader( shader );
|
|
error_shader:
|
|
close_window( window );
|
|
destroy_texture( texture );
|
|
error_window:
|
|
return 1;
|
|
}
|