]> Pileus Git - grits/blob - examples/plugin/teapot.c
Switch from GtkGLExt to internal OpenGL handling
[grits] / examples / plugin / teapot.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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 <GL/gl.h>
19 #include <GL/glut.h>
20
21 #include <grits.h>
22
23 #include "teapot.h"
24
25 /***********
26  * Helpers *
27  ***********/
28 static gboolean rotate(gpointer _teapot)
29 {
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));
34         }
35         return TRUE;
36 }
37
38 static void expose(GritsCallback *callback, GritsOpenGL *opengl, gpointer _teapot)
39 {
40         GritsPluginTeapot *teapot = GRITS_PLUGIN_TEAPOT(_teapot);
41         g_debug("GritsPluginTeapot: 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(teapot->rotation, 1, 1, 0);
60         glColor4f(0.9, 0.9, 0.7, 1.0);
61         glDisable(GL_CULL_FACE);
62         glutSolidTeapot(2.5);
63 }
64
65
66 /***********
67  * Methods *
68  ***********/
69 GritsPluginTeapot *grits_plugin_teapot_new(GritsViewer *viewer, GritsPrefs *prefs)
70 {
71         g_debug("GritsPluginTeapot: new");
72         GritsPluginTeapot *teapot = g_object_new(GRITS_TYPE_PLUGIN_TEAPOT, NULL);
73         teapot->viewer = viewer;
74
75         /* Add renderers */
76         GritsCallback *callback = grits_callback_new(expose, teapot);
77         grits_viewer_add(viewer, GRITS_OBJECT(callback), GRITS_LEVEL_OVERLAY+1, 0);
78
79         return teapot;
80 }
81
82 static GtkWidget *grits_plugin_teapot_get_config(GritsPlugin *_teapot)
83 {
84         GritsPluginTeapot *teapot = GRITS_PLUGIN_TEAPOT(_teapot);
85         return GTK_WIDGET(teapot->button);
86 }
87
88
89 /****************
90  * GObject code *
91  ****************/
92 /* Plugin init */
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)
98 {
99         g_debug("GritsPluginTeapot: plugin_init");
100         /* Add methods to the interface */
101         iface->get_config = grits_plugin_teapot_get_config;
102 }
103 /* Class/Object init */
104 static void grits_plugin_teapot_init(GritsPluginTeapot *teapot)
105 {
106         g_debug("GritsPluginTeapot: init");
107         /* Set defaults */
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;
111 }
112 static void grits_plugin_teapot_dispose(GObject *gobject)
113 {
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);
119 }
120 static void grits_plugin_teapot_class_init(GritsPluginTeapotClass *klass)
121 {
122         g_debug("GritsPluginTeapot: class_init");
123         GObjectClass *gobject_class = (GObjectClass*)klass;
124         gobject_class->dispose  = grits_plugin_teapot_dispose;
125 }