diff --git a/.gitignore b/.gitignore index fc8463e..2ba72a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -**/tags.* \ No newline at end of file +**/tags.* +bin/* +build/* \ No newline at end of file diff --git a/COPYING b/LICENSE similarity index 86% rename from COPYING rename to LICENSE index 10926e8..be3f7b2 100644 --- a/COPYING +++ b/LICENSE @@ -1,23 +1,21 @@ - GNU GENERAL PUBLIC LICENSE - Version 3, 29 June 2007 + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble - The GNU General Public License is a free, copyleft license for -software and other kinds of works. + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, -the GNU General Public License is intended to guarantee your freedom to +our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free -software for all its users. We, the Free Software Foundation, use the -GNU General Public License for most of our software; it applies also to -any other work released this way by its authors. You can apply it to -your programs, too. +software for all its users. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you @@ -26,44 +24,34 @@ them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. - To protect your rights, we need to prevent others from denying you -these rights or asking you to surrender the rights. Therefore, you have -certain responsibilities if you distribute copies of the software, or if -you modify it: responsibilities to respect the freedom of others. + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must pass on to the recipients the same -freedoms that you received. You must make sure that they, too, receive -or can get the source code. And you must show them these terms so they -know their rights. + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. - Developers that use the GNU GPL protect your rights with two steps: -(1) assert copyright on the software, and (2) offer you this License -giving you legal permission to copy, distribute and/or modify it. + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. - For the developers' and authors' protection, the GPL clearly explains -that there is no warranty for this free software. For both users' and -authors' sake, the GPL requires that modified versions be marked as -changed, so that their problems will not be attributed erroneously to -authors of previous versions. - - Some devices are designed to deny users access to install or run -modified versions of the software inside them, although the manufacturer -can do so. This is fundamentally incompatible with the aim of -protecting users' freedom to change the software. The systematic -pattern of such abuse occurs in the area of products for individuals to -use, which is precisely where it is most unacceptable. Therefore, we -have designed this version of the GPL to prohibit the practice for those -products. If such problems arise substantially in other domains, we -stand ready to extend this provision to those domains in future versions -of the GPL, as needed to protect the freedom of users. - - Finally, every program is threatened constantly by software patents. -States should not allow patents to restrict development and use of -software on general-purpose computers, but in those that do, we wish to -avoid the special danger that patents applied to a free program could -make it effectively proprietary. To prevent this, the GPL assures that -patents cannot be used to render the program non-free. + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. The precise terms and conditions for copying, distribution and modification follow. @@ -72,7 +60,7 @@ modification follow. 0. Definitions. - "This License" refers to version 3 of the GNU General Public License. + "This License" refers to version 3 of the GNU Affero General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. @@ -549,35 +537,45 @@ to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. - 13. Use with the GNU Affero General Public License. + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed -under version 3 of the GNU Affero General Public License into a single +under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, -but the special requirements of the GNU Affero General Public License, -section 13, concerning interaction through a network will apply to the -combination as such. +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of -the GNU General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the -Program specifies that a certain numbered version of the GNU General +Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the -GNU General Public License, you may choose any version ever published +GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future -versions of the GNU General Public License can be used, that proxy's +versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. @@ -635,41 +633,29 @@ the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by + it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + GNU Affero General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program. If not, see . + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. - If the program does terminal interaction, make it output a short -notice like this when it starts in an interactive mode: - - Copyright (C) - This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, your program's commands -might be different; for a GUI interface, you would use an "about box". + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. -For more information on this, and how to apply and follow the GNU GPL, see -. - - The GNU General Public License does not permit incorporating your program -into proprietary programs. If your program is a subroutine library, you -may consider it more useful to permit linking proprietary applications with -the library. If this is what you want to do, use the GNU Lesser General -Public License instead of this License. But first, please read -. - +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/data/shader.frag b/data/shader.frag new file mode 100755 index 0000000..fb8a76c --- /dev/null +++ b/data/shader.frag @@ -0,0 +1,9 @@ +#version 330 core + +in vec4 color; +out vec4 out_frag_color; + +void main(void) +{ + out_frag_color = color; +} \ No newline at end of file diff --git a/data/shader.vert b/data/shader.vert new file mode 100755 index 0000000..588e6fa --- /dev/null +++ b/data/shader.vert @@ -0,0 +1,16 @@ +#version 330 core + +uniform mat4 projection_matrix; +uniform mat4 model_matrix; +uniform mat4 view_matrix; + +layout(location=0) in vec3 in_position; +layout(location=1) in vec3 in_color; + +out vec4 color; + +void main(void) +{ + gl_Position = projection_matrix * view_matrix * model_matrix * vec4(in_position, 1); + color = vec4(1 * in_color.rgb, 1); +} \ No newline at end of file diff --git a/include/base.h b/include/base.h new file mode 100644 index 0000000..493eca0 --- /dev/null +++ b/include/base.h @@ -0,0 +1,34 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: Base header + * + * Copyright (C) 2023 Arthur Menges + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once + +enum +{ + X_AXIS, + Y_AXIS, + Z_AXIS, + + N_AXIS +}; diff --git a/include/graphics.h b/include/graphics.h new file mode 100644 index 0000000..88d2cb3 --- /dev/null +++ b/include/graphics.h @@ -0,0 +1,43 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: OpenGL utils header + * + * Copyright (C) 2023 Arthur Menges + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include + +void graphicsDraw(void); + +void graphicsInitBuffers( GLuint *vao_out, GLuint *buffer_out, + GLuint *color_buffer_out ); + +void graphicsInitShaders( GLuint *program_out, GLuint *m_out, + GLuint *v_out, GLuint *p_out ); + +void graphicsDebugCallback( GLenum source, GLenum type, GLuint id, + GLenum severity, GLsizei length, const GLchar *msg, + const void *data ); + +void graphicsInitGL(void); + +void graphicsShutdownGL(void); diff --git a/include/ui.h b/include/ui.h new file mode 100644 index 0000000..d6725f7 --- /dev/null +++ b/include/ui.h @@ -0,0 +1,32 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: User interface header + * + * Copyright (C) 2023 Arthur Menges + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include + +extern float rotation_angles[N_AXIS]; + +void on_activate(GtkApplication *app, gpointer user_data); + diff --git a/src/graphics.c b/src/graphics.c new file mode 100644 index 0000000..a42594a --- /dev/null +++ b/src/graphics.c @@ -0,0 +1,508 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: GL functions + * + * Copyright (C) 2023 Arthur Menges + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../include/base.h" +#include "../include/ui.h" + +#define VERTEX_SHADER_FILE "data/shader.vert" +#define FRAG_SHADER_FILE "data/shader.frag" + +/* -------------------------------------------------------------------------- */ + +static GLuint position_buffer; +static GLuint color_buffer; +static GLuint program; +static GLuint m_location; +static GLuint v_location; +static GLuint p_location; +static GLuint indices_nb; + +// v4----- v5 +// /| /| +// v1------v0| +// | | | | +// | |v7---|-|v6 +// |/ |/ +// v2------v3 +// +static GLfloat vertex_base[] = { + 0.5, 0.5, 0.5, // v0 + -0.5, 0.5, 0.5, // v1 + -0.5,-0.5, 0.5, // v2 + 0.5,-0.5, 0.5, // v3 + 0.5, 0.5,-0.5, // v4 + -0.5, 0.5,-0.5, // v5 + -0.5,-0.5,-0.5, // v6 + 0.5,-0.5,-0.5, // v7 +}; + +static GLubyte indices[] = { + 0,1, + 1,2, + 2,3, + 3,0, + + 4,5, + 5,6, + 6,7, + 7,4, + + 0,4, + 1,5, + 2,6, + 3,7, +}; + + +static GLfloat color_base[] = { + 0.8, 0.8, 0.8, // blanc + 0.8, 0.8, 0.2, // jaune + 0.8, 0.2, 0.2, // rouge + 0.2, 0.2, 0.2, // noir + 0.2, 0.2, 0.2, // gris + 0.2, 0.8, 0.8, // cyan + 0.2, 0.8, 0.2, // vert + 0.8, 0.2, 0.8, // magenta +}; + +/* -------------------------------------------------------------------------- */ + +void graphicsDebugCallback( GLenum source, GLenum type, GLuint id, GLenum severity, + GLsizei length, const GLchar *msg, const void *data ); + +static inline void compute_i(float *res) +{ + /* initialize to the identity matrix */ + res[0] = 1.f; res[4] = 0.f; res[8] = 0.f; res[12] = 0.f; + res[1] = 0.f; res[5] = 1.f; res[9] = 0.f; res[13] = 0.f; + res[2] = 0.f; res[6] = 0.f; res[10] = 1.f; res[14] = 0.f; + res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; +} + +static inline void compute_mvp(float *res, float phi, float theta, float psi) +{ + float x = phi *(G_PI / 180.f); + float y = theta *(G_PI / 180.f); + float z = psi *(G_PI / 180.f); + float c1 = cosf(x), s1 = sinf(x); + float c2 = cosf(y), s2 = sinf(y); + float c3 = cosf(z), s3 = sinf(z); + float c3c2 = c3 * c2; + float s3c1 = s3 * c1; + float c3s2s1 = c3 * s2 * s1; + float s3s1 = s3 * s1; + float c3s2c1 = c3 * s2 * c1; + float s3c2 = s3 * c2; + float c3c1 = c3 * c1; + float s3s2s1 = s3 * s2 * s1; + float c3s1 = c3 * s1; + float s3s2c1 = s3 * s2 * c1; + float c2s1 = c2 * s1; + float c2c1 = c2 * c1; + + compute_i(res); + + /* apply all three rotations using the three matrices: + * + * ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤ + * ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥ + * ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦ + */ + res[0] = c3c2; res[4] = s3c1 + c3s2s1; res[8] = s3s1 - c3s2c1; res[12] = 0.f; + res[1] = -s3c2; res[5] = c3c1 - s3s2s1; res[9] = c3s1 + s3s2c1; res[13] = 0.f; + res[2] = s2; res[6] = -c2s1; res[10] = c2c1; res[14] = 0.f; + res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f; + +} + +/* Create and compile a shader */ +static GLuint createShader(int type, const char *src) +{ + GLuint shader; + int status; + + shader = glCreateShader(type); + glShaderSource(shader, 1, &src, NULL); + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &status); + if(status == GL_FALSE) { + int log_len; + char *buffer; + + glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len); + + buffer = g_malloc(log_len + 1); + glGetShaderInfoLog(shader, log_len, NULL, buffer); + + g_warning("Compile failure in %s shader:\n%s", + type == GL_VERTEX_SHADER ? "vertex" : "fragment", + buffer); + + g_free(buffer); + + glDeleteShader(shader); + + return 0; + } + + return shader; +} + +static inline char *readShaderFile(char *filename) +{ + int fd = open(filename, O_RDONLY); + + if(fd < 0) { + printf("Couldn't read shader: %s\n",filename); + return NULL; + } + + int shader_size = lseek(fd, 0, SEEK_END) +1 ; + char *shader_contents = malloc(sizeof(char) * shader_size); + + lseek(fd, 0, SEEK_SET); + read(fd,shader_contents,shader_size); + + shader_contents[shader_size-1]='\0'; + + //printf("Shader %s content is :\n%s\n", filename, shader_contents); + + close(fd); + + return shader_contents; +} + +/* -------------------------------------------------------------------------- */ + +void graphicsDraw(void) +{ + float m[16]; + float v[16]; + float p[16]; + + /* Compute the model view projection matrix using the + * rotation angles specified through the GtkRange widgets + */ + compute_mvp(p, + rotation_angles[X_AXIS], + rotation_angles[Y_AXIS], + rotation_angles[Z_AXIS]); + compute_i(m); + compute_i(v); + + glClearColor(0, 0, 0, 1); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* Use our shaders */ + glUseProgram(program); + + /* Update the "mvp" matrix we use in the shader */ + glUniformMatrix4fv(m_location, 1, GL_FALSE, &m[0]); + glUniformMatrix4fv(v_location, 1, GL_FALSE, &v[0]); + glUniformMatrix4fv(p_location, 1, GL_FALSE, &p[0]); + + /* Use the vertices in our buffer */ + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, position_buffer); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); + + // couleurs + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, color_buffer); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); + + glEnable(GL_DEPTH_TEST); + + glDrawElements(GL_LINES, indices_nb, GL_UNSIGNED_BYTE, indices); + + /* We finished using the buffers and program */ + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glUseProgram(0); + + glFlush(); +} + + +/* Initialize the GL buffers */ +void graphicsInitBuffers( GLuint *vao_out, GLuint *buffer_out, + GLuint *color_buffer_out) +{ + GLuint vao, vertex_buffer, color_buffer; + + indices_nb = sizeof(indices) / sizeof(indices[0]); + + int vertex_nb = indices_nb * 3; + + + printf("Initialization of buffers with %u indices (=%u vertex)\n", + indices_nb, + vertex_nb); + + printf("Real vertex number is %lu long\n", + sizeof(vertex_base) / sizeof(vertex_base[0])); + + // We only use one VAO, so we always keep it bound + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + // vertices + glGenBuffers(1, &vertex_buffer); + glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_base), vertex_base, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + // colors + glGenBuffers(1, &color_buffer); + glBindBuffer(GL_ARRAY_BUFFER, color_buffer); + glBufferData(GL_ARRAY_BUFFER, sizeof(color_base), color_base, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + if(vao_out != NULL) + *vao_out = vao; + + if(buffer_out != NULL) + *buffer_out = vertex_buffer; + + if(color_buffer_out != NULL) + *color_buffer_out = color_buffer; +} + + + +/* Initialize the shaders and link them into a program */ +void graphicsInitShaders( GLuint *program_out, GLuint *m_out, + GLuint *v_out, GLuint *p_out) +{ + GLuint vertex, fragment; + GLuint program = 0; + GLuint m = 0; + GLuint v = 0; + GLuint p = 0; + int status; + + vertex = createShader(GL_VERTEX_SHADER, readShaderFile(VERTEX_SHADER_FILE)); + + if(vertex == 0) { + *program_out = 0; + return; + } + + fragment = createShader(GL_FRAGMENT_SHADER, readShaderFile(FRAG_SHADER_FILE)); + + if(fragment == 0) { + glDeleteShader(vertex); + *program_out = 0; + return; + } + + program = glCreateProgram(); + glAttachShader(program, vertex); + glAttachShader(program, fragment); + + glLinkProgram(program); + + glGetProgramiv(program, GL_LINK_STATUS, &status); + + if(status == GL_FALSE) { + int log_len; + char *buffer; + + glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len); + + buffer = g_malloc(log_len + 1); + glGetProgramInfoLog(program, log_len, NULL, buffer); + + g_warning("Linking failure:\n%s", buffer); + + g_free(buffer); + + glDeleteProgram(program); + program = 0; + + goto out; + } + + /* Get the location of the "mvp" uniform */ + m = glGetUniformLocation(program, "model_matrix"); + v = glGetUniformLocation(program, "view_matrix"); + p = glGetUniformLocation(program, "projection_matrix"); + + glDetachShader(program, vertex); + glDetachShader(program, fragment); + +out: + glDeleteShader(vertex); + glDeleteShader(fragment); + + if(program_out != NULL) + *program_out = program; + + if(m_out != NULL) + *m_out = m; + + if(v_out != NULL) + *v_out = v; + + if(p_out != NULL) + *p_out = p; +} + +void graphicsInitGL(void) +{ + glEnable(GL_DEBUG_OUTPUT); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); + glEnable(GL_MULTISAMPLE); + //glEnable(GL_POLYGON_SMOOTH); + //glEnable(GL_PROGRAM_POINT_SIZE); + + graphicsInitBuffers(NULL, &position_buffer, &color_buffer); + graphicsInitShaders(&program, &m_location, &v_location, &p_location); + glDebugMessageCallback(graphicsDebugCallback, NULL); +} + +void graphicsShutdownGL(void) +{ + glDeleteBuffers(1, &position_buffer); + glDeleteBuffers(1, &color_buffer); + glDeleteProgram(program); +} + + +/* -------------------------------------------------------------------------- */ + +void graphicsDebugCallback( GLenum source, GLenum type, GLuint id, GLenum severity, + GLsizei length, const GLchar *msg, const void *data ) +{ + const char *errsource; + const char *errtype; + const char *errseverity; + const GLubyte *string; + GLenum code; + + switch (source) { + case GL_DEBUG_SOURCE_API: + errsource = "API"; + break; + + case GL_DEBUG_SOURCE_WINDOW_SYSTEM: + errsource = "WINDOW SYSTEM"; + break; + + case GL_DEBUG_SOURCE_SHADER_COMPILER: + errsource = "SHADER COMPILER"; + break; + + case GL_DEBUG_SOURCE_THIRD_PARTY: + errsource = "THIRD PARTY"; + break; + + case GL_DEBUG_SOURCE_APPLICATION: + errsource = "APPLICATION"; + break; + + case GL_DEBUG_SOURCE_OTHER: + errsource = "UNKNOWN"; + break; + + default: + errsource = "UNKNOWN"; + break; + } + + switch (type) { + case GL_DEBUG_TYPE_ERROR: + errtype = "ERROR"; + break; + + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: + errtype = "DEPRECATED BEHAVIOR"; + break; + + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: + errtype = "UNDEFINED BEHAVIOR"; + break; + + case GL_DEBUG_TYPE_PORTABILITY: + errtype = "PORTABILITY"; + break; + + case GL_DEBUG_TYPE_PERFORMANCE: + errtype = "PERFORMANCE"; + break; + + case GL_DEBUG_TYPE_OTHER: + errtype = "OTHER"; + break; + + case GL_DEBUG_TYPE_MARKER: + errtype = "MARKER"; + break; + + default: + errtype = "UNKNOWN"; + break; + } + + switch (severity) { + case GL_DEBUG_SEVERITY_HIGH: + errseverity = "CRITICAL"; + break; + + case GL_DEBUG_SEVERITY_MEDIUM: + errseverity = "ERROR"; + break; + + case GL_DEBUG_SEVERITY_LOW: + errseverity = "WARNING"; + break; + + case GL_DEBUG_SEVERITY_NOTIFICATION: + errseverity = "INFO"; + break; + + default: + errseverity = "UNKNOWN"; + break; + } + + code = glGetError(); + string = gluErrorString(code); + + printf("[%s] %s (%s) from %s: %s\n", + errseverity, string, errtype, errsource, msg); +} + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..4884235 --- /dev/null +++ b/src/main.c @@ -0,0 +1,45 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: Main file + * + * Copyright (C) 2023 Arthur Menges + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include + +#include "../include/base.h" +#include "../include/ui.h" + +/* -------------------------------------------------------------------------- */ + +int main(int argc, char **argv) +{ + GtkApplication *app; + int res; + + app = gtk_application_new(NULL, G_APPLICATION_DEFAULT_FLAGS); + g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL); + res = g_application_run(G_APPLICATION(app), argc, argv); + g_object_unref(app); + + return res; +} + diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..71565b6 --- /dev/null +++ b/src/ui.c @@ -0,0 +1,191 @@ +/* + * Gem-graph OpenGL experiments + * + * Desc: User interface functions + * + * Copyright (C) 2023 Arthur Menges + * Copyright (C) 2023 Adrien Bourmault + * + * This file is part of Gem-graph. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include "../include/base.h" +#include "../include/graphics.h" + +/* -------------------------------------------------------------------------- */ + +float rotation_angles[N_AXIS] = { 0.0 }; // Rotation angles on each axis + +static GtkWidget *gl_area = NULL; + +/* -------------------------------------------------------------------------- */ + +static void on_axis_value_change(GtkAdjustment *adjustment, gpointer data); + +static inline GtkWidget *create_axis_slider(int axis) +{ + GtkWidget *box, *label, *slider; + GtkAdjustment *adj; + const char *text; + + box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); + + switch (axis) { + case X_AXIS: + text = "X"; + break; + + case Y_AXIS: + text = "Y"; + break; + + case Z_AXIS: + text = "Z"; + break; + + default: + g_assert_not_reached(); + } + + label = gtk_label_new(text); + gtk_box_append(GTK_BOX(box), label); + gtk_widget_show(label); + + adj = gtk_adjustment_new(0.0, 0.0, 360.0, 1.0, 12.0, 0.0); + g_signal_connect(adj, "value-changed", + G_CALLBACK(on_axis_value_change), + GINT_TO_POINTER(axis)); + slider = gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, adj); + gtk_box_append(GTK_BOX(box), slider); + gtk_widget_set_hexpand(slider, TRUE); + gtk_widget_show(slider); + + gtk_widget_show(box); + + return box; +} + +/* -------------------------------------------------------------------------- */ + +static void on_axis_value_change(GtkAdjustment *adjustment, gpointer data) +{ + int axis = GPOINTER_TO_INT(data); + + g_assert(axis >= 0 && axis < N_AXIS); + + /* Update the rotation angle */ + rotation_angles[axis] = gtk_adjustment_get_value(adjustment); + + /* Update the contents of the GL drawing area */ + gtk_widget_queue_draw(gl_area); +} + +static gboolean on_render(GtkGLArea * area, GdkGLContext * context) +{ + if(gtk_gl_area_get_error(area) != NULL) + return FALSE; + + graphicsDraw(); + + return TRUE; +} + + +/* We need to set up our state when we realize the GtkGLArea widget */ +static void on_realize(GtkWidget *widget) +{ + gtk_gl_area_make_current(GTK_GL_AREA(widget)); + + if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) + return; + + graphicsInitGL(); +} + +/* We should tear down the state when unrealizing */ +static void on_unrealize(GtkWidget *widget) +{ + gtk_gl_area_make_current(GTK_GL_AREA(widget)); + + if(gtk_gl_area_get_error(GTK_GL_AREA(widget)) != NULL) + return; + + graphicsShutdownGL(); +} + +static void on_close_window(GtkWidget *widget) +{ + /* Reset the state */ + gl_area = NULL; + + rotation_angles[X_AXIS] = 0.0; + rotation_angles[Y_AXIS] = 0.0; + rotation_angles[Z_AXIS] = 0.0; +} + +void on_activate(GtkApplication *app, gpointer user_data) +{ + GtkWidget *window, *box, *button, *controls; + int i, minor=4, major=0; + + window = gtk_application_window_new(app); + gtk_window_set_default_size(GTK_WINDOW(window), 400, 600); + gtk_window_set_title(GTK_WINDOW(window), "GemGL (essais pour Gem-graph en OpenGL)"); + g_signal_connect(window, "destroy", G_CALLBACK(on_close_window), NULL); + + box = gtk_box_new(GTK_ORIENTATION_VERTICAL, FALSE); + gtk_widget_set_margin_start(box, 12); + gtk_widget_set_margin_end(box, 12); + gtk_widget_set_margin_top(box, 12); + gtk_widget_set_margin_bottom(box, 12); + gtk_box_set_spacing(GTK_BOX(box), 6); + gtk_window_set_child(GTK_WINDOW(window), box); + + gl_area = gtk_gl_area_new(); + gtk_gl_area_set_required_version( GTK_GL_AREA(gl_area), minor, major); + gtk_gl_area_get_required_version( GTK_GL_AREA(gl_area), &minor, &major); + gtk_widget_set_hexpand(gl_area, TRUE); + gtk_widget_set_vexpand(gl_area, TRUE); + gtk_widget_set_size_request(gl_area, 100, 200); + gtk_box_append(GTK_BOX(box), gl_area); + + // We need to initialize and free GL resources, so we use + // the realize and unrealize signals on the widget + // + g_signal_connect(gl_area, "realize", G_CALLBACK(on_realize), NULL); + g_signal_connect(gl_area, "unrealize", G_CALLBACK(on_unrealize), NULL); + + // The main "draw" call for GtkGLArea + g_signal_connect(gl_area, "render", G_CALLBACK(on_render), NULL); + + controls = gtk_box_new(GTK_ORIENTATION_VERTICAL, FALSE); + gtk_box_append(GTK_BOX(box), controls); + gtk_widget_set_hexpand(controls, TRUE); + + for(i = 0; i < N_AXIS; i++) + gtk_box_append(GTK_BOX(controls), create_axis_slider(i)); + + button = gtk_button_new_with_label("Fermer"); + gtk_widget_set_hexpand(button, TRUE); + gtk_box_append(GTK_BOX(box), button); + g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_window_destroy), window); + + gtk_widget_show(window); +}