]> Pileus Git - grits/blob - src/gis-object.c
Add support for GisMarker to Gis{Viewer,OpenGL}
[grits] / src / gis-object.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 <glib.h>
19 #include <GL/glu.h>
20
21 #include "gis-object.h"
22 #include "gis-util.h"
23
24 /* TODO
25  *   - Manage normals for GisPoint
26  */
27
28 /* GisPoint */
29 GisPoint *gis_point_new()
30 {
31         return g_new0(GisPoint, 1);
32 }
33
34 void gis_point_set_lle(GisPoint *self, gdouble lat, gdouble lon, gdouble elev)
35 {
36         self->lat  = lat;
37         self->lon  = lon;
38         self->elev = elev;
39         lle2xyz(self->lat, self->lon, self->elev,
40                         &self->x, &self->y, &self->z);
41 }
42
43 void gis_point_set_xyz(GisPoint *self, gdouble x, gdouble y, gdouble z)
44 {
45         self->x = x;
46         self->y = y;
47         self->z = z;
48 }
49
50 void gis_point_set_coords(GisPoint *self, gdouble x, gdouble y)
51 {
52         self->cx = x;
53         self->cy = y;
54 }
55
56 void gis_point_project(GisPoint *self, GisProjection *proj)
57 {
58         gluProject(self->x, self->y, self->z,
59                    proj->model, proj->proj, proj->view,
60                    &self->px, &self->py, &self->pz);
61 }
62
63 GisPoint *gis_point_ref(GisPoint *self)
64 {
65         self->refs++;
66         return self;
67 }
68
69 void gis_point_unref(GisPoint *self)
70 {
71         self->refs--;
72         if (self->refs <= 0)
73                 g_free(self);
74 }
75
76
77 /* GisTriangle */
78 GisTriangle *gis_triangle_new(GisPoint *a, GisPoint *b, GisPoint *c, guint tex)
79 {
80         GisTriangle *self = g_new0(GisTriangle, 1);
81         GIS_OBJECT(self)->type = GIS_TYPE_TRIANGLE;
82         gis_point_set_xyz(&GIS_OBJECT(self)->center,
83                 (a->x + b->x + c->x)/3,
84                 (a->y + b->y + c->y)/3,
85                 (a->z + b->z + c->z)/3);
86         self->verts[0] = gis_point_ref(a);
87         self->verts[1] = gis_point_ref(b);
88         self->verts[2] = gis_point_ref(c);
89         self->tex      = tex;
90         return self;
91 }
92
93 void gis_triangle_free(GisTriangle *self)
94 {
95         gis_point_unref(self->verts[0]);
96         gis_point_unref(self->verts[1]);
97         gis_point_unref(self->verts[2]);
98         g_free(self);
99 }
100
101
102 /* GisQuad */
103 GisQuad *gis_quad_new(GisPoint *a, GisPoint *b, GisPoint *c, GisPoint *d, guint tex)
104 {
105         GisQuad *self = g_new0(GisQuad, 1);
106         GIS_OBJECT(self)->type = GIS_TYPE_QUAD;
107         gis_point_set_xyz(&GIS_OBJECT(self)->center,
108                 (a->x + b->x + c->x + d->x)/4,
109                 (a->y + b->y + c->y + d->y)/4,
110                 (a->z + b->z + c->z + d->z)/4);
111         self->verts[0] = gis_point_ref(a);
112         self->verts[1] = gis_point_ref(b);
113         self->verts[2] = gis_point_ref(c);
114         self->verts[3] = gis_point_ref(d);
115         self->tex      = tex;
116         return self;
117 }
118
119 void gis_quad_free(GisQuad *self)
120 {
121         gis_point_unref(self->verts[0]);
122         gis_point_unref(self->verts[1]);
123         gis_point_unref(self->verts[2]);
124         gis_point_unref(self->verts[3]);
125         g_free(self);
126 }
127
128
129 /* GisCallback */
130 GisCallback *gis_callback_new(GisCallbackFunc callback, gpointer user_data)
131 {
132         GisCallback *self = g_new0(GisCallback, 1);
133         GIS_OBJECT(self)->type = GIS_TYPE_CALLBACK;
134         self->callback  = callback;
135         self->user_data = user_data;
136         return self;
137 }
138
139 void gis_callback_free(GisCallback *self)
140 {
141         g_free(self);
142 }
143
144
145 /* GisCallback */
146 GisMarker *gis_marker_new(const gchar *label)
147 {
148         GisMarker *self = g_new0(GisMarker, 1);
149         GIS_OBJECT(self)->type = GIS_TYPE_MARKER;
150         self->label = g_strdup(label);;
151         return self;
152 }
153
154 void gis_marker_free(GisMarker *self)
155 {
156         g_free(self->label);
157         g_free(self);
158 }