-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve.h
279 lines (236 loc) · 8.92 KB
/
curve.h
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
//
// curve.h
// test
//
//
//
#ifndef curve_h
#define curve_h
#include <glad/glad.h>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
using namespace std;
class Curve
{
public:
vector<float> cntrlPoints;
vector <float> coordinates;
vector <float> normals;
vector <int> indices;
vector <float> vertices;
vector<float> texCoords;
const double pi = 3.14159265389;
const int nt = 40;
const int ntheta = 20;
// Texture properties
unsigned int diffuseMap;
unsigned int specularMap;
float shininess;
Curve(vector<float>& tmp, unsigned int dMap, unsigned int sMap, float shiny)
: diffuseMap(dMap), specularMap(sMap), shininess(shiny)
{
this->cntrlPoints = tmp;
this->fishVAO = hollowBezier(cntrlPoints.data(), ((unsigned int)cntrlPoints.size() / 3) - 1);
cout << cntrlPoints.size() << endl;
cout << coordinates.size() << endl;
cout << normals.size() << endl;
cout << indices.size() << endl;
cout << vertices.size() << endl;
}
~Curve()
{
glDeleteVertexArrays(1, &fishVAO);
glDeleteVertexArrays(1, &bezierVAO);
glDeleteBuffers(1, &bezierVBO);
glDeleteBuffers(1, &bezierEBO);
}
void draw(Shader& lightingShader, glm::mat4 model, glm::vec3 amb)
{
lightingShader.use();
lightingShader.setMat4("model", model);
lightingShader.setVec3("material.ambient", amb);
lightingShader.setVec3("material.diffuse", amb);
lightingShader.setVec3("material.specular", glm::vec3(0.5f, 0.5f, 0.5f));
lightingShader.setFloat("material.shininess", 32.0f);
// Set texture properties
lightingShader.setInt("material.diffuseMap", 0); // 0 corresponds to GL_TEXTURE0
lightingShader.setInt("material.specularMap", 1); // 1 corresponds to GL_TEXTURE1
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, diffuseMap);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, specularMap);
glBindVertexArray(fishVAO);
glDrawElements(GL_TRIANGLES, (unsigned int)indices.size(), GL_UNSIGNED_INT, (void*)0);
// unbind VAO
glBindVertexArray(0);
}
private:
unsigned int fishVAO;
unsigned int bezierVAO;
unsigned int bezierVBO;
unsigned int bezierEBO;
unsigned int drawControlPoints()
{
unsigned int controlPointVAO;
unsigned int controlPointVBO;
glGenVertexArrays(1, &controlPointVAO);
glGenBuffers(1, &controlPointVBO);
glBindVertexArray(controlPointVAO);
glBindBuffer(GL_ARRAY_BUFFER, controlPointVBO);
glBufferData(GL_ARRAY_BUFFER, (unsigned int)cntrlPoints.size() * sizeof(float), cntrlPoints.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
return controlPointVAO;
}
long long nCr(int n, int r)
{
if (r > n / 2)
r = n - r; // because C(n, r) == C(n, n - r)
long long ans = 1;
int i;
for (i = 1; i <= r; i++)
{
ans *= n - r + i;
ans /= i;
}
return ans;
}
void BezierCurve(double t, float xy[2], GLfloat ctrlpoints[], int L)
{
double y = 0;
double x = 0;
t = t > 1.0 ? 1.0 : t;
for (int i = 0; i < L + 1; i++)
{
long long ncr = nCr(L, i);
double oneMinusTpow = pow(1 - t, double(L - i));
double tPow = pow(t, double(i));
double coef = oneMinusTpow * tPow * ncr;
x += coef * ctrlpoints[i * 3];
y += coef * ctrlpoints[(i * 3) + 1];
}
xy[0] = float(x);
xy[1] = float(y);
}
unsigned int hollowBezier(GLfloat ctrlpoints[], int L)
{
int i, j;
float x, y, z, r; // current coordinates
float theta;
float nx, ny, nz, lengthInv; // vertex normal
float u, v; // texture coordinates
const float dtheta = 2 * pi / ntheta; // angular step size
float t = 0;
float dt = 1.0 / nt;
float xy[2];
for (i = 0; i <= nt; ++i) // step through y
{
BezierCurve(t, xy, ctrlpoints, L);
r = xy[0];
y = xy[1];
theta = 0;
t += dt;
lengthInv = 1.0 / r;
for (j = 0; j <= ntheta; ++j)
{
double cosa = cos(theta);
double sina = sin(theta);
z = r * cosa;
x = r * sina;
coordinates.push_back(x);
coordinates.push_back(y);
coordinates.push_back(z);
// normalized vertex normal (nx, ny, nz)
// center point of the circle (0,y,0)
nx = (x - 0) * lengthInv;
ny = (y - y) * lengthInv;
nz = (z - 0) * lengthInv;
normals.push_back(nx);
normals.push_back(ny);
normals.push_back(nz);
// Calculate texture coordinates (s, t)
u = static_cast<float>(j) / ntheta;
v = static_cast<float>(i) / nt;
texCoords.push_back(u);
texCoords.push_back(v);
theta += dtheta;
}
}
// generate index list of triangles
// k1--k1+1
// | / |
// | / |
// k2--k2+1
int k1, k2;
for (int i = 0; i < nt; ++i)
{
k1 = i * (ntheta + 1); // beginning of current stack
k2 = k1 + ntheta + 1; // beginning of next stack
for (int j = 0; j < ntheta; ++j, ++k1, ++k2)
{
// k1 => k2 => k1+1
indices.push_back(k1);
indices.push_back(k2);
indices.push_back(k1 + 1);
// k1+1 => k2 => k2+1
indices.push_back(k1 + 1);
indices.push_back(k2);
indices.push_back(k2 + 1);
}
}
size_t count = coordinates.size();
for (i = 0, j = 0; i < count; i += 3, j += 2)
{
//cout << count << ' ' << i + 2 << endl;
vertices.push_back(coordinates[i]);
vertices.push_back(coordinates[i + 1]);
vertices.push_back(coordinates[i + 2]);
if (i < normals.size())
vertices.push_back(normals[i]);
if (i + 1 < normals.size())
vertices.push_back(normals[i + 1]);
if (i + 2 < normals.size())
vertices.push_back(normals[i + 2]);
//// Add texture coordinates
//if (j < texCoords.size())
// vertices.push_back(texCoords[j]);
//if (j + 1 < texCoords.size())
// vertices.push_back(texCoords[j + 1]);
}
glGenVertexArrays(1, &bezierVAO);
glBindVertexArray(bezierVAO);
// create VBO to copy vertex data to VBO
glGenBuffers(1, &bezierVBO);
glBindBuffer(GL_ARRAY_BUFFER, bezierVBO); // for vertex data
glBufferData(GL_ARRAY_BUFFER, // target
(unsigned int)vertices.size() * sizeof(float), // data size, # of bytes
vertices.data(), // ptr to vertex data
GL_STATIC_DRAW); // usage
// create EBO to copy index data
glGenBuffers(1, &bezierEBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bezierEBO); // for index data
glBufferData(GL_ELEMENT_ARRAY_BUFFER, // target
(unsigned int)indices.size() * sizeof(unsigned int), // data size, # of bytes
indices.data(), // ptr to index data
GL_STATIC_DRAW); // usage
// activate attrib arrays
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
// set attrib arrays with stride and offset
int stride = 24; // should be 24 bytes
glVertexAttribPointer(0, 3, GL_FLOAT, false, stride, (void*)0);
glVertexAttribPointer(1, 3, GL_FLOAT, false, stride, (void*)(sizeof(float) * 3));
glVertexAttribPointer(2, 2, GL_FLOAT, false, stride, (void*)(sizeof(float) * 6)); // Add this line for texture coordinates
//
// unbind VAO, VBO and EBO
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return bezierVAO;
}
};
#endif /* curve_h */