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