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)
@ -62,17 +61,13 @@ int main(void)
position->x += mouseDelta.x; position->x += mouseDelta.x;
position->y += mouseDelta.y; position->y += mouseDelta.y;
} }
// Reset index on release
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) triangleIndex = -1;
// Enable/disable backface culling (2-sided triangles, slower to render) // Enable/disable backface culling (2-sided triangles, slower to render)
if (IsKeyPressed(KEY_LEFT)) if (IsKeyPressed(KEY_LEFT)) rlEnableBackfaceCulling();
{ if (IsKeyPressed(KEY_RIGHT)) rlDisableBackfaceCulling();
rlEnableBackfaceCulling();
}
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))
@ -89,92 +84,79 @@ int main(void)
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
if (IsKeyDown(KEY_SPACE)) if (linesMode)
{
// Draw triangle with lines
rlBegin(RL_LINES);
// Three lines, six points
// Define color for next vertex
rlColor4ub(255, 0, 0, 255);
// Define vertex
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
rlColor4ub(0, 255, 0, 255);
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
rlColor4ub(0, 255, 0, 255);
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
rlColor4ub(0, 0, 255, 255);
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
rlColor4ub(0, 0, 255, 255);
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
rlColor4ub(255, 0, 0, 255);
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
rlEnd();
}
else
{
// Draw triangle as a triangle
rlBegin(RL_TRIANGLES);
// One triangle, three points
// Define color for next vertex
rlColor4ub(255, 0, 0, 255);
// Define vertex
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
rlColor4ub(0, 255, 0, 255);
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
rlColor4ub(0, 0, 255, 255);
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
rlEnd();
}
// Render the vertex handles, reacting to mouse movement/input
for (unsigned int i = 0; i < 3; i++)
{
Vector2 position = trianglePositions[i];
float size = 4.0f;
Vector2 mousePosition = GetMousePosition();
// If the cursor is within the handle circle
if (Vector2Distance(mousePosition, position) < size)
{ {
float fillAlpha = 0.0f; // Draw triangle with lines
if (triangleIndex == -1) rlBegin(RL_LINES);
{ // Three lines, six points
fillAlpha = 0.5f; // Define color for next vertex
} rlColor4ub(255, 0, 0, 255);
// Define vertex
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
rlColor4ub(0, 255, 0, 255);
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
// If handle selected/clicked rlColor4ub(0, 255, 0, 255);
if (i == triangleIndex) rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
{ rlColor4ub(0, 0, 255, 255);
fillAlpha = 1.0f; rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
}
// If clicked, set selected index to handle index rlColor4ub(0, 0, 255, 255);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
{ rlColor4ub(255, 0, 0, 255);
triangleIndex = i; rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
} rlEnd();
}
// If visible, draw DARKGRAY circle with varying alpha. else
if (fillAlpha > 0.0f) {
{ // Draw triangle as a triangle
Color fillColor = ColorAlpha(DARKGRAY, fillAlpha); rlBegin(RL_TRIANGLES);
// One triangle, three points
DrawCircleV(position, size, fillColor); // Define color for next vertex
} rlColor4ub(255, 0, 0, 255);
// Define vertex
rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
rlColor4ub(0, 255, 0, 255);
rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
rlColor4ub(0, 0, 255, 255);
rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
rlEnd();
} }
// Draw handle outline // Render the vertex handles, reacting to mouse movement/input
DrawCircleLinesV(position, size, BLACK); // TODO: Vertex selection can be moved to update logic
} for (unsigned int i = 0; i < 3; i++)
{
Vector2 position = trianglePositions[i];
Vector2 mousePosition = GetMousePosition();
// Draw controls // If the cursor is within the handle circle
DrawText("space for lines\nleft for backface culling\nright for no backface culling\nclick and drag points\nr to reset", 10, 10, 20, DARKGRAY); if (Vector2Distance(mousePosition, position) < handleRadius)
{
float fillAlpha = 0.0f;
if (triangleIndex == -1) fillAlpha = 0.5f;
// If handle selected/clicked
if (i == triangleIndex) fillAlpha = 1.0f;
// If clicked, set selected index to handle index
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) triangleIndex = i;
// If visible, draw DARKGRAY circle with varying alpha.
if (fillAlpha > 0.0f) DrawCircleV(position, handleRadius, ColorAlpha(DARKGRAY, fillAlpha));
}
// Draw handle outline
DrawCircleLinesV(position, handleRadius, BLACK);
}
// Draw controls
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();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------