/* Dessin avec shaders et VAA CC BY-SA Edouard.Thiel@univ-amu.fr - 04/01/2025 */ #include #include #include // Pour générer glad.h : https://glad.dav1d.de/ // C/C++, gl 4.5, OpenGL, Core, extensions: add all, local files #include "glad.h" #include const double FRAMES_PER_SEC = 30.0; const double ANIM_DURATION = 4.0; // En salle TP mettre à 0 si l'affichage "bave" const int NUM_SAMPLES = 16; class MyApp { bool m_ok = false; GLFWwindow* m_window = nullptr; double m_aspect_ratio = 1.0; bool m_anim_flag = false; float m_anim_y = 0; const char* m_vertex_shader_text = "#version 330\n" "in vec4 vPos;\n" "in vec3 vCol;\n" "out vec3 color;\n" "\n" "void main()\n" "{\n" " gl_Position = vPos;\n" " color = vCol;\n" "}\n"; const char* m_fragment_shader_text = "#version 330\n" "in vec3 color;\n" "out vec4 fragColor;\n" "\n" "void main()\n" "{\n" " fragColor = vec4(color, 1.0);\n" "}\n"; GLuint m_program = 0; GLint m_vPos_loc, m_vCol_loc; void animate() { // Change la coordonnée en fonction du temps double time = glfwGetTime(); // durée depuis init double slice = time / ANIM_DURATION; double a = slice - std::floor(slice); // partie fractionnaire m_anim_y = a; } void initGL() { std::cout << __func__ << std::endl; glEnable (GL_DEPTH_TEST); const GLuint vertex_shader = glCreateShader (GL_VERTEX_SHADER); glShaderSource (vertex_shader, 1, &m_vertex_shader_text, NULL); compile_shader (vertex_shader, "vertex"); const GLuint fragment_shader = glCreateShader (GL_FRAGMENT_SHADER); glShaderSource (fragment_shader, 1, &m_fragment_shader_text, NULL); compile_shader (fragment_shader, "fragment"); m_program = glCreateProgram(); glAttachShader (m_program, vertex_shader); glAttachShader (m_program, fragment_shader); link_program (m_program); // Récupère l'identifiant des "variables" dans les shaders m_vPos_loc = glGetAttribLocation (m_program, "vPos"); m_vCol_loc = glGetAttribLocation (m_program, "vCol"); } void displayGL() { //glClearColor (0.95, 1.0, 0.8, 1.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram (m_program); float y = 0.9-m_anim_y; GLfloat vertices[] = { -0.7, -0.5, -0.1, 0.8, -0.2, -0.1, 0.1, y, 0.3, -0.6, 0.7, -0.2, 0.8, 0.8, -0.2, 0.1, -0.9, 0.7 }; GLfloat colors[] = { 1.0, 0.6, 0.6, 1.0, 0.6, 0.6, 1.0, 0.6, 0.6, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0 }; glVertexAttribPointer (m_vPos_loc, 3, GL_FLOAT, GL_FALSE, 0, vertices); glVertexAttribPointer (m_vCol_loc, 3, GL_FLOAT, GL_FALSE, 0, colors); glEnableVertexAttribArray (m_vPos_loc); glEnableVertexAttribArray (m_vCol_loc); glDrawArrays (GL_TRIANGLES, 0, 6); glDisableVertexAttribArray (m_vPos_loc); glDisableVertexAttribArray (m_vCol_loc); } void compile_shader (GLuint shader, const char* name) { std::cout << "Compile " << name << " shader...\n"; glCompileShader (shader); GLint isCompiled = 0; glGetShaderiv (shader, GL_COMPILE_STATUS, &isCompiled); if (isCompiled == GL_FALSE) m_ok = false; GLsizei maxLength = 2048, length; char infoLog[maxLength]; glGetShaderInfoLog (shader, maxLength, &length, infoLog); if (length == 0) return; if (isCompiled == GL_TRUE) std::cout << "Compilation messages:\n"; else std::cout << "### Compilation errors:\n"; std::cout << infoLog << std::endl; } void link_program (GLuint program) { std::cout << "Link program...\n"; glLinkProgram (program); GLint status; glGetProgramiv (program, GL_LINK_STATUS, &status); if (status == GL_FALSE) m_ok = false; GLsizei maxLength = 2048, length; char infoLog[maxLength]; glGetProgramInfoLog (program, maxLength, &length, infoLog); if (length == 0) return; if (status == GL_TRUE) std::cout << "Linking messages:\n"; else std::cout << "### Linking errors:\n"; std::cout << infoLog << std::endl; } void set_viewport (int width, int height) { glViewport (0, 0, width, height); m_aspect_ratio = (double) width / height; } static void on_reshape_func (GLFWwindow* window, int width, int height) { std::cout << __func__ << " " << width << " " << height << std::endl; MyApp* that = static_cast(glfwGetWindowUserPointer (window)); that->set_viewport (width, height); } static void on_key_func (GLFWwindow* window, int key, int scancode, int action, int mods) { //std::cout << __func__ << " " << key << " " << scancode << " " // << action << " " << mods << std::endl; // action = GLFW_PRESS ou GLFW_REPEAT ou GLFW_RELEASE if (action == GLFW_RELEASE) return; MyApp* that = static_cast(glfwGetWindowUserPointer (window)); int trans_key = translate_qwerty_to_azerty (key, scancode); switch (trans_key) { case GLFW_KEY_A : that->m_anim_flag = !that->m_anim_flag; break; case GLFW_KEY_ESCAPE : that->m_ok = false; break; } } static int translate_qwerty_to_azerty (int key, int scancode) { // https://www.glfw.org/docs/latest/group__keys.html // QWERTY -> AZERTY switch (key) { case GLFW_KEY_Q : return GLFW_KEY_A; case GLFW_KEY_A : return GLFW_KEY_Q; case GLFW_KEY_W : return GLFW_KEY_Z; case GLFW_KEY_Z : return GLFW_KEY_W; case GLFW_KEY_SEMICOLON : return GLFW_KEY_M; } // Détection des différences non corrigées const char* name = glfwGetKeyName (key, scancode); if (name != NULL) { int capital = toupper(name[0]); if (capital != key) { std::cout << __func__ << " DIFF " << capital << " " << key << std::endl; } } return key; } static void on_error_func (int error, const char* description) { std::cerr << "Error: " << description << std::endl; } public: MyApp() { if (!glfwInit()) { std::cerr << "GLFW: initialization failed" << std::endl; return; } glfwSetErrorCallback (on_error_func); // Hints à spécifier avant la création de la fenêtre // https://www.glfw.org/docs/latest/window.html#window_hints_fb if (NUM_SAMPLES > 0) glfwWindowHint (GLFW_SAMPLES, NUM_SAMPLES); // On demande une version spécifique d'OpenGL glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE); // Création de la fenêtre m_window = glfwCreateWindow (640, 480, "Shaders et VAA", NULL, NULL); if (!m_window) { std::cerr << "GLFW: window creation failed" << std::endl; return; } // Les callbacks pour GLFW étant statiques, on mémorise l'instance glfwSetWindowUserPointer (m_window, this); glfwSetWindowSizeCallback (m_window, on_reshape_func); glfwSetKeyCallback (m_window, on_key_func); // Rend le contexte GL courant. Tous les appels GL seront placés après. glfwMakeContextCurrent (m_window); glfwSwapInterval (1); m_ok = true; // Initialisation de la machinerie GL en utilisant GLAD. gladLoadGL(); std::cout << "Loaded OpenGL " << GLVersion.major << "." << GLVersion.minor << std::endl; // Mise à jour viewport et ratio avec taille réelle de la fenêtre int width, height; glfwGetWindowSize (m_window, &width, &height); set_viewport (width, height); initGL(); } void run() { while (m_ok && !glfwWindowShouldClose (m_window)) { displayGL(); glfwSwapBuffers (m_window); if (m_anim_flag) { glfwWaitEventsTimeout (1.0/FRAMES_PER_SEC); animate(); } else glfwWaitEvents(); } } ~MyApp() { if (m_window) glfwDestroyWindow (m_window); glfwTerminate(); } }; // MyApp int main() { MyApp app; app.run(); }