-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleafvis.cpp
176 lines (150 loc) · 5.86 KB
/
leafvis.cpp
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
#include <math.h>
#include "leafvis.h"
leafVis::leafVis()
{
pData = 0;
}
#ifndef max
#define max(a, b) \
((a) > (b)) ? (a) : (b)
#endif
float equation(float x)
{
return std::sqrt(max(0.0f, 1.0f - 80.f * static_cast<float>(cos (x) * cos(x)) * (x * x) /20.0f));
}
void leafVis::build_model()
{
if (pData)
{
delete[] pData;
delete[] pIndices;
}
//example equation: y=+-sqrt(1 - x*x/100) (also is symmetric around x)
unsigned int nInternalQuaterSteps = 5;
//number of points
dataCount = 3*(nInternalQuaterSteps*2+1) + 2;
//number of triangles
unsigned int nTriangles = 2*4*nInternalQuaterSteps+4;
//number of indices
indicesCount = 3*nTriangles;
pData = new VertexData [dataCount];
pIndices = new unsigned int [indicesCount];
//fill in pData array
//right up
unsigned int startIndex = 0;
for (unsigned int i=0; i<nInternalQuaterSteps+1; i++)
{
float xPos = (1.0f +i)/(nInternalQuaterSteps+2);
float yPos = equation(xPos);
pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
pData[startIndex+i].nor = glm::vec3(0,0,-1);
pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);
}
//right down
startIndex += nInternalQuaterSteps+1;
for (unsigned int i=0; i<nInternalQuaterSteps; i++)
{
float xPos = (1.0f + nInternalQuaterSteps-i)/(nInternalQuaterSteps+2);
float yPos = -equation(xPos);
pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
pData[startIndex+i].nor = glm::vec3(0,0,-1);
pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);
}
startIndex += nInternalQuaterSteps;
//left up
for (unsigned int i=0; i<nInternalQuaterSteps+1; i++)
{
float xPos = -(1.0f+i)/(nInternalQuaterSteps+2);
float yPos = equation(xPos);
pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
pData[startIndex+i].nor = glm::vec3(0,0,-1);
pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);
}
//left down
startIndex += nInternalQuaterSteps+1;
for (unsigned int i=0; i<nInternalQuaterSteps; i++)
{
float xPos = -(1.0f + nInternalQuaterSteps-i)/(nInternalQuaterSteps+2);
float yPos = -equation(xPos);
pData[startIndex+i].pos = glm::vec3(xPos/2, (yPos+1)/2,0);
pData[startIndex+i].nor = glm::vec3(0,0,-1);
pData[startIndex+i].tex = glm::vec2((xPos+1)/2, (yPos+1)/2);
}
startIndex += nInternalQuaterSteps;
//center up
for (unsigned int i=0; i<nInternalQuaterSteps+1; i++)
{
float xPos = (1.0f + i)/(nInternalQuaterSteps+2);
float yPos = equation(xPos);
pData[startIndex+i].pos = glm::vec3(0, (yPos+1)/2,0);
pData[startIndex+i].nor = glm::vec3(0,0,-1);
pData[startIndex+i].tex = glm::vec2(0.5f,0.5f);
}
//center down
startIndex += nInternalQuaterSteps+1;
for (unsigned int i=0; i<nInternalQuaterSteps; i++)
{
float xPos = (1.0f + nInternalQuaterSteps-i)/(nInternalQuaterSteps+2);
float yPos = -equation(xPos);
pData[startIndex+i].pos = glm::vec3(0, (yPos+1)/2,0);
pData[startIndex+i].nor = glm::vec3(0,0,-1);
pData[startIndex+i].tex = glm::vec2(0.5f,0.5f);
}
startIndex += nInternalQuaterSteps;
//generate north pole
pData[startIndex].pos = glm::vec3(0,1,0);
pData[startIndex].nor = glm::vec3(0,0,-1);
pData[startIndex].tex = glm::vec2(0.5f, 1.0f);
//generate south pole
pData[startIndex+1].pos = glm::vec3(0,0,0);
pData[startIndex+1].nor = glm::vec3(0,0,-1);
pData[startIndex+1].tex = glm::vec2(0.5f, 0.0f);
//fill in pIndices array
//fill in side triangles (first 6*radialStep*(heightStep-1))
//fill in pData array
startIndex = 0;
//center and right
unsigned int startIndex1 = 0;//right
unsigned int startIndex2 = 2*(2*nInternalQuaterSteps+1);//center
for (unsigned int i=0; i<2*nInternalQuaterSteps; i++)
{
pIndices[startIndex+6*i+0] = startIndex2+i;
pIndices[startIndex+6*i+1] = startIndex1+i;
pIndices[startIndex+6*i+2] = startIndex1+i+1;
pIndices[startIndex+6*i+3] = startIndex2+i;
pIndices[startIndex+6*i+4] = startIndex1+i+1;
pIndices[startIndex+6*i+5] = startIndex2+i+1;
}
startIndex += 3*4*nInternalQuaterSteps;
//left and center
startIndex1 = 2*(2*nInternalQuaterSteps+1);//center
startIndex2 = 1*(2*nInternalQuaterSteps+1);//left
for (unsigned int i=0; i<2*nInternalQuaterSteps; i++)
{
pIndices[startIndex+6*i+0] = startIndex2+i;
pIndices[startIndex+6*i+1] = startIndex1+i;
pIndices[startIndex+6*i+2] = startIndex1+i+1;
pIndices[startIndex+6*i+3] = startIndex2+i;
pIndices[startIndex+6*i+4] = startIndex1+i+1;
pIndices[startIndex+6*i+5] = startIndex2+i+1;
}
startIndex += 3*4*nInternalQuaterSteps;
//connect north pole
unsigned int northPoleIndex = 3*(2*nInternalQuaterSteps+1);
pIndices[startIndex+0] = 2*nInternalQuaterSteps+1;//left top
pIndices[startIndex+1] = 2*(2*nInternalQuaterSteps+1);//center top
pIndices[startIndex+2] = northPoleIndex;
pIndices[startIndex+3] = 2*(2*nInternalQuaterSteps+1);//center top
pIndices[startIndex+4] = 0;//right top
pIndices[startIndex+5] = northPoleIndex;
startIndex += 3*2;
//connect south pole
unsigned int southPoleIndex = 3*(2*nInternalQuaterSteps+1)+1;
pIndices[startIndex+0] = 2*nInternalQuaterSteps+1+2*nInternalQuaterSteps;//left bottom
pIndices[startIndex+1] = southPoleIndex;
pIndices[startIndex+2] = 2*(2*nInternalQuaterSteps+1)+2*nInternalQuaterSteps;//center bottom
pIndices[startIndex+3] = 2*(2*nInternalQuaterSteps+1)+2*nInternalQuaterSteps;//center bottom
pIndices[startIndex+4] = southPoleIndex;
pIndices[startIndex+5] = 2*nInternalQuaterSteps;//right bottom
startIndex += 3*2;
}