Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

182 lignes
6.3 KiB

  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. // Generate mesh using points
  21. Mesh GenMeshPoints(int numPoints);
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main()
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. InitWindow(screenWidth, screenHeight, "raylib [models] example - point rendering");
  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 = GenMeshPoints(numPoints);
  44. Model model = LoadModelFromMesh(mesh);
  45. //SetTargetFPS(60);
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while(!WindowShouldClose())
  49. {
  50. // Update
  51. //----------------------------------------------------------------------------------
  52. UpdateCamera(&camera, CAMERA_ORBITAL);
  53. if (IsKeyPressed(KEY_SPACE)) useDrawModelPoints = !useDrawModelPoints;
  54. if (IsKeyPressed(KEY_UP))
  55. {
  56. numPoints = (numPoints*10 > MAX_POINTS)? MAX_POINTS : numPoints*10;
  57. numPointsChanged = true;
  58. }
  59. if (IsKeyPressed(KEY_DOWN))
  60. {
  61. numPoints = (numPoints/10 < MIN_POINTS)? MIN_POINTS : numPoints/10;
  62. numPointsChanged = true;
  63. }
  64. // Upload a different point cloud size
  65. if (numPointsChanged)
  66. {
  67. UnloadModel(model);
  68. mesh = GenMeshPoints(numPoints);
  69. model = LoadModelFromMesh(mesh);
  70. numPointsChanged = false;
  71. }
  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. else
  84. {
  85. // The old method must continually draw the "points" (lines)
  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. // Draw UI text
  106. DrawText(TextFormat("Point Count: %d", numPoints), 20, screenHeight - 50, 40, WHITE);
  107. DrawText("Up - increase points", 20, 70, 20, WHITE);
  108. DrawText("Down - decrease points", 20, 100, 20, WHITE);
  109. DrawText("Space - drawing function", 20, 130, 20, WHITE);
  110. if (useDrawModelPoints) DrawText("Using: DrawModelPoints()", 20, 160, 20, GREEN);
  111. else DrawText("Using: DrawPoint3D()", 20, 160, 20, RED);
  112. DrawFPS(10, 10);
  113. EndDrawing();
  114. //----------------------------------------------------------------------------------
  115. }
  116. // De-Initialization
  117. //--------------------------------------------------------------------------------------
  118. UnloadModel(model);
  119. CloseWindow();
  120. //--------------------------------------------------------------------------------------
  121. return 0;
  122. }
  123. // Generate a spherical point cloud
  124. Mesh GenMeshPoints(int numPoints)
  125. {
  126. Mesh mesh = {
  127. .triangleCount = 1,
  128. .vertexCount = numPoints,
  129. .vertices = (float *)MemAlloc(numPoints*3*sizeof(float)),
  130. .colors = (unsigned char*)MemAlloc(numPoints*4*sizeof(unsigned char)),
  131. };
  132. // https://en.wikipedia.org/wiki/Spherical_coordinate_system
  133. for (int i = 0; i < numPoints; i++)
  134. {
  135. float theta = PI*rand()/RAND_MAX;
  136. float phi = 2.0f*PI*rand()/RAND_MAX;
  137. float r = 10.0f*rand()/RAND_MAX;
  138. mesh.vertices[i*3 + 0] = r*sin(theta)*cos(phi);
  139. mesh.vertices[i*3 + 1] = r*sin(theta)*sin(phi);
  140. mesh.vertices[i*3 + 2] = r*cos(theta);
  141. Color color = ColorFromHSV(r*360.0f, 1.0f, 1.0f);
  142. mesh.colors[i*4 + 0] = color.r;
  143. mesh.colors[i*4 + 1] = color.g;
  144. mesh.colors[i*4 + 2] = color.b;
  145. mesh.colors[i*4 + 3] = color.a;
  146. }
  147. // Upload mesh data from CPU (RAM) to GPU (VRAM) memory
  148. UploadMesh(&mesh, false);
  149. return mesh;
  150. }