Welcome to mirror list, hosted at ThFree Co, Russian Federation.

geometry.cpp « Graphics « Source - github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1128e4a459f5f437799de7ed49a2922d30a8ab34 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//-----------------------------------------------------------------------------
//           Name: geometry.cpp
//         Author: Freeglut
//        License: X-Consortium license
//    Description: Data and utility functions for rendering several useful 
//                 geometric shapes. This code is a modified version of the 
//                 code found in "freeglut_teapot.c" and "freeglut_geometry.c", 
//                 which is part of the open source project, Freeglut.
//                 http://freeglut.sourceforge.net/
//               
//                 Modified by Wolfire Games LLC for use in the project 
//                 Overgrowth.
//
// ----------------------------------------------------------------------------
// Freeglut Copyright
// ------------------
// 
// Freeglut code without an explicit copyright is covered by the following
// copyright :
// 
// Copyright(c) 1999 - 2000 Pawel W.Olszta.All Rights Reserved.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this softwareand associated documentation files(the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and /or sell
// copies or substantial portions of the Software.
// 
// The above  copyright notice and this permission notice  shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE  IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING  BUT  NOT LIMITED  TO THE WARRANTIES  OF MERCHANTABILITY,
// FITNESS  FOR  A PARTICULAR PURPOSE  AND NONINFRINGEMENT.IN  NO EVENT  SHALL
// PAWEL W.OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN  AN ACTION  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF  OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 
// Except as contained in this notice, the name of Pawel W.Olszta shall not be
// used  in advertising or otherwise to promote the sale, use or other dealings
// in this Software without prior written authorization from Pawel W.Olszta.
// ---------------------------------------------------------------------------
/*
 * freeglut_geometry.c
 *
 * Freeglut geometry rendering methods.
 *
 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
 * Creation date: Fri Dec 3 1999
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
/*
 * freeglut_teapot.c
 *
 * Teapot(tm) rendering code.
 *
 * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
 * Written by Pawel W. Olszta, <olszta@sourceforge.net>
 * Creation date: Fri Dec 24 1999
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
/*
 * Original teapot code copyright follows:
 */
/*
 * (c) Copyright 1993, Silicon Graphics, Inc.
 *
 * ALL RIGHTS RESERVED
 *
 * Permission to use, copy, modify, and distribute this software
 * for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that
 * both the copyright notice and this permission notice appear in
 * supporting documentation, and that the name of Silicon
 * Graphics, Inc. not be used in advertising or publicity
 * pertaining to distribution of the software without specific,
 * written prior permission.
 *
 * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU
 * "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR
 * OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  IN NO
 * EVENT SHALL SILICON GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE
 * ELSE FOR ANY DIRECT, SPECIAL, INCIDENTAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER,
 * INCLUDING WITHOUT LIMITATION, LOSS OF PROFIT, LOSS OF USE,
 * SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD PARTIES, WHETHER OR
 * NOT SILICON GRAPHICS, INC.  HAS BEEN ADVISED OF THE POSSIBILITY
 * OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 *
 * US Government Users Restricted Rights
 *
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions set forth in FAR 52.227f.19(c)(2) or subparagraph
 * (c)(1)(ii) of the Rights in Technical Data and Computer
 * Software clause at DFARS 252.227f-7013 and/or in similar or
 * successor clauses in the FAR or the DOD or NASA FAR
 * Supplement.  Unpublished-- rights reserved under the copyright
 * laws of the United States.  Contractor/manufacturer is Silicon
 * Graphics, Inc., 2011 N.  Shoreline Blvd., Mountain View, CA
 * 94039-7311.
 *
 * OpenGL(TM) is a trademark of Silicon Graphics, Inc.
 */
//
// The following functions are defined here:
//
// void renderWireTeapot(GLdouble size);
// void renderSolidTeapot(GLdouble size);
// void renderWireCube(GLdouble size);
// void renderSolidCube(GLdouble size);
// void renderWireSphere(GLdouble radius, GLint slices, GLint stacks);
// void renderSolidSphere(GLdouble radius, GLint slices, GLint stacks);
// void renderWireCone(GLdouble base, GLdouble height, GLint slices, GLint stacks);
// void renderSolidCone(GLdouble base, GLdouble height, GLint slices, GLint stacks);
// void renderWireCylinder(GLdouble base, GLdouble height, GLint slices, GLint stacks);
// void renderSolidCyliner(GLdouble base, GLdouble height, GLint slices, GLint stacks);
// void renderWireTorus(GLdouble innerRadius, GLdouble outerRadius, GLint sides, GLint rings);
// void renderSolidTorus(GLdouble innerRadius, GLdouble outerRadius, GLint sides, GLint rings);
// void renderWireDodecahedron(void);
// void renderSolidDodecahedron(void);
// void renderWireOctahedron(void);
// void renderSolidOctahedron(void);
// void renderWireTetrahedron(void);
// void renderSolidTetrahedron(void);
// void renderWireIcosahedron(void);
// void renderSolidIcosahedron(void);
// void renderWireSierpinskiSponge(int num_levels, GLdouble offset[3], GLdouble scale);
// void renderSolidSierpinskiSponge(int num_levels, GLdouble offset[3], GLdouble scale);
//-----------------------------------------------------------------------------

#include <Graphics/geometry.h>
#include <Graphics/graphics.h>

#include <Math/vec3math.h>
#include <Memory/allocation.h>

#include <cmath>

static void circleTable(double **sint,double **cost,const int n);

/* -- INTERFACE FUNCTIONS -------------------------------------------------- */

/*
 * Compute lookup table of cos and sin values forming a cirle
 *
 * Notes:
 *    It is the responsibility of the caller to free these tables
 *    The size of the table is (n+1) to form a connected loop
 *    The last entry is exactly the same as the first
 *    The sign of n can be flipped to get the reverse loop
 */

static void circleTable(double **sint,double **cost,const int n)
{
  int i;
  
  /* Table size, the sign of n flips the circle direction */
  
  const int size = abs(n);
  
  /* Determine the angle between samples */
  
  const double angle = 2*PI/(double)n;
  
  /* Allocate memory for n samples, plus duplicate of first entry at the end */
  
  *sint = (double *) calloc(sizeof(double), size+1);
  *cost = (double *) calloc(sizeof(double), size+1);
  
  /* Bail out if memory allocation fails, fgError never returns */
  
  if (!(*sint) || !(*cost))
  {
    OG_FREE(*sint);
    *sint = NULL;
    OG_FREE(*cost);
    *cost = NULL;
    FatalError("Error", "Failed to allocate memory in circleTable");
  }
  
  /* Compute cos and sin around the circle */
  
  for (i=0; i<size; i++)
  {
    (*sint)[i] = sin(angle*i);
    (*cost)[i] = cos(angle*i);
  }
  
  /* Last sample is duplicate of the first */
  
  (*sint)[size] = (*sint)[0];
  (*cost)[size] = (*cost)[0];
}

void GetWireCylinderVertArray(GLint slices, std::vector<vec3> &data)
{
    // x right, y up, z forward
    float rotationStep = (2.0f*PI_f)/(float)slices;
    float radius = 0.5f;
    float halfheight = 0.5f;

    vec3 centerTop( 0,halfheight,0 );
    vec3 centerBottom( 0, -halfheight,0);
   
    data.reserve( slices * 2 * 10 );

    for( int i = 0; i < slices; i++ )
    {
        float rotation1 = i*rotationStep;
        float rotation2 = (i+1)*rotationStep;

        vec3 offset1(radius*(std::cos(rotation1)-std::sin(rotation1)), 0.0f, radius*(std::sin(rotation1)+std::cos(rotation1)));
        vec3 pointTop1 = offset1+centerTop;
        vec3 pointBottom1 = offset1+centerBottom;

        vec3 offset2(radius*(std::cos(rotation2)-std::sin(rotation2)), 0.0f, radius*(std::sin(rotation2)+std::cos(rotation2)));
        vec3 pointTop2 = offset2+centerTop;
        vec3 pointBottom2 = offset2+centerBottom;

        data.push_back( centerTop );
        data.push_back( pointTop1 );

        data.push_back( pointTop1 );
        data.push_back( pointTop2 );

        data.push_back( pointTop1 );
        data.push_back( pointBottom1 );

        data.push_back( pointBottom1 );
        data.push_back( pointBottom2 );

        data.push_back( pointBottom1 );
        data.push_back( centerBottom );
    }      
}

void GetWireBoxVertArray( std::vector<vec3> &data )
{
    vec3 corners[] =
    {
        //Upper four corners
        vec3( 0.5f,   0.5f, 0.5f ),
        vec3( 0.5f,   0.5f, -0.5f ),
        vec3( -0.5f,  0.5f, -0.5f ),
        vec3( -0.5f,  0.5f, 0.5f ),

        //Lower four corners
        vec3( 0.5f,  -0.5f, 0.5f ),
        vec3( 0.5f,  -0.5f, -0.5f ),
        vec3( -0.5f, -0.5f, -0.5f ),
        vec3( -0.5f, -0.5f, 0.5f ),
    };

    //We start with spinning around the top and the bottom.
    for( int i = 1; i <= 4; i++ )
    {
        int prev_index = i - 1;
        int cur_index = i % 4;

        data.push_back( corners[prev_index] );
        data.push_back( corners[cur_index] );
    }

    for( int i = 1; i <= 4; i++ )
    {
        int prev_index = 4 + i - 1;
        int cur_index = 4 + i % 4;

        data.push_back( corners[prev_index] );
        data.push_back( corners[cur_index] );
    }

    //Then we match each bottom point with the one right above.
    for( int i = 0; i < 4; i++ )
    {
        data.push_back( corners[i] );
        data.push_back( corners[i+4] );
    }
}

// Get interleaved vert array (3 verts) for GL_LINES
void GetWireSphereVertArray(GLdouble radius, GLint slices, GLint stacks, std::vector<float> &data) {
    double r,x,y,z;
    double *sint1,*cost1;
    double *sint2,*cost2;
    circleTable(&sint1,&cost1,-slices  );
    circleTable(&sint2,&cost2, stacks*2);
    for (int i=1; i<stacks; i++) {
        z = cost2[i];
        r = sint2[i];
        for(int j=0; j<=slices; j++){
            x = cost1[j];
            y = sint1[j];
            //data.push_back((float)x);
            //data.push_back((float)y);
            //data.push_back((float)z);
            data.push_back((float)(x*r*radius));
            data.push_back((float)(y*r*radius));
            data.push_back((float)(z*radius));
            if(j!=0 && j!=slices){
                //data.push_back((float)x);
                //data.push_back((float)y);
                //data.push_back((float)z);
                data.push_back((float)(x*r*radius));
                data.push_back((float)(y*r*radius));
                data.push_back((float)(z*radius));
            }
        }
    }
    for (int i=0; i<slices; i++) {
        for(int j=0; j<=stacks; j++){
            x = cost1[i]*sint2[j];
            y = sint1[i]*sint2[j];
            z = cost2[j];
            //data.push_back((float)x);
            //data.push_back((float)y);
            //data.push_back((float)z);
            data.push_back((float)(x*radius));
            data.push_back((float)(y*radius));
            data.push_back((float)(z*radius));
            if(j!=0 && j!=stacks){
                //data.push_back((float)x);
                //data.push_back((float)y);
                //data.push_back((float)z);
                data.push_back((float)(x*radius));
                data.push_back((float)(y*radius));
                data.push_back((float)(z*radius));
            }
        }
    }
    OG_FREE(sint1);
    OG_FREE(cost1);
    OG_FREE(sint2);
    OG_FREE(cost2);
}

void GetUnitBoxVertArray(std::vector<float> &verts) 
{
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(-0.5f);
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(-0.5f);
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(-0.5f); 
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(0.5f); 
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(0.5f);
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(0.5f); 
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(-0.5f); 
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(0.5f); 
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(0.5f); 
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(-0.5f); 
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(0.5f); 
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(-0.5f); verts.push_back(-0.5f);
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(-0.5f);
    verts.push_back(-0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
    verts.push_back(0.5f); verts.push_back(0.5f); verts.push_back(0.5f);
}

void GetUnitBoxVertArray(std::vector<float> &verts, std::vector<unsigned> &faces) 
{

    verts.push_back(0.500000);
    verts.push_back(-0.500000);
    verts.push_back(-0.500000);

    verts.push_back(0.500000);
    verts.push_back(-0.500000);
    verts.push_back(0.500000);

    verts.push_back(-0.500000);
    verts.push_back(-0.500000);
    verts.push_back(0.500000);

    verts.push_back(-0.500000);
    verts.push_back(-0.500000);
    verts.push_back(-0.500000);

    verts.push_back(0.500000);
    verts.push_back(0.500000);
    verts.push_back(-0.500000);

    verts.push_back(0.500000);
    verts.push_back(0.500000);
    verts.push_back(0.500000);

    verts.push_back(-0.500000);
    verts.push_back(0.500000);
    verts.push_back(0.500000);

    verts.push_back(-0.500000);
    verts.push_back(0.500000);
    verts.push_back(-0.500000);

    faces.push_back(1-1);
    faces.push_back(2-1);
    faces.push_back(4-1);
    faces.push_back(5-1);
    faces.push_back(8-1);
    faces.push_back(6-1);
    faces.push_back(1-1);
    faces.push_back(5-1);
    faces.push_back(2-1);
    faces.push_back(2-1);
    faces.push_back(6-1);
    faces.push_back(3-1);
    faces.push_back(3-1);
    faces.push_back(7-1);
    faces.push_back(4-1);
    faces.push_back(5-1);
    faces.push_back(1-1);
    faces.push_back(8-1);
    faces.push_back(2-1);
    faces.push_back(3-1);
    faces.push_back(4-1);
    faces.push_back(8-1);
    faces.push_back(7-1);
    faces.push_back(6-1);
    faces.push_back(5-1);
    faces.push_back(6-1);
    faces.push_back(2-1);
    faces.push_back(6-1);
    faces.push_back(7-1);
    faces.push_back(3-1);
    faces.push_back(7-1);
    faces.push_back(8-1);
    faces.push_back(4-1);
    faces.push_back(1-1);
    faces.push_back(4-1);
    faces.push_back(8-1);
}