]> Pileus Git - grits/blob - src/objects/grits-volume.c
Switch from GtkGLExt to internal OpenGL handling
[grits] / src / objects / grits-volume.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 /**
19  * SECTION:grits-volume
20  * @short_description: 3-D gridded vovlume
21  *
22  * Each #GritsVolume consistes of a 3-dimentional grid of data points each
23  * consisting of a value.
24  *
25  * Currently iso-surfaces are extracted and displayed when rendering the volume
26  * data.
27  */
28
29 #include <config.h>
30 #include <math.h>
31 #include <glib.h>
32 #include <GL/gl.h>
33 #include "grits-volume.h"
34
35 /* Drawing */
36 static void draw_points(VolGrid *grid, gdouble level)
37 {
38         glPointSize(200./grid->xs);
39         glBegin(GL_POINTS);
40         for (int x = 0; x < grid->xs; x++)
41         for (int y = 0; y < grid->ys; y++)
42         for (int z = 0; z < grid->zs; z++) {
43                 VolPoint *pt = vol_grid_get(grid, x, y, z);
44                 if (pt->value < level)
45                         continue;
46
47                 g_debug("(%d,%d,%d) value=%f", x, y, z, pt->value);
48
49                 glColor4f(1.0, 1.0, 1.0, pt->value/100);
50                 glVertex3dv((double*)&pt->c);
51         }
52         glEnd();
53 }
54
55 static void draw_iso(GList *tris)
56 {
57         g_debug("GritsVolume: draw_iso");
58         glDisable(GL_CULL_FACE);
59         glBegin(GL_TRIANGLES);
60         for (GList *cur = tris; cur; cur = cur->next) {
61                 VolTriangle *tri = cur->data;
62                 VolCoord c[3] = {tri->v[0]->c,    tri->v[1]->c,    tri->v[2]->c   };
63                 VolCoord n[3] = {tri->v[0]->norm, tri->v[1]->norm, tri->v[2]->norm};
64
65                 /* Normalize normal vector */
66                 for (int i = 0; i < 3; i++) {
67                         double total = sqrt(
68                                 n[i].x * n[i].x +
69                                 n[i].y * n[i].y +
70                                 n[i].z * n[i].z);
71                         if (total == 0)
72                                 continue; // wtf
73                         n[i].x = n[i].x / total;
74                         n[i].y = n[i].y / total;
75                         n[i].z = n[i].z / total;
76                 }
77
78                 //for (int i = 0; i < 3; i++)
79                 //      g_debug("norm=%f,%f,%f",
80                 //              n[i].x, n[i].y, n[i].z);
81                 //glNormal3dv((double*)&tri->norm);
82                 glNormal3dv((double*)&n[0]); glVertex3dv((double*)&c[0]);
83                 glNormal3dv((double*)&n[1]); glVertex3dv((double*)&c[1]);
84                 glNormal3dv((double*)&n[2]); glVertex3dv((double*)&c[2]);
85         }
86         glEnd();
87 }
88
89 static void draw(GritsObject *_volume, GritsOpenGL *opengl)
90 {
91         g_debug("GritsVolume: draw");
92         GritsVolume *volume = GRITS_VOLUME(_volume);
93
94         gfloat amb[4] = {};
95         gfloat dif[4] = {};
96         switch (volume->disp) {
97         case GRITS_VOLUME_SURFACE:
98                 glDisable(GL_COLOR_MATERIAL);
99                 amb[0] = (double)volume->color[0] / 0xff;
100                 amb[1] = (double)volume->color[1] / 0xff;
101                 amb[2] = (double)volume->color[2] / 0xff;
102                 amb[3] = 1;
103                 dif[0] = (double)volume->color[0] / 0xff;
104                 dif[1] = (double)volume->color[1] / 0xff;
105                 dif[2] = (double)volume->color[2] / 0xff;
106                 dif[3] = 1;
107                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
108                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif);
109                 draw_iso(volume->tris);
110                 break;
111         case GRITS_VOLUME_POINTS:
112                 draw_points(volume->grid, volume->level);
113                 break;
114         }
115 }
116
117 /* Idle update */
118 gboolean update_iso(gpointer _volume)
119 {
120         GritsVolume *volume = _volume;
121         if (volume->tris) {
122                 g_list_foreach(volume->tris, (GFunc)vol_triangle_free, NULL);
123                 g_list_free(volume->tris);
124         }
125         volume->tris = marching_cubes(volume->grid, volume->level);
126         volume->update_id = 0;
127         grits_object_queue_draw(GRITS_OBJECT(volume));
128         return FALSE;
129 }
130
131 /***********
132  * Methods *
133  ***********/
134 void grits_volume_set_level(GritsVolume *volume, gdouble level)
135 {
136         volume->level = level;
137         if (!volume->update_id)
138                 volume->update_id = g_idle_add(update_iso, volume);
139 }
140
141 GritsVolume *grits_volume_new(VolGrid *grid)
142 {
143         g_debug("GritsVolume: new - %p[%d][%d][%d]",
144                         grid, grid->xs, grid->ys, grid->zs);
145         GritsVolume *volume = g_object_new(GRITS_TYPE_VOLUME, NULL);
146         volume->grid = grid;
147         return volume;
148 }
149
150 /* GObject code */
151 G_DEFINE_TYPE(GritsVolume, grits_volume, GRITS_TYPE_OBJECT);
152 static void grits_volume_init(GritsVolume *volume)
153 {
154 }
155
156 static void grits_volume_finalize(GObject *_volume)
157 {
158         GritsVolume *volume = GRITS_VOLUME(_volume);
159         volume->color[0] = 1;
160         volume->color[1] = 1;
161         volume->color[2] = 1;
162         volume->color[3] = 1;
163         //g_debug("GritsVolume: finalize - %s", volume->label);
164 }
165
166 static void grits_volume_class_init(GritsVolumeClass *klass)
167 {
168         g_debug("GritsVolume: class_init");
169         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
170         gobject_class->finalize = grits_volume_finalize;
171
172         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
173         object_class->draw = draw;
174 }