diff options
Diffstat (limited to 'src/others/mimesh/g3dviewer-0.2.99.4/src/glarea.c')
-rw-r--r-- | src/others/mimesh/g3dviewer-0.2.99.4/src/glarea.c | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/src/others/mimesh/g3dviewer-0.2.99.4/src/glarea.c b/src/others/mimesh/g3dviewer-0.2.99.4/src/glarea.c new file mode 100644 index 0000000..dda4275 --- /dev/null +++ b/src/others/mimesh/g3dviewer-0.2.99.4/src/glarea.c | |||
@@ -0,0 +1,236 @@ | |||
1 | /* $Id: glarea.c 47 2006-05-22 17:53:38Z mmmaddd $ */ | ||
2 | |||
3 | /* | ||
4 | G3DViewer - 3D object viewer | ||
5 | |||
6 | Copyright (C) 2005, 2006 Markus Dahms <mad@automagically.de> | ||
7 | |||
8 | This program is free software; you can redistribute it and/or modify | ||
9 | it under the terms of the GNU General Public License as published by | ||
10 | the Free Software Foundation; either version 2 of the License, or | ||
11 | (at your option) any later version. | ||
12 | |||
13 | This program is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 | GNU General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU General Public License | ||
19 | along with this program; if not, write to the Free Software | ||
20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | */ | ||
22 | |||
23 | #ifdef HAVE_CONFIG_H | ||
24 | # include <config.h> | ||
25 | #endif | ||
26 | |||
27 | #include <stdlib.h> | ||
28 | |||
29 | #include <gtk/gtk.h> | ||
30 | #include <gtk/gtkgl.h> | ||
31 | #include <GL/gl.h> | ||
32 | |||
33 | #include "main.h" | ||
34 | #include "gl.h" | ||
35 | #include "glarea.h" | ||
36 | #include "gui_glade.h" | ||
37 | #include "trackball.h" | ||
38 | |||
39 | /* | ||
40 | * updates glarea widget (redraw) | ||
41 | */ | ||
42 | void glarea_update(GtkWidget *glarea) | ||
43 | { | ||
44 | gtk_widget_queue_draw_area(glarea, 0, 0, | ||
45 | glarea->allocation.width, glarea->allocation.height); | ||
46 | } | ||
47 | |||
48 | /* | ||
49 | * handler for "expose" event | ||
50 | */ | ||
51 | gint glarea_expose(GtkWidget *widget, GdkEventExpose *event) | ||
52 | { | ||
53 | GdkGLDrawable *gldrawable; | ||
54 | GdkGLContext *glcontext; | ||
55 | G3DViewer *viewer = (G3DViewer*)g_object_get_data(G_OBJECT(widget), | ||
56 | "viewer"); | ||
57 | |||
58 | if(event->count > 0) return TRUE; | ||
59 | |||
60 | gldrawable = gtk_widget_get_gl_drawable(widget); | ||
61 | glcontext = gtk_widget_get_gl_context(widget); | ||
62 | |||
63 | if(!gdk_gl_drawable_gl_begin(gldrawable, glcontext)) return TRUE; | ||
64 | |||
65 | gl_draw(viewer->glflags, viewer->zoom, viewer->aspect, viewer->bgcolor, | ||
66 | viewer->quat, viewer->model); | ||
67 | |||
68 | gdk_gl_drawable_swap_buffers(gldrawable); | ||
69 | gdk_gl_drawable_gl_end(gldrawable); | ||
70 | |||
71 | return TRUE; | ||
72 | } | ||
73 | |||
74 | /* | ||
75 | * handler for "configure" event | ||
76 | */ | ||
77 | gint glarea_configure(GtkWidget *widget, GdkEventConfigure *event) | ||
78 | { | ||
79 | GdkGLDrawable *gldrawable; | ||
80 | GdkGLContext *glcontext; | ||
81 | G3DViewer *viewer; | ||
82 | |||
83 | gldrawable = gtk_widget_get_gl_drawable(widget); | ||
84 | glcontext = gtk_widget_get_gl_context(widget); | ||
85 | |||
86 | if(!gdk_gl_drawable_gl_begin(gldrawable, glcontext)) return TRUE; | ||
87 | |||
88 | viewer = (G3DViewer*)g_object_get_data(G_OBJECT(widget), "viewer"); | ||
89 | glViewport(0,0, widget->allocation.width, widget->allocation.height); | ||
90 | viewer->aspect = (gfloat)widget->allocation.width / | ||
91 | (gfloat)widget->allocation.height; | ||
92 | #if DEBUG > 3 | ||
93 | g_printerr("DEBUG: glarea_configure (%f)\n", viewer->aspect); | ||
94 | #endif | ||
95 | |||
96 | gdk_gl_drawable_gl_end(gldrawable); | ||
97 | |||
98 | return TRUE; | ||
99 | } | ||
100 | |||
101 | /* | ||
102 | * handler for "destroy" event | ||
103 | */ | ||
104 | gint glarea_destroy(GtkWidget *widget) | ||
105 | { | ||
106 | G3DViewer *viewer = (G3DViewer*)g_object_get_data(G_OBJECT(widget), | ||
107 | "viewer"); | ||
108 | if(viewer->interface.glarea) | ||
109 | { | ||
110 | /* FIXME: */ | ||
111 | } | ||
112 | |||
113 | return TRUE; | ||
114 | } | ||
115 | |||
116 | /* | ||
117 | * handler for "scroll" event (mouse wheel) | ||
118 | */ | ||
119 | gint glarea_scroll(GtkWidget *widget, GdkEventScroll *event) | ||
120 | { | ||
121 | GdkRectangle area; | ||
122 | G3DViewer *viewer = (G3DViewer*)g_object_get_data(G_OBJECT(widget), | ||
123 | "viewer"); | ||
124 | |||
125 | #define ZOOM_BY 10 | ||
126 | if(event->direction == GDK_SCROLL_DOWN) viewer->zoom += ZOOM_BY; | ||
127 | else viewer->zoom -= ZOOM_BY; | ||
128 | #undef ZOOM_BY | ||
129 | |||
130 | area.x = 0; | ||
131 | area.y = 0; | ||
132 | area.width = widget->allocation.width; | ||
133 | area.height = widget->allocation.height; | ||
134 | |||
135 | if(viewer->zoom < 1) viewer->zoom = 1; | ||
136 | if(viewer->zoom > 120) viewer->zoom = 120; | ||
137 | |||
138 | glarea_update(widget); | ||
139 | |||
140 | return FALSE; | ||
141 | } | ||
142 | |||
143 | /* | ||
144 | * handler for "button pressed" event | ||
145 | */ | ||
146 | gint glarea_button_pressed(GtkWidget *widget, GdkEventButton *event) | ||
147 | { | ||
148 | G3DViewer *viewer = (G3DViewer*)g_object_get_data(G_OBJECT(widget), | ||
149 | "viewer"); | ||
150 | |||
151 | /* left mouse buttom: rotate object */ | ||
152 | if(event->button == 1) | ||
153 | { | ||
154 | viewer->mouse.beginx = event->x; | ||
155 | viewer->mouse.beginy = event->y; | ||
156 | return TRUE; | ||
157 | } | ||
158 | /* right mouse button: pop-up menu */ | ||
159 | else if(event->button == 3) | ||
160 | { | ||
161 | if(event->type == GDK_BUTTON_PRESS) | ||
162 | { | ||
163 | GtkWidget *menu = | ||
164 | (GtkWidget*)g_object_get_data(G_OBJECT(widget), | ||
165 | "menu"); | ||
166 | gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL, | ||
167 | event->button, event->time); | ||
168 | return TRUE; | ||
169 | } | ||
170 | } | ||
171 | |||
172 | return FALSE; | ||
173 | } | ||
174 | |||
175 | /* | ||
176 | * handler for "motion notify" event | ||
177 | */ | ||
178 | gint glarea_motion_notify(GtkWidget *widget, GdkEventMotion *event) | ||
179 | { | ||
180 | gint x, y; | ||
181 | gchar *text; | ||
182 | GdkRectangle area; | ||
183 | GdkModifierType state; | ||
184 | G3DViewer *viewer = (G3DViewer*)g_object_get_data(G_OBJECT(widget), | ||
185 | "viewer"); | ||
186 | |||
187 | if(event->is_hint) | ||
188 | gdk_window_get_pointer(event->window, &x, &y, &state); | ||
189 | else | ||
190 | { | ||
191 | x = event->x; | ||
192 | y = event->y; | ||
193 | state = event->state; | ||
194 | } | ||
195 | |||
196 | area.x = 0; | ||
197 | area.y = 0; | ||
198 | area.width = widget->allocation.width; | ||
199 | area.height = widget->allocation.height; | ||
200 | |||
201 | /* left button pressed */ | ||
202 | if(state & GDK_BUTTON1_MASK) | ||
203 | { | ||
204 | gfloat spin_quat[4]; | ||
205 | trackball(spin_quat, | ||
206 | (2.0*viewer->mouse.beginx - area.width) / area.width, | ||
207 | ( area.height - 2.0*viewer->mouse.beginy) / area.height, | ||
208 | ( 2.0*x - area.width) / area.width, | ||
209 | ( area.height - 2.0*y) / area.height); | ||
210 | add_quats(spin_quat, viewer->quat, viewer->quat); | ||
211 | |||
212 | text = g_strdup_printf("quat: %-.2f, %-.2f, %-.2f, %-.2f", | ||
213 | viewer->quat[0], viewer->quat[1], | ||
214 | viewer->quat[2], viewer->quat[3]); | ||
215 | gui_glade_status(viewer, text); | ||
216 | g_free(text); | ||
217 | |||
218 | glarea_update(widget); | ||
219 | } | ||
220 | |||
221 | /* middle mouse button */ | ||
222 | if(state & GDK_BUTTON2_MASK) | ||
223 | { | ||
224 | viewer->zoom += | ||
225 | ((y - viewer->mouse.beginy) / (gfloat)area.height) * 40; | ||
226 | if(viewer->zoom < 1) viewer->zoom = 1; | ||
227 | if(viewer->zoom > 120) viewer->zoom = 120; | ||
228 | |||
229 | glarea_update(widget); | ||
230 | } | ||
231 | viewer->mouse.beginx = x; | ||
232 | viewer->mouse.beginy = y; | ||
233 | |||
234 | return TRUE; | ||
235 | } | ||
236 | |||