2023-06-08

Why are there gaps in my fibonacci sphere?

I recently took an interest in fibonacci spheres, so I decided to implement one. It works as expected, except for the fact that there are visual gaps in the sphere

fibonacci sphere with gaps

Now I don't know if there's supposed to be multiple layers like there is, but the main problem is the gaps. Here's my sphere generation function

// This code generates points on a sphere using Fibonacci spiral sampling
// The number of samples and the array to store the points are passed as arguments

#include <math.h>

void fibonacci_sphere(int samples, float points[][3]) {
    float phi = M_PI * (sqrtf(5.0f) - 1.0f);  // golden angle in radians

    for (int i = 0; i < samples; i++) {
        float y = 1.0f - ((float)i / (float)(samples - 1)) * 2.0f;  // y goes from 1 to -1
        float radius = sqrtf(1.0f - y * y);  // radius at y

        float theta = phi * i;  // golden angle increment

        points[i][0] = cosf(theta) * radius;
        points[i][1] = y;
        points[i][2] = sinf(theta) * radius;
    }
}

And this is my basic tessellation shader

#version 450 core

// determines what type of tessellation to do
layout(triangles, equal_spacing, cw) in;

// input from control shader
in vec3 vertex_coord[];
// output vec
out vec3 vert;

// allows for object transformations
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;

void main()
{
    // gets barycentric coordinates from the triangles
    vec3 u = gl_TessCoord.x * vertex_coord[0];
    vec3 v = gl_TessCoord.y * vertex_coord[1];
    vec3 w = gl_TessCoord.z * vertex_coord[2];
    // makes every triangle an equal distance from the center (that's how spheres are formed)
    vec3 pos = normalize(u + v + w);

    // output tessellated shape
    gl_Position = projection * view * model * vec4(pos, 1.0);
}


The vertex shaders are just the vertices from the CPU side of things, so that's not the problem.

I did some research across a few forums, and tried a pretty cool website that uses AI to analyze your code and find bugs and help visualize what happens.

EDIT:

This is the non-wireframe version of the sphere enter image description here

And this is the part where I actually upload the data to get processed by the GPU

// fibonacci sphere vertices
float vertices[100][3];
fibonacci_sphere(100, vertices);

unsigned int vbo, vao;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);

glBindVertexArray(vao);

// upload vertex data to gpu
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * sizeof(double), &vertices[0], GL_STATIC_DRAW);

// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);

// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

// amount of tessellation to do per triangle
glPatchParameteri(GL_PATCH_VERTICES, 3);
glBindVertexArray(vao);
glDrawArrays(GL_PATCHES, 0, 100);


No comments:

Post a Comment