models_point_rendering.c 6.4 KB

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