diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a8f9e5..c208874 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ # CMake entry point cmake_minimum_required (VERSION 3.0) project(OpenGL_Analog_Clock C) -cmake_policy(SET CMP0072 OLD) +cmake_policy(SET CMP0072 NEW) find_package(OpenGL REQUIRED) find_package(GLEW REQUIRED) @@ -28,13 +28,16 @@ add_definitions( -D_CRT_SECURE_NO_WARNINGS ) -add_executable(clock +add_executable( + clock src/main.c src/clock.c src/shader.c src/matrix.c + src/model.c ) -target_link_libraries(clock +target_link_libraries( + clock ${ALL_LIBS} ) diff --git a/Include/clock.h b/Include/clock.h index c1046d7..f5acaa1 100644 --- a/Include/clock.h +++ b/Include/clock.h @@ -15,8 +15,9 @@ #include // Include project headers -#include +#include #include +#include // Define macros #define xfree(p) if (p) free((void *)p) @@ -24,5 +25,7 @@ // Declare functions int clock_init(GLFWwindow **); +int clock_run(GLFWwindow *); #endif + diff --git a/Include/matrix.h b/Include/matrix.h index 80eb229..ca01589 100644 --- a/Include/matrix.h +++ b/Include/matrix.h @@ -1,9 +1,12 @@ #ifndef MATRIX_H #define MATRIX_H -GLdouble *matrix_new(void); +GLfloat *matrix_new(void); -int matrix_transform(GLdouble *, int, - GLdouble **, int); +GLfloat *matrix_multip(GLfloat *, GLfloat *); + +GLfloat *matrix_transform(GLfloat *, int, + GLfloat *); #endif + diff --git a/Include/model.h b/Include/model.h new file mode 100644 index 0000000..df554bc --- /dev/null +++ b/Include/model.h @@ -0,0 +1,12 @@ +#ifndef MODEL_LOADER_H +#define MODEL_LOADER_H + +typedef struct _model { + GLfloat *buffer; + size_t bufsize; +} Model; + +Model * model_load(const char *path); + +#endif + diff --git a/Include/shader.h b/Include/shader.h index 6d8b68a..9d5eb58 100644 --- a/Include/shader.h +++ b/Include/shader.h @@ -4,3 +4,4 @@ int LoadShaders(GLuint *, const char *, const char *); #endif + diff --git a/cube.obj b/cube.obj new file mode 100644 index 0000000..0ab08cc --- /dev/null +++ b/cube.obj @@ -0,0 +1,1009 @@ +# Blender v2.79 (sub 0) OBJ File: '' +# www.blender.org +o Text.011 +v 0.044197 0.476004 0.054248 +v 0.034951 0.476004 0.054248 +v 0.034951 0.411665 0.054248 +v 0.044197 0.411665 0.054248 +v 0.025801 0.476004 0.054248 +v 0.014669 0.476004 0.054248 +v -0.005426 0.451476 0.054248 +v -0.025614 0.476004 0.054248 +v -0.036840 0.476004 0.054248 +v -0.011086 0.444966 0.054248 +v -0.038821 0.411665 0.054248 +v -0.027595 0.411665 0.054248 +v -0.005426 0.438268 0.054248 +v 0.016461 0.411665 0.054248 +v 0.027593 0.411665 0.054248 +v 0.000140 0.444966 0.054248 +s off +f 3 1 2 +f 3 4 1 +f 10 8 9 +f 10 7 8 +f 7 5 6 +f 7 16 5 +f 10 16 7 +f 11 16 10 +f 11 13 16 +f 13 15 16 +f 11 12 13 +f 14 15 13 +o Text.010 +v -0.209934 0.369665 0.054248 +v -0.219180 0.369665 0.054248 +v -0.219180 0.305326 0.054248 +v -0.209934 0.305326 0.054248 +v -0.228330 0.369665 0.054248 +v -0.239462 0.369665 0.054248 +v -0.259557 0.345137 0.054248 +v -0.279745 0.369665 0.054248 +v -0.290971 0.369665 0.054248 +v -0.265217 0.338628 0.054248 +v -0.292952 0.305326 0.054248 +v -0.281726 0.305326 0.054248 +v -0.259557 0.331930 0.054248 +v -0.237670 0.305326 0.054248 +v -0.226538 0.305326 0.054248 +v -0.253991 0.338628 0.054248 +s off +f 19 17 18 +f 19 20 17 +f 26 24 25 +f 26 23 24 +f 23 21 22 +f 23 32 21 +f 26 32 23 +f 27 32 26 +f 27 29 32 +f 29 31 32 +f 27 28 29 +f 30 31 29 +o Text.009 +v -0.318448 0.232687 0.054248 +v -0.329580 0.232687 0.054248 +v -0.349674 0.208159 0.054248 +v -0.369862 0.232687 0.054248 +v -0.381089 0.232687 0.054248 +v -0.355334 0.201649 0.054248 +v -0.383070 0.168348 0.054248 +v -0.371844 0.168348 0.054248 +v -0.349674 0.194951 0.054248 +v -0.327787 0.168348 0.054248 +v -0.316655 0.168348 0.054248 +v -0.344108 0.201649 0.054248 +s off +f 38 36 37 +f 38 35 36 +f 35 33 34 +f 35 44 33 +f 38 44 35 +f 39 44 38 +f 39 41 44 +f 41 43 44 +f 39 40 41 +f 42 43 41 +o Text.008 +v -0.372364 0.036231 0.054248 +v -0.383496 0.036231 0.054248 +v -0.403590 0.011703 0.054248 +v -0.423779 0.036231 0.054248 +v -0.435005 0.036231 0.054248 +v -0.409251 0.005193 0.054248 +v -0.436986 -0.028108 0.054248 +v -0.425760 -0.028108 0.054248 +v -0.403590 -0.001505 0.054248 +v -0.381704 -0.028108 0.054248 +v -0.370572 -0.028108 0.054248 +v -0.398024 0.005193 0.054248 +v -0.444345 0.036231 0.054248 +v -0.453590 0.036231 0.054248 +v -0.453590 -0.028108 0.054248 +v -0.444345 -0.028108 0.054248 +s off +f 50 48 49 +f 50 47 48 +f 47 45 46 +f 47 56 45 +f 50 56 47 +f 51 56 50 +f 51 53 56 +f 53 55 56 +f 51 52 53 +f 54 55 53 +f 59 57 58 +f 59 60 57 +o Text.007 +v -0.268101 -0.153016 0.054248 +v -0.277346 -0.153016 0.054248 +v -0.277346 -0.217355 0.054248 +v -0.268101 -0.217355 0.054248 +v -0.291685 -0.153016 0.054248 +v -0.300931 -0.153016 0.054248 +v -0.300931 -0.217355 0.054248 +v -0.291685 -0.217355 0.054248 +v -0.315270 -0.153016 0.054248 +v -0.324515 -0.153016 0.054248 +v -0.324515 -0.217355 0.054248 +v -0.315270 -0.217355 0.054248 +v -0.331685 -0.153016 0.054248 +v -0.340836 -0.153016 0.054248 +v -0.360081 -0.198204 0.054248 +v -0.379420 -0.153016 0.054248 +v -0.388666 -0.153016 0.054248 +v -0.360836 -0.217921 0.054248 +v -0.358949 -0.217921 0.054248 +s off +f 63 61 62 +f 63 64 61 +f 67 65 66 +f 67 68 65 +f 71 69 70 +f 71 72 69 +f 78 76 77 +f 78 75 76 +f 75 73 74 +f 75 79 73 +f 78 79 75 +o Text.006 +v -0.161916 -0.322436 0.054248 +v -0.171162 -0.322436 0.054248 +v -0.171162 -0.386775 0.054248 +v -0.161916 -0.386775 0.054248 +v -0.185501 -0.322436 0.054248 +v -0.194746 -0.322436 0.054248 +v -0.194746 -0.386775 0.054248 +v -0.185501 -0.386775 0.054248 +v -0.201916 -0.322436 0.054248 +v -0.211067 -0.322436 0.054248 +v -0.230312 -0.367625 0.054248 +v -0.249651 -0.322436 0.054248 +v -0.258897 -0.322436 0.054248 +v -0.231067 -0.387341 0.054248 +v -0.229180 -0.387341 0.054248 +s off +f 82 80 81 +f 82 83 80 +f 86 84 85 +f 86 87 84 +f 93 91 92 +f 93 90 91 +f 90 88 89 +f 90 94 88 +f 93 94 90 +o Text.005 +v 0.047002 -0.387321 0.054248 +v 0.037757 -0.387321 0.054248 +v 0.037757 -0.451660 0.054248 +v 0.047002 -0.451660 0.054248 +v 0.030587 -0.387321 0.054248 +v 0.021436 -0.387321 0.054248 +v 0.002191 -0.432509 0.054248 +v -0.017148 -0.387321 0.054248 +v -0.026394 -0.387321 0.054248 +v 0.001436 -0.452226 0.054248 +v 0.003323 -0.452226 0.054248 +s off +f 97 95 96 +f 97 98 95 +f 104 102 103 +f 104 101 102 +f 101 99 100 +f 101 105 99 +f 104 105 101 +o Text.004 +v 0.264892 -0.297204 0.054248 +v 0.255741 -0.297204 0.054248 +v 0.236496 -0.342392 0.054248 +v 0.217157 -0.297204 0.054248 +v 0.207912 -0.297204 0.054248 +v 0.235741 -0.362109 0.054248 +v 0.237628 -0.362109 0.054248 +s off +f 111 109 110 +f 111 108 109 +f 108 106 107 +f 108 112 106 +f 111 112 108 +o Text.003 +v 0.418246 -0.154818 0.054248 +v 0.409095 -0.154818 0.054248 +v 0.389850 -0.200006 0.054248 +v 0.370510 -0.154818 0.054248 +v 0.361265 -0.154818 0.054248 +v 0.389095 -0.219723 0.054248 +v 0.390982 -0.219723 0.054248 +v 0.354096 -0.154818 0.054248 +v 0.344850 -0.154818 0.054248 +v 0.344850 -0.219157 0.054248 +v 0.354096 -0.219157 0.054248 +s off +f 118 116 117 +f 118 115 116 +f 115 113 114 +f 115 119 113 +f 118 119 115 +f 122 120 121 +f 122 123 120 +o Text.002 +v 0.466149 0.030824 0.054248 +v 0.456904 0.030824 0.054248 +v 0.456904 -0.033515 0.054248 +v 0.466149 -0.033515 0.054248 +v 0.442565 0.030824 0.054248 +v 0.433320 0.030824 0.054248 +v 0.433320 -0.033515 0.054248 +v 0.442565 -0.033515 0.054248 +v 0.418980 0.030824 0.054248 +v 0.409735 0.030824 0.054248 +v 0.409735 -0.033515 0.054248 +v 0.418980 -0.033515 0.054248 +s off +f 126 124 125 +f 126 127 124 +f 130 128 129 +f 130 131 128 +f 134 132 133 +f 134 135 132 +o Text.001 +v 0.395704 0.203849 0.054248 +v 0.386458 0.203849 0.054248 +v 0.386458 0.139510 0.054248 +v 0.395704 0.139510 0.054248 +v 0.372119 0.203849 0.054248 +v 0.362874 0.203849 0.054248 +v 0.362874 0.139510 0.054248 +v 0.372119 0.139510 0.054248 +s off +f 138 136 137 +f 138 139 136 +f 142 140 141 +f 142 143 140 +o Text +v 0.233338 0.384084 0.054248 +v 0.224093 0.384084 0.054248 +v 0.224093 0.319745 0.054248 +v 0.233338 0.319745 0.054248 +s off +f 146 144 145 +f 146 147 144 +o Sphere +v 0.000000 -0.500000 0.000000 +v 0.019030 0.490393 -0.095671 +v 0.037329 0.461940 -0.187665 +v 0.054193 0.415735 -0.272448 +v 0.068975 0.353553 -0.346760 +v 0.081106 0.277785 -0.407747 +v 0.090120 0.191342 -0.453064 +v 0.095671 0.097545 -0.480970 +v 0.097545 0.000000 -0.490393 +v 0.095671 -0.097545 -0.480970 +v 0.090120 -0.191342 -0.453064 +v 0.081106 -0.277785 -0.407747 +v 0.068975 -0.353553 -0.346760 +v 0.054193 -0.415735 -0.272448 +v 0.037329 -0.461940 -0.187665 +v 0.019030 -0.490393 -0.095671 +v 0.037329 0.490393 -0.090120 +v 0.073223 0.461940 -0.176777 +v 0.106304 0.415735 -0.256640 +v 0.135299 0.353553 -0.326641 +v 0.159095 0.277785 -0.384089 +v 0.176777 0.191342 -0.426777 +v 0.187665 0.097545 -0.453064 +v 0.191342 0.000000 -0.461940 +v 0.187665 -0.097545 -0.453064 +v 0.176777 -0.191342 -0.426777 +v 0.159095 -0.277785 -0.384089 +v 0.135299 -0.353553 -0.326641 +v 0.106304 -0.415735 -0.256640 +v 0.073223 -0.461940 -0.176777 +v 0.037329 -0.490393 -0.090120 +v 0.054193 0.490393 -0.081106 +v 0.106304 0.461940 -0.159095 +v 0.154329 0.415735 -0.230970 +v 0.196424 0.353553 -0.293969 +v 0.230970 0.277785 -0.345671 +v 0.256640 0.191342 -0.384089 +v 0.272448 0.097545 -0.407746 +v 0.277785 0.000000 -0.415735 +v 0.272448 -0.097545 -0.407746 +v 0.256640 -0.191342 -0.384089 +v 0.230970 -0.277785 -0.345671 +v 0.196424 -0.353553 -0.293969 +v 0.154329 -0.415735 -0.230970 +v 0.106304 -0.461940 -0.159095 +v 0.054193 -0.490393 -0.081106 +v 0.068975 0.490393 -0.068975 +v 0.135299 0.461940 -0.135299 +v 0.196424 0.415735 -0.196424 +v 0.250000 0.353553 -0.250000 +v 0.293969 0.277785 -0.293969 +v 0.326641 0.191342 -0.326641 +v 0.346760 0.097545 -0.346760 +v 0.353554 0.000000 -0.353553 +v 0.346760 -0.097545 -0.346760 +v 0.326641 -0.191342 -0.326641 +v 0.293969 -0.277785 -0.293969 +v 0.250000 -0.353553 -0.250000 +v 0.196424 -0.415735 -0.196424 +v 0.135299 -0.461940 -0.135299 +v 0.068975 -0.490393 -0.068975 +v 0.081106 0.490393 -0.054193 +v 0.159095 0.461940 -0.106304 +v 0.230970 0.415735 -0.154329 +v 0.293969 0.353553 -0.196424 +v 0.345671 0.277785 -0.230970 +v 0.384089 0.191342 -0.256640 +v 0.407747 0.097545 -0.272447 +v 0.415735 0.000000 -0.277785 +v 0.407747 -0.097545 -0.272447 +v 0.384089 -0.191342 -0.256640 +v 0.345671 -0.277785 -0.230970 +v 0.293969 -0.353553 -0.196424 +v 0.230970 -0.415735 -0.154329 +v 0.159095 -0.461940 -0.106304 +v 0.081106 -0.490393 -0.054193 +v 0.090120 0.490393 -0.037329 +v 0.176777 0.461940 -0.073223 +v 0.256640 0.415735 -0.106304 +v 0.326641 0.353553 -0.135299 +v 0.384089 0.277785 -0.159095 +v 0.426777 0.191342 -0.176777 +v 0.453064 0.097545 -0.187665 +v 0.461940 0.000000 -0.191342 +v 0.453064 -0.097545 -0.187665 +v 0.426777 -0.191342 -0.176777 +v 0.384089 -0.277785 -0.159095 +v 0.326641 -0.353553 -0.135299 +v 0.256640 -0.415735 -0.106304 +v 0.176777 -0.461940 -0.073223 +v 0.090120 -0.490393 -0.037329 +v 0.095671 0.490393 -0.019030 +v 0.187665 0.461940 -0.037329 +v 0.272448 0.415735 -0.054193 +v 0.346760 0.353553 -0.068975 +v 0.407747 0.277785 -0.081106 +v 0.453064 0.191342 -0.090120 +v 0.480970 0.097545 -0.095671 +v 0.490393 0.000000 -0.097545 +v 0.480970 -0.097545 -0.095671 +v 0.453064 -0.191342 -0.090120 +v 0.407747 -0.277785 -0.081106 +v 0.346760 -0.353553 -0.068975 +v 0.272448 -0.415735 -0.054193 +v 0.187665 -0.461940 -0.037329 +v 0.095671 -0.490393 -0.019030 +v 0.097545 0.490393 0.000000 +v 0.191342 0.461940 0.000000 +v 0.277785 0.415735 0.000000 +v 0.353553 0.353553 0.000000 +v 0.415735 0.277785 0.000000 +v 0.461940 0.191342 0.000000 +v 0.490393 0.097545 0.000000 +v 0.500000 0.000000 0.000000 +v 0.490393 -0.097545 0.000000 +v 0.461940 -0.191342 0.000000 +v 0.415735 -0.277785 0.000000 +v 0.353553 -0.353553 0.000000 +v 0.277785 -0.415735 0.000000 +v 0.191342 -0.461940 0.000000 +v 0.097545 -0.490393 0.000000 +v -0.000000 0.500000 0.000000 +v -0.097545 0.490393 0.000000 +v -0.191342 0.461940 0.000000 +v -0.277785 0.415735 0.000000 +v -0.353553 0.353553 0.000000 +v -0.415735 0.277785 0.000000 +v -0.461940 0.191342 0.000000 +v -0.490392 0.097545 0.000000 +v -0.500000 0.000000 0.000000 +v -0.490392 -0.097545 0.000000 +v -0.461940 -0.191342 0.000000 +v -0.415735 -0.277785 0.000000 +v -0.353553 -0.353553 0.000000 +v -0.277785 -0.415735 0.000000 +v -0.191342 -0.461940 0.000000 +v -0.097545 -0.490393 0.000000 +v -0.095671 0.490393 -0.019030 +v -0.187665 0.461940 -0.037329 +v -0.272448 0.415735 -0.054193 +v -0.346760 0.353553 -0.068975 +v -0.407746 0.277785 -0.081106 +v -0.453064 0.191342 -0.090120 +v -0.480970 0.097545 -0.095671 +v -0.490393 0.000000 -0.097545 +v -0.480970 -0.097545 -0.095671 +v -0.453064 -0.191342 -0.090120 +v -0.407746 -0.277785 -0.081106 +v -0.346760 -0.353553 -0.068975 +v -0.272447 -0.415735 -0.054193 +v -0.187665 -0.461940 -0.037329 +v -0.095671 -0.490393 -0.019030 +v -0.090120 0.490393 -0.037329 +v -0.176777 0.461940 -0.073223 +v -0.256640 0.415735 -0.106304 +v -0.326641 0.353553 -0.135299 +v -0.384089 0.277785 -0.159095 +v -0.426777 0.191342 -0.176777 +v -0.453063 0.097545 -0.187665 +v -0.461940 0.000000 -0.191342 +v -0.453063 -0.097545 -0.187665 +v -0.426777 -0.191342 -0.176777 +v -0.384089 -0.277785 -0.159095 +v -0.326641 -0.353553 -0.135299 +v -0.256640 -0.415735 -0.106304 +v -0.176777 -0.461940 -0.073223 +v -0.090120 -0.490393 -0.037329 +v -0.081106 0.490393 -0.054193 +v -0.159095 0.461940 -0.106304 +v -0.230970 0.415735 -0.154329 +v -0.293969 0.353553 -0.196424 +v -0.345671 0.277785 -0.230970 +v -0.384089 0.191342 -0.256640 +v -0.407746 0.097545 -0.272447 +v -0.415735 0.000000 -0.277785 +v -0.407746 -0.097545 -0.272447 +v -0.384089 -0.191342 -0.256640 +v -0.345671 -0.277785 -0.230970 +v -0.293969 -0.353553 -0.196424 +v -0.230970 -0.415735 -0.154329 +v -0.159095 -0.461940 -0.106304 +v -0.081106 -0.490393 -0.054193 +v -0.068975 0.490393 -0.068975 +v -0.135299 0.461940 -0.135299 +v -0.196424 0.415735 -0.196424 +v -0.250000 0.353553 -0.250000 +v -0.293969 0.277785 -0.293969 +v -0.326641 0.191342 -0.326641 +v -0.346760 0.097545 -0.346760 +v -0.353553 0.000000 -0.353553 +v -0.346760 -0.097545 -0.346760 +v -0.326641 -0.191342 -0.326641 +v -0.293969 -0.277785 -0.293969 +v -0.250000 -0.353553 -0.250000 +v -0.196424 -0.415735 -0.196424 +v -0.135299 -0.461940 -0.135299 +v -0.068975 -0.490393 -0.068975 +v -0.054193 0.490393 -0.081106 +v -0.106304 0.461940 -0.159095 +v -0.154329 0.415735 -0.230970 +v -0.196424 0.353553 -0.293969 +v -0.230970 0.277785 -0.345671 +v -0.256640 0.191342 -0.384089 +v -0.272447 0.097545 -0.407746 +v -0.277785 0.000000 -0.415735 +v -0.272447 -0.097545 -0.407746 +v -0.256640 -0.191342 -0.384089 +v -0.230970 -0.277785 -0.345671 +v -0.196424 -0.353553 -0.293969 +v -0.154329 -0.415735 -0.230970 +v -0.106304 -0.461940 -0.159095 +v -0.054193 -0.490393 -0.081106 +v -0.037329 0.490393 -0.090120 +v -0.073223 0.461940 -0.176776 +v -0.106304 0.415735 -0.256640 +v -0.135299 0.353553 -0.326641 +v -0.159095 0.277785 -0.384089 +v -0.176777 0.191342 -0.426776 +v -0.187665 0.097545 -0.453063 +v -0.191342 0.000000 -0.461939 +v -0.187665 -0.097545 -0.453063 +v -0.176777 -0.191342 -0.426776 +v -0.159095 -0.277785 -0.384089 +v -0.135299 -0.353553 -0.326641 +v -0.106304 -0.415735 -0.256640 +v -0.073223 -0.461940 -0.176776 +v -0.037329 -0.490393 -0.090120 +v -0.019030 0.490393 -0.095671 +v -0.037329 0.461940 -0.187665 +v -0.054193 0.415735 -0.272447 +v -0.068975 0.353553 -0.346760 +v -0.081106 0.277785 -0.407746 +v -0.090120 0.191342 -0.453063 +v -0.095671 0.097545 -0.480969 +v -0.097545 0.000000 -0.490392 +v -0.095671 -0.097545 -0.480969 +v -0.090120 -0.191342 -0.453063 +v -0.081106 -0.277785 -0.407746 +v -0.068975 -0.353553 -0.346760 +v -0.054193 -0.415735 -0.272447 +v -0.037329 -0.461940 -0.187665 +v -0.019030 -0.490393 -0.095671 +v 0.000000 0.490393 -0.097545 +v 0.000000 0.461940 -0.191341 +v 0.000000 0.415735 -0.277785 +v 0.000000 0.353553 -0.353553 +v 0.000000 0.277785 -0.415734 +v 0.000000 0.191342 -0.461940 +v 0.000000 0.097545 -0.490392 +v 0.000000 0.000000 -0.500000 +v 0.000000 -0.097545 -0.490392 +v 0.000000 -0.191342 -0.461940 +v 0.000000 -0.277785 -0.415734 +v 0.000000 -0.353553 -0.353553 +v 0.000000 -0.415735 -0.277785 +v 0.000000 -0.461940 -0.191341 +v 0.000000 -0.490393 -0.097545 +s off +f 398 158 399 +f 392 150 151 +f 399 159 400 +f 393 151 152 +f 400 160 401 +f 394 152 153 +f 401 161 402 +f 395 153 154 +f 402 162 403 +f 396 154 155 +f 403 163 404 +f 397 155 156 +f 390 269 149 +f 148 404 163 +f 397 157 398 +f 391 149 150 +f 157 171 172 +f 149 165 150 +f 157 173 158 +f 150 166 151 +f 158 174 159 +f 151 167 152 +f 160 174 175 +f 152 168 153 +f 161 175 176 +f 153 169 154 +f 161 177 162 +f 154 170 155 +f 163 177 178 +f 155 171 156 +f 149 269 164 +f 148 163 178 +f 176 190 191 +f 168 184 169 +f 177 191 192 +f 169 185 170 +f 177 193 178 +f 170 186 171 +f 164 269 179 +f 148 178 193 +f 171 187 172 +f 164 180 165 +f 172 188 173 +f 165 181 166 +f 174 188 189 +f 166 182 167 +f 174 190 175 +f 167 183 168 +f 180 194 195 +f 187 203 188 +f 180 196 181 +f 189 203 204 +f 181 197 182 +f 189 205 190 +f 183 197 198 +f 190 206 191 +f 183 199 184 +f 191 207 192 +f 184 200 185 +f 192 208 193 +f 185 201 186 +f 179 269 194 +f 148 193 208 +f 186 202 187 +f 198 214 199 +f 206 222 207 +f 200 214 215 +f 208 222 223 +f 201 215 216 +f 194 269 209 +f 148 208 223 +f 201 217 202 +f 194 210 195 +f 202 218 203 +f 195 211 196 +f 204 218 219 +f 196 212 197 +f 204 220 205 +f 198 212 213 +f 205 221 206 +f 217 233 218 +f 210 226 211 +f 219 233 234 +f 211 227 212 +f 219 235 220 +f 212 228 213 +f 221 235 236 +f 213 229 214 +f 221 237 222 +f 215 229 230 +f 222 238 223 +f 215 231 216 +f 209 269 224 +f 148 223 238 +f 216 232 217 +f 209 225 210 +f 236 252 237 +f 230 244 245 +f 237 253 238 +f 230 246 231 +f 224 269 239 +f 148 238 253 +f 231 247 232 +f 224 240 225 +f 232 248 233 +f 225 241 226 +f 234 248 249 +f 226 242 227 +f 235 249 250 +f 227 243 228 +f 236 250 251 +f 228 244 229 +f 249 263 264 +f 241 257 242 +f 250 264 265 +f 242 258 243 +f 251 265 266 +f 243 259 244 +f 251 267 252 +f 245 259 260 +f 253 267 268 +f 245 261 246 +f 239 269 254 +f 148 253 268 +f 246 262 247 +f 239 255 240 +f 247 263 248 +f 240 256 241 +f 280 296 281 +f 273 289 274 +f 281 297 282 +f 274 290 275 +f 283 297 298 +f 276 290 291 +f 284 298 299 +f 276 292 277 +f 270 269 285 +f 148 284 299 +f 278 292 293 +f 270 286 271 +f 278 294 279 +f 271 287 272 +f 280 294 295 +f 273 287 288 +f 148 299 314 +f 293 307 308 +f 285 301 286 +f 293 309 294 +f 287 301 302 +f 295 309 310 +f 288 302 303 +f 295 311 296 +f 288 304 289 +f 296 312 297 +f 289 305 290 +f 298 312 313 +f 291 305 306 +f 298 314 299 +f 291 307 292 +f 285 269 300 +f 311 327 312 +f 304 320 305 +f 312 328 313 +f 306 320 321 +f 313 329 314 +f 306 322 307 +f 300 269 315 +f 148 314 329 +f 307 323 308 +f 301 315 316 +f 308 324 309 +f 301 317 302 +f 310 324 325 +f 303 317 318 +f 310 326 311 +f 304 318 319 +f 315 331 316 +f 323 339 324 +f 317 331 332 +f 325 339 340 +f 318 332 333 +f 325 341 326 +f 319 333 334 +f 326 342 327 +f 319 335 320 +f 327 343 328 +f 321 335 336 +f 328 344 329 +f 321 337 322 +f 315 269 330 +f 148 329 344 +f 323 337 338 +f 334 350 335 +f 343 357 358 +f 336 350 351 +f 343 359 344 +f 337 351 352 +f 330 269 345 +f 148 344 359 +f 337 353 338 +f 331 345 346 +f 338 354 339 +f 331 347 332 +f 340 354 355 +f 332 348 333 +f 340 356 341 +f 334 348 349 +f 341 357 342 +f 353 369 354 +f 347 361 362 +f 355 369 370 +f 347 363 348 +f 355 371 356 +f 349 363 364 +f 357 371 372 +f 349 365 350 +f 358 372 373 +f 351 365 366 +f 358 374 359 +f 351 367 352 +f 345 269 360 +f 148 359 374 +f 353 367 368 +f 345 361 346 +f 372 388 373 +f 366 380 381 +f 373 389 374 +f 366 382 367 +f 360 269 375 +f 148 374 389 +f 367 383 368 +f 360 376 361 +f 368 384 369 +f 361 377 362 +f 370 384 385 +f 362 378 363 +f 370 386 371 +f 364 378 379 +f 372 386 387 +f 364 380 365 +f 376 392 377 +f 385 399 400 +f 378 392 393 +f 385 401 386 +f 379 393 394 +f 387 401 402 +f 379 395 380 +f 387 403 388 +f 381 395 396 +f 388 404 389 +f 381 397 382 +f 375 269 390 +f 148 389 404 +f 382 398 383 +f 376 390 391 +f 383 399 384 +f 398 157 158 +f 392 391 150 +f 399 158 159 +f 393 392 151 +f 400 159 160 +f 394 393 152 +f 401 160 161 +f 395 394 153 +f 402 161 162 +f 396 395 154 +f 403 162 163 +f 397 396 155 +f 397 156 157 +f 391 390 149 +f 157 156 171 +f 149 164 165 +f 157 172 173 +f 150 165 166 +f 158 173 174 +f 151 166 167 +f 160 159 174 +f 152 167 168 +f 161 160 175 +f 153 168 169 +f 161 176 177 +f 154 169 170 +f 163 162 177 +f 155 170 171 +f 176 175 190 +f 168 183 184 +f 177 176 191 +f 169 184 185 +f 177 192 193 +f 170 185 186 +f 171 186 187 +f 164 179 180 +f 172 187 188 +f 165 180 181 +f 174 173 188 +f 166 181 182 +f 174 189 190 +f 167 182 183 +f 180 179 194 +f 187 202 203 +f 180 195 196 +f 189 188 203 +f 181 196 197 +f 189 204 205 +f 183 182 197 +f 190 205 206 +f 183 198 199 +f 191 206 207 +f 184 199 200 +f 192 207 208 +f 185 200 201 +f 186 201 202 +f 198 213 214 +f 206 221 222 +f 200 199 214 +f 208 207 222 +f 201 200 215 +f 201 216 217 +f 194 209 210 +f 202 217 218 +f 195 210 211 +f 204 203 218 +f 196 211 212 +f 204 219 220 +f 198 197 212 +f 205 220 221 +f 217 232 233 +f 210 225 226 +f 219 218 233 +f 211 226 227 +f 219 234 235 +f 212 227 228 +f 221 220 235 +f 213 228 229 +f 221 236 237 +f 215 214 229 +f 222 237 238 +f 215 230 231 +f 216 231 232 +f 209 224 225 +f 236 251 252 +f 230 229 244 +f 237 252 253 +f 230 245 246 +f 231 246 247 +f 224 239 240 +f 232 247 248 +f 225 240 241 +f 234 233 248 +f 226 241 242 +f 235 234 249 +f 227 242 243 +f 236 235 250 +f 228 243 244 +f 249 248 263 +f 241 256 257 +f 250 249 264 +f 242 257 258 +f 251 250 265 +f 243 258 259 +f 251 266 267 +f 245 244 259 +f 253 252 267 +f 245 260 261 +f 246 261 262 +f 239 254 255 +f 247 262 263 +f 240 255 256 +f 280 295 296 +f 273 288 289 +f 281 296 297 +f 274 289 290 +f 283 282 297 +f 276 275 290 +f 284 283 298 +f 276 291 292 +f 278 277 292 +f 270 285 286 +f 278 293 294 +f 271 286 287 +f 280 279 294 +f 273 272 287 +f 293 292 307 +f 285 300 301 +f 293 308 309 +f 287 286 301 +f 295 294 309 +f 288 287 302 +f 295 310 311 +f 288 303 304 +f 296 311 312 +f 289 304 305 +f 298 297 312 +f 291 290 305 +f 298 313 314 +f 291 306 307 +f 311 326 327 +f 304 319 320 +f 312 327 328 +f 306 305 320 +f 313 328 329 +f 306 321 322 +f 307 322 323 +f 301 300 315 +f 308 323 324 +f 301 316 317 +f 310 309 324 +f 303 302 317 +f 310 325 326 +f 304 303 318 +f 315 330 331 +f 323 338 339 +f 317 316 331 +f 325 324 339 +f 318 317 332 +f 325 340 341 +f 319 318 333 +f 326 341 342 +f 319 334 335 +f 327 342 343 +f 321 320 335 +f 328 343 344 +f 321 336 337 +f 323 322 337 +f 334 349 350 +f 343 342 357 +f 336 335 350 +f 343 358 359 +f 337 336 351 +f 337 352 353 +f 331 330 345 +f 338 353 354 +f 331 346 347 +f 340 339 354 +f 332 347 348 +f 340 355 356 +f 334 333 348 +f 341 356 357 +f 353 368 369 +f 347 346 361 +f 355 354 369 +f 347 362 363 +f 355 370 371 +f 349 348 363 +f 357 356 371 +f 349 364 365 +f 358 357 372 +f 351 350 365 +f 358 373 374 +f 351 366 367 +f 353 352 367 +f 345 360 361 +f 372 387 388 +f 366 365 380 +f 373 388 389 +f 366 381 382 +f 367 382 383 +f 360 375 376 +f 368 383 384 +f 361 376 377 +f 370 369 384 +f 362 377 378 +f 370 385 386 +f 364 363 378 +f 372 371 386 +f 364 379 380 +f 376 391 392 +f 385 384 399 +f 378 377 392 +f 385 400 401 +f 379 378 393 +f 387 386 401 +f 379 394 395 +f 387 402 403 +f 381 380 395 +f 388 403 404 +f 381 396 397 +f 382 397 398 +f 376 375 390 +f 383 398 399 diff --git a/shaders/SimpleFragmentShader.fragmentshader b/shaders/SimpleFragmentShader.fragmentshader index 3b04c78..26db610 100644 --- a/shaders/SimpleFragmentShader.fragmentshader +++ b/shaders/SimpleFragmentShader.fragmentshader @@ -1,12 +1,8 @@ #version 330 core -// Ouput data out vec3 color; -void main() -{ +void main() { + color = vec3(1, 0, 0); - // Output color = red - color = vec3(1,0,0); - -} \ No newline at end of file +} diff --git a/src/clock.c b/src/clock.c index 592713f..ff80fb0 100644 --- a/src/clock.c +++ b/src/clock.c @@ -1,6 +1,7 @@ #include -int clock_init(GLFWwindow **window) { +int +clock_init(GLFWwindow **window) { // Initialise GLFW if(!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); @@ -40,3 +41,141 @@ int clock_init(GLFWwindow **window) { return 0; } +int +clock_run(GLFWwindow *window) { + GLuint VertexArrayID; + GLuint programID; + + + glGenVertexArrays(1, &VertexArrayID); + glBindVertexArray(VertexArrayID); + + // Create and compile our GLSL program from the shaders + if (LoadShaders(&programID, + "../shaders/SimpleVertexShader.vertexshader", + "../shaders/SimpleFragmentShader.fragmentshader")) { + fprintf(stderr, "Could not load shaders.\n"); + return -1; + } + + static GLfloat g_vertex_buffer_data[] = { + -0.3f, -0.3f, 0.0f, 1.0f, + 0.3f, -0.3f, 0.0f, 1.0f, + 0.0f, 0.3f, 0.0f, 1.0f + }; + Model *model; + model = model_load("../cube.obj"); + + GLuint vertexbuffer; + glGenBuffers(1, &vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); + glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), model->buffer, GL_STATIC_DRAW); + + double xpos, ypos; + glfwGetCursorPos(window, &xpos, &ypos); + + GLfloat **matrices = malloc(2 * sizeof(GLfloat *)); + + GLfloat *temp = matrix_new(); + + //temp[0] = cos(M_PI*2/60); + //temp[1] = -sin(M_PI*2/60); + //temp[4] = sin(M_PI*2/60); + //temp[5] = cos(M_PI*2/60); + + matrices[0] = temp; + temp = matrix_new(); + + temp[0] = cos(M_PI*2/60/4); + temp[8] = -sin(M_PI*2/60/4); + temp[2] = sin(M_PI*2/60/4); + temp[10] = cos(M_PI*2/60/4); + + matrices[1] = temp; + + GLfloat *mat = matrix_new(); + + for (int i = 1; i >= 0; i--) { + temp = matrix_multip(matrices[i], mat); + free(mat); + mat = temp; + } + + GLfloat *projection = matrix_new(); + GLfloat *buffer; + projection[14] = -1.0f; + buffer = matrix_new(); + buffer[0] = (GLfloat)9/16; + temp = matrix_multip(projection, buffer); + free(projection); + projection = buffer; + temp = malloc(model->bufsize * 4 * sizeof(GLfloat)); + buffer = malloc(model->bufsize * 4 * sizeof(GLfloat)); + memcpy(temp, model->buffer, model->bufsize * 4 * sizeof(GLfloat)); + + do { + // Clear the screen. It's not mentioned before Tutorial 02, + // but it can cause flickering, so it's there nonetheless. + glClear(GL_COLOR_BUFFER_BIT); + + // Use our shader + glUseProgram(programID); + //glfwGetCursorPos(window, &xpos, &ypos); + //g_vertex_buffer_data[0] = (float)xpos / 1280 * 2 - 1 - 0.15; + //g_vertex_buffer_data[1] = 2 - (float)ypos / 720 * 2 - 1 - 0.15; + + //g_vertex_buffer_data[3] = (float)xpos / 1280 * 2 - 1 + 0.15; + //g_vertex_buffer_data[4] = 2 - (float)ypos / 720 * 2 - 1 - 0.15; + + //g_vertex_buffer_data[6] = (float)xpos / 1280 * 2 - 1; + //g_vertex_buffer_data[7] = 2 - (float)ypos / 720 * 2 - 1 + 0.15; + buffer = matrix_transform(temp, model->bufsize, mat); + free(temp); + temp = buffer; + buffer = matrix_transform(buffer, model->bufsize, projection); + memcpy(model->buffer, buffer, model->bufsize * 4 * sizeof(GLfloat)); + free(buffer); + + + GLuint vertexbuffer; + glGenBuffers(1, &vertexbuffer); + glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); + glBufferData(GL_ARRAY_BUFFER, model->bufsize * 4 * sizeof(GLfloat), model->buffer, GL_STATIC_DRAW); + + // 1rst attribute buffer : vertices + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); + glVertexAttribPointer( + 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. + 4, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + NULL // array buffer offset + ); + + // Draw the triangle ! + glDrawArrays(GL_TRIANGLES, 0, model->bufsize); // 3 indices starting at 0 -> 1 triangle + + glDisableVertexAttribArray(0); + + // Swap buffers + glfwSwapBuffers(window); + glfwPollEvents(); + + // Check if the ESC key was pressed or the window was closed + usleep(1000000/60); + } while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && + glfwWindowShouldClose(window) == 0); + + // Close OpenGL window and terminate GLFW + for (int i = 0; i < 2; i++) { + free(matrices[i]); + } + free(matrices); + glfwTerminate(); + + return 0; + +} + diff --git a/src/main.c b/src/main.c index 344ab96..4a9733d 100644 --- a/src/main.c +++ b/src/main.c @@ -1,106 +1,14 @@ // Include APIs #include "clock.h" -int main(int argc, char *argv[]) { +int +main(int argc, char *argv[]) { GLFWwindow *window; - GLuint VertexArrayID; - GLuint programID; - if (clock_init(&window)) { return -1; } - glGenVertexArrays(1, &VertexArrayID); - glBindVertexArray(VertexArrayID); - - // Create and compile our GLSL program from the shaders - if (LoadShaders(&programID, - "../shaders/SimpleVertexShader.vertexshader", - "../shaders/SimpleFragmentShader.fragmentshader")) { - fprintf(stderr, "Could not load shaders.\n"); - return -1; - } - - static GLdouble g_vertex_buffer_data[] = { - -0.3f, -0.3f, 0.3f, - 0.3f, -0.3f, -0.3f, - 0.0f, 0.3f, -0.0f - }; - - GLuint vertexbuffer; - glGenBuffers(1, &vertexbuffer); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); - - double xpos, ypos; - glfwGetCursorPos(window, &xpos, &ypos); - - GLdouble **matrices = calloc(1, sizeof(GLdouble *)); - - GLdouble *rot_mat = matrix_new(); - - rot_mat[0] = cos(M_PI*2/60); - rot_mat[1] = -sin(M_PI*2/60); - rot_mat[4] = sin(M_PI*2/60); - rot_mat[5] = cos(M_PI*2/60); - - *matrices = rot_mat; - - - do { - // Clear the screen. It's not mentioned before Tutorial 02, - // but it can cause flickering, so it's there nonetheless. - glClear(GL_COLOR_BUFFER_BIT); - - // Use our shader - glUseProgram(programID); - //glfwGetCursorPos(window, &xpos, &ypos); - //g_vertex_buffer_data[0] = (float)xpos / 1280 * 2 - 1 - 0.15; - //g_vertex_buffer_data[1] = 2 - (float)ypos / 720 * 2 - 1 - 0.15; - - //g_vertex_buffer_data[3] = (float)xpos / 1280 * 2 - 1 + 0.15; - //g_vertex_buffer_data[4] = 2 - (float)ypos / 720 * 2 - 1 - 0.15; - - //g_vertex_buffer_data[6] = (float)xpos / 1280 * 2 - 1; - //g_vertex_buffer_data[7] = 2 - (float)ypos / 720 * 2 - 1 + 0.15; - matrix_transform(g_vertex_buffer_data, 3, - matrices, 1); - - GLuint vertexbuffer; - glGenBuffers(1, &vertexbuffer); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); - - // 1rst attribute buffer : vertices - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glVertexAttribPointer( - 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. - 3, // size - GL_DOUBLE, // type - GL_FALSE, // normalized? - 0, // stride - NULL // array buffer offset - ); - - // Draw the triangle ! - glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle - - glDisableVertexAttribArray(0); - - // Swap buffers - glfwSwapBuffers(window); - glfwPollEvents(); - - // Check if the ESC key was pressed or the window was closed - usleep(1000000/60); - } while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && - glfwWindowShouldClose(window) == 0); - - // Close OpenGL window and terminate GLFW - glfwTerminate(); - - return 0; + return clock_run(window); } diff --git a/src/matrix.c b/src/matrix.c index 92fa0fb..e9fb47b 100644 --- a/src/matrix.c +++ b/src/matrix.c @@ -1,9 +1,23 @@ #include "clock.h" -static GLdouble * -matrix_multip(GLdouble *mat1, GLdouble *mat2) { - GLdouble *result; - GLdouble dot_prod; + +GLfloat * +matrix_new() { + GLfloat *mat; + + mat = calloc(16, sizeof(GLfloat)); + + for (int i = 0; i < 4; i++) { + mat[i*4+i] = 1; + } + + return mat; +} + +GLfloat * +matrix_multip(GLfloat *mat1, GLfloat *mat2) { + GLfloat *result; + GLfloat dot_prod; result = matrix_new(); @@ -17,54 +31,36 @@ matrix_multip(GLdouble *mat1, GLdouble *mat2) { result[j+i*4] = dot_prod; } } - + return result; } -GLdouble * -matrix_new() { - GLdouble *mat; - mat = calloc(16, sizeof(GLdouble)); - - for (int i = 0; i < 4; i++) { - mat[i*4+i] = 1; - } +GLfloat * +matrix_transform(GLfloat *vects, int vectcount, + GLfloat *mat) { + GLfloat dot_prod; + GLfloat *helper = NULL; + GLfloat *result; - return mat; -} + result = calloc(vectcount*4, sizeof(GLfloat)); -int -matrix_transform(GLdouble *vects, int vectcount, - GLdouble **mats, int matcount) { - GLdouble *mat; - GLdouble dot_prod; - GLdouble *helper = NULL; - GLdouble *result; - - mat = matrix_new(); - result = calloc(vectcount*3, sizeof(GLdouble)); - - for (int i = matcount-1; i >= 0; i--) { - xfree(helper); - helper = matrix_multip(mats[i], mat); - free(mat); - mat = helper; - } - for (int k = 0; k < vectcount; k++) { - for (int j = 0; j < 3; j++) { + for (int j = 0; j < 4; j++) { dot_prod = 0; - for (int i = 0; i < 3; i++) { - dot_prod += vects[k*3+i] * mat[i+j*4]; + for (int i = 0; i < 4; i++) { + dot_prod += vects[k*4+i] * mat[i+j*4]; + } + result[j+k*4] = dot_prod; + } + if (result[k*4+3] != 0.0f) { + GLfloat div = result[k*4+3]; + for (int i = 0; i < 4; i++) { + result[k*4+i] /= div; } - result[j+k*3] = dot_prod; } } - memcpy(vects, result, vectcount * 3 * sizeof(GLdouble)); - free(result); - - return 0; + return result; } diff --git a/src/model.c b/src/model.c new file mode 100644 index 0000000..8cad5ae --- /dev/null +++ b/src/model.c @@ -0,0 +1,70 @@ +#include + +Model * +model_new(size_t size) { + Model *model = calloc(1, sizeof(Model)); + model->buffer = calloc((size ? size : 1) *4 , sizeof(GLfloat)); + model->bufsize = size; +} + +void +model_free(Model *self) { + free(self->buffer); + free(self); +} + +Model * +model_load(const char *path) { + Model *model; + GLfloat *vertices; + int *faces; + size_t vertcount, facecount; + char type, check, *line; + FILE *f; + + f = fopen(path, "r"); + line = malloc(255); + vertices = malloc(3 * sizeof(GLfloat)); + faces = malloc(3 * sizeof(int)); + type = 0; + vertcount = 0; + facecount = 0; + + do { + check = fscanf(f, "%c", &type); + if (check == EOF) { + break; + } + else if (type == 'v') { + vertices = realloc(vertices, (vertcount+1)*3*sizeof(GLfloat)); + check = fscanf(f, "%f %f %f\n", &(vertices[vertcount*3]), + &(vertices[vertcount*3+1]), + &(vertices[vertcount*3+2])); + vertcount++; + } + else if (type == 'f') { + faces = realloc(faces, (facecount+1)*3*sizeof(GLfloat)); + check = fscanf(f, "%d %d %d\n", &(faces[facecount*3]), + &(faces[facecount*3+1]), + &(faces[facecount*3+2])); + facecount++; + } + else { + check = fscanf(f, "%*[^\n]\n", NULL); + continue; + } + } while(check != EOF); + + model = model_new(facecount*3); + for (int i = 0; i < facecount; i++) { + for (int j = 0; j < 3; j++) { + for (int k = 0; k < 3; k++) { + model->buffer[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k]; + } + model->buffer[i*12+j*4+3] = 1; + } + } + + return model; +} + diff --git a/src/shader.c b/src/shader.c index e11d36a..fd0ee40 100644 --- a/src/shader.c +++ b/src/shader.c @@ -116,4 +116,3 @@ end: return retval; } -