File: ellipsoid.c

package info (click to toggle)
sipp 3.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,268 kB
  • ctags: 744
  • sloc: ansic: 8,534; makefile: 304; lex: 65; sh: 14
file content (268 lines) | stat: -rw-r--r-- 10,292 bytes parent folder | download | duplicates (3)
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
/**
 ** sipp - SImple Polygon Processor
 **
 **  A general 3d graphic package
 **
 **  Copyright Equivalent Software HB  1992
 **
 ** 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 1, or 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 can receive a copy of the GNU General Public License from the
 ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 **/

/**
 ** ellipsoid.c - Creating ellipsiods and spheres as sipp objects.
 **/

#include <xalloca.h>
#include <math.h>

#include <sipp.h>
#include <primitives.h>


Object *
sipp_ellipsoid(x_rad, y_rad, z_rad, res, surface, shader, texture)
    double  x_rad;
    double  y_rad;
    double  z_rad;
    int     res;
    void   *surface;
    Shader *shader;
    int     texture;
{
    int      i, j;
    double   factor;
    double   factor1;
    double   factor2;
    double   zradprim;
    double   zradprim1;
    double   zradprim2;
    double  *x_arr;
    double  *y_arr;
    double  *u_arr;
    double  *v1, *v2;
    Object  *ellipsoid;
    bool     old_user_refs;
    
    /* Odd resolutions make ugly spheres since the poles will be */
    /* different in size. */
    if (res & 1) {
        res++;
    }

    /* Create two arrays with the coordinates of the points */
    /* around the perimeter at Z = 0 */
    x_arr = (double *) alloca((res + 1) * sizeof(double));
    y_arr = (double *) alloca((res + 1) * sizeof(double));
    u_arr = (double *) alloca((res + 1) * sizeof(double));
    if (texture == SPHERICAL || texture == NATURAL) {
        v1    = (double *) alloca((res + 1) * sizeof(double));
        v2    = (double *) alloca((res + 1) * sizeof(double));
    }
    for (i = 0; i <= res; i++) {
        x_arr[i] = x_rad * cos(i * 2.0 * M_PI / res);
        y_arr[i] = y_rad * sin(i * 2.0 * M_PI / res);
        u_arr[i] = (double)i / (double)res;
    }

    /* Create the top pole */
    factor = sin(2.0 * M_PI / res);
    zradprim = z_rad * cos(2.0 * M_PI / res);
    if (texture == SPHERICAL || texture == NATURAL) {
        for (i = 0; i < res; i++) {
            v2[i] = (atan(zradprim 
                          / sqrt(factor * factor * (x_arr[i] * x_arr[i] 
                                                    + y_arr[i] * y_arr[i])))
                     / M_PI + 0.5);
        }
        v2[i] = v2[0];
    }
    for (i = 0; i < res; i++) {
        switch (texture) {
          case NATURAL:
          case SPHERICAL:
            vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
                           u_arr[i], v2[i], 0.0);
            vertex_tx_push(factor * x_arr[i + 1], factor * y_arr[i + 1],
                           zradprim, 
                           u_arr[i + 1], v2[i + 1], 0.0);
            vertex_tx_push(0.0, 0.0, z_rad, u_arr[i + 1], 1.0, 0.0);
            vertex_tx_push(0.0, 0.0, z_rad, u_arr[i], 1.0, 0.0);
            break;

          case CYLINDRICAL:
            vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
                           u_arr[i], zradprim / (2.0 * z_rad) + 0.5, 0.0);
            vertex_tx_push(factor * x_arr[i + 1], factor * y_arr[i + 1],
                           zradprim, 
                           u_arr[i + 1], zradprim / (2.0 * z_rad) + 0.5, 0.0);
            vertex_tx_push(0.0, 0.0, z_rad, u_arr[i + 1], 1.0, 0.0);
            vertex_tx_push(0.0, 0.0, z_rad, u_arr[i], 1.0, 0.0);
            break;

          case WORLD:
          default:
            vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
                           x_arr[i] * factor, y_arr[i] * factor, zradprim);
            vertex_tx_push(factor * x_arr[i + 1],
                           factor * y_arr[i + 1],
                           zradprim, 
                           factor * x_arr[i + 1],
                           factor * y_arr[i + 1],
                           zradprim);
            vertex_tx_push(0.0, 0.0, z_rad, 0.0, 0.0, z_rad);
            break;
        }
        polygon_push();
    }

    /* Create the surface between the poles. */
    factor2 = factor;
    zradprim2 = zradprim;
    for (j = 1; j < res / 2 - 1; j++) {
        factor1 = factor2;
        factor2 = sin((j + 1) * M_PI / (res / 2));
        zradprim1 = zradprim2;
        zradprim2 = z_rad * cos((j + 1) * M_PI / (res / 2));
        if (texture == SPHERICAL || texture == NATURAL) {
         for (i = 0; i<=res; i++) v1[i] = v2[i];
            for (i = 0; i < res; i++) {
                v2[i] = (atan(zradprim2
                              / sqrt(factor2 * factor2 
                                     * (x_arr[i] * x_arr[i] 
                                        + y_arr[i] * y_arr[i])))
                         / M_PI + 0.5);
            }
            v2[i] = v2[0];
        }

        for (i = 0; i < res; i++) {
            switch (texture) {
              case NATURAL:
              case SPHERICAL:
                vertex_tx_push(factor1 * x_arr[i], factor1 * y_arr[i], 
                               zradprim1, 
                               u_arr[i], v1[i], 0.0);
                vertex_tx_push(factor2 * x_arr[i], factor2 * y_arr[i], 
                               zradprim2, 
                               u_arr[i], v2[i], 0.0);
                vertex_tx_push(factor2 * x_arr[i + 1], factor2 * y_arr[i + 1],
                               zradprim2, 
                               u_arr[i + 1], v2[i + 1], 0.0);
                vertex_tx_push(factor1 * x_arr[i + 1], factor1 * y_arr[i + 1],
                               zradprim1, 
                               u_arr[i + 1], v1[i + 1], 0.0);
                break;

              case CYLINDRICAL:
                vertex_tx_push(factor1 * x_arr[i], factor1 * y_arr[i], 
                               zradprim1, 
                               u_arr[i], zradprim1 / (2.0 * z_rad) + 0.5, 0.0);
                vertex_tx_push(factor2 * x_arr[i], factor2 * y_arr[i], 
                               zradprim2, 
                               u_arr[i], zradprim2 / (2.0 * z_rad) + 0.5, 0.0);
                vertex_tx_push(factor2 * x_arr[i + 1], factor2 * y_arr[i + 1],
                               zradprim2, 
                               u_arr[i + 1], zradprim2 / (2.0 * z_rad) + 0.5, 
                               0.0);
                vertex_tx_push(factor1 * x_arr[i + 1], factor1 * y_arr[i + 1],
                               zradprim1, 
                               u_arr[i + 1], zradprim1 / (2.0 * z_rad) + 0.5, 
                               0.0);
                break;

              case WORLD:
              default:
                vertex_tx_push(factor1 * x_arr[i], factor1 * y_arr[i], 
                               zradprim1, 
                               factor1 * x_arr[i], factor1 * y_arr[i], 
                               zradprim1);
                vertex_tx_push(factor2 * x_arr[i], factor2 * y_arr[i], 
                               zradprim2, 
                               factor2 * x_arr[i], factor2 * y_arr[i], 
                               zradprim2);
                vertex_tx_push(factor2 * x_arr[i + 1], factor2 * y_arr[i + 1],
                               zradprim2, 
                               factor2 * x_arr[i + 1], factor2 * y_arr[i + 1],
                               zradprim2);
                vertex_tx_push(factor1 * x_arr[i + 1], factor1 * y_arr[i + 1],
                               zradprim1, 
                               factor1 * x_arr[i + 1], factor1 * y_arr[i + 1],
                               zradprim1);
            }
            polygon_push();
        }
    }

    /* Create the bottom pole */
    factor = sin(2.0 * M_PI / res);
    zradprim = -z_rad * cos(2.0 * M_PI / res);
    for (i = 0; i < res; i++) {
        switch (texture) {
          case NATURAL:
          case SPHERICAL:
            vertex_tx_push(factor * x_arr[i + 1], factor * y_arr[i + 1],
                           zradprim, 
                           u_arr[i + 1], v2[i + 1], 0.0);
            vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
                           u_arr[i], v2[i], 0.0);
            vertex_tx_push(0.0, 0.0, -z_rad, 
                           u_arr[i], 0.0, 0.0);
            vertex_tx_push(0.0, 0.0, -z_rad, 
                           u_arr[i + 1], 0.0, 0.0);
            break;

          case CYLINDRICAL:
            vertex_tx_push(factor * x_arr[i + 1], factor * y_arr[i + 1],
                           zradprim, 
                           u_arr[i + 1], zradprim / (2.0 * z_rad) + 0.5, 0.0);
            vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
                           u_arr[i], zradprim / (2.0 * z_rad) + 0.5, 0.0);
            vertex_tx_push(0.0, 0.0, -z_rad, 
                           u_arr[i], 0.0, 0.0);
            vertex_tx_push(0.0, 0.0, -z_rad, 
                           u_arr[i + 1], 0.0, 0.0);
            break;

          case WORLD:
          default:
            vertex_tx_push(x_arr[i + 1] * factor, y_arr[i + 1] * factor,
                           zradprim, 
                           x_arr[i + 1] * factor, y_arr[i + 1] * factor,
                           zradprim);
            vertex_tx_push(x_arr[i] * factor, y_arr[i] * factor, zradprim, 
                           x_arr[i] * factor, y_arr[i] * factor, zradprim);
            vertex_tx_push(0.0, 0.0, -z_rad, 0.0, 0.0, -z_rad);
            break;
        }
        polygon_push();
    }

    ellipsoid = object_create();

    old_user_refs = sipp_user_refcount (FALSE);
    object_add_surface(ellipsoid, surface_create(surface, shader));
    sipp_user_refcount (old_user_refs);

    return ellipsoid;
}


Object *
sipp_sphere(radius, res, surface, shader, texture)
    double  radius;
    int     res;
    void   *surface;
    Shader *shader;
    int     texture;
{
    return sipp_ellipsoid(radius, radius, radius, res, surface, shader,
                          texture);
}