Update models_point_rendering.c

This commit is contained in:
Ray 2024-08-24 18:56:06 +02:00
parent bc0bd98763
commit 414133dbe7

View file

@ -14,13 +14,15 @@
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include <stdlib.h> // Required for: rand()
#include <math.h> // Required for: cos(), sin()
#define MAX_POINTS 10000000 // 10 million #include <stdlib.h> // Required for: rand()
#define MIN_POINTS 1000 // 1 thousand #include <math.h> // Required for: cos(), sin()
static float RandFloat(); #define MAX_POINTS 10000000 // 10 million
#define MIN_POINTS 1000 // 1 thousand
// Generate mesh using points
Mesh GenMeshPoints(int numPoints);
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -31,23 +33,26 @@ int main()
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
const int screenWidth = 800; const int screenWidth = 800;
const int screenHeight = 450; const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [models] example - point rendering"); InitWindow(screenWidth, screenHeight, "raylib [models] example - point rendering");
SetTargetFPS(60);
Camera camera = { Camera camera = {
.position = {3.0f, 3.0f, 3.0f}, .position = { 3.0f, 3.0f, 3.0f },
.target = {0.0f, 0.0f, 0.0f}, .target = { 0.0f, 0.0f, 0.0f },
.up = {0.0f, 1.0f, 0.0f}, .up = { 0.0f, 1.0f, 0.0f },
.fovy = 45.0f, .fovy = 45.0f,
.projection = CAMERA_PERSPECTIVE, .projection = CAMERA_PERSPECTIVE
}; };
Vector3 position = {0.0f, 0.0f, 0.0f}; Vector3 position = { 0.0f, 0.0f, 0.0f };
bool useDrawModelPoints = true; bool useDrawModelPoints = true;
bool numPointsChanged = false; bool numPointsChanged = false;
int numPoints = 1000; int numPoints = 1000;
Mesh mesh = GenPoints(numPoints);
Mesh mesh = GenMeshPoints(numPoints);
Model model = LoadModelFromMesh(mesh); Model model = LoadModelFromMesh(mesh);
//SetTargetFPS(60);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
@ -60,30 +65,30 @@ int main()
if (IsKeyPressed(KEY_SPACE)) useDrawModelPoints = !useDrawModelPoints; if (IsKeyPressed(KEY_SPACE)) useDrawModelPoints = !useDrawModelPoints;
if (IsKeyPressed(KEY_UP)) if (IsKeyPressed(KEY_UP))
{ {
numPoints = (numPoints * 10 > MAX_POINTS) ? MAX_POINTS : numPoints * 10; numPoints = (numPoints*10 > MAX_POINTS)? MAX_POINTS : numPoints*10;
numPointsChanged = true; numPointsChanged = true;
TraceLog(LOG_INFO, "num points %d", numPoints);
} }
if (IsKeyPressed(KEY_DOWN)) if (IsKeyPressed(KEY_DOWN))
{ {
numPoints = (numPoints / 10 < MIN_POINTS) ? MIN_POINTS : numPoints / 10; numPoints = (numPoints/10 < MIN_POINTS)? MIN_POINTS : numPoints/10;
numPointsChanged = true; numPointsChanged = true;
TraceLog(LOG_INFO, "num points %d", numPoints);
} }
// upload a different point cloud size // Upload a different point cloud size
if (numPointsChanged) if (numPointsChanged)
{ {
UnloadModel(model); UnloadModel(model);
mesh = GenPoints(numPoints); mesh = GenMeshPoints(numPoints);
model = LoadModelFromMesh(mesh); model = LoadModelFromMesh(mesh);
numPointsChanged = false; numPointsChanged = false;
} }
//----------------------------------------------------------------------------------
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
BeginMode3D(camera); BeginMode3D(camera);
// The new method only uploads the points once to the GPU // The new method only uploads the points once to the GPU
@ -91,42 +96,43 @@ int main()
{ {
DrawModelPoints(model, position, 1.0f, WHITE); DrawModelPoints(model, position, 1.0f, WHITE);
} }
// The old method must continually draw the "points" (lines)
else else
{ {
// The old method must continually draw the "points" (lines)
for (int i = 0; i < numPoints; i++) for (int i = 0; i < numPoints; i++)
{ {
Vector3 pos = { Vector3 pos = {
.x = mesh.vertices[i * 3 + 0], .x = mesh.vertices[i*3 + 0],
.y = mesh.vertices[i * 3 + 1], .y = mesh.vertices[i*3 + 1],
.z = mesh.vertices[i * 3 + 2], .z = mesh.vertices[i*3 + 2],
}; };
Color color = { Color color = {
.r = mesh.colors[i * 4 + 0], .r = mesh.colors[i*4 + 0],
.g = mesh.colors[i * 4 + 1], .g = mesh.colors[i*4 + 1],
.b = mesh.colors[i * 4 + 2], .b = mesh.colors[i*4 + 2],
.a = mesh.colors[i * 4 + 3], .a = mesh.colors[i*4 + 3],
}; };
DrawPoint3D(pos, color); DrawPoint3D(pos, color);
} }
} }
// Draw a unit sphere for reference // Draw a unit sphere for reference
DrawSphereWires(position, 1.0f, 10, 10, YELLOW); DrawSphereWires(position, 1.0f, 10, 10, YELLOW);
EndMode3D(); EndMode3D();
// Text formatting // Draw UI text
Color color = WHITE;
int fps = GetFPS();
if ((fps < 30) && (fps >= 15)) color = ORANGE;
else if (fps < 15) color = RED;
DrawText(TextFormat("%2i FPS", fps), 20, 20, 40, color);
DrawText(TextFormat("Point Count: %d", numPoints), 20, screenHeight - 50, 40, WHITE); DrawText(TextFormat("Point Count: %d", numPoints), 20, screenHeight - 50, 40, WHITE);
DrawText("Up - increase points", 20, 70, 20, WHITE); DrawText("Up - increase points", 20, 70, 20, WHITE);
DrawText("Down - decrease points", 20, 100, 20, WHITE); DrawText("Down - decrease points", 20, 100, 20, WHITE);
DrawText("Space - drawing function", 20, 130, 20, WHITE); DrawText("Space - drawing function", 20, 130, 20, WHITE);
if (useDrawModelPoints) DrawText("DrawModelPoints()", 20, 160, 20, GREEN);
else DrawText("DrawPoint3D()", 20, 160, 20, RED); if (useDrawModelPoints) DrawText("Using: DrawModelPoints()", 20, 160, 20, GREEN);
else DrawText("Using: DrawPoint3D()", 20, 160, 20, RED);
DrawFPS(10, 10);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
@ -134,38 +140,43 @@ int main()
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadModel(model); UnloadModel(model);
CloseWindow(); CloseWindow();
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;
} }
// Generate a spherical point cloud // Generate a spherical point cloud
Mesh GenPoints(int numPoints) Mesh GenMeshPoints(int numPoints)
{ {
Mesh mesh = { Mesh mesh = {
.triangleCount = 1, .triangleCount = 1,
.vertexCount = numPoints, .vertexCount = numPoints,
.vertices = (float *)MemAlloc(numPoints * 3 * sizeof(float)), .vertices = (float *)MemAlloc(numPoints*3*sizeof(float)),
.colors = (unsigned char*)MemAlloc(numPoints * 4 * sizeof(unsigned char)), .colors = (unsigned char*)MemAlloc(numPoints*4*sizeof(unsigned char)),
}; };
// https://en.wikipedia.org/wiki/Spherical_coordinate_system // https://en.wikipedia.org/wiki/Spherical_coordinate_system
for (int i = 0; i < numPoints; i++) for (int i = 0; i < numPoints; i++)
{ {
float theta = PI * rand() / RAND_MAX; float theta = PI*rand()/RAND_MAX;
float phi = 2.0f * PI * rand() / RAND_MAX; float phi = 2.0f*PI*rand()/RAND_MAX;
float r = 10.0f * rand() / RAND_MAX; float r = 10.0f*rand()/RAND_MAX;
mesh.vertices[i * 3 + 0] = r * sin(theta) * cos(phi);
mesh.vertices[i * 3 + 1] = r * sin(theta) * sin(phi); mesh.vertices[i*3 + 0] = r*sin(theta)*cos(phi);
mesh.vertices[i * 3 + 2] = r * cos(theta); mesh.vertices[i*3 + 1] = r*sin(theta)*sin(phi);
Color color = ColorFromHSV(r * 360.0f, 1.0f, 1.0f); mesh.vertices[i*3 + 2] = r*cos(theta);
mesh.colors[i * 4 + 0] = color.r;
mesh.colors[i * 4 + 1] = color.g; Color color = ColorFromHSV(r*360.0f, 1.0f, 1.0f);
mesh.colors[i * 4 + 2] = color.b;
mesh.colors[i * 4 + 3] = color.a; mesh.colors[i*4 + 0] = color.r;
mesh.colors[i*4 + 1] = color.g;
mesh.colors[i*4 + 2] = color.b;
mesh.colors[i*4 + 3] = color.a;
} }
// Upload mesh data from CPU (RAM) to GPU (VRAM) memory // Upload mesh data from CPU (RAM) to GPU (VRAM) memory
UploadMesh(&mesh, false); UploadMesh(&mesh, false);
return mesh; return mesh;
} }