Setup for test on R4

This commit is contained in:
PedroEdiaz
2024-10-18 20:26:53 -06:00
parent b164d15927
commit fe3b99d480
10 changed files with 227 additions and 58 deletions

View File

@@ -6,6 +6,7 @@
#undef F
#undef G
#undef H
#define A -2.0,-0.05,-0.05,
#define B -2.0,-0.05, 0.05,
#define C -2.0, 0.05,-0.05,

View File

@@ -1,8 +1,14 @@
const char * vs =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec3 aNormal;"
"layout (location = 0) in float aPos_x;"
"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;"
"layout (location = 7) in float aNormal_w;"
"uniform float idx;"
"uniform mat4 fix;"
@@ -16,12 +22,30 @@ const char * vs =
"void main()"
"{"
" index=idx;"
" vec3 aNormal = vec3(aNormal_x,aNormal_y,aNormal_z);"
" vec3 aPos = vec3(aPos_x,aPos_y,aPos_z);"
" Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;"
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
" FragPos = vec3( rot * mdl * vec4(aPos, 1.0));"
"}";
const char * fs_plain =
"#version 330 core\n"
"uniform sampler2DArray palette;"
"in float index;"
"out vec4 FragColor;"
"in vec3 Normal;"
"in vec3 FragPos;"
"void main()"
"{"
" FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;"
"}";
const char * fs =
"#version 330 core\n"

View File

@@ -1,6 +1,7 @@
#include "main.h"
#include <cglm/quat.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#define ANGLE ((float)0x01/0xff*2*GLM_PI)
@@ -17,6 +18,17 @@ vec3 axis[3] =
{0, 0, 1},
};
void __key_callback(GLFWwindow * window, int key, int scancode, int action, int mods )
{
if( action != GLFW_PRESS)
return;
if( GLFW_KEY_0 <= key && key <= GLFW_KEY_9 )
{
printf("%d\n", key-GLFW_KEY_0 );
}
}
void __window_callback(GLFWwindow * window, int w, int h)
{
int m;
@@ -37,8 +49,8 @@ void __mouse_callback(GLFWwindow* window, int button, int action, int mods)
return;
glfwGetCursorPos(window, &xpos, &ypos);
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN, GL_UNSIGNED_BYTE, &green_value);
glReadPixels((int)xpos, (int)(window_height - ypos), 1, 1, GL_GREEN,
GL_UNSIGNED_BYTE, &green_value);
switch(green_value)
{

View File

@@ -1,14 +1,16 @@
#include "main.h"
#include "data/cube.h"
#include "data/axis.h"
#include "data/shaders.h"
#include <GL/glew.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,
@@ -20,8 +22,9 @@ unsigned char palette[] =
void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n)
{
float alpha;
vec4 lado1, lado2;
vec4 u1, u2;
vec4 u1, u2, proy;
switch (n)
{
@@ -35,21 +38,20 @@ void calc_normal(float* v1, float* v2, float* v3,float* normal, unsigned char n)
return;
case 4:
#if 0
glm_vec4_sub(v2, v1, lado1);
glm_vec4_sub(v3, v1, lado2);
glm_vec4_copy(lado1, u1);
float alfa = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1);
vec4 proy;
glm_vec4_scale(u1, alfa, proy);
alpha = glm_vec4_dot(lado2, u1) / glm_vec4_dot(u1, u1);
glm_vec4_scale(u1, alpha, proy);
glm_vec4_sub(lado2, proy, u2);
glm_vec4_normalize(u2);
glm_vec4_copy(u2, normal);
default:
break;
#endif
return;
}
}
@@ -58,24 +60,25 @@ 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 += 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, 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+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();
int main( void )
{
float * n_cube, *n_axis;
id_t shader, texture;
id_t shader, texture, shader_plain;
mesh_t m_cube, m_axis;
window_t window;
@@ -87,63 +90,83 @@ int main( void )
glewInit();
if( !( shader = create_shader() ) )
goto error_shader;
gload_program( shader, vs, VERTEX );
gload_program( shader, fs, FRAGMENT );
use_shader( shader );
load_fix_matrix( shader, (float)WIDTH/HEIGHT );
n_cube = fill_normal( d_cube, 3 );
n_axis = fill_normal( d_axis, 3 );
if( !( m_cube = create_mesh( d_cube, n_cube ) ) )
goto error_mesh_cube;
if( !( m_axis = create_mesh( d_axis, n_axis ) ) )
goto error_mesh_axis;
free( n_cube );
free( n_axis );
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, 0, 0 );
load_mdl_matrix( shader_plain, 0, 0 );
draw_mesh( m_axis );
load_mdl_matrix( shader, 1, 1 );
load_mdl_matrix( shader_plain, 1, 1 );
draw_mesh( m_axis );
load_mdl_matrix( shader, 2, 2 );
load_mdl_matrix( shader_plain, 2, 2 );
draw_mesh( m_axis );
#endif
load_mdl_matrix( shader, 0, 3 );
draw_mesh( m_cube );
}
destroy_texture( texture );
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;
}

View File

@@ -22,7 +22,7 @@ int is_open_window(window_t window);
void close_window(window_t window);
mesh_t create_mesh( float * d, float * n);
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m );
void destroy_mesh(mesh_t p);

View File

@@ -7,30 +7,43 @@ struct obj
unsigned int vertex, vao, n_vbo, d_vbo;
};
mesh_t create_mesh( float * d, float * n )
mesh_t create_mesh( float * d, float * n, unsigned char * coordanate, unsigned char m )
{
struct obj * p;
p=malloc(sizeof(struct obj));
p->vertex=(*d)/3;
p->vertex=(*d)/m;
glGenVertexArrays( 1, &p->vao );
glGenBuffers( 1, &p->d_vbo );
glBindVertexArray( p->vao );
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 );
glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(float), NULL );
glVertexAttribPointer( 0,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
glEnableVertexAttribArray(0);
glVertexAttribPointer( 1,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) );
glEnableVertexAttribArray(1);
glVertexAttribPointer( 2,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) );
glEnableVertexAttribArray(2);
glVertexAttribPointer( 3,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) );
glEnableVertexAttribArray(3);
glGenBuffers( 1, &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 );
glVertexAttribPointer( 1,3,GL_FLOAT, 0, 3*sizeof(float), NULL );
glEnableVertexAttribArray(1);
glVertexAttribPointer( 4,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[0]*sizeof(float)) );
glEnableVertexAttribArray(4);
glVertexAttribPointer( 5,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[1]*sizeof(float)) );
glEnableVertexAttribArray(5);
glVertexAttribPointer( 6,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[2]*sizeof(float)) );
glEnableVertexAttribArray(6);
glVertexAttribPointer( 7,1,GL_FLOAT, 0, m*sizeof(float), (float*)(coordanate[3]*sizeof(float)) );
glEnableVertexAttribArray(7);
return p;
}

View File

@@ -37,8 +37,8 @@ unsigned char gload_program( unsigned int program, const char * src,
shader = glCreateShader(type[i]);
glShaderSource( shader, 1, (const GLchar **)&src, ((void*)0 ));
glCompileShader(shader);
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
glGetShaderiv( shader, GL_COMPILE_STATUS, &status );
if( !status )
{

93
src/surface.c Normal file
View File

@@ -0,0 +1,93 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
typedef void(*function_t)(float*,int, int, int);
void mobius(float *d_surface, int i, int j, int grid_size)
{
const float width = 0.5;
float u = (2*M_PI) * ((float)i/grid_size );
float v = (2*width) * ((float)j/grid_size ) - width;
d_surface[0] = cos(u) + v * cos(u / 2) * cos(u);
d_surface[1] = sin(u) + v * cos(u / 2) * sin(u);
d_surface[2] = v * sin(u / 2);
}
void toro(float *d_surface, int i, int j, int grid_size)
{
float u = (2*M_PI) * ((float)i/grid_size );
float v = (2*M_PI) * ((float)j/grid_size );
d_surface[0] = (1 + 0.5*cos(v))*cos(u);
d_surface[1] = (1 + 0.5*cos(v))*sin(u);
d_surface[2] = 0.5*sin(v);
}
void klein(float *d_surface, int i, int j, int grid_size)
{
float u = (2*M_PI) * ((float)i/grid_size );
float v = (2*M_PI) * ((float)j/grid_size );
d_surface[0] = (0.5*cos(v) + 0.5) * cos(u);
d_surface[1] = (0.5*cos(v) + 0.5) * sin(u);
d_surface[2] = sin(v) * cos(u/2);
d_surface[3] = sin(v)*sin(u/2);
}
float * generate_surface(int grid_size)
{
unsigned char m = 4;
long size = grid_size*grid_size*2*9*m;
function_t f = klein;
float * d_surface;
int k=0;
d_surface = malloc((size+1)*sizeof(float));
d_surface[0] = size;
for (int i = 0; i < grid_size; i++)
{
for (int j = 0; j < grid_size; j++)
{
// triangle 1, front
f(&d_surface[k + 1], i, j, grid_size);
k+=m;
f(&d_surface[k + 1], i + 1, j, grid_size);
k+=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, font
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;
// triangle 2, back
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;
}
}
return d_surface;
}

View File

@@ -4,12 +4,11 @@
void __window_callback(GLFWwindow *, int, int);
void __mouse_callback(GLFWwindow *, int, int, int);
void __scroll_callback(GLFWwindow *, double, double);
void __key_callback(GLFWwindow *, int, int, int, int);
window_t init_window(unsigned int w, unsigned int h, const char * name)
{
void * window;
void __mouse_callback(GLFWwindow *window, int button, int action, int mods);
void __scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
if( !glfwInit() )
return NULL;
@@ -29,6 +28,7 @@ window_t init_window(unsigned int w, unsigned int h, const char * name)
glfwSetWindowSizeCallback(window, __window_callback);
glfwSetMouseButtonCallback((GLFWwindow*)window, __mouse_callback);
glfwSetScrollCallback((GLFWwindow*)window, __scroll_callback);
glfwSetKeyCallback((GLFWwindow*)window, __key_callback);
__window_callback( window, w, h );