Merge: roberto -> main

This commit is contained in:
PedroEdiaz
2024-10-17 22:30:55 -06:00
parent 252aab3dd2
commit 7c1f691b46
9 changed files with 133 additions and 59 deletions

View File

@@ -8,13 +8,23 @@ OBJ = \
src/shader.o \ src/shader.o \
src/input.o \ src/input.o \
src/mesh.o \ src/mesh.o \
src/alan.o \
src/main.o src/main.o
CFLAGS = \ CFLAGS = \
-I./ext/cglm/include \ -I./ext/cglm/include \
-I./ext/glfw/include \ -I./ext/glfw/include \
-Wall -Wno-unused-function -std=c99 \ -Wall -Wno-unused-function -std=c99 \
WAYLAND-LIB = \
xdg-shell \
relative-pointer-unstable-v1 \
xdg-decoration-unstable-v1 \
pointer-constraints-unstable-v1 \
viewporter \
idle-inhibit-unstable-v1 \
fractional-scale-v1 \
xdg-activation-v1 \
wayland
help: help:
@echo "Para compilar el proyecto a tu sistema operativo" @echo "Para compilar el proyecto a tu sistema operativo"
@@ -28,6 +38,8 @@ help:
@echo "Para ejecturar el programa sin instalarlos se puede usar:" @echo "Para ejecturar el programa sin instalarlos se puede usar:"
@echo " $(MAKE) run-linux" @echo " $(MAKE) run-linux"
src/main.o: src/data/axis.h src/data/cube.h src/data/shaders.h
# WINDOWS # WINDOWS
windows: $(OBJ) glfw.dll windows: $(OBJ) glfw.dll
$(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32 -lglew32 $(CC) $(CFLAGS) $(OBJ) -o $(BIN) -L. -lglfw -lopengl32 -lglew32
@@ -38,11 +50,11 @@ glfw.dll:
# LINUX # LINUX
linux-x11: $(OBJ) linux-x11: $(OBJ)
$(MAKE) BKN=_GLFW_X11 libglfw.so $(MAKE) BKN=_GLFW_X11 libglfw.so
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
linux-wayland: $(OBJ) linux-wayland: $(OBJ)
$(MAKE) BKN=_GLFW_WAYLAND libglfw.so $(MAKE) BKN=_GLFW_WAYLAND libglfw.so
$(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw $(CC) -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw -lm
run-linux: run-linux:
LD_LIBRARY_PATH=. ./$(BIN) LD_LIBRARY_PATH=. ./$(BIN)
@@ -50,15 +62,20 @@ run-linux:
# COCOA # COCOA
cocoa: $(OBJ) cocoa: $(OBJ)
$(MAKE) BKN=_GLFW_COCOA libglfw.so $(MAKE) BKN=_GLFW_COCOA libglfw.so
$(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw $(CC) -framework OpenGL -o $(BIN) $(OBJ) -L. -lGLEW -lGL -lglfw
libglfw.so: libglfw.so:
$(CC) -fPIC -shared -D$(BKN) -D_GLFW_BUILD_DLL ./ext/glfw/src/*.c -o $@ for i in $(WAYLAND-LIB); \
do \
wayland-scanner client-header ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol.h; \
wayland-scanner private-code ext/glfw/deps/wayland/$$i.xml ext/glfw/deps/wayland/$$i-client-protocol-code.h; \
done
$(CC) -fPIC -shared -D$(BKN) -D_GLFW_BUILD_DLL -Iext/glfw/deps/wayland ./ext/glfw/src/*.c -o $@
clean: clean:
rm $(OBJ) $(BIN) rm $(OBJ) $(BIN) ext/glfw/deps/wayland/*.h
.SUFFIXES: .c .o .SUFFIXES: .c .o
.c.o: .c.o:
$(CC) $(CFLAGS) -c -o $@ $< $(CC) $(CFLAGS) -c -o $@ $<

View File

@@ -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 );
} }

View File

@@ -1,45 +1,39 @@
#undef A #undef A
#undef B #undef B
#undef C #undef C
#undef D #undef D
#undef E #undef E
#undef F #undef F
#undef G #undef G
#undef H #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,
#define D -2.0, 0.05, 0.05,
#define E 2.0,-0.05,-0.05,
#define F 2.0,-0.05, 0.05,
#define G 2.0, 0.05,-0.05,
#define H 2.0, 0.05, 0.05,
const float X = 2.0; float d_axis[] =
const float Y = 0.05;
const float Z = 0.05;
#define A -X,-Y,-Z,
#define B -X,-Y, Z,
#define C -X, Y,-Z,
#define D -X, Y, Z,
#define E X,-Y,-Z,
#define F X,-Y, Z,
#define G X, Y,-Z,
#define H X, Y, Z,
narray_float_t d_axis =
{ {
3*3*2*6, 3*3*2*6,
A C E A C E
C E G G E C
E G F E G F
G F H H F G
F H B F H B
H B D D B H
B D A B D A
D A C C A D
C D G C D G
D G H H G D
A B E E B A
B E F B E F
}; };

View File

@@ -7,26 +7,25 @@
#define G 1, 1,-1, #define G 1, 1,-1,
#define H 1, 1, 1, #define H 1, 1, 1,
narray_float_t d_cube = float d_cube[] =
{ {
3*3*2*6, 3*3*2*6,
A C E A C E
C E G G E C
E G F E G F
G F H H F G
F H B F H B
H B D D B H
B D A B D A
D A C C A D
C D G C D G
D G H H G D
A B E E B A
B E F B E F
}; };

View File

@@ -2,6 +2,7 @@ const char * vs =
"#version 330 core\n" "#version 330 core\n"
"layout (location = 0) in vec3 aPos;" "layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec3 aNormal;"
"uniform float idx;" "uniform float idx;"
"uniform mat4 fix;" "uniform mat4 fix;"
@@ -9,13 +10,19 @@ const char * vs =
"uniform mat4 mdl;" "uniform mat4 mdl;"
"out float index;" "out float index;"
"out vec3 Normal;"
"out vec3 FragPos;"
"void main()" "void main()"
"{" "{"
" index=idx;" " index=idx;"
" Normal = mat3(transpose(inverse(rot*mdl))) * aNormal;"
" gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n" " gl_Position = fix * rot * mdl * vec4( aPos, 1.0 );\n"
" FragPos = vec3( rot * mdl * vec4(aPos, 1.0));"
"}"; "}";
const char * fs = const char * fs =
"#version 330 core\n" "#version 330 core\n"
@@ -23,8 +30,19 @@ const char * fs =
"in float index;" "in float index;"
"out vec4 FragColor;" "out vec4 FragColor;"
"in vec3 Normal;"
"in vec3 FragPos;"
"void main()" "void main()"
"{" "{"
" FragColor = texture( palette, vec3( 0, 0, index ) ).rgba;"
" vec4 color = texture( palette, vec3( 0, 0, index ) ).rgba;"
" vec3 lightPos = vec3(0,0,-15);"
" vec3 lightDir = normalize(lightPos - FragPos);"
" float diffuse = max(dot(Normal, lightDir), 0.0);"
" float ambient = 0.5;"
" FragColor = (ambient + diffuse)*color;"
"}"; "}";

45
src/main.c Normal file → Executable file
View File

@@ -3,7 +3,9 @@
#include "data/axis.h" #include "data/axis.h"
#include "data/shaders.h" #include "data/shaders.h"
#include <GL/glew.h> #include <GL/glew.h>
#include <stdlib.h>
#include <cglm/vec3.h>
#define WIDTH 512 #define WIDTH 512
#define HEIGHT 512 #define HEIGHT 512
@@ -16,10 +18,39 @@ unsigned char palette[] =
0x7A,0x1C,0xAC,0xff, 0x7A,0x1C,0xAC,0xff,
}; };
void calc_normal(float* v1, float* v2, float* v3, float* normal)
{
vec3 lado1, lado2;
glm_vec3_sub(v2, v1, lado1);
glm_vec3_sub(v3, v1, lado2);
glm_vec3_cross(lado1, lado2, normal);
glm_vec3_normalize(normal);
}
float * fill_normal( float * d )
{
float * n;
n = malloc( (*d+1)*sizeof(float));
*n = *d;
for (int i = 0; i < *d; i += 9)
{
vec3 norm_vec;
calc_normal((d+1)+i, (d+1)+i+3, (d+1)+i+6, norm_vec);
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 );
}
return n;
}
const char * wname = "manigraph: manifold grapher"; const char * wname = "manigraph: manifold grapher";
int main( void ) int main( void )
{ {
float * n_cube, *n_axis;
id_t shader, texture; id_t shader, texture;
mesh_t m_cube, m_axis; mesh_t m_cube, m_axis;
window_t window; window_t window;
@@ -37,16 +68,22 @@ int main( void )
gload_program( shader, vs, VERTEX ); gload_program( shader, vs, VERTEX );
gload_program( shader, fs, FRAGMENT ); gload_program( shader, fs, FRAGMENT );
use_shader( shader ); use_shader( shader );
load_fix_matrix( shader, (float)WIDTH/HEIGHT ); load_fix_matrix( shader, (float)WIDTH/HEIGHT );
if( !( m_cube = create_mesh( d_cube ) ) ) n_cube = fill_normal( d_cube );
n_axis = fill_normal( d_axis );
if( !( m_cube = create_mesh( d_cube, n_cube ) ) )
goto error_mesh_cube; goto error_mesh_cube;
if( !( m_axis = create_mesh( d_axis ) ) ) if( !( m_axis = create_mesh( d_axis, n_axis ) ) )
goto error_mesh_axis; goto error_mesh_axis;
free( n_cube );
free( n_axis );
texture=create_palette_texture( palette ); texture=create_palette_texture( palette );
use_texture( texture ); use_texture( texture );

View File

@@ -7,7 +7,6 @@ typedef void * mesh_t;
typedef float * quat_t; typedef float * quat_t;
typedef float * mat4_t; typedef float * mat4_t;
typedef float narray_float_t[];
typedef unsigned char narray_u8_t[]; typedef unsigned char narray_u8_t[];
enum enum
@@ -23,7 +22,7 @@ int is_open_window(window_t window);
void close_window(window_t window); void close_window(window_t window);
mesh_t create_mesh(narray_float_t mesh); mesh_t create_mesh( float * d, float * n);
void destroy_mesh(mesh_t p); void destroy_mesh(mesh_t p);

View File

@@ -13,14 +13,14 @@ mat4 ortho[] =
}, },
{ {
{ 0, 1, 0, 0 }, { 0, 1, 0, 0 },
{ 1, 0, 0, 0 }, {-1, 0, 0, 0 },
{ 0, 0, 1, 0 }, { 0, 0, 1, 0 },
{ 0, 0, 0, 1 }, { 0, 0, 0, 1 },
}, },
{ {
{ 0, 0, 1, 0 }, { 0, 0, 1, 0 },
{ 0, 1, 0, 0 }, { 0, 1, 0, 0 },
{ 1, 0, 0, 0 }, {-1, 0, 0, 0 },
{ 0, 0, 0, 1 }, { 0, 0, 0, 1 },
}, },
}; };

View File

@@ -4,28 +4,34 @@
struct obj struct obj
{ {
unsigned int vertex, vao, vbo; unsigned int vertex, vao, n_vbo, d_vbo;
}; };
mesh_t create_mesh( narray_float_t mesh ) mesh_t create_mesh( float * d, float * n )
{ {
struct obj * p; struct obj * p;
p=malloc(sizeof(struct obj)); p=malloc(sizeof(struct obj));
p->vertex=(*mesh)/3; p->vertex=(*d)/3;
glGenVertexArrays( 1, &p->vao ); glGenVertexArrays( 1, &p->vao );
glGenBuffers( 1, &p->vbo );
glGenBuffers( 1, &p->d_vbo );
glBindVertexArray( p->vao ); glBindVertexArray( p->vao );
glBindBuffer( GL_ARRAY_BUFFER, p->vbo ); glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo );
glBufferData( GL_ARRAY_BUFFER, (p->vertex*3)*sizeof(int), mesh+1, glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), d+1,
GL_STATIC_DRAW ); GL_STATIC_DRAW );
glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(float), NULL );
glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(int), NULL );
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glGenBuffers( 1, &p->n_vbo );
glBindBuffer( GL_ARRAY_BUFFER, p->n_vbo );
glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), n+1,
GL_STATIC_DRAW );
glVertexAttribPointer( 1,3,GL_FLOAT, 0, 3*sizeof(float), NULL );
glEnableVertexAttribArray(1);
return p; return p;
} }
@@ -34,7 +40,8 @@ void destroy_mesh( mesh_t p )
struct obj * obj ; struct obj * obj ;
obj = p; obj = p;
glDeleteVertexArrays( 1, &obj->vao ); glDeleteVertexArrays( 1, &obj->vao );
glDeleteBuffers( 1, &obj->vbo ); glDeleteBuffers( 1, &obj->d_vbo );
glDeleteBuffers( 1, &obj->n_vbo );
free( p ); free( p );
} }