2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
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/>.
28 static gboolean rotate(gpointer _teapot)
30 GritsPluginTeapot *teapot = _teapot;
31 if (gtk_toggle_button_get_active(teapot->button)) {
32 teapot->rotation += 1.0;
33 gtk_widget_queue_draw(GTK_WIDGET(teapot->viewer));
38 static void expose(GritsCallback *callback, GritsOpenGL *opengl, gpointer _teapot)
40 GritsPluginTeapot *teapot = GRITS_PLUGIN_TEAPOT(_teapot);
41 g_debug("GritsPluginTeapot: expose");
43 glMatrixMode(GL_PROJECTION);
45 glOrtho(1,-1, -1,1, -10,10);
47 glMatrixMode(GL_MODELVIEW);
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);
58 glTranslatef(-0.5, -0.5, -2);
59 glRotatef(teapot->rotation, 1, 1, 0);
60 glColor4f(0.9, 0.9, 0.7, 1.0);
61 glDisable(GL_CULL_FACE);
69 GritsPluginTeapot *grits_plugin_teapot_new(GritsViewer *viewer, GritsPrefs *prefs)
71 g_debug("GritsPluginTeapot: new");
72 GritsPluginTeapot *teapot = g_object_new(GRITS_TYPE_PLUGIN_TEAPOT, NULL);
73 teapot->viewer = viewer;
76 GritsCallback *callback = grits_callback_new(expose, teapot);
77 grits_viewer_add(viewer, GRITS_OBJECT(callback), GRITS_LEVEL_OVERLAY+1, 0);
82 static GtkWidget *grits_plugin_teapot_get_config(GritsPlugin *_teapot)
84 GritsPluginTeapot *teapot = GRITS_PLUGIN_TEAPOT(_teapot);
85 return GTK_WIDGET(teapot->button);
93 static void grits_plugin_teapot_plugin_init(GritsPluginInterface *iface);
94 G_DEFINE_TYPE_WITH_CODE(GritsPluginTeapot, grits_plugin_teapot, G_TYPE_OBJECT,
95 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
96 grits_plugin_teapot_plugin_init));
97 static void grits_plugin_teapot_plugin_init(GritsPluginInterface *iface)
99 g_debug("GritsPluginTeapot: plugin_init");
100 /* Add methods to the interface */
101 iface->get_config = grits_plugin_teapot_get_config;
103 /* Class/Object init */
104 static void grits_plugin_teapot_init(GritsPluginTeapot *teapot)
106 g_debug("GritsPluginTeapot: init");
108 teapot->button = GTK_TOGGLE_BUTTON(gtk_toggle_button_new_with_label("Rotate"));
109 teapot->rotate_id = g_timeout_add(1000/60, rotate, teapot);
110 teapot->rotation = 30.0;
112 static void grits_plugin_teapot_dispose(GObject *gobject)
114 g_debug("GritsPluginTeapot: dispose");
115 GritsPluginTeapot *teapot = GRITS_PLUGIN_TEAPOT(gobject);
116 g_source_remove(teapot->rotate_id);
117 /* Drop references */
118 G_OBJECT_CLASS(grits_plugin_teapot_parent_class)->dispose(gobject);
120 static void grits_plugin_teapot_class_init(GritsPluginTeapotClass *klass)
122 g_debug("GritsPluginTeapot: class_init");
123 int argc = 1; char *argv[] = {"", NULL};
124 glutInit(&argc, argv);
125 GObjectClass *gobject_class = (GObjectClass*)klass;
126 gobject_class->dispose = grits_plugin_teapot_dispose;