now in 3D

This commit is contained in:
2025-11-09 11:59:46 +01:00
parent b54bb6a136
commit 6a874a01bb
3 changed files with 41 additions and 15 deletions

View File

@@ -1,7 +1,7 @@
#version 460 core #version 460 core
layout(std430, binding = 3) buffer layoutName { layout(std430, binding = 3) buffer layoutName {
vec2 o_Points[]; float o_Points[];
}; };
layout(local_size_x = 64) in; layout(local_size_x = 64) in;
@@ -16,23 +16,35 @@ highp float rand(vec2 co)
return fract(sin(sn) * c); return fract(sin(sn) * c);
} }
vec3 unpack(uint index) {
return vec3(o_Points[index * 3], o_Points[index * 3 + 1], o_Points[index * 3 + 2]);
}
void store(vec3 vect, uint index) {
o_Points[index * 3] = vect.x;
o_Points[index * 3 + 1] = vect.y;
o_Points[index * 3 + 2] = vect.z;
}
void main() { void main() {
const uint total = gl_NumWorkGroups.x * gl_WorkGroupSize.x; const uint total = gl_NumWorkGroups.x * gl_WorkGroupSize.x;
uint currentPoint = gl_GlobalInvocationID.x; uint currentIndex = gl_GlobalInvocationID.x;
uint index = uint(rand(o_Points[currentPoint] + currentPoint) * 69); vec3 currentPoint = unpack(currentIndex);
mat3 transformation; uint index = uint(rand(currentPoint.xy + currentPoint.z + currentIndex) * 69);
mat4 transformation;
switch (index % 3) { switch (index % 3) {
case 0: case 0:
transformation = mat3(0.5, 0, 0, 0, 0.5, 0, 0, 0.36, 1); transformation = mat4(0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0, 0.36, 0, 1);
break; break;
case 1: case 1:
transformation = mat3(0.5, 0, 0, 0, 0.5, 0, -0.5, -0.5, 1); transformation = mat4(0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, -0.5, -0.5, 0, 1);
break; break;
case 2: case 2:
transformation = mat3(0.5, 0, 0, 0, 0.5, 0, 0.5, -0.5, 1); transformation = mat4(0.5, 0, 0, 0, 0, 0.5, 0, 0, 0, 0, 0, 0, 0.5, -0.5, 0, 1);
break; break;
} }
o_Points[currentPoint] = (transformation * vec3(o_Points[currentPoint], 1.0)).xy; vec3 result = (transformation * vec4(currentPoint, 1.0)).xyz;
store(result, currentIndex);
} }

View File

@@ -2,10 +2,14 @@
layout(std430, binding = 3) buffer layoutName layout(std430, binding = 3) buffer layoutName
{ {
vec2 data_SSBO[]; float data_SSBO[];
}; };
vec3 unpack(uint index) {
return vec3(data_SSBO[index * 3], data_SSBO[index * 3 + 1], data_SSBO[index * 3 + 2]);
}
void main() void main()
{ {
gl_Position = vec4(data_SSBO[gl_InstanceID], 0.0, 1.0); gl_Position = vec4(unpack(gl_InstanceID), 1.0);
} }

View File

@@ -54,7 +54,7 @@ static void KeyCallback(GLFWwindow* window, int key, int scancode, int action, i
if (key == GLFW_KEY_R) { if (key == GLFW_KEY_R) {
s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath); s_ComputeShader = ReloadComputeShader(s_ComputeShader, s_ComputeShaderPath);
s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath); s_GraphicsShader = ReloadGraphicsShader(s_GraphicsShader, s_VertexShaderPath, s_FragmentShaderPath);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * PARTICLE_COUNT * 2, nullptr, GL_DYNAMIC_COPY); glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * PARTICLE_COUNT * 3, nullptr, GL_DYNAMIC_COPY);
} }
} }
@@ -66,11 +66,11 @@ static GLuint CreateDummyVAO() {
glCreateBuffers(1, &vertexBuffer); glCreateBuffers(1, &vertexBuffer);
// Buffer with just one point // Buffer with just one point
float vertices[] = {0.0f, 0.0f}; float vertices[] = {0.0f, 0.0f, 0.0f};
glNamedBufferData(vertexBuffer, sizeof(vertices), vertices, GL_STATIC_DRAW); glNamedBufferData(vertexBuffer, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, 0, sizeof(float) * 2); glVertexArrayVertexBuffer(vertexArray, 0, vertexBuffer, 0, sizeof(float) * 3);
glEnableVertexArrayAttrib(vertexArray, 0); glEnableVertexArrayAttrib(vertexArray, 0);
glVertexArrayAttribFormat(vertexArray, 0, 2, GL_FLOAT, GL_FALSE, 0); glVertexArrayAttribFormat(vertexArray, 0, 3, GL_FLOAT, GL_FALSE, 0);
glVertexArrayAttribBinding(vertexArray, 0, 0); glVertexArrayAttribBinding(vertexArray, 0, 0);
return vertexArray; return vertexArray;
@@ -81,7 +81,7 @@ static void CreateGpuBuffer() {
glGenBuffers(1, &ssbo); glGenBuffers(1, &ssbo);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo); glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo); glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, ssbo);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * 2 * PARTICLE_COUNT, nullptr, glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(float) * 3 * PARTICLE_COUNT, nullptr,
GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays. GL_DYNAMIC_COPY); // sizeof(data) only works for statically sized C/C++ arrays.
} }
@@ -174,6 +174,16 @@ int main() {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0, 0, 0, 1); glClearColor(0, 0, 0, 1);
// float positions[3 * PARTICLE_COUNT];
// glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, sizeof(float) * 3 * PARTICLE_COUNT, positions);
// std::cout << "Positions :\n";
// for (size_t i = 0; i < PARTICLE_COUNT; i++)
// {
// std::cout << "\t" << positions[i * 3] << " " << positions[i * 3 + 1] << " " << positions[i * 3 + 2] << "\n";
// }
fps++; fps++;
} }