Skip to content

Commit 5eb0b1e

Browse files
author
FreehandBlock51
committed
maybe implemented sphere drawing
1 parent 0db40dc commit 5eb0b1e

4 files changed

Lines changed: 90 additions & 0 deletions

File tree

‎include/viewer/sphere.h‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
/**
4+
* Utilities for rendering a sphere
5+
*/
6+
7+
8+
#include <gl_includes.h>
9+
#include <cglm/cglm.h>
10+
#include "sim/sphere.h"
11+
12+
/**
13+
* The minimum size required of a vertex array for a sphere
14+
*/
15+
#define CSPHERE_VERTEX_ARRAY_SIZE 49
16+
17+
/**
18+
* Creates a transformation matrix for a sphere
19+
*/
20+
void csphere_make_transform(csphere_t sphere, mat4 transform);
21+
22+
/**
23+
* Generates vertices to render a given sphere.
24+
* Intended to be drawn with the sphere_* shaders
25+
*/
26+
void csphere_gen_vertices(csphere_t sphere, GLfloat vertices[CSPHERE_VERTEX_ARRAY_SIZE]);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#version 330 core
2+
3+
in vec3 in_position_relative;
4+
in float radius;
5+
6+
out vec4 out_color;
7+
8+
uniform vec4 color;
9+
10+
void main() {
11+
if (magnitude(in_position_relative) > radius) {
12+
discard;
13+
}
14+
15+
out_color = color;
16+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 330 core
2+
3+
layout(location=0) in struct {
4+
float radius;
5+
float u;
6+
float v;
7+
} input;
8+
9+
uniform mat4 model;
10+
uniform mat4 view;
11+
uniform mat4 projection;
12+
13+
out vec3 out_position_relative;
14+
out float radius;
15+
16+
void main() {
17+
float r_current = input.radius * cos(input.u);
18+
vec3 position = vec3(
19+
r_current * cos(input.v),
20+
r_current * sin(input.v),
21+
r_current
22+
);
23+
24+
out_position_relative = position;
25+
radius = input.radius;
26+
gl_Position = projection * view * model * vec4(position, 1.0f);
27+
}

‎src/viewer/sphere.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "viewer/sphere.h"
2+
3+
#include "common/defines.h"
4+
5+
void csphere_make_transform(csphere_t sphere, mat4 transform) {
6+
glm_mat4_identity(transform);
7+
glm_translate(transform, vec3_to_cglm(sphere.position));
8+
}
9+
10+
void csphere_gen_vertices(csphere_t sphere, GLfloat vertices[CSPHERE_VERTEX_ARRAY_SIZE]) {
11+
safe_assert(vertices != NULL,);
12+
13+
size_t i = 0;
14+
for (float u = 0; u < 2 * GLM_PIf; u += GLM_PI_4f) {
15+
for (float v = 0; v < 2 * GLM_PIf; v += GLM_PI_4f) {
16+
vertices[i++] = sphere.radius;
17+
vertices[i++] = u;
18+
vertices[i++] = v;
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)