2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <gtk/gtkgl.h>
31 static gboolean rotate(gpointer _self);
32 static void gis_plugin_example_plugin_init(GisPluginInterface *iface);
33 static void gis_plugin_example_expose(GisPlugin *_self);
34 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self);
35 G_DEFINE_TYPE_WITH_CODE(GisPluginExample, gis_plugin_example, G_TYPE_OBJECT,
36 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
37 gis_plugin_example_plugin_init));
38 static void gis_plugin_example_plugin_init(GisPluginInterface *iface)
40 g_debug("GisPluginExample: plugin_init");
41 /* Add methods to the interface */
42 iface->expose = gis_plugin_example_expose;
43 iface->get_config = gis_plugin_example_get_config;
45 /* Class/Object init */
46 static void gis_plugin_example_init(GisPluginExample *self)
48 g_debug("GisPluginExample: init");
50 self->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
51 self->rotate_id = g_timeout_add(1000/60, rotate, self);
52 self->rotation = 30.0;
55 static void gis_plugin_example_dispose(GObject *gobject)
57 g_debug("GisPluginExample: dispose");
58 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
59 g_source_remove(self->rotate_id);
61 G_OBJECT_CLASS(gis_plugin_example_parent_class)->dispose(gobject);
63 static void gis_plugin_example_finalize(GObject *gobject)
65 g_debug("GisPluginExample: finalize");
66 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
68 G_OBJECT_CLASS(gis_plugin_example_parent_class)->finalize(gobject);
71 static void gis_plugin_example_class_init(GisPluginExampleClass *klass)
73 g_debug("GisPluginExample: class_init");
74 GObjectClass *gobject_class = (GObjectClass*)klass;
75 gobject_class->dispose = gis_plugin_example_dispose;
76 gobject_class->finalize = gis_plugin_example_finalize;
82 static gboolean rotate(gpointer _self)
84 GisPluginExample *self = _self;
85 if (gtk_toggle_button_get_active(self->button)) {
86 self->rotation += 1.0;
87 gis_opengl_redraw(self->opengl);
95 GisPluginExample *gis_plugin_example_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
97 g_debug("GisPluginExample: new");
98 GisPluginExample *self = g_object_new(GIS_TYPE_PLUGIN_EXAMPLE, NULL);
99 self->opengl = opengl;
104 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self)
106 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
107 return GTK_WIDGET(self->button);
110 static void gis_plugin_example_expose(GisPlugin *_self)
112 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
113 g_debug("GisPluginExample: expose");
114 glDisable(GL_TEXTURE_2D);
115 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
116 glOrtho(-1,1,-1,1,-10,10);
117 glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
119 float light_ambient[] = {0.1f, 0.1f, 0.0f};
120 float light_diffuse[] = {0.9f, 0.9f, 0.9f};
121 float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
122 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
123 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
124 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
126 glEnable(GL_LIGHTING);
127 glEnable(GL_COLOR_MATERIAL);
129 glTranslatef(0.5, -0.5, -2);
130 glRotatef(self->rotation, 1, 0, 1);
131 glColor4f(0.9, 0.9, 0.7, 1.0);
132 gdk_gl_draw_teapot(TRUE, 0.25);
133 glColor4f(1.0, 1.0, 1.0, 1.0);
135 glDisable(GL_LIGHT0);
136 glDisable(GL_LIGHTING);
137 glDisable(GL_COLOR_MATERIAL);
139 glMatrixMode(GL_PROJECTION); glPopMatrix();
140 glMatrixMode(GL_MODELVIEW ); glPopMatrix();