Change projection w/keyboard

This commit is contained in:
PedroEdiaz
2024-10-22 23:07:30 -06:00
parent 6727c5fa9a
commit 2a5a101080
7 changed files with 140 additions and 48 deletions

View File

@@ -4,9 +4,6 @@
void set_clean_color_context( unsigned char r, unsigned char g, unsigned char b )
{
glEnable( GL_DEPTH_TEST );
glEnable( GL_CULL_FACE );
glCullFace( GL_BACK );
glClearColor( (float)r/0xff, (float)g/0xff, (float)b/0xff, 1.0 );
}

View File

@@ -5,6 +5,7 @@ const char * vs =
"layout (location = 1) in float aPos_y;"
"layout (location = 2) in float aPos_z;"
"layout (location = 3) in float aPos_w;"
"layout (location = 4) in float aNormal_x;"
"layout (location = 5) in float aNormal_y;"
"layout (location = 6) in float aNormal_z;"
@@ -63,7 +64,7 @@ const char * fs =
" vec3 lightPos = vec3(0,0,-15);"
" vec3 lightDir = normalize(lightPos - FragPos);"
" float diffuse = max(dot(Normal, lightDir),0.0); "
" float diffuse = abs(dot(Normal, lightDir)); "
" FragColor = (0.5 + 0.5*diffuse)*color;"
"}";

View File

@@ -18,15 +18,60 @@ vec3 axis[3] =
{0, 0, 1},
};
extern struct projection projection;
void __key_callback_input(GLFWwindow * window, int key, int scancode, int action, int mods )
{
if( action != GLFW_PRESS)
return;
if( GLFW_KEY_0 <= key && key <= GLFW_KEY_9 )
if( GLFW_KEY_0<key && key<=GLFW_KEY_9 )
{
printf("%d\n", key-GLFW_KEY_0 );
unsigned char selected_coord;
selected_coord = key-GLFW_KEY_0-1;
if( selected_coord >= projection.m )
return;
if( selected_coord == projection.x )
return;
if( selected_coord == projection.y )
return;
if( selected_coord == projection.z )
return;
selected_coord = projection.w;
}
if( projection.w >= projection.m )
return;
switch( key )
{
unsigned char tmp;
case GLFW_KEY_P:
tmp=projection.w;
projection.w=projection.x;
projection.x=tmp;
break;
case GLFW_KEY_O:
tmp=projection.w;
projection.w=projection.y;
projection.y=tmp;
break;
case GLFW_KEY_I:
tmp=projection.w;
projection.w=projection.z;
projection.z=tmp;
break;
}
set_projection_mesh( projection );
return;
}
void __window_callback_input(GLFWwindow * window, int w, int h)

View File

@@ -5,10 +5,12 @@
#include <stdlib.h>
#include <stdio.h>
#define WIDTH 512
#define HEIGHT 512
float * generate_data_surface(unsigned int, unsigned char *);
float * generate_normals_surface(float *, unsigned char);
unsigned char palette[] =
{
0xEB,0xD3,0xF8,0xff,
@@ -17,12 +19,15 @@ unsigned char palette[] =
0x7A,0x1C,0xAC,0xff,
};
unsigned char coordanate[4] = {0,1,2,3};
const char * wname = "manigraph: manifold grapher";
float * generate_data_surface(unsigned int, unsigned char *);
float * generate_normals_surface(float *, unsigned char);
struct projection projection =
{
.x=0,
.y=1,
.z=2,
.w=3,
};
void mlog( char * msg )
{
@@ -94,18 +99,25 @@ int main( void )
float * n_surface, *d_surface;
d_surface = generate_data_surface(16,&m);
n_surface = generate_normals_surface(d_surface, m);
if( !(m_surface = create_mesh(d_surface, n_surface, coordanate, m)))
projection.m=m;
if( !(m_surface = create_mesh(d_surface, n_surface, m)))
{
mlog("[MESH] Error al inicializar...\n");
goto error_mesh_surface;
}
projection.mesh=m_surface;
free(n_surface);
free(d_surface);
}
mlog("[MESH] Inicializando...\n");
{
if(!(m_axis = create_mesh(d_axis, NULL, coordanate, 3)))
if(!(m_axis = create_mesh(d_axis, NULL, 3)))
{
mlog("[MESH] Error al inicializar...\n");
goto error_mesh_axis;

View File

@@ -12,6 +12,22 @@ typedef void * mesh_t;
typedef float * quat_t;
typedef float * mat4_t;
/*
This struct represent the proyection, where:
mesh: data of surface.
m: the dimention of the surface.
x: the coordanate of the x axis.
y: the coordanate of the y axis.
z: the coordanate of the z axis.
w: the coordanate of the w axis.
*/
struct projection
{
mesh_t mesh;
unsigned char m, x, y, z, w;
};
/*
Init window:
w: default width;
@@ -31,11 +47,12 @@ void close_window(window_t window);
Create mesh:
d: array of floats with the vertex data.
n: array of floats with the normal data.
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, unsigned char m );
mesh_t create_mesh( float * d, float * n, unsigned char m );
void set_projection_mesh( struct projection );
void destroy_mesh(mesh_t p);

View File

@@ -1,19 +1,49 @@
#include "main.h"
#include <GL/glew.h>
#include <stdlib.h>
#include <stdio.h>
struct obj
{
unsigned int vertex, vao, n_vbo, d_vbo;
};
void set_projection_mesh( struct projection projection )
{
struct obj * p;
p=projection.mesh;
glBindVertexArray( p->vao );
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
glVertexAttribPointer( 0,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.x*sizeof(float)) );
glVertexAttribPointer( 1,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.y*sizeof(float)) );
glVertexAttribPointer( 2,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.z*sizeof(float)) );
glVertexAttribPointer( 3,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.w*sizeof(float)) );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
glVertexAttribPointer( 4,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.x*sizeof(float)) );
glVertexAttribPointer( 5,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.y*sizeof(float)) );
glVertexAttribPointer( 6,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.z*sizeof(float)) );
glVertexAttribPointer( 7,1,GL_FLOAT, 0, projection.m*sizeof(float),
(float*)(projection.w*sizeof(float)) );
}
/*
In this function we load all the vertex and normal datas on two
diferents buffers, so we can access the coordanates that we want
to display using the layout location in GLSL.
This trick can be done with glVertexAttribPointer.
*/
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m )
mesh_t create_mesh( float * d, float * n, unsigned char m )
{
unsigned char i;
struct obj * p;
@@ -23,35 +53,41 @@ mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned c
p->vertex=(*d)/m;
glGenVertexArrays( 1, &p->vao );
glBindVertexArray( p->vao );
glGenBuffers( 1, &p->d_vbo );
glBindVertexArray( p->vao );
glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), d+1,
GL_STATIC_DRAW );
for( i=0; i<4; ++i )
if( n )
{
glVertexAttribPointer( i,1,GL_FLOAT, 0, m*sizeof(float),
(float*)(coordanate[i]*sizeof(float)) );
glEnableVertexAttribArray(i);
glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
GL_STATIC_DRAW );
}
if( !n )
return p;
glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*m*sizeof(float), n+1,
GL_STATIC_DRAW );
for( i=0; i<4; ++i )
{
glVertexAttribPointer( i+4,1,GL_FLOAT, 0, m*sizeof(float),
(float*)(coordanate[i]*sizeof(float)) );
glEnableVertexAttribArray(i);
glEnableVertexAttribArray(i+4);
}
{
struct projection projection =
{
.x=0,
.y=1,
.z=2,
.w=3,
};
projection.m=m;
projection.mesh=p;
set_projection_mesh( projection );
}
return p;
}

View File

@@ -53,7 +53,7 @@ float * generate_data_surface(int grid_size, unsigned char *m )
f = klein;
*m = 4;
size = grid_size*grid_size*12*(*m);
size = grid_size*grid_size*6*(*m);
d_surface = malloc((size+1)*sizeof(float));
d_surface[0] = size;
@@ -70,22 +70,6 @@ float * generate_data_surface(int grid_size, unsigned char *m )
f(&d_surface[k + 1], i + 1, j + 1, grid_size);
k+=*m;
// 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);
k+=*m;
f(&d_surface[k + 1], i, j + 1, grid_size);
k+=*m;
f(&d_surface[k + 1], i, j, grid_size);
k+=*m;
// triangle 2, Back
f(&d_surface[k + 1], i, j, grid_size);
k+=*m;