Debian Codegraph @

examples/book/glarea/exampleglarea.h

raw
1 /* gtkmm example Copyright (C) 2025 gtkmm development team
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #ifndef GTKMM_EXAMPLEGLAREA_H
18 #define GTKMM_EXAMPLEGLAREA_H
19
20 #include <gtkmm.h>
21 #include <glm/glm.hpp> // For glm::vec2, glm::vec3, glm::mat4
22 #include <epoxy/gl.h> // Include Epoxy for OpenGL function loading
23 #include "colors.h" // Include for Color struct and constants
24
25 // Class for our OpenGL Canvas, inherits from Gtk::GLArea.
26 class ExampleGLArea : public Gtk::GLArea
27 {
28 public:
29 ExampleGLArea();
30
31 protected:
32 // Signal handlers:
33 void on_realize() override;
34 void on_unrealize() override;
35 bool on_render(const Glib::RefPtr<Gdk::GLContext>& context) override;
36
37 // Mouse handling methods:
38 void on_mouse_press(int button, double x, double y);
39 void on_mouse_release(int n_press, double x, double y);
40 void on_mouse_motion(double x, double y);
41 bool on_scroll(double double_y);
42
43 private:
44 // OpenGL resource IDs
45 GLuint m_vao = 0;
46 GLuint m_position_buffer = 0;
47 GLuint m_index_buffer = 0;
48 GLuint m_edge_index_buffer = 0;
49 GLuint m_program = 0;
50 GLint m_mvp_location = -1;
51 GLint m_use_vertex_color_location = -1;
52 GLint m_line_color_uniform_location = -1;
53
54 // Camera and interaction state
55 float m_rotation_angles[3];
56 glm::vec2 m_last_mouse_pos;
57 bool m_is_rotating = false;
58 bool m_is_panning = false;
59 glm::vec3 m_camera_position;
60 glm::vec3 m_camera_target;
61 float m_rotation_speed = 0.5f;
62 float m_pan_speed = 0.005f;
63 float m_zoom_speed = 0.4f;
64
65 // Cube data (constants) - MUST be static const when defined outside the class
66 static const GLfloat m_default_cube_vertex_data[24 * 8];
67 static const GLuint m_default_cube_index_data[36];
68 static const GLuint m_edge_index_data[12 * 2];
69
70 // Private helper methods for OpenGL setup and drawing
71 void init_buffers();
72 GLuint create_shader(GLenum type, const char* src);
73 void init_shaders();
74 glm::mat4 compute_mvp();
75 void draw_object();
76 };
77
78 #endif // GTKMM_EXAMPLEGLAREA_H