Last active
July 21, 2026 08:06
-
-
Save jweinst1/5db7ea0ef093f1f7ca0522fceadbfe53 to your computer and use it in GitHub Desktop.
sphere made via vertex in C++
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
| #include <SDL3/SDL.h> | |
| #include <SDL3/SDL_main.h> | |
| #include <cmath> | |
| #include <vector> | |
| // Calculates the X and Y screen coordinates for a circle centered anywhere | |
| void get_arbitrary_circle_point_trig( | |
| float centerX, float centerY, | |
| float radius, float angle_radians, | |
| float* out_x, float* out_y | |
| ) { | |
| // 1. Calculate the local x distance, then shift it by the center X | |
| *out_x = centerX + (radius * cosf(angle_radians)); | |
| // 2. Calculate the local y distance, then shift it by the center Y | |
| *out_y = centerY + (radius * sinf(angle_radians)); | |
| } | |
| void gen_circle_points(float centerX, | |
| float centerY, | |
| float radius, | |
| SDL_FPoint* points) { | |
| int total_steps = (int)(2.0f * SDL_PI_F * radius); | |
| float angle_step = (2.0f * SDL_PI_F) / total_steps; | |
| for (int i = 0; i < total_steps; i++) | |
| { | |
| float angle = i * angle_step; | |
| float x, y; | |
| get_arbitrary_circle_point_trig(centerX, centerY, radius, angle, &x, &y); | |
| points->x = x; | |
| points->y = y; | |
| ++points; | |
| } | |
| } | |
| struct SpherePoints { | |
| std::vector<SDL_FPoint> _outerPoints; | |
| std::vector<SDL_FPoint> _innerPoints; | |
| void clear() { | |
| _outerPoints.clear(); | |
| _innerPoints.clear(); | |
| } | |
| void createInnerOuter(float centerX, float centerY, | |
| float outerRadius, float innerRadius, unsigned outerSteps) { | |
| unsigned total_outer_steps = outerSteps; | |
| unsigned total_inner_steps = total_outer_steps / 2; | |
| float angle_outer_step = (2.0f * SDL_PI_F) / total_outer_steps; | |
| float angle_inner_step = (2.0f * SDL_PI_F) / total_inner_steps; | |
| for (int i = 0; i < total_outer_steps; ++i) | |
| { | |
| float angle = i * angle_outer_step; | |
| float outx = 0.0f; | |
| float outy = 0.0f; | |
| get_arbitrary_circle_point_trig(centerX, centerY, outerRadius, angle, &outx, &outy); | |
| _outerPoints.push_back(SDL_FPoint{outx, outy}); | |
| } | |
| for (int i = 0; i < total_inner_steps; ++i) | |
| { | |
| float angle = i * angle_inner_step; | |
| float outx = 0.0f; | |
| float outy = 0.0f; | |
| get_arbitrary_circle_point_trig(centerX, centerY, innerRadius, angle, &outx, &outy); | |
| _innerPoints.push_back(SDL_FPoint{outx, outy}); | |
| } | |
| } | |
| void createTriangleCircle(std::vector<SDL_Vertex>* vrxs) { | |
| size_t k = 0; | |
| for (size_t i = 0; i < _innerPoints.size(); ++i) | |
| { | |
| SDL_Vertex myVertex1 = { {_innerPoints[i].x, _innerPoints[i].y}, {0.5f, 0.0f, 0.0f, 1.0f}}; | |
| SDL_Vertex myVertex2 = { {_outerPoints[k].x, _outerPoints[k].y}, {1.0f, 0.0f, 0.0f, 1.0f}}; | |
| ++k; | |
| SDL_Vertex myVertex3 = { {_outerPoints[k].x, _outerPoints[k].y}, {1.0f, 0.0f, 0.0f, 1.0f}}; | |
| ++k; | |
| vrxs->push_back(myVertex1); | |
| vrxs->push_back(myVertex2); | |
| vrxs->push_back(myVertex3); | |
| } | |
| } | |
| }; | |
| int main(int argc, char *argv[]) | |
| { | |
| bool quit = false; | |
| SDL_Window *window = SDL_CreateWindow("Triangle Example", 800, 600, 0); | |
| SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); | |
| static constexpr float centerX = 400.0f; | |
| static constexpr float centerY = 300.0f; | |
| std::vector<SDL_Vertex> vertexes; | |
| SpherePoints spnts; | |
| float circleInner = 20.0f; | |
| float circleOuter = 50.0f; | |
| while (!quit) { | |
| SDL_Event ev; | |
| while (SDL_PollEvent(&ev) != 0) { | |
| switch(ev.type) { | |
| case SDL_EVENT_QUIT: | |
| quit = true; | |
| break; | |
| } | |
| } | |
| vertexes.clear(); | |
| spnts.clear(); | |
| spnts.createInnerOuter(centerX, centerY, circleOuter, circleInner, 500); | |
| spnts.createTriangleCircle(&vertexes); | |
| SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
| SDL_RenderClear(renderer); | |
| SDL_SetRenderDrawColor(renderer, 233, 0, 0, 255); | |
| //SDL_RenderPoints(renderer, topPoints, circlePointCount); | |
| SDL_RenderGeometry(renderer, NULL, vertexes.data(), vertexes.size(), NULL, 0); | |
| circleOuter += 0.05f; | |
| SDL_Delay(16); | |
| SDL_RenderPresent(renderer); | |
| } | |
| SDL_DestroyRenderer(renderer); | |
| SDL_DestroyWindow(window); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment