Document code, and clean up

This commit is contained in:
PedroEdiaz
2024-10-21 16:50:49 -06:00
parent 6e54aa9fea
commit 17e1cf8804
13 changed files with 245 additions and 193 deletions

View File

@@ -1,10 +1,8 @@
#include "main.h"
#include "data/axis.h"
#include "data/shaders.h"
#include <stdlib.h>
#include <GL/glew.h>
#include <stdlib.h>
#include <stdlib.h>
#include <cglm/vec3.h>
#define WIDTH 512
@@ -14,7 +12,6 @@ unsigned char coordanate[4] = {0,1,2,3};
unsigned char palette[] =
{
16,
0xEB,0xD3,0xF8,0xff,
0xEB,0xD4,0xF8,0xff,
0xEB,0xD5,0xF8,0xff,
@@ -35,8 +32,10 @@ void calc_normal(float* v1, float* v2, float* v3, float* normal)
float * fill_normal( float * d )
{
float * n;
n = malloc( (*d+1)*sizeof(float));
n = malloc((*d+1)*sizeof(float));
*n = *d;
for (int i = 0; i < *d; i += 9)
{
@@ -50,99 +49,116 @@ float * fill_normal( float * d )
}
const char * wname = "manigraph: manifold grapher";
float * generate_surface();
float * generate_surface(unsigned int, unsigned char *m);
int main( void )
{
id_t shader, texture, shader_plain;
mesh_t m_cube, m_axis;
mesh_t m_surface, 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 */
/* Setup window */
{
float * n_cube, *d_cube;
d_cube = generate_surface(16);
n_cube = fill_normal( d_cube );
if( !( m_cube = create_mesh( d_cube, n_cube, coordanate ) ) )
goto error_mesh_cube;
free( n_cube );
free( d_cube);
if(!(window = init_window(WIDTH, HEIGHT, wname)))
goto error_window;
use_window(window);
set_clean_color_context(0x2E, 0x07, 0x3F);
}
/* Fill m_axis */
/* Setup context */
{
if(!(init_context()))
goto error_context;
}
/* Setup color texture */
{
texture=create_palette_texture(palette, 4);
use_texture(texture);
}
/* Setup shader */
{
if(!(shader = create_shader()))
goto error_shader;
load_program_to_shader(shader, vs, VERTEX);
load_program_to_shader(shader, fs, FRAGMENT);
load_fix_matrix(shader, (float)WIDTH/HEIGHT);
}
/* Setup shader w/out illumination */
{
if(!(shader_plain = create_shader()))
goto error_shader_plain;
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);
}
/* Fill mesh of surface */
{
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)))
goto error_mesh_surface;
free(n_surface);
free(d_surface);
}
/* Fill mesh of axis */
{
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);
if(!(m_axis = create_mesh(d_axis, n_axis, coordanate)))
goto error_mesh_axis;
free( n_axis );
free(n_axis);
}
while( is_open_window( window ) )
while(is_open_window(window))
{
quat_t q;
q=poll_input( window );
load_rot_matrix( shader, q );
load_rot_matrix( shader_plain, 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 );
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);
load_mdl_matrix(shader, 0, 3);
#else
load_mdl_matrix(shader_plain, 0, 3);
#endif
load_mdl_matrix( shader, 0, 3 );
draw_mesh( m_cube );
draw_mesh(m_surface);
}
destroy_mesh( m_axis );
destroy_mesh( m_cube );
destroy_shader( shader_plain );
destroy_shader( shader );
destroy_texture( texture );
close_window( window );
destroy_mesh(m_axis);
destroy_mesh(m_surface);
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 );
destroy_mesh(m_surface);
error_mesh_surface:
destroy_shader(shader_plain);
error_shader_plain:
destroy_shader( shader );
destroy_shader(shader);
error_shader:
close_window( window );
destroy_texture( texture );
close_window(window);
destroy_texture(texture);
error_context:
error_window:
return 1;
}