File: cone.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 (267 lines) | stat: -rw-r--r-- 8,126 bytes parent folder | download | duplicates (4)
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
/*
 * File:  sipp_cone.c
 * 
 * Create a, possibly truncated, cone. Cylinder is a special case
 * of a truncated cone with the same top and bottom radius.
 *
 * Author:  David Jones
 *          djones@awesome.berkeley.edu
 *
 * Adapted for inclusion into the SIPP package by Jonas Yngvesson
 */

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

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


Object *
sipp_cone(radius_bot, radius_top, length, res, surface, shader, texture)
    double    radius_bot;
    double    radius_top;
    double    length;
    int	      res;
    void    * surface;
    Shader  * shader;
    int       texture;
{
    Object  * cone;
    double  * xb;
    double  * yb;
    double  * ub;
    double  * vb;
    double  * xt;
    double  * yt;
    double  * ut;
    double  * vt;
    double    frac;
    double    half_length;
    int       bot_exists;
    int       top_exists;
    int       i;
    bool      old_user_refs;
    
    /*
     * If both top and bottom radii is zero it's a line
     * and can't be rendered.
     */
    if (radius_bot == 0.0 && radius_top == 0.0) {
        return NULL;
    }

    if (radius_bot > 0.0) {
        xb = (double *) alloca((res + 1) * sizeof(double));
        yb = (double *) alloca((res + 1) * sizeof(double));
        ub = (double *) alloca((res + 1) * sizeof(double));
        vb = (double *) alloca((res + 1) * sizeof(double));
        bot_exists = 1;
    } else if (radius_bot == 0.0 ){
        bot_exists = 0;
    } else {
        return NULL;
    }

    if (radius_top > 0.0) {
        xt = (double *) alloca((res + 1) * sizeof(double));
        yt = (double *) alloca((res + 1) * sizeof(double));
        ut = (double *) alloca((res + 1) * sizeof(double));
        vt = (double *) alloca((res + 1) * sizeof(double));
        top_exists = 1;
    } else if (radius_top == 0.0) {
        top_exists = 0;
    } else {
        return NULL;
    }
    
    half_length = length * 0.5;

    /* list of coordinates */
    for (i = 0; i <= res ; ++i) {
        frac = ((double) i) / ((double) res);
        if (bot_exists) {
            xb[i] = radius_bot * cos(frac * 2.0 * M_PI);
            yb[i] = radius_bot * sin(frac * 2.0 * M_PI);
            switch (texture) {
              case NATURAL:
              case CYLINDRICAL:
                ub[i] = frac;
                vb[i] = 0.0;
                break;

              case SPHERICAL:
                ub[i] = frac;
                vb[i] = atan(-half_length / radius_bot) / M_PI + 0.5;
                break;

              case WORLD:
              default:
                ub[i] = xb[i];
                vb[i] = yb[i];
                break;
            }
        }
        if (top_exists) {
            xt[i] = radius_top * cos(frac * 2.0 * M_PI);
            yt[i] = radius_top * sin(frac * 2.0 * M_PI);
            switch (texture) {
              case NATURAL:
              case CYLINDRICAL:
                ut[i] = frac;
                vt[i] = 1.0;
                break;

              case SPHERICAL:
                ut[i] = frac;
                vt[i] = atan(half_length / radius_top) / M_PI + 0.5;
                break;

              case WORLD:
              default:
                ut[i] = xt[i];
                vt[i] = yt[i];
                break;
            }
        }
    }
    
    /* empty object */
    cone = object_create();
    
    old_user_refs = sipp_user_refcount (FALSE);

    /* The bottom surface */
    if (bot_exists) {
        for (i = res; i > 0 ; --i) {
            vertex_tx_push(xb[i], yb[i], -half_length,
                           ub[i], vb[i], -half_length);
            vertex_tx_push(xb[i - 1], yb[i - 1], -half_length,
                           ub[i - 1], vb[i - 1], -half_length);
            switch (texture) {
              case NATURAL:
              case CYLINDRICAL:
                vertex_tx_push(0.0, 0.0, -half_length,
                               ub[i - 1], vb[i - 1], -half_length);
                vertex_tx_push(0.0, 0.0, -half_length,
                               ub[i], vb[1], -half_length);
                break;

              case SPHERICAL:
                vertex_tx_push(0.0, 0.0, -half_length,
                               ub[i - 1], 0.0, -half_length);
                vertex_tx_push(0.0, 0.0, -half_length,
                               ub[i], 0.0, -half_length);
                break;

              case WORLD:
              default:
                vertex_tx_push(0.0, 0.0, -half_length, 
                               0.0, 0.0, -half_length);
                break;
            }
            polygon_push();
        } 
        object_add_surface(cone, surface_create(surface, shader));
    }
    
    /* The top surface */
    if (top_exists) {
        for (i = 0; i < res ; ++i) {
            vertex_tx_push(xt[i], yt[i], half_length,
                           ut[i], vt[i], half_length);
            vertex_tx_push(xt[i + 1], yt[i + 1], half_length,
                           ut[i + 1], vt[i + 1], half_length);
            switch (texture) {
              case NATURAL:
              case CYLINDRICAL:
                vertex_tx_push(0.0, 0.0, half_length,  
                               ut[i + 1], vt[i + 1], half_length);
                vertex_tx_push(0.0, 0.0, half_length,  
                               ut[i], vt[i], half_length);
                break;

              case SPHERICAL:
                vertex_tx_push(0.0, 0.0, half_length,  
                               ut[i + 1], 1.0, half_length);
                vertex_tx_push(0.0, 0.0, half_length,  
                               ut[i], 1.0, half_length);
                break;

              case WORLD:
              default:
                vertex_tx_push(0.0, 0.0, half_length,
                               0.0, 0.0, half_length);
                break;
            }
            polygon_push();
        }
        object_add_surface(cone, surface_create(surface, shader));
    }
    
    /* The side surface */
    for (i = 0; i < res ; ++i) {
        if (top_exists && bot_exists) {
            vertex_tx_push(xb[i], yb[i], -half_length,
                           ub[i], vb[i], -half_length);
            vertex_tx_push(xb[i+1], yb[i+1], -half_length,
                           ub[i+1], vb[i+1], -half_length);
            vertex_tx_push(xt[i+1], yt[i+1], half_length,
                           ut[i+1], vt[i+1], half_length);
            vertex_tx_push(xt[i], yt[i], half_length,
                           ut[i], vt[i], half_length);
        } else if (top_exists) {
            vertex_tx_push(0.0, 0.0, -half_length,
                           0.0, 0.0, -half_length);
            vertex_tx_push(xt[i+1], yt[i+1], half_length,
                           ut[i+1], vt[i+1], half_length);
            vertex_tx_push(xt[i], yt[i], half_length,
                           ut[i], vt[i], half_length);
        } else {
            vertex_tx_push(xb[i], yb[i], -half_length,
                           ub[i], vb[i], -half_length);
            vertex_tx_push(xb[i+1], yb[i+1], -half_length,
                           ub[i+1], vb[i+1], -half_length);
            switch (texture) {
              case NATURAL:
              case CYLINDRICAL:
              case SPHERICAL:
                vertex_tx_push(0.0, 0.0, half_length,
                               0.0, 1.0, half_length);
                break;

              case WORLD:
              default:
                vertex_tx_push(0.0, 0.0, half_length,
                               0.0, 0.0, half_length);
                break;
            }
        }
        polygon_push();
    }
    object_add_surface(cone, surface_create(surface, shader));

    sipp_user_refcount (old_user_refs);
    
    return cone;
}



Object *
sipp_cylinder(radius, length, res, surface, shader, texture)
    double    radius;
    double    length;
    int       res;
    void     *surface;
    Shader   *shader;
    int       texture;
{
    Object   *cylinder;

    cylinder = sipp_cone(radius, radius, length, res, surface, shader,
                         texture); 

    return cylinder;
}