|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib example - point rendering |
| 4 | +* |
| 5 | +* Example originally created with raylib 5.0, last time updated with raylib 5.0 |
| 6 | +* |
| 7 | +* Example contributed by Reese Gallagher (@satchelfrost) and reviewed by Ramon Santamaria (@raysan5) |
| 8 | +* |
| 9 | +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, |
| 10 | +* BSD-like license that allows static linking with closed source software |
| 11 | +* |
| 12 | +* Copyright (c) 2024 Reese Gallagher (@satchelfrost) |
| 13 | +* |
| 14 | +********************************************************************************************/ |
| 15 | + |
| 16 | +#include "raylib.h" |
| 17 | +#include <stdlib.h> // Required for: rand() |
| 18 | +#include <math.h> // Required for: cos(), sin() |
| 19 | + |
| 20 | +#define MAX_POINTS 10000000 // 10 million |
| 21 | +#define MIN_POINTS 1000 // 1 thousand |
| 22 | + |
| 23 | +static float RandFloat(); |
| 24 | + |
| 25 | +//------------------------------------------------------------------------------------ |
| 26 | +// Program main entry point |
| 27 | +//------------------------------------------------------------------------------------ |
| 28 | +int main() |
| 29 | +{ |
| 30 | + // Initialization |
| 31 | + //-------------------------------------------------------------------------------------- |
| 32 | + const int screenWidth = 800; |
| 33 | + const int screenHeight = 450; |
| 34 | + InitWindow(screenWidth, screenHeight, "raylib [models] example - point rendering"); |
| 35 | + SetTargetFPS(60); |
| 36 | + |
| 37 | + Camera camera = { |
| 38 | + .position = {3.0f, 3.0f, 3.0f}, |
| 39 | + .target = {0.0f, 0.0f, 0.0f}, |
| 40 | + .up = {0.0f, 1.0f, 0.0f}, |
| 41 | + .fovy = 45.0f, |
| 42 | + .projection = CAMERA_PERSPECTIVE, |
| 43 | + }; |
| 44 | + |
| 45 | + Vector3 position = {0.0f, 0.0f, 0.0f}; |
| 46 | + bool useDrawModelPoints = true; |
| 47 | + bool numPointsChanged = false; |
| 48 | + int numPoints = 1000; |
| 49 | + Mesh mesh = GenPoints(numPoints); |
| 50 | + Model model = LoadModelFromMesh(mesh); |
| 51 | + //-------------------------------------------------------------------------------------- |
| 52 | + |
| 53 | + // Main game loop |
| 54 | + while(!WindowShouldClose()) |
| 55 | + { |
| 56 | + // Update |
| 57 | + //---------------------------------------------------------------------------------- |
| 58 | + UpdateCamera(&camera, CAMERA_ORBITAL); |
| 59 | + |
| 60 | + if (IsKeyPressed(KEY_SPACE)) useDrawModelPoints = !useDrawModelPoints; |
| 61 | + if (IsKeyPressed(KEY_UP)) |
| 62 | + { |
| 63 | + numPoints = (numPoints * 10 > MAX_POINTS) ? MAX_POINTS : numPoints * 10; |
| 64 | + numPointsChanged = true; |
| 65 | + TraceLog(LOG_INFO, "num points %d", numPoints); |
| 66 | + } |
| 67 | + if (IsKeyPressed(KEY_DOWN)) |
| 68 | + { |
| 69 | + numPoints = (numPoints / 10 < MIN_POINTS) ? MIN_POINTS : numPoints / 10; |
| 70 | + numPointsChanged = true; |
| 71 | + TraceLog(LOG_INFO, "num points %d", numPoints); |
| 72 | + } |
| 73 | + |
| 74 | + // upload a different point cloud size |
| 75 | + if (numPointsChanged) |
| 76 | + { |
| 77 | + UnloadModel(model); |
| 78 | + mesh = GenPoints(numPoints); |
| 79 | + model = LoadModelFromMesh(mesh); |
| 80 | + numPointsChanged = false; |
| 81 | + } |
| 82 | + |
| 83 | + // Draw |
| 84 | + //---------------------------------------------------------------------------------- |
| 85 | + BeginDrawing(); |
| 86 | + ClearBackground(BLACK); |
| 87 | + BeginMode3D(camera); |
| 88 | + |
| 89 | + // The new method only uploads the points once to the GPU |
| 90 | + if (useDrawModelPoints) |
| 91 | + { |
| 92 | + DrawModelPoints(model, position, 1.0f, WHITE); |
| 93 | + } |
| 94 | + // The old method must continually draw the "points" (lines) |
| 95 | + else |
| 96 | + { |
| 97 | + for (int i = 0; i < numPoints; i++) |
| 98 | + { |
| 99 | + Vector3 pos = { |
| 100 | + .x = mesh.vertices[i * 3 + 0], |
| 101 | + .y = mesh.vertices[i * 3 + 1], |
| 102 | + .z = mesh.vertices[i * 3 + 2], |
| 103 | + }; |
| 104 | + Color color = { |
| 105 | + .r = mesh.colors[i * 4 + 0], |
| 106 | + .g = mesh.colors[i * 4 + 1], |
| 107 | + .b = mesh.colors[i * 4 + 2], |
| 108 | + .a = mesh.colors[i * 4 + 3], |
| 109 | + }; |
| 110 | + DrawPoint3D(pos, color); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Draw a unit sphere for reference |
| 115 | + DrawSphereWires(position, 1.0f, 10, 10, YELLOW); |
| 116 | + EndMode3D(); |
| 117 | + |
| 118 | + // Text formatting |
| 119 | + Color color = WHITE; |
| 120 | + int fps = GetFPS(); |
| 121 | + if ((fps < 30) && (fps >= 15)) color = ORANGE; |
| 122 | + else if (fps < 15) color = RED; |
| 123 | + DrawText(TextFormat("%2i FPS", fps), 20, 20, 40, color); |
| 124 | + DrawText(TextFormat("Point Count: %d", numPoints), 20, screenHeight - 50, 40, WHITE); |
| 125 | + DrawText("Up - increase points", 20, 70, 20, WHITE); |
| 126 | + DrawText("Down - decrease points", 20, 100, 20, WHITE); |
| 127 | + DrawText("Space - drawing function", 20, 130, 20, WHITE); |
| 128 | + if (useDrawModelPoints) DrawText("DrawModelPoints()", 20, 160, 20, GREEN); |
| 129 | + else DrawText("DrawPoint3D()", 20, 160, 20, RED); |
| 130 | + EndDrawing(); |
| 131 | + //---------------------------------------------------------------------------------- |
| 132 | + } |
| 133 | + |
| 134 | + // De-Initialization |
| 135 | + //-------------------------------------------------------------------------------------- |
| 136 | + UnloadModel(model); |
| 137 | + CloseWindow(); |
| 138 | + //-------------------------------------------------------------------------------------- |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +// Generate a spherical point cloud |
| 143 | +Mesh GenPoints(int numPoints) |
| 144 | +{ |
| 145 | + Mesh mesh = { |
| 146 | + .triangleCount = 1, |
| 147 | + .vertexCount = numPoints, |
| 148 | + .vertices = (float *)MemAlloc(numPoints * 3 * sizeof(float)), |
| 149 | + .colors = (unsigned char*)MemAlloc(numPoints * 4 * sizeof(unsigned char)), |
| 150 | + }; |
| 151 | + |
| 152 | + // https://en.wikipedia.org/wiki/Spherical_coordinate_system |
| 153 | + for (int i = 0; i < numPoints; i++) |
| 154 | + { |
| 155 | + float theta = PI * rand() / RAND_MAX; |
| 156 | + float phi = 2.0f * PI * rand() / RAND_MAX; |
| 157 | + float r = 10.0f * rand() / RAND_MAX; |
| 158 | + mesh.vertices[i * 3 + 0] = r * sin(theta) * cos(phi); |
| 159 | + mesh.vertices[i * 3 + 1] = r * sin(theta) * sin(phi); |
| 160 | + mesh.vertices[i * 3 + 2] = r * cos(theta); |
| 161 | + Color color = ColorFromHSV(r * 360.0f, 1.0f, 1.0f); |
| 162 | + mesh.colors[i * 4 + 0] = color.r; |
| 163 | + mesh.colors[i * 4 + 1] = color.g; |
| 164 | + mesh.colors[i * 4 + 2] = color.b; |
| 165 | + mesh.colors[i * 4 + 3] = color.a; |
| 166 | + } |
| 167 | + |
| 168 | + // Upload mesh data from CPU (RAM) to GPU (VRAM) memory |
| 169 | + UploadMesh(&mesh, false); |
| 170 | + return mesh; |
| 171 | +} |
0 commit comments