Last active
November 20, 2024 17:57
-
-
Save ske2004/235fe7787fae6069e93eff3cba6bad88 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct { | |
| float x, y, z; | |
| } vector3; | |
| // Implementation defined, in OpenGL it would probably push the vertices into | |
| // vertex buffer and add indices to the index buffer. | |
| void make_tri(vector3 p1, vector3 p2, vector3 p3); | |
| int main() { | |
| #define SEGMENTS 9 | |
| // (#1) Add points for the top | |
| const vector3 t = {0, 1, 0}; | |
| vector3 top_circle[SEGMENTS]; | |
| for (int i = 0; i < SEGMENTS; i++) { | |
| // This is our theta (θ), in degrees for simplicity | |
| // In actual code convert it to radians before passing to cos/sin | |
| const float angle = (SEGMENTS/(float)i)*360; | |
| top_circle[i] = (vector3){t.x+cos(angle), t.y, t.z+sin(angle)}; | |
| } | |
| // (#2) Add points for the bottom | |
| const vector3 b = {0, -1, 0}; | |
| vector3 bottom_circle[SEGMENTS]; | |
| for (int i = 0; i < SEGMENTS; i++) { | |
| // This is our theta (θ), in degrees for simplicity | |
| // In actual code convert it to radians before passing to cos/sin | |
| const float angle = (SEGMENTS/(float)i)*360; | |
| bottom_circle[i] = (vector3){b.x+cos(angle), b.y, b.z+sin(angle)}; | |
| } | |
| // (#3) Fill top & bottom circles | |
| for (int i = 0; i < SEGMENTS-1; i++) { | |
| vector3 p1 = t; | |
| vector3 p2 = top_circle[i+1]; | |
| vector3 p3 = top_circle[i]; | |
| make_tri(p1, p2, p3); | |
| } | |
| for (int i = 0; i < SEGMENTS-1; i++) { | |
| vector3 p1 = b; | |
| vector3 p2 = bottom_circle[i+1]; | |
| vector3 p3 = bottom_circle[i]; | |
| make_tri(p1, p2, p3); | |
| } | |
| // (#4) Fill the sides of the cylinder | |
| for (int i = 0; i < SEGMENTS-1; i++) { | |
| // First part of the quad | |
| { | |
| vector3 p1 = top_circle[i]; | |
| vector3 p2 = bottom_circle[i]; | |
| vector3 p3 = bottom_circle[i+1]; | |
| make_tri(p1, p2, p3); | |
| } | |
| // Second part of the quad | |
| { | |
| vector3 p1 = bottom_circle[i+1]; | |
| vector3 p2 = top_circle[i+1]; | |
| vector3 p3 = top_circle[i]; | |
| make_tri(p1, p2, p3); | |
| } | |
| } | |
| // The cylinder should be ready to use in the mesh generated by `make_tri` ^_^ !! | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment