]> Pileus Git - grits/blob - src/plugins/example.c
Splitting GIS into a shared library, and a lot more
[grits] / src / plugins / example.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
3  * 
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.
8  * 
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.
13  * 
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/>.
16  */
17
18 #include <config.h>
19 #include <gtk/gtk.h>
20 #include <gtk/gtkgl.h>
21 #include <GL/gl.h>
22
23 #include <gis/gis.h>
24
25 #include "example.h"
26
27 /****************
28  * GObject code *
29  ****************/
30 /* Plugin init */
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)
38 {
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;
43 }
44 /* Class/Object init */
45 static void gis_plugin_example_init(GisPluginExample *self)
46 {
47         g_debug("GisPluginExample: init");
48         /* Set defaults */
49         self->opengl   = NULL;
50         self->button   = NULL;
51         self->rotation = 30.0;
52 }
53 static void gis_plugin_example_dispose(GObject *gobject)
54 {
55         g_debug("GisPluginExample: dispose");
56         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
57         /* Drop references */
58         G_OBJECT_CLASS(gis_plugin_example_parent_class)->dispose(gobject);
59 }
60 static void gis_plugin_example_finalize(GObject *gobject)
61 {
62         g_debug("GisPluginExample: finalize");
63         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
64         /* Free data */
65         G_OBJECT_CLASS(gis_plugin_example_parent_class)->finalize(gobject);
66
67 }
68 static void gis_plugin_example_class_init(GisPluginExampleClass *klass)
69 {
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;
74 }
75
76 /***********
77  * Helpers *
78  ***********/
79 static gboolean rotate(gpointer _self)
80 {
81         GisPluginExample *self = _self;
82         if (gtk_toggle_button_get_active(self->button)) {
83                 self->rotation += 1.0;
84                 gis_opengl_redraw(self->opengl);
85         }
86         return TRUE;
87 }
88
89 /***********
90  * Methods *
91  ***********/
92 GisPluginExample *gis_plugin_example_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
93 {
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"));
98
99         /* Set up OpenGL Stuff */
100         g_timeout_add(1000/60, rotate, self);
101
102         return self;
103 }
104
105 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self)
106 {
107         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
108         return GTK_WIDGET(self->button);
109 }
110
111 static void gis_plugin_example_expose(GisPlugin *_self)
112 {
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();
119
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);
126         glEnable(GL_LIGHT0);
127         glEnable(GL_LIGHTING);
128         glEnable(GL_COLOR_MATERIAL);
129
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);
135
136         glDisable(GL_LIGHT0);
137         glDisable(GL_LIGHTING);
138         glDisable(GL_COLOR_MATERIAL);
139
140         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
141         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
142         return;
143 }
144