commit e356e8fe3826e5448b282eb3f791bb2dcf70fc22
parent 24019d11af4bdbfd14da099f2a4aa9d5d43883ab
Author: bsandro <email@bsandro.tech>
Date: Sun, 23 Nov 2025 20:12:46 +0200
fixed the sphere
Diffstat:
| M | sphere.c | | | 64 | +++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
1 file changed, 49 insertions(+), 15 deletions(-)
diff --git a/sphere.c b/sphere.c
@@ -110,23 +110,46 @@ void __attribute((destructor)) end() {
void generateSphere(float radius, int stacks, int sectors, GLfloat outVertices[]) {
float stackStep = M_PI/stacks;
- float sectorStep = 2.f*M_PI/sectors;
+ float sectorStep = (2.f*M_PI)/sectors;
+ int offset = 0;
// deliberate <=
for (int i=0;i<=stacks;++i) {
float stackAngle = M_PI_2 - (float)i*stackStep;
float xy = radius*cosf(stackAngle);
- float z = sinf(stackAngle);
+ float z = radius*sinf(stackAngle);
for (int j=0;j<=sectors;++j) {
float sectorAngle = (float)j*sectorStep;
float x = xy*cosf(sectorAngle);
float y = xy*sinf(sectorAngle);
- int offset = i*sectors*3+j*3;
- assert(offset<(stacks+1)*(sectors+1)*3);
- outVertices[offset] = x;
- outVertices[offset+1] = y;
- outVertices[offset+2] = z;
+ assert(offset < (stacks+1)*(sectors+1)*3 - 2);
+ outVertices[offset++] = x;
+ outVertices[offset++] = y;
+ outVertices[offset++] = z;
}
}
+ printf("offset: %d\n", offset);
+}
+
+void generateElements(int stacks, int sectors, GLuint outElements[]) {
+ GLuint offset = 0;
+ for (int i=0;i<stacks;++i) {
+ int k1 = i*(sectors+1);
+ int k2 = k1+sectors+1;
+ for (int j=0;j<sectors;++j,++k1,++k2) {
+ if (i!=0) {
+ outElements[offset++] = k1;
+ outElements[offset++] = k2;
+ outElements[offset++] = k1+1;
+ }
+ if (i!=(stacks-1)) {
+ outElements[offset++] = k1+1;
+ outElements[offset++] = k2;
+ outElements[offset++] = k2+1;
+ }
+ }
+ }
+ printf("elements: %lu, total: %lu\n", offset, (stacks-1)*sectors*6);
+ assert(offset <= (stacks-1)*sectors*6);
}
int main(int argc, char *argv[]) {
@@ -164,12 +187,16 @@ int main(int argc, char *argv[]) {
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
-#define stacks 50
-#define sectors 50
+#define stacks 24
+#define sectors 24
+ const float radius = 1.1f;
// x,y,z
GLfloat vertices[(stacks+1)*(sectors+1)*3];
- generateSphere(1.f, stacks, sectors, vertices);
- //
+ GLuint elements[(stacks-1)*sectors*6];
+ generateSphere(radius, stacks, sectors, vertices);
+ generateElements(stacks, sectors, elements);
+
+ printf("%4lu vertices\n%4lu elements\n", LEN(vertices), LEN(elements));
vec3 positions[1] = {{0,0,0}};
@@ -180,6 +207,11 @@ int main(int argc, char *argv[]) {
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+ GLuint ebo;
+ glGenBuffers(1, &ebo);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
+
GLuint vertex = mkShader(GL_VERTEX_SHADER, vertexSrc);
GLuint fragment = mkShader(GL_FRAGMENT_SHADER, fragmentSrc);
GLuint shaderPrg = linkProgram(vertex, fragment);
@@ -252,9 +284,9 @@ int main(int argc, char *argv[]) {
glUseProgram(shaderPrg);
// ultra useful debug
- glLineWidth(4.0f);
- glPointSize(8.0f);
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+ glLineWidth(1.0f);
+ glPointSize(4.0f);
+ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
for (int i=0; i<LEN(positions); ++i) {
mat4 model;
@@ -264,7 +296,9 @@ int main(int argc, char *argv[]) {
glm_rotate(model, glm_rad(angle), (vec3){ 1.f, 0.3f, 0.5f });
GLuint loc_model = glGetUniformLocation(shaderPrg, "model");
glUniformMatrix4fv(loc_model, 1, GL_FALSE, model[0]);
- glDrawArrays(GL_POINTS, 0, LEN(vertices));
+
+ //glDrawArrays(GL_POINTS, 0, LEN(vertices));
+ glDrawElements(GL_TRIANGLES, LEN(elements), GL_UNSIGNED_INT, 0);
}
glXSwapBuffers(dpy, win);