File: torus.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 (215 lines) | stat: -rw-r--r-- 5,581 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
/**
 ** 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.
 **/

/**
 ** torus.c - Creating a torus as a sipp object.
 **/

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

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


#define SWAPARR(a, b) {Vector *tmp; tmp = a; a = b; b = tmp;}


static Vector *arr1;
static Vector *arr2;
static Vector *tx1;
static Vector *tx2;

/*
 * Prototypes of internal functions.
 */
static void
arr_rot _ANSI_ARGS_((int    len,
                     double angle,
                     int    texture));

static void
push_band _ANSI_ARGS_((int    len));


static void
arr_rot(len, angle, texture)
    int    len;
    double angle;
    int    texture;
{
    int    i;
    double sa, ca;

    sa = sin(angle);
    ca = cos(angle);
    for (i = 0; i < len; i++) {
        arr2[i].x = arr1[i].x * ca - arr1[i].y * sa;
        arr2[i].y = arr1[i].x * sa + arr1[i].y * ca;
        arr2[i].z = arr1[i].z;
        switch (texture) {
          case NATURAL:
          case CYLINDRICAL:
          case SPHERICAL:
            tx2[i].x = tx1[i].x + angle / (2.0 * M_PI);
            tx2[i].y = tx1[i].y;
            tx2[i].z = tx1[i].z;
            break;

          case WORLD:
          default:
            tx2[i] = arr2[i];
            break;
        }
    }
    switch (texture) {
      case NATURAL:
      case CYLINDRICAL:
      case SPHERICAL:
        tx2[i].x = tx1[i].x + angle / (2.0 * M_PI);
        tx2[i].y = tx1[i].y;
        tx2[i].z = tx1[i].z;
        break;

      case WORLD:
      default:
        tx2[i] = tx2[0];
        break;
    }
}


static void
push_band(len)
    int    len;
{
    int i, j;

    for (i = 0; i < len; i++) {
        j = (i + 1) % len;
        vertex_tx_push(arr1[i].x, arr1[i].y, arr1[i].z, 
                       tx1[i].x, tx1[i].y, tx1[i].z);
        vertex_tx_push(arr2[i].x, arr2[i].y, arr2[i].z, 
                       tx2[i].x, tx2[i].y, tx2[i].z);
        vertex_tx_push(arr2[j].x, arr2[j].y, arr2[j].z, 
                       tx2[i + 1].x, tx2[i + 1].y, tx2[i + 1].z);
        vertex_tx_push(arr1[j].x, arr1[j].y, arr1[j].z, 
                       tx1[i + 1].x, tx1[i + 1].y, tx1[i + 1].z);
        polygon_push();
    }
}



Object *
sipp_torus(bigradius, smallradius, res1, res2, surface, shader, texture)
    double  bigradius;      /* Radius of the ring */
    double  smallradius;    /* Radius of the "tube" */
    int     res1;           /* Number of polygons around the ring */
    int     res2;           /* Number of polygons around the tube */
    void   *surface;
    Shader *shader;
    int     texture;
{
    Object *torus;
    double  angle;
    int     i;
    bool    old_user_refs;

    /* Create two arrays to hold vertices around the tube */
    arr1 = (Vector *)alloca(res2 * sizeof(Vector));
    arr2 = (Vector *)alloca(res2 * sizeof(Vector));

    /* Create two arrays to hold texture coordinates  around the tube */
    tx1 = (Vector *)alloca((res2 + 1) * sizeof(Vector));
    tx2 = (Vector *)alloca((res2 + 1) * sizeof(Vector));

    for (i = 0; i < res2; i++) {
        angle = i * 2.0 * M_PI / res2 - M_PI / 4.0;
        arr1[i].x = bigradius + smallradius * cos(angle);
        arr1[i].y = 0.0;
        arr1[i].z = smallradius * sin(angle);
        switch (texture) {
          case NATURAL:
            tx1[i].x = 0.0;
            tx1[i].y = (double)i / (double)res2;
            tx1[i].z = 0.0;
            break;

          case CYLINDRICAL:
            tx1[i].x = 0.0;
            tx1[i].y = (arr1[i].z + smallradius) / (2.0 * smallradius);
            tx1[i].z = 0.0;
            break;

          case SPHERICAL:
            tx1[i].x = 0.0;
            tx1[i].y = atan(arr1[i].z / arr1[i].x) / M_PI + 0.5;
            tx1[i].z = 0.0;
            break;

          case WORLD:
          default:
            tx1[i] = arr1[i];
            break;
        }
    }

    switch (texture) {
      case NATURAL:
        tx1[i].x = 0.0;
        tx1[i].y = (double)i / (double)res2;
        tx1[i].z = 0.0;
        break;
        
      case CYLINDRICAL:
        tx1[i].x = 0.0;
        tx1[i].y = (arr1[0].z + smallradius) / (2.0 * smallradius);
        tx1[i].z = 0.0;
        break;
        
      case SPHERICAL:
        tx1[i].x = 0.0;
        tx1[i].y = atan(arr1[0].z / arr1[0].x) / M_PI + 0.5;
        tx1[i].z = 0.0;
        break;
        
      case WORLD:
      default:
        tx1[i] = tx1[0];
        break;
    }
    
    /* Sweep out the torus by rotating the two perimeters */
    /* defined in arr1 and arr2. */
    for (i = 0; i < res1; i++) {
        arr_rot(res2, 2.0 * M_PI / (double)res1, texture);
        push_band(res2);
        SWAPARR(arr1, arr2)
        SWAPARR(tx1, tx2)
    }

    torus = object_create();

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

    return torus;
}