2 * Copyright (C) 2009-2010 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/>.
19 * SECTION:grits-volume
20 * @short_description: 3-D gridded vovlume
22 * Each #GritsVolume consistes of a 3-dimentional grid of data points each
23 * consisting of a value.
25 * Currently iso-surfaces are extracted and displayed when rendering the volume
33 #include <gdk/gdkgl.h>
34 #include "grits-volume.h"
37 static void draw_points(VolGrid *grid, gdouble level)
39 glPointSize(200./grid->xs);
41 for (int x = 0; x < grid->xs; x++)
42 for (int y = 0; y < grid->ys; y++)
43 for (int z = 0; z < grid->zs; z++) {
44 VolPoint *pt = vol_grid_get(grid, x, y, z);
45 if (pt->value < level)
48 g_debug("(%d,%d,%d) value=%f", x, y, z, pt->value);
50 glColor4f(1.0, 1.0, 1.0, pt->value/100);
51 glVertex3dv((double*)&pt->c);
56 static void draw_iso(GList *tris)
58 g_debug("GritsVolume: draw_iso");
59 glDisable(GL_CULL_FACE);
60 glBegin(GL_TRIANGLES);
61 for (GList *cur = tris; cur; cur = cur->next) {
62 VolTriangle *tri = cur->data;
63 VolCoord c[3] = {tri->v[0]->c, tri->v[1]->c, tri->v[2]->c };
64 VolCoord n[3] = {tri->v[0]->norm, tri->v[1]->norm, tri->v[2]->norm};
66 /* Normalize normal vector */
67 for (int i = 0; i < 3; i++) {
74 n[i].x = n[i].x / total;
75 n[i].y = n[i].y / total;
76 n[i].z = n[i].z / total;
79 //for (int i = 0; i < 3; i++)
80 // g_debug("norm=%f,%f,%f",
81 // n[i].x, n[i].y, n[i].z);
82 //glNormal3dv((double*)&tri->norm);
83 glNormal3dv((double*)&n[0]); glVertex3dv((double*)&c[0]);
84 glNormal3dv((double*)&n[1]); glVertex3dv((double*)&c[1]);
85 glNormal3dv((double*)&n[2]); glVertex3dv((double*)&c[2]);
90 static void draw(GritsObject *_volume, GritsOpenGL *opengl)
92 g_debug("GritsVolume: draw");
93 GritsVolume *volume = GRITS_VOLUME(_volume);
97 switch (volume->disp) {
98 case GRITS_VOLUME_SURFACE:
99 glDisable(GL_COLOR_MATERIAL);
100 amb[0] = (double)volume->color[0] / 0xff;
101 amb[1] = (double)volume->color[1] / 0xff;
102 amb[2] = (double)volume->color[2] / 0xff;
104 dif[0] = (double)volume->color[0] / 0xff;
105 dif[1] = (double)volume->color[1] / 0xff;
106 dif[2] = (double)volume->color[2] / 0xff;
108 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
109 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif);
110 draw_iso(volume->tris);
112 case GRITS_VOLUME_POINTS:
113 draw_points(volume->grid, volume->level);
119 gboolean update_iso(gpointer _volume)
121 GritsVolume *volume = _volume;
123 g_list_foreach(volume->tris, (GFunc)vol_triangle_free, NULL);
124 g_list_free(volume->tris);
126 volume->tris = marching_cubes(volume->grid, volume->level);
127 volume->update_id = 0;
128 grits_object_queue_draw(GRITS_OBJECT(volume));
135 void grits_volume_set_level(GritsVolume *volume, gdouble level)
137 volume->level = level;
138 if (!volume->update_id)
139 volume->update_id = g_idle_add(update_iso, volume);
142 GritsVolume *grits_volume_new(VolGrid *grid)
144 g_debug("GritsVolume: new - %p[%d][%d][%d]",
145 grid, grid->xs, grid->ys, grid->zs);
146 GritsVolume *volume = g_object_new(GRITS_TYPE_VOLUME, NULL);
152 G_DEFINE_TYPE(GritsVolume, grits_volume, GRITS_TYPE_OBJECT);
153 static void grits_volume_init(GritsVolume *volume)
157 static void grits_volume_finalize(GObject *_volume)
159 GritsVolume *volume = GRITS_VOLUME(_volume);
160 volume->color[0] = 1;
161 volume->color[1] = 1;
162 volume->color[2] = 1;
163 volume->color[3] = 1;
164 //g_debug("GritsVolume: finalize - %s", volume->label);
167 static void grits_volume_class_init(GritsVolumeClass *klass)
169 g_debug("GritsVolume: class_init");
170 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
171 gobject_class->finalize = grits_volume_finalize;
173 GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
174 object_class->draw = draw;