Update shapes_rlgl_triangle.c

This commit is contained in:
Ray 2025-11-15 16:47:11 +01:00
parent 74f7112614
commit 470a326588

View File

@ -16,6 +16,7 @@
********************************************************************************************/ ********************************************************************************************/
#include "raylib.h" #include "raylib.h"
#include "rlgl.h" #include "rlgl.h"
#include "raymath.h" #include "raymath.h"
@ -33,11 +34,13 @@ int main(void)
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rlgl triangle"); InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rlgl triangle");
// Starting postions and rendered triangle positions // Starting postions and rendered triangle positions
Vector2 startingPositions[] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }}; Vector2 startingPositions[3] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }};
Vector2 trianglePositions[] = { startingPositions[0], startingPositions[1], startingPositions[2] }; Vector2 trianglePositions[3] = { startingPositions[0], startingPositions[1], startingPositions[2] };
// Currently selected vertex, -1 means none // Currently selected vertex, -1 means none
int triangleIndex = -1; int triangleIndex = -1;
bool linesMode = false;
float handleRadius = 8.0f;
SetTargetFPS(60); SetTargetFPS(60);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
@ -47,11 +50,7 @@ int main(void)
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// Reset index on release if (IsKeyPressed(KEY_SPACE)) linesMode = !linesMode;
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
triangleIndex = -1;
}
// If the user has selected a vertex, offset it by the mouse's delta this frame // If the user has selected a vertex, offset it by the mouse's delta this frame
if (triangleIndex != -1) if (triangleIndex != -1)
@ -63,16 +62,12 @@ int main(void)
position->y += mouseDelta.y; position->y += mouseDelta.y;
} }
// Enable/disable backface culling (2-sided triangles, slower to render) // Reset index on release
if (IsKeyPressed(KEY_LEFT)) if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) triangleIndex = -1;
{
rlEnableBackfaceCulling();
}
if (IsKeyPressed(KEY_RIGHT)) // Enable/disable backface culling (2-sided triangles, slower to render)
{ if (IsKeyPressed(KEY_LEFT)) rlEnableBackfaceCulling();
rlDisableBackfaceCulling(); if (IsKeyPressed(KEY_RIGHT)) rlDisableBackfaceCulling();
}
// Reset triangle vertices to starting positions and reset backface culling // Reset triangle vertices to starting positions and reset backface culling
if (IsKeyPressed(KEY_R)) if (IsKeyPressed(KEY_R))
@ -91,7 +86,7 @@ int main(void)
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (IsKeyDown(KEY_SPACE)) if (linesMode)
{ {
// Draw triangle with lines // Draw triangle with lines
rlBegin(RL_LINES); rlBegin(RL_LINES);
@ -131,50 +126,37 @@ int main(void)
} }
// Render the vertex handles, reacting to mouse movement/input // Render the vertex handles, reacting to mouse movement/input
// TODO: Vertex selection can be moved to update logic
for (unsigned int i = 0; i < 3; i++) for (unsigned int i = 0; i < 3; i++)
{ {
Vector2 position = trianglePositions[i]; Vector2 position = trianglePositions[i];
float size = 4.0f;
Vector2 mousePosition = GetMousePosition(); Vector2 mousePosition = GetMousePosition();
// If the cursor is within the handle circle // If the cursor is within the handle circle
if (Vector2Distance(mousePosition, position) < size) if (Vector2Distance(mousePosition, position) < handleRadius)
{ {
float fillAlpha = 0.0f; float fillAlpha = 0.0f;
if (triangleIndex == -1) if (triangleIndex == -1) fillAlpha = 0.5f;
{
fillAlpha = 0.5f;
}
// If handle selected/clicked // If handle selected/clicked
if (i == triangleIndex) if (i == triangleIndex) fillAlpha = 1.0f;
{
fillAlpha = 1.0f;
}
// If clicked, set selected index to handle index // If clicked, set selected index to handle index
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) triangleIndex = i;
{
triangleIndex = i;
}
// If visible, draw DARKGRAY circle with varying alpha. // If visible, draw DARKGRAY circle with varying alpha.
if (fillAlpha > 0.0f) if (fillAlpha > 0.0f) DrawCircleV(position, handleRadius, ColorAlpha(DARKGRAY, fillAlpha));
{
Color fillColor = ColorAlpha(DARKGRAY, fillAlpha);
DrawCircleV(position, size, fillColor);
}
} }
// Draw handle outline // Draw handle outline
DrawCircleLinesV(position, size, BLACK); DrawCircleLinesV(position, handleRadius, BLACK);
} }
// Draw controls // Draw controls
DrawText("space for lines\nleft for backface culling\nright for no backface culling\nclick and drag points\nr to reset", 10, 10, 20, DARKGRAY); DrawText("SPACE: Toggle lines mode", 10, 10, 20, DARKGRAY);
DrawText("LEFT-RIGHT: Toggle backface culling", 10, 40, 20, DARKGRAY);
DrawText("MOUSE: Click and drag vertex points", 10, 70, 20, DARKGRAY);
DrawText("R: Reset triangle to start positions", 10, 100, 20, DARKGRAY);
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------