]> Pileus Git - grits/blob - src/gis-prims.c
23376bb778cbf8bb1934ca79b500cb05d4986d37
[grits] / src / gis-prims.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-prims.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_point_set_xyz(&GIS_PRIMITIVE(self)->center,
82                 (a->x + b->x + c->x)/3,
83                 (a->y + b->y + c->y)/3,
84                 (a->z + b->z + c->z)/3);
85         self->verts[0] = gis_point_ref(a);
86         self->verts[1] = gis_point_ref(b);
87         self->verts[2] = gis_point_ref(c);
88         self->tex      = tex;
89         return self;
90 }
91
92 void gis_triangle_free(GisTriangle *self)
93 {
94         gis_point_unref(self->verts[0]);
95         gis_point_unref(self->verts[1]);
96         gis_point_unref(self->verts[2]);
97         g_free(self);
98 }
99
100
101 /* GisQuad */
102 GisQuad *gis_quad_new(GisPoint *a, GisPoint *b, GisPoint *c, GisPoint *d, guint tex)
103 {
104         GisQuad *self = g_new0(GisQuad, 1);
105         gis_point_set_xyz(&GIS_PRIMITIVE(self)->center,
106                 (a->x + b->x + c->x + d->x)/4,
107                 (a->y + b->y + c->y + d->y)/4,
108                 (a->z + b->z + c->z + d->z)/4);
109         self->verts[0] = gis_point_ref(a);
110         self->verts[1] = gis_point_ref(b);
111         self->verts[2] = gis_point_ref(c);
112         self->verts[3] = gis_point_ref(d);
113         self->tex      = tex;
114         return self;
115 }
116
117 void gis_quad_free(GisQuad *self)
118 {
119         gis_point_unref(self->verts[0]);
120         gis_point_unref(self->verts[1]);
121         gis_point_unref(self->verts[2]);
122         gis_point_unref(self->verts[3]);
123         g_free(self);
124 }
125
126
127 /* GisCallback */
128 GisCallback *gis_callback_new(GisCallbackFunc callback, gpointer user_data)
129 {
130         GisCallback *self = g_new0(GisCallback, 1);
131         self->callback  = callback;
132         self->user_data = user_data;
133         return self;
134 }
135
136 void gis_callback_free(GisCallback *self)
137 {
138         g_free(self);
139 }