File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ]);
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments