diff --git a/Makefile b/Makefile index 6dae268..1c0ed9c 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ OBJ = \ CFLAGS = \ -I./ext/cglm/include \ -I./ext/glfw/include \ - -Wall -Wno-unused-function -std=c99 \ + -Wall -Wno-unused-function -std=c99 \ WAYLAND-LIB = \ xdg-shell \ diff --git a/src/data/shaders.h b/src/data/shaders.h index 40b5ef4..8d8da0c 100644 --- a/src/data/shaders.h +++ b/src/data/shaders.h @@ -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,6 +22,8 @@ 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));" diff --git a/src/input.c b/src/input.c index b761aa2..5b80e03 100644 --- a/src/input.c +++ b/src/input.c @@ -1,6 +1,7 @@ #include "main.h" #include #include +#include #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) { diff --git a/src/main.c b/src/main.c index 297318d..93599f8 100755 --- a/src/main.c +++ b/src/main.c @@ -9,6 +9,8 @@ #define WIDTH 512 #define HEIGHT 512 +unsigned char coordanate[4] = {0,1,2,3}; + unsigned char palette[] = { 16, @@ -82,7 +84,7 @@ int main( void ) { float * n_cube; n_cube = fill_normal( d_cube ); - if( !( m_cube = create_mesh( d_cube, n_cube ) ) ) + if( !( m_cube = create_mesh( d_cube, n_cube, coordanate ) ) ) goto error_mesh_cube; free( n_cube ); } @@ -91,7 +93,7 @@ int main( void ) { float * n_axis; n_axis = fill_normal( d_axis ); - if( !( m_axis = create_mesh( d_axis, n_axis ) ) ) + if( !( m_axis = create_mesh( d_axis, n_axis, coordanate ) ) ) goto error_mesh_axis; free( n_axis ); } diff --git a/src/main.h b/src/main.h index 71057cf..155af45 100644 --- a/src/main.h +++ b/src/main.h @@ -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 ); void destroy_mesh(mesh_t p); diff --git a/src/mesh.c b/src/mesh.c index 7ba7f28..da44e3b 100755 --- a/src/mesh.c +++ b/src/mesh.c @@ -7,7 +7,7 @@ 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 ) { struct obj * p; @@ -22,15 +22,27 @@ mesh_t create_mesh( float * d, float * n ) glBindBuffer( GL_ARRAY_BUFFER, p->d_vbo ); glBufferData( GL_ARRAY_BUFFER, p->vertex*3*sizeof(float), d+1, GL_STATIC_DRAW ); - glVertexAttribPointer( 0,3,GL_FLOAT, 0, 3*sizeof(float), NULL ); + glVertexAttribPointer( 0,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[0]*sizeof(float)) ); glEnableVertexAttribArray(0); + glVertexAttribPointer( 1,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[1]*sizeof(float)) ); + glEnableVertexAttribArray(1); + glVertexAttribPointer( 2,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[2]*sizeof(float)) ); + glEnableVertexAttribArray(2); + glVertexAttribPointer( 3,1,GL_FLOAT, 0, 3*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, GL_STATIC_DRAW ); - glVertexAttribPointer( 1,3,GL_FLOAT, 0, 3*sizeof(float), NULL ); - glEnableVertexAttribArray(1); + glVertexAttribPointer( 4,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[0]*sizeof(float)) ); + glEnableVertexAttribArray(4); + glVertexAttribPointer( 5,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[1]*sizeof(float)) ); + glEnableVertexAttribArray(5); + glVertexAttribPointer( 6,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[2]*sizeof(float)) ); + glEnableVertexAttribArray(6); + glVertexAttribPointer( 7,1,GL_FLOAT, 0, 3*sizeof(float), (float*)(coordanate[3]*sizeof(float)) ); + glEnableVertexAttribArray(7); return p; } diff --git a/src/window.c b/src/window.c index a02fb78..1ac027a 100644 --- a/src/window.c +++ b/src/window.c @@ -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 );