]> Pileus Git - grits/blob - src/plugins/example.c
01ef9cad5f11a9647723eae9d8bc382041492b97
[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 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)
39 {
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;
44 }
45 /* Class/Object init */
46 static void gis_plugin_example_init(GisPluginExample *self)
47 {
48         g_debug("GisPluginExample: init");
49         /* Set defaults */
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;
53         self->opengl    = NULL;
54 }
55 static void gis_plugin_example_dispose(GObject *gobject)
56 {
57         g_debug("GisPluginExample: dispose");
58         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
59         g_source_remove(self->rotate_id);
60         /* Drop references */
61         G_OBJECT_CLASS(gis_plugin_example_parent_class)->dispose(gobject);
62 }
63 static void gis_plugin_example_finalize(GObject *gobject)
64 {
65         g_debug("GisPluginExample: finalize");
66         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
67         /* Free data */
68         G_OBJECT_CLASS(gis_plugin_example_parent_class)->finalize(gobject);
69
70 }
71 static void gis_plugin_example_class_init(GisPluginExampleClass *klass)
72 {
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;
77 }
78
79 /***********
80  * Helpers *
81  ***********/
82 static gboolean rotate(gpointer _self)
83 {
84         GisPluginExample *self = _self;
85         if (gtk_toggle_button_get_active(self->button)) {
86                 self->rotation += 1.0;
87                 gis_opengl_redraw(self->opengl);
88         }
89         return TRUE;
90 }
91
92 /***********
93  * Methods *
94  ***********/
95 GisPluginExample *gis_plugin_example_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
96 {
97         g_debug("GisPluginExample: new");
98         GisPluginExample *self = g_object_new(GIS_TYPE_PLUGIN_EXAMPLE, NULL);
99         self->opengl = opengl;
100
101         return self;
102 }
103
104 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self)
105 {
106         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
107         return GTK_WIDGET(self->button);
108 }
109
110 static void gis_plugin_example_expose(GisPlugin *_self)
111 {
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();
118
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);
125         glEnable(GL_LIGHT0);
126         glEnable(GL_LIGHTING);
127         glEnable(GL_COLOR_MATERIAL);
128
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);
134
135         glDisable(GL_LIGHT0);
136         glDisable(GL_LIGHTING);
137         glDisable(GL_COLOR_MATERIAL);
138
139         glMatrixMode(GL_PROJECTION); glPopMatrix(); 
140         glMatrixMode(GL_MODELVIEW ); glPopMatrix();
141         return;
142 }
143