File: sphmodel.h

package info (click to toggle)
python-visual 3.2.9-4.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,796 kB
  • ctags: 2,664
  • sloc: cpp: 11,958; sh: 8,185; python: 3,709; ansic: 480; makefile: 311
file content (70 lines) | stat: -rw-r--r-- 1,763 bytes parent folder | download | duplicates (2)
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
#ifndef VISUAL_SPHMODEL_H
#define VISUAL_SPHMODEL_H

// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.

#include <cmath>
#include <cstdlib>

#include "vertex.h"

// sphmodel.h - Deep magic to generate sphere models
//   I generate spheres by subdividing an icosahedron, a Platonic
//     solid with 12 vertices and 20 sides, each an equilateral
//     triangle.  Each triangle is subdivided into four, yielding
//     a figure with 80 sides, 320 sides, and so on.  The complexity
//     is due to the fact that an 80-face "sphere" should have 42
//     vertices, but a naiive algorithm will generate many more
//     (multiple vertices at the same spot).  I avoid this with lots
//     of unmaintainable pointer goop.  If you have to modify this
//     file significantly, just start over.  I warned you!


namespace visual {

struct sph_model
{
	float* verts;
	vertex* proj;
	float* color;
	int* indices;
	int nverts;
	int ni;

	static inline void
	normalize(float v[3])
	{
		float n = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
		float m = 1.0 / std::sqrt( n );

		v[0] *= m;
		v[1] *= m;
		v[2] *= m;
	}

	static sph_model& get( int depth);

	inline float*
	newe( int span)
	{
		float* e = verts + 3*(nverts - 1);  // one before the beginning
		nverts += span-1;
		return e;
	}

	sph_model( int depth);

	inline float*
	avgptr( float* a, float* b)
	{ return (float*)( std::ptrdiff_t(a) + ((std::ptrdiff_t(b)-std::ptrdiff_t(a))>>1) ); }

	void subdivide( int span, float* v1, float* v2, float *v3
		, float* s1, float* s2, float* s3
		, float* e1, float* e2, float* e3 );
};

} // !namespace visual

#endif // !VISUAL_SPHMODEL_H