-
Notifications
You must be signed in to change notification settings - Fork 0
/
jellyfish.c
169 lines (126 loc) · 5.19 KB
/
jellyfish.c
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
#include "jellyfish.h"
#define NUM_POINTS 64
#define NUM_SLICES 25
vec3 bezierPoints[NUM_SLICES];
void halfBezier(vec3 P0, vec3 P1, vec3 P2, vec3 P3, int resolution, vec3* points) {
for (int i = 0; i <= resolution; i++) {
float t = i / (float)resolution / 2;
vec3 A = vec3_lerp(P0, P1, t);
vec3 B = vec3_lerp(P1, P2, t);
vec3 C = vec3_lerp(P2, P3, t);
vec3 D = vec3_lerp(A, B, t);
vec3 E = vec3_lerp(B, C, t);
points[i] = vec3_lerp(D, E, t);
}
}
Mesh generateDome(vec2 size, float inset) {
halfBezier((vec3){size.x, 0, 0}, (vec3){size.x - inset, 2 * size.y, 0}, (vec3){-size.x + inset, 2 * size.y, 0}, (vec3){-size.x, 0, 0}, NUM_SLICES - 1, bezierPoints);
const int vertexNumber = (NUM_POINTS * (NUM_SLICES - 1));
vec3 domeVertices[vertexNumber + 1];
unsigned int indices[vertexNumber * 6];
// Generate dome vertices
for (int i = 0; i < NUM_SLICES - 1; i++) {
double radius = bezierPoints[i].x;
double height = bezierPoints[i].y;
for (int j = 0; j < NUM_POINTS; j++) {
double angle = 2 * M_PI * (float)j / NUM_POINTS;
domeVertices[j + NUM_POINTS * i].x = radius * cos(angle);
domeVertices[j + NUM_POINTS * i].y = height;
domeVertices[j + NUM_POINTS * i].z = radius * sin(angle);
}
}
domeVertices[vertexNumber] = (vec3){bezierPoints[NUM_SLICES - 1].x, bezierPoints[NUM_SLICES - 1].y, 0.0};
// Generate indices for triangles
for (int i = 0; i < NUM_SLICES - 1; i++) {
for (int j = 0; j < NUM_POINTS; j++) {
int baseIndex = j + NUM_POINTS * i;
int baseIndexAbove = baseIndex + NUM_POINTS;
int nextIndex = (j + 1) % NUM_POINTS + i * NUM_POINTS;
int nextIndexAbove = (j + 1) % NUM_POINTS + (i + 1) * NUM_POINTS;
if (baseIndexAbove >= vertexNumber) baseIndexAbove = vertexNumber;
if (nextIndexAbove >= vertexNumber) nextIndexAbove = vertexNumber;
// First triangle
indices[baseIndex * 6 + 0] = baseIndex;
indices[baseIndex * 6 + 1] = baseIndexAbove;
indices[baseIndex * 6 + 2] = nextIndexAbove;
// Second triangle
indices[baseIndex * 6 + 3] = baseIndex;
indices[baseIndex * 6 + 4] = nextIndexAbove;
indices[baseIndex * 6 + 5] = nextIndex;
}
}
GLuint vao = createIndexedVAO(domeVertices, vertexNumber + 1, (unsigned int *)indices, vertexNumber * 6);
return (Mesh){vao, vertexNumber + 1, vertexNumber * 6};
}
Mesh genarateTentacles(vec3 pos, vec3 size, int resolution) {
const int vertexCount = 2 * (resolution + 1);
const int indiciesCount = 6 * resolution;
vec3 *vertices = (vec3*)malloc(sizeof(vec3) * vertexCount);
unsigned int *indices = (unsigned int*)malloc(sizeof(int) * indiciesCount);
for (int i = 0; i < vertexCount; i += 2) {
float x = (float)i / ((float)resolution * 2.0f);
float width = 1 - exp((x - 1) * 10);
vertices[i].x = pos.x - (size.x / 2) * width;
vertices[i].y = pos.y - size.y * x;
vertices[i].z = pos.z - (size.z / 2) * width;
vertices[i + 1].x = pos.x + (size.x / 2) * width;
vertices[i + 1].y = pos.y - size.y * x;
vertices[i + 1].z = pos.z + (size.z / 2) * width;
}
int j = 0;
for (int i = 0; i < indiciesCount; i += 6) {
indices[i] = j;
indices[i + 1] = j + 1;
indices[i + 2] = j + 3;
indices[i + 3] = j;
indices[i + 4] = j + 3;
indices[i + 5] = j + 2;
j += 2;
}
GLuint vao = createIndexedVAO(vertices, vertexCount, indices, indiciesCount);
free(vertices);
free(indices);
return (Mesh){vao, vertexCount, indiciesCount};
}
static const int resolution = 20;
static const vec2 pos = {0.75, 1.25};
static const vec2 size = {0.75, 5.5};
static Mesh extDome, intDome;
static Mesh tentacles[4];
void initJellyfish() {
extDome = generateDome((vec2){3.0, 1.25}, 0.0);
intDome = generateDome((vec2){3.0, 1.0}, 0.2);
tentacles[0] = genarateTentacles((vec3){pos.x, pos.y, 0}, (vec3){size.x, size.y, 0}, resolution);
tentacles[1] = genarateTentacles((vec3){0, pos.y, pos.x}, (vec3){0, size.y, size.x}, resolution);
tentacles[2] = genarateTentacles((vec3){-pos.x, pos.y, 0}, (vec3){size.x, size.y, 0}, resolution);
tentacles[3] = genarateTentacles((vec3){0, pos.y, -pos.x}, (vec3){0, size.y, size.x}, resolution);
}
void renderJellyfish(mat4 projection, mat4 view, vec3 camPos, float time) {
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDepthFunc(GL_ALWAYS);
glUseProgram(jellyfishShader);
glUniformMatrix4fv(glGetUniformLocation(jellyfishShader, "projection"), 1, GL_FALSE, (GLfloat*)&projection);
glUniformMatrix4fv(glGetUniformLocation(jellyfishShader, "view"), 1, GL_FALSE, (GLfloat*)&view);
glUniform1f(glGetUniformLocation(jellyfishShader, "time"), time);
glUniform3fv(glGetUniformLocation(jellyfishShader, "camPos"), 1, (GLfloat*)&camPos);
glUniform1f(glGetUniformLocation(jellyfishShader, "camDist"), length(camPos) - 3.0);
for (int i = 0; i < 4; i++) {
glBindVertexArray(tentacles[i].VAO);
glDrawElements(GL_TRIANGLES, tentacles[i].indexCount, GL_UNSIGNED_INT, NULL);
}
glBindVertexArray(intDome.VAO);
glDrawElements(GL_TRIANGLES, intDome.indexCount, GL_UNSIGNED_INT, NULL);
glBindVertexArray(extDome.VAO);
glDrawElements(GL_TRIANGLES, extDome.indexCount, GL_UNSIGNED_INT, NULL);
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LESS);
}
void cleanupJellyfish() {
freeMesh(extDome);
freeMesh(intDome);
for (int i = 0; i < 4; i++) {
freeMesh(tentacles[i]);
}
}