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 void gis_plugin_example_plugin_init(GisPluginInterface *iface);
32 static void gis_plugin_example_expose(GisPlugin *_self);
33 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self);
34 G_DEFINE_TYPE_WITH_CODE(GisPluginExample, gis_plugin_example, G_TYPE_OBJECT,
35 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
36 gis_plugin_example_plugin_init));
37 static void gis_plugin_example_plugin_init(GisPluginInterface *iface)
39 g_debug("GisPluginExample: plugin_init");
40 /* Add methods to the interface */
41 iface->expose = gis_plugin_example_expose;
42 iface->get_config = gis_plugin_example_get_config;
44 /* Class/Object init */
45 static void gis_plugin_example_init(GisPluginExample *self)
47 g_debug("GisPluginExample: init");
51 self->rotation = 30.0;
53 static void gis_plugin_example_dispose(GObject *gobject)
55 g_debug("GisPluginExample: dispose");
56 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
58 G_OBJECT_CLASS(gis_plugin_example_parent_class)->dispose(gobject);
60 static void gis_plugin_example_finalize(GObject *gobject)
62 g_debug("GisPluginExample: finalize");
63 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
65 G_OBJECT_CLASS(gis_plugin_example_parent_class)->finalize(gobject);
68 static void gis_plugin_example_class_init(GisPluginExampleClass *klass)
70 g_debug("GisPluginExample: class_init");
71 GObjectClass *gobject_class = (GObjectClass*)klass;
72 gobject_class->dispose = gis_plugin_example_dispose;
73 gobject_class->finalize = gis_plugin_example_finalize;
79 static gboolean rotate(gpointer _self)
81 GisPluginExample *self = _self;
82 if (gtk_toggle_button_get_active(self->button)) {
83 self->rotation += 1.0;
84 gis_opengl_redraw(self->opengl);
92 GisPluginExample *gis_plugin_example_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
94 g_debug("GisPluginExample: new");
95 GisPluginExample *self = g_object_new(GIS_TYPE_PLUGIN_EXAMPLE, NULL);
96 self->opengl = opengl;
97 self->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
99 /* Set up OpenGL Stuff */
100 g_timeout_add(1000/60, rotate, self);
105 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self)
107 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
108 return GTK_WIDGET(self->button);
111 static void gis_plugin_example_expose(GisPlugin *_self)
113 GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
114 g_debug("GisPluginExample: expose");
115 glDisable(GL_TEXTURE_2D);
116 glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity();
117 glOrtho(-1,1,-1,1,-10,10);
118 glMatrixMode(GL_MODELVIEW ); glPushMatrix(); glLoadIdentity();
120 float light_ambient[] = {0.1f, 0.1f, 0.0f};
121 float light_diffuse[] = {0.9f, 0.9f, 0.9f};
122 float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
123 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
124 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
125 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
127 glEnable(GL_LIGHTING);
128 glEnable(GL_COLOR_MATERIAL);
130 glTranslatef(0.5, -0.5, -2);
131 glRotatef(self->rotation, 1, 0, 1);
132 glColor4f(0.9, 0.9, 0.7, 1.0);
133 gdk_gl_draw_teapot(TRUE, 0.25);
134 glColor4f(1.0, 1.0, 1.0, 1.0);
136 glDisable(GL_LIGHT0);
137 glDisable(GL_LIGHTING);
138 glDisable(GL_COLOR_MATERIAL);
140 glMatrixMode(GL_PROJECTION); glPopMatrix();
141 glMatrixMode(GL_MODELVIEW ); glPopMatrix();