File: BigSphere.cpp

package info (click to toggle)
pinball 0.3.20201218-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 8,452 kB
  • sloc: cpp: 15,230; makefile: 840; sh: 381; xml: 24
file content (170 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download | duplicates (9)
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
/***************************************************************************
                          BigSphere.cpp  -  description
                             -------------------
    begin                : Wed Jan 26 2000
    copyright            : (C) 2000 by Henrik Enqvist
    email                : henqvist@excite.com
***************************************************************************/

#include "Private.h"
#include "BigSphere.h"
#include "Polygon.h"

#define MEDIAN(v1, v2, vo) \
  vo.x = v1.x + (v2.x - v1.x)/2; \
  vo.y = v1.y + (v2.y - v1.y)/2; \
  vo.z = v1.z + (v2.z - v1.z)/2;

BigSphere::BigSphere(float fSize, int level, float fR, float fG, float fB, float fA) : Shape3D(1, 1) {
  // top vertex
  this->add(0, -1*fSize, 0, 
	    fR, fG, fB, fA,
	    0.0f, 0.0f);
  
  // front left
  this->add(-EM_SIN_45*fSize, 0, EM_SIN_45*fSize,
	    fR, fG, fB, fA,
	    0.0f, 0.0f);
  // front right
  this->add(EM_SIN_45*fSize, 0, EM_SIN_45*fSize,
	    fR, fG, fB, fA,
	    0.0f, 0.0f);
  // back left
  this->add(EM_SIN_45*fSize, 0, -EM_SIN_45*fSize,
	    fR, fG, fB, fA,
	    0.0f, 0.0f);
  // back right
  this->add(-EM_SIN_45*fSize, 0, -EM_SIN_45*fSize,
	    fR, fG, fB, fA,
	    0.0f, 0.0f);
  // bottom vertex
  this->add(0, 1*fSize, 0,
	    fR, fG, fB, fA,
	    0.0f, 0.0f);

  // TODO: Check if this causes a memory leak!!!
  vector<int> triangles;

  // push back first eight triangles
  triangles.push_back(0);
  triangles.push_back(1);
  triangles.push_back(2);

  triangles.push_back(0);
  triangles.push_back(2);
  triangles.push_back(3);

  triangles.push_back(0);
  triangles.push_back(3);
  triangles.push_back(4);

  triangles.push_back(0);
  triangles.push_back(4);
  triangles.push_back(1);

  triangles.push_back(5);
  triangles.push_back(2);
  triangles.push_back(1);

  triangles.push_back(5);
  triangles.push_back(3);
  triangles.push_back(2);

  triangles.push_back(5);
  triangles.push_back(4);
  triangles.push_back(3);

  triangles.push_back(5);
  triangles.push_back(1);
  triangles.push_back(4);

  int start = 0;
  int end = triangles.size();

  // split each triangle into three triangles
  while (level-- > 0) {
    for (int a = start; a < end; a += 3) {
      int A = triangles[a];
      int B = triangles[a+1];
      int C = triangles[a+2];
      Vertex3D vtxA = *(this->getVertex3D(triangles[a]));
      Vertex3D vtxB = *(this->getVertex3D(triangles[a+1]));
      Vertex3D vtxC = *(this->getVertex3D(triangles[a+2]));
			
      // new vertices
      Vertex3D vtxD, vtxE, vtxF;
			
      MEDIAN(vtxA, vtxB, vtxD);
      MEDIAN(vtxB, vtxC, vtxE);
      MEDIAN(vtxC, vtxA, vtxF);
			
      // move vertices out to the sphere surface
      EMath::normalizeVector(vtxD);
      EMath::normalizeVector(vtxE);
      EMath::normalizeVector(vtxF);
      EMath::scaleVector(vtxD, fSize);
      EMath::scaleVector(vtxE, fSize);
      EMath::scaleVector(vtxF, fSize);
			
      EM_COUT("BigSphere::BigSphere() vtxA " << vtxA.x <<" "<< vtxA.y <<" "<< vtxA.z, 0);
      EM_COUT("BigSphere::BigSphere() vtxB " << vtxB.x <<" "<< vtxB.y <<" "<< vtxB.z, 0);
      EM_COUT("BigSphere::BigSphere() vtxC " << vtxC.x <<" "<< vtxC.y <<" "<< vtxC.z, 0);
			
      EM_COUT("BigSphere::BigSphere() vtxD " << vtxD.x <<" "<< vtxD.y <<" "<< vtxD.z, 0);
      EM_COUT("BigSphere::BigSphere() vtxE " << vtxE.x <<" "<< vtxE.y <<" "<< vtxE.z, 0);
      EM_COUT("BigSphere::BigSphere() vtxF " << vtxF.x <<" "<< vtxF.y <<" "<< vtxF.z, 0);
			
      int D, E, F;
      if ((D = this->find(vtxD.x, vtxD.y, vtxD.z, 0.001)) == -1) {
	D = this->add(vtxD.x, vtxD.y, vtxD.z,
		      fR, fG, fB, fA,	0.0f, 0.0f);
      }
      if ((E = this->find(vtxE.x, vtxE.y, vtxE.z, 0.001)) == -1) {
	E = this->add(vtxE.x, vtxE.y, vtxE.z,
		      fR, fG, fB, fA,	0.0f, 0.0f);
      }
      if ((F = this->find(vtxF.x, vtxF.y, vtxF.z, 0.001)) == -1) {
	F = this->add(vtxF.x, vtxF.y, vtxF.z,
		      fR, fG, fB, fA,	0.0f, 0.0f);
      }
			
      // push back four new triangles
      triangles.push_back(A);
      triangles.push_back(D);
      triangles.push_back(F);
			
      triangles.push_back(D);
      triangles.push_back(B);
      triangles.push_back(E);
			
      triangles.push_back(F);
      triangles.push_back(E);
      triangles.push_back(C);
			
      triangles.push_back(D);
      triangles.push_back(E);
      triangles.push_back(F);
    }
    // discard the old triangles
    start = end;
    end = triangles.size();
  }

  // add the triangles to the shape
  for (int a = start; a < end; a +=  3) {
    Polygon3D * p;
    p = new Polygon3D(this, 3);
    p->add(triangles[a]);
    p->add(triangles[a+1]);
    p->add(triangles[a+2]);
    if (fA < 0.95f) {
      p->setProperty(EM_POLY_TRANS);
    }
    this->add(p);
  }
  if (fA < 0.95f) {
    this->setProperty(EM_SHAPE3D_USE_TRANS);
  }

  this->countNormals();
}