File: prism.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 (218 lines) | stat: -rw-r--r-- 6,412 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
/*
 * File: prism.c 
 *
 * Create a prism from a polygonal cross-section in XY.
 * The points must be given in counter-clockwise order as viewed from
 * the front (+ve Z).
 *
 * Author:  David Jones
 *          djones@awesome.berkeley.edu
 *          11may91 djones
 *
 * Adapted for inclusion into the SIPP package: Inge Wallin
 * Extended with 2D texture support:            Jonas Yngvesson
 */

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

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


Object *
sipp_prism(num_points, points, length, surface, shader, texture)
    int	      num_points;
    Vector  * points;
    double    length;
    void    * surface;
    Shader  * shader;
    int       texture;
{
    Object  * prism;
    double  * u;
    double  * v;
    int	      i;
    int	      j;

    switch (texture) {
      case NATURAL:
        u = (double *)alloca((num_points + 1) * sizeof(double));
        u[0] = 0.0;
        for (i = 1; i <= num_points; i++) {
            u[i] = u[i - 1] + sqrt(((points[i % num_points].x 
                                     - points[i - 1].x) 
                                    * (points[i % num_points].x 
                                       - points[i - 1].x))
                                   + ((points[i % num_points].y 
                                       - points[i - 1].y) 
                                      * (points[i % num_points].y 
                                         - points[i - 1].y)));
        }
        for (i = 0; i <= num_points; i++) {
            u[i] /= u[num_points];
        }
        break;

      case SPHERICAL:
        v = (double *)alloca((num_points + 1) * sizeof(double));
        for (i = 0; i < num_points; i++) {
            v[i] = (atan(length / 2.0
                         / sqrt(points[i].x * points[i].x 
                                + points[i].y * points[i].y))
                    / M_PI + 0.5);
        }
        v[i] = v[0];

      case CYLINDRICAL:    /* Fall trough */
        u = (double *)alloca((num_points + 1) * sizeof(double));
        u[0] = atan2(points[0].y, points[0].x);
        for (i = 1; i < num_points; i++) {
            u[i] = atan2(points[i].y, points[i].x) - u[0];
            if (u[i] < 0.0) {
                u[i] += 2.0 * M_PI;
            }
            u[i] /= (2.0 * M_PI);
        }
        u[0] = 0.0;
        u[i] = 1.0;
        break;

      case WORLD:
      default:
        break;
    }

    prism = object_create();

    /* The top. */
    for (i = 0; i < num_points ; ++i) {
        switch (texture) {
          case NATURAL:
          case CYLINDRICAL:
          case SPHERICAL:
            vertex_tx_push(points[i].x, points[i].y, length / 2.0, 
                           0.0, 1.0, 0.0);
            break;

          case WORLD:
          default:
            vertex_tx_push(points[i].x, points[i].y, length / 2.0, 
                           points[i].x, points[i].y, length / 2.0);
            break;
        }
    }
    polygon_push();
    object_add_surface(prism, surface_create(surface, shader) );

    /* The bottom */
    for (i = num_points - 1; i >= 0 ; --i) {
        switch (texture) {
          case NATURAL:
          case CYLINDRICAL:
          case SPHERICAL:
            vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
                           0.0, 0.0, 0.0);
            break;

          case WORLD:
          default:
            vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
                           points[i].x, points[i].y, -length / 2.0);
            break;
        }
    }
    polygon_push();
    object_add_surface(prism, surface_create(surface, shader) );

    /* The sides */
    for (i = 0; i < num_points ; ++i) {
        j = i + 1;
        if (j == num_points)
            j=0;
        switch (texture) {
          case NATURAL:
          case CYLINDRICAL:
            vertex_tx_push(points[i].x, points[i].y,  length / 2.0, 
                           u[i], 1.0, 0.0);
            vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
                           u[i], 0.0, 0.0);
            vertex_tx_push(points[j].x, points[j].y, -length / 2.0, 
                           u[i + 1], 0.0, 0.0);
            vertex_tx_push(points[j].x, points[j].y,  length / 2.0, 
                           u[i + 1], 1.0, 0.0);
            break;

          case SPHERICAL:
            vertex_tx_push(points[i].x, points[i].y,  length / 2.0, 
                           u[i], v[i], 0.0);
            vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
                           u[i], 1.0 - v[i], 0.0);
            vertex_tx_push(points[j].x, points[j].y, -length / 2.0, 
                           u[i + 1], 1.0 - v[i + 1], 0.0);
            vertex_tx_push(points[j].x, points[j].y,  length / 2.0, 
                           u[i + 1], v[i + 1], 0.0);
            break;

          case WORLD:
          default:
            vertex_tx_push(points[i].x, points[i].y,  length / 2.0, 
                           points[i].x, points[i].y,  length / 2.0);
            vertex_tx_push(points[i].x, points[i].y, -length / 2.0, 
                           points[i].x, points[i].y, -length / 2.0);
            vertex_tx_push(points[j].x, points[j].y, -length / 2.0, 
                           points[j].x, points[j].y, -length / 2.0);
            vertex_tx_push(points[j].x, points[j].y,  length / 2.0, 
                           points[j].x, points[j].y,  length / 2.0);
            break;
        }
        polygon_push();
        object_add_surface(prism, surface_create(surface, shader) );
    }
    
    return prism;
}



/*
 * A square block. Generated as a prism.
 */
Object *
sipp_block(xsize, ysize, zsize, surface, shader, texture)
    double    xsize;
    double    ysize;
    double    zsize;
    void    * surface;
    Shader  * shader;
    int       texture;
{
    Vector coor[4];

    xsize /= 2.0;
    ysize /= 2.0;

    coor[0].x =  xsize; coor[0].y = -ysize;
    coor[1].x =  xsize; coor[1].y =  ysize;
    coor[2].x = -xsize; coor[2].y =  ysize;
    coor[3].x = -xsize; coor[3].y = -ysize;

    return sipp_prism(4, coor, zsize, surface, shader, texture);
}



/*
 * A cube.
 */
Object *
sipp_cube(size, surface, shader, texture)
    double    size;
    void    * surface;
    Shader  * shader;
    int       texture;
{
    return sipp_block(size, size, size, surface, shader, texture);
}