]> Pileus Git - grits/blob - examples/plugin/teapot.c
e9c97960f9460375f7b1ae94e63ef44fb4fdabfb
[grits] / examples / plugin / teapot.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 <gtk/gtkgl.h>
19 #include <GL/gl.h>
20
21 #include <gis/gis.h>
22
23 #include "teapot.h"
24
25 /***********
26  * Helpers *
27  ***********/
28 static gboolean rotate(gpointer _self)
29 {
30         GisPluginTeapot *self = _self;
31         if (gtk_toggle_button_get_active(self->button)) {
32                 self->rotation += 1.0;
33                 gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
34         }
35         return TRUE;
36 }
37
38 static gpointer expose(GisCallback *callback, gpointer _self)
39 {
40         GisPluginTeapot *self = GIS_PLUGIN_TEAPOT(_self);
41         g_debug("GisPluginTeapot: expose");
42
43         glMatrixMode(GL_PROJECTION);
44         glLoadIdentity();
45         glOrtho(1,-1, -1,1, -10,10);
46
47         glMatrixMode(GL_MODELVIEW);
48         glLoadIdentity();
49
50         float light_ambient[]  = {0.1f, 0.1f, 0.0f, 1.0f};
51         float light_diffuse[]  = {0.9f, 0.9f, 0.9f, 1.0f};
52         float light_position[] = {-30.0f, 50.0f, 40.0f, 1.0f};
53         glLightfv(GL_LIGHT0, GL_AMBIENT,  light_ambient);
54         glLightfv(GL_LIGHT0, GL_DIFFUSE,  light_diffuse);
55         glLightfv(GL_LIGHT0, GL_POSITION, light_position);
56         glEnable(GL_COLOR_MATERIAL);
57
58         glTranslatef(-0.5, -0.5, -2);
59         glRotatef(self->rotation, 1, 1, 0);
60         glColor4f(0.9, 0.9, 0.7, 1.0);
61         glDisable(GL_CULL_FACE);
62         gdk_gl_draw_teapot(TRUE, 0.25);
63
64         return NULL;
65 }
66
67
68 /***********
69  * Methods *
70  ***********/
71 GisPluginTeapot *gis_plugin_teapot_new(GisViewer *viewer, GisPrefs *prefs)
72 {
73         g_debug("GisPluginTeapot: new");
74         GisPluginTeapot *self = g_object_new(GIS_TYPE_PLUGIN_TEAPOT, NULL);
75         self->viewer = viewer;
76
77         /* Add renderers */
78         GisCallback *callback = gis_callback_new(expose, self);
79         gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
80
81         return self;
82 }
83
84 static GtkWidget *gis_plugin_teapot_get_config(GisPlugin *_self)
85 {
86         GisPluginTeapot *self = GIS_PLUGIN_TEAPOT(_self);
87         return GTK_WIDGET(self->button);
88 }
89
90
91 /****************
92  * GObject code *
93  ****************/
94 /* Plugin init */
95 static void gis_plugin_teapot_plugin_init(GisPluginInterface *iface);
96 G_DEFINE_TYPE_WITH_CODE(GisPluginTeapot, gis_plugin_teapot, G_TYPE_OBJECT,
97                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
98                         gis_plugin_teapot_plugin_init));
99 static void gis_plugin_teapot_plugin_init(GisPluginInterface *iface)
100 {
101         g_debug("GisPluginTeapot: plugin_init");
102         /* Add methods to the interface */
103         iface->get_config = gis_plugin_teapot_get_config;
104 }
105 /* Class/Object init */
106 static void gis_plugin_teapot_init(GisPluginTeapot *self)
107 {
108         g_debug("GisPluginTeapot: init");
109         /* Set defaults */
110         self->button    = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
111         self->rotate_id = g_timeout_add(1000/60, rotate, self);
112         self->rotation  = 30.0;
113 }
114 static void gis_plugin_teapot_dispose(GObject *gobject)
115 {
116         g_debug("GisPluginTeapot: dispose");
117         GisPluginTeapot *self = GIS_PLUGIN_TEAPOT(gobject);
118         g_source_remove(self->rotate_id);
119         /* Drop references */
120         G_OBJECT_CLASS(gis_plugin_teapot_parent_class)->dispose(gobject);
121 }
122 static void gis_plugin_teapot_class_init(GisPluginTeapotClass *klass)
123 {
124         g_debug("GisPluginTeapot: class_init");
125         GObjectClass *gobject_class = (GObjectClass*)klass;
126         gobject_class->dispose  = gis_plugin_teapot_dispose;
127 }