~ubuntu-branches/ubuntu/natty/gdis/natty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Copyright (C) 2003 by Sean David Fleming

sean@ivec.org

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

The GNU GPL can also be found at http://www.gnu.org
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "gdis.h"
#include "coords.h"
#include "matrix.h"
#include "numeric.h"
#include "opengl.h"
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif

/* externals */
extern struct sysenv_pak sysenv;


struct varray_pak
{
/* config */
gint gl_method;
gint periodic;
gint num_indices;
gint num_vertices;

/* data */
GLuint *indices;
/*
GLdouble *vertices;
GLdouble *new_vertices;
GLdouble *normals;
*/
GLfloat *interleaved;

GLdouble *colours;
};


/**********************/
/* creation primitive */
/**********************/
gpointer va_init(void)
{
struct varray_pak *va;

va = g_malloc(sizeof(struct varray_pak));

va->gl_method = 0;
va->periodic = FALSE;
va->num_indices = 0;
va->num_vertices = 0;

va->indices = NULL;
/*
va->vertices = NULL;
va->new_vertices = NULL;
va->normals = NULL;
*/
va->interleaved = NULL;

va->colours = NULL;

return(va);
}

/*************************/
/* destruction primitive */
/*************************/
void va_free(gpointer ptr_varray)
{
struct varray_pak *va = ptr_varray;

g_free(va->indices);
/*
g_free(va->vertices);
g_free(va->new_vertices);
g_free(va->normals);
*/

g_free(va->interleaved);

g_free(va->colours);
g_free(va);
}


/* FIXME - these should really be guints */
gint cyclic_clamp(gint n, gint start, gint stop)
{
gint range, offset;

range = stop - start + 1;
offset = n - start;
offset /= range;

return(n - offset*range);
}

gint cyclic_inc(gint n, gint start, gint stop)
{
if (n < stop)
  return(n+1);
else
  return(start);
}

gint cyclic_dec(gint n, gint start, gint stop)
{
if (n > start)
  return(n-1);
else
  return(stop);
}

/* CURRENT */
#define DEBUG_VA_GENERATE_INDICES 0
void va_generate_indices(gint depth, struct varray_pak *va)
{
gint d, i, j, m, n;
gint mdiv, div, mstart, nstart;

g_assert(va != NULL);

n = 18 * depth*depth;

#if DEBUG_VA_GENERATE_INDICES
printf("allocating for indices: %d\n", n);
#endif

va->num_indices = n;
va->indices = g_malloc(n * sizeof(GLuint));

i = 0;
n=1;
mstart = 0;
for (d=1 ; d<depth+1 ; d++)
  {
  nstart = n;
  div = d*6;

mdiv = 6*(d-1);

  m = mstart;

/* special case start */
  for (j=div ; j-- ; )
    {

*(va->indices+i++) = n;
#if DEBUG_VA_GENERATE_INDICES
printf(" %d", *(va->indices+i-1));
#endif

*(va->indices+i++) = cyclic_dec(n, nstart, nstart+div-1);
#if DEBUG_VA_GENERATE_INDICES
printf(" %d", *(va->indices+i-1));
#endif
n++;

*(va->indices+i++) = m++;
#if DEBUG_VA_GENERATE_INDICES
printf(" %d", *(va->indices+i-1));
#endif

    if ((j+1) % d == 0)
      m--;
    else
      {

/* missing triangle */
*(va->indices+i++) = cyclic_dec(m, mstart, nstart-1);
#if DEBUG_VA_GENERATE_INDICES
printf(" (%d", *(va->indices+i-1));
#endif
*(va->indices+i++) = cyclic_clamp(m, mstart, nstart-1);
#if DEBUG_VA_GENERATE_INDICES
printf(" %d", *(va->indices+i-1));
#endif
*(va->indices+i++) = cyclic_dec(n, nstart, nstart+div-1);
#if DEBUG_VA_GENERATE_INDICES
printf(" %d)", *(va->indices+i-1));
#endif
 
      }


    if (m >= nstart)
      {
      m -= nstart;
      m += mstart;
      }

    }

#if DEBUG_VA_GENERATE_INDICES
printf("\n");
#endif

  mstart = nstart;
  }

}

/*******************************/
/* sphere array initialization */
/*******************************/
#define DEBUG_MAKE_SPHERE 0
void va_make_sphere(gpointer ptr_varray)
{
gint i, j, k, n;
gdouble layers, divisions, alpha, beta, phi, theta, sp, cp, st, ct;
struct varray_pak *va = ptr_varray;

va->gl_method = GL_TRIANGLES;

/* number of layers - azimuthal sweep */
layers = sysenv.render.sphere_quality+1.0;
alpha = 0.5*PI/layers;

/* alloc */
n=0;
for (i=0 ; i<(gint) 1.0+layers ; i++)
  n += i;
n *= 6;
n++;

/* TODO - index & vertices depend on quality */
/*
sysenv.render.sphere_quality;
*/

/* setup indices */
va_generate_indices(layers, va);
/*
va->num_indices = 18;
va->indices = g_malloc(va->num_indices * sizeof(GLuint));
memcpy(va->indices, sphere_indices, va->num_indices*sizeof(GLuint));
*/

/* setup vertices and normals */
va->num_vertices = n;
/*
va->vertices = g_malloc(3 * n * sizeof(GLdouble));
va->new_vertices = g_malloc(3 * n * sizeof(GLdouble));
va->normals = g_malloc(3 * n * sizeof(GLdouble));
*/
va->interleaved = g_malloc(6 * n * sizeof(GLfloat));


/* initial vertex */
VEC3SET((va->interleaved), 0.0, 0.0, -1.0);
VEC3SET((va->interleaved+3), 0.0, 0.0, -1.0);

/* compute vertices */
k = 2;
for (i=1 ; i<(gint) 1.0+layers ; i++)
  {
#if DEBUG_MAKE_SPHERE
printf("[%d/%d]\n", i, (gint) layers);
#endif

  phi = i*alpha;
  sp = tbl_sin(phi);
  cp = tbl_cos(phi);

/* divisions at this level - hexagonal sweep */
  divisions = i*6.0;
  beta = 2.0*PI/divisions;
  for (j=0 ; j<(gint) divisions ; j++)
    {
    theta = j*beta;
    st = tbl_sin(theta);
    ct = tbl_cos(theta);

    VEC3SET((va->interleaved+3*k), ct*sp, st*sp, -cp);
    k++;
    VEC3SET((va->interleaved+3*k), ct*sp, st*sp, -cp);
    k++;

#if DEBUG_MAKE_SPHERE
printf("%7.4f %7.4f %7.4f\n", ct*sp, st*sp, -cp);
#endif
    }
  }

#if DEBUG_MAKE_SPHERE
printf("k = %d, n = %d\n", k, n);
#endif

/*
memcpy(va->normals, va->vertices, 3*n*sizeof(GLdouble));
*/
}

/*********************/
/* drawing primitive */
/*********************/
void va_draw_sphere(gpointer ptr_varray, gdouble *x, gdouble r)
{
gint i;
GLfloat *interleaved;
struct varray_pak *va = ptr_varray;

/* setup vertex/normal arrays */
interleaved = va->interleaved;
/*
memcpy(vertices, va->vertices, 3*va->num_vertices*sizeof(GLdouble));
*/

/* expand to required radius and move to desired location */
for (i=va->num_vertices ; i-- ; )
  {
  ARR3SET((interleaved+6*i), (va->interleaved+6*i+3));
  VEC3MUL((interleaved+6*i), r);
  ARR3ADD((interleaved+6*i), x);
  }

/* draw vertex array */
/*
glVertexPointer(3, GL_DOUBLE, 0, vertices);
if (va->normals)
  glNormalPointer(GL_DOUBLE, 0, va->normals);
*/

glVertexPointer(3, GL_FLOAT, 6*sizeof(GLfloat), &interleaved[0]);
glNormalPointer(GL_FLOAT, 6*sizeof(GLfloat), &interleaved[3]);


glDrawElements(va->gl_method, va->num_indices, GL_UNSIGNED_INT, va->indices);
}