]> Pileus Git - grits/blob - src/plugins/example.c
3d6a6683964c5fcef1241ee46b9225864d6664a9
[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  * Helpers *
29  ***********/
30 static gboolean rotate(gpointer _self)
31 {
32         GisPluginExample *self = _self;
33         if (gtk_toggle_button_get_active(self->button)) {
34                 self->rotation += 1.0;
35                 gis_opengl_redraw(self->opengl);
36         }
37         return TRUE;
38 }
39
40
41 /***********
42  * Methods *
43  ***********/
44 GisPluginExample *gis_plugin_example_new(GisWorld *world, GisView *view, GisOpenGL *opengl)
45 {
46         g_debug("GisPluginExample: new");
47         GisPluginExample *self = g_object_new(GIS_TYPE_PLUGIN_EXAMPLE, NULL);
48         self->opengl = opengl;
49
50         return self;
51 }
52
53 static GtkWidget *gis_plugin_example_get_config(GisPlugin *_self)
54 {
55         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
56         return GTK_WIDGET(self->button);
57 }
58
59 static void gis_plugin_example_expose(GisPlugin *_self)
60 {
61         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(_self);
62         g_debug("GisPluginExample: expose");
63
64         glMatrixMode(GL_PROJECTION);
65         glLoadIdentity();
66         glOrtho(1,-1, -1,1, -10,10);
67
68         glMatrixMode(GL_MODELVIEW);
69         glLoadIdentity();
70
71         float light_ambient[]  = {0.1f, 0.1f, 0.0f, 1.0f};
72         float light_diffuse[]  = {0.9f, 0.9f, 0.9f, 1.0f};
73         float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
74         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
75         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
76         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
77         glEnable(GL_COLOR_MATERIAL);
78
79         glTranslatef(-0.5, -0.5, -2);
80         glRotatef(self->rotation, 1, 1, 0);
81         glColor4f(0.9, 0.9, 0.7, 1.0);
82         glDisable(GL_CULL_FACE);
83         gdk_gl_draw_teapot(TRUE, 0.25);
84 }
85
86
87 /****************
88  * GObject code *
89  ****************/
90 /* Plugin init */
91 static void gis_plugin_example_plugin_init(GisPluginInterface *iface);
92 G_DEFINE_TYPE_WITH_CODE(GisPluginExample, gis_plugin_example, G_TYPE_OBJECT,
93                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
94                         gis_plugin_example_plugin_init));
95 static void gis_plugin_example_plugin_init(GisPluginInterface *iface)
96 {
97         g_debug("GisPluginExample: plugin_init");
98         /* Add methods to the interface */
99         iface->expose     = gis_plugin_example_expose;
100         iface->get_config = gis_plugin_example_get_config;
101 }
102 /* Class/Object init */
103 static void gis_plugin_example_init(GisPluginExample *self)
104 {
105         g_debug("GisPluginExample: init");
106         /* Set defaults */
107         self->button    = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
108         self->rotate_id = g_timeout_add(1000/60, rotate, self);
109         self->rotation  = 30.0;
110         self->opengl    = NULL;
111 }
112 static void gis_plugin_example_dispose(GObject *gobject)
113 {
114         g_debug("GisPluginExample: dispose");
115         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
116         g_source_remove(self->rotate_id);
117         /* Drop references */
118         G_OBJECT_CLASS(gis_plugin_example_parent_class)->dispose(gobject);
119 }
120 static void gis_plugin_example_finalize(GObject *gobject)
121 {
122         g_debug("GisPluginExample: finalize");
123         GisPluginExample *self = GIS_PLUGIN_EXAMPLE(gobject);
124         /* Free data */
125         G_OBJECT_CLASS(gis_plugin_example_parent_class)->finalize(gobject);
126
127 }
128 static void gis_plugin_example_class_init(GisPluginExampleClass *klass)
129 {
130         g_debug("GisPluginExample: class_init");
131         GObjectClass *gobject_class = (GObjectClass*)klass;
132         gobject_class->dispose  = gis_plugin_example_dispose;
133         gobject_class->finalize = gis_plugin_example_finalize;
134 }