]> Pileus Git - grits/blob - src/objects/grits-poly.c
865d2887f1ebafd79d08aa353a28d488698ca9ec
[grits] / src / objects / grits-poly.c
1 /*
2  * Copyright (C) 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-poly
20  * @short_description: Single point polys
21  *
22  * Each #GritsPoly represents a 3 dimentional polygon.
23  */
24
25 #include <config.h>
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 #include "grits-poly.h"
29
30 /* Drawing */
31 static void grits_poly_tess(gdouble (**points)[3])
32 {
33         //g_debug("GritsPoly: tess");
34
35         /* Tesselate */
36         GLUtesselator *tess = gluNewTess();
37         gluTessCallback(tess, GLU_TESS_BEGIN,  glBegin);
38         gluTessCallback(tess, GLU_TESS_VERTEX, glVertex3dv);
39         gluTessCallback(tess, GLU_TESS_END,    glEnd);
40         gluTessBeginPolygon(tess, NULL);
41         for (int pi = 0; points[pi]; pi++) {
42                 gluTessBeginContour(tess);
43                 for (int ci = 0; points[pi][ci][0]; ci++) {
44                         gluTessVertex(tess,
45                                 points[pi][ci],
46                                 points[pi][ci]);
47                 }
48                 gluTessEndContour(tess);
49         }
50         gluTessEndPolygon(tess);
51         gluDeleteTess(tess);
52
53         /* Outline */
54         for (int pi = 0; points[pi]; pi++) {
55                 glColor4d(1,1,1,0.2);
56                 glBegin(GL_LINE_LOOP);
57                 for (int ci = 0; points[pi][ci][0]; ci++)
58                         glVertex3dv(points[pi][ci]);
59                 glEnd();
60         }
61 }
62 static gboolean grits_poly_genlist(gpointer _poly)
63 {
64         //g_debug("GritsPoly: genlist");
65         GritsPoly *poly = GRITS_POLY(_poly);
66         guint list = glGenLists(1);
67         glNewList(list, GL_COMPILE);
68         grits_poly_tess(poly->points);
69         glEndList();
70         poly->list = list;
71         return FALSE;
72 }
73
74 static void grits_poly_draw(GritsObject *_poly, GritsOpenGL *opengl)
75 {
76         //g_debug("GritsPoly: draw");
77         GritsPoly *poly = GRITS_POLY(_poly);
78
79         if (!poly->list)
80                 return;
81
82         glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT);
83         glDisable(GL_TEXTURE_2D);
84         glDisable(GL_ALPHA_TEST);
85         glDisable(GL_CULL_FACE);
86         glDisable(GL_LIGHTING);
87         glColor4dv(poly->color);
88         glCallList(poly->list);
89         glPopAttrib();
90 }
91
92 /**
93  * grits_poly_new:
94  * @points:  TODO
95  * @npoints: TODO
96  *
97  * Create a new GritsPoly which TODO.
98  *
99  * Returns: the new #GritsPoly.
100  */
101 GritsPoly *grits_poly_new(gdouble (**points)[3])
102 {
103         //g_debug("GritsPoly: new - %p", points);
104         GritsPoly *poly = g_object_new(GRITS_TYPE_POLY, NULL);
105         poly->points    = points;
106         g_idle_add(grits_poly_genlist, poly);
107         return poly;
108 }
109
110 /* GObject code */
111 G_DEFINE_TYPE(GritsPoly, grits_poly, GRITS_TYPE_OBJECT);
112 static void grits_poly_init(GritsPoly *poly)
113 {
114 }
115
116 static void grits_poly_finalize(GObject *_poly)
117 {
118         g_debug("GritsPoly: finalize");
119         GritsPoly *poly = GRITS_POLY(_poly);
120         (void)poly;
121         // TODO: free points
122 }
123
124 static void grits_poly_class_init(GritsPolyClass *klass)
125 {
126         g_debug("GritsPoly: class_init");
127         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
128         gobject_class->finalize = grits_poly_finalize;
129
130         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
131         object_class->draw = grits_poly_draw;
132 }