mirror of
https://github.com/raysan5/raylib.git
synced 2025-12-06 06:13:10 +00:00
Update shapes_rlgl_triangle.c
This commit is contained in:
parent
74f7112614
commit
470a326588
@ -16,6 +16,7 @@
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#include "rlgl.h"
|
||||
#include "raymath.h"
|
||||
|
||||
@ -33,11 +34,13 @@ int main(void)
|
||||
InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rlgl triangle");
|
||||
|
||||
// Starting postions and rendered triangle positions
|
||||
Vector2 startingPositions[] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }};
|
||||
Vector2 trianglePositions[] = { startingPositions[0], startingPositions[1], startingPositions[2] };
|
||||
Vector2 startingPositions[3] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }};
|
||||
Vector2 trianglePositions[3] = { startingPositions[0], startingPositions[1], startingPositions[2] };
|
||||
|
||||
// Currently selected vertex, -1 means none
|
||||
int triangleIndex = -1;
|
||||
bool linesMode = false;
|
||||
float handleRadius = 8.0f;
|
||||
|
||||
SetTargetFPS(60);
|
||||
//--------------------------------------------------------------------------------------
|
||||
@ -47,11 +50,7 @@ int main(void)
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
// Reset index on release
|
||||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
triangleIndex = -1;
|
||||
}
|
||||
if (IsKeyPressed(KEY_SPACE)) linesMode = !linesMode;
|
||||
|
||||
// If the user has selected a vertex, offset it by the mouse's delta this frame
|
||||
if (triangleIndex != -1)
|
||||
@ -63,16 +62,12 @@ int main(void)
|
||||
position->y += mouseDelta.y;
|
||||
}
|
||||
|
||||
// Enable/disable backface culling (2-sided triangles, slower to render)
|
||||
if (IsKeyPressed(KEY_LEFT))
|
||||
{
|
||||
rlEnableBackfaceCulling();
|
||||
}
|
||||
// Reset index on release
|
||||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) triangleIndex = -1;
|
||||
|
||||
if (IsKeyPressed(KEY_RIGHT))
|
||||
{
|
||||
rlDisableBackfaceCulling();
|
||||
}
|
||||
// Enable/disable backface culling (2-sided triangles, slower to render)
|
||||
if (IsKeyPressed(KEY_LEFT)) rlEnableBackfaceCulling();
|
||||
if (IsKeyPressed(KEY_RIGHT)) rlDisableBackfaceCulling();
|
||||
|
||||
// Reset triangle vertices to starting positions and reset backface culling
|
||||
if (IsKeyPressed(KEY_R))
|
||||
@ -91,7 +86,7 @@ int main(void)
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
if (IsKeyDown(KEY_SPACE))
|
||||
if (linesMode)
|
||||
{
|
||||
// Draw triangle with lines
|
||||
rlBegin(RL_LINES);
|
||||
@ -131,50 +126,37 @@ int main(void)
|
||||
}
|
||||
|
||||
// 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++)
|
||||
{
|
||||
Vector2 position = trianglePositions[i];
|
||||
|
||||
float size = 4.0f;
|
||||
|
||||
Vector2 mousePosition = GetMousePosition();
|
||||
|
||||
// If the cursor is within the handle circle
|
||||
if (Vector2Distance(mousePosition, position) < size)
|
||||
if (Vector2Distance(mousePosition, position) < handleRadius)
|
||||
{
|
||||
float fillAlpha = 0.0f;
|
||||
if (triangleIndex == -1)
|
||||
{
|
||||
fillAlpha = 0.5f;
|
||||
}
|
||||
if (triangleIndex == -1) fillAlpha = 0.5f;
|
||||
|
||||
// If handle selected/clicked
|
||||
if (i == triangleIndex)
|
||||
{
|
||||
fillAlpha = 1.0f;
|
||||
}
|
||||
if (i == triangleIndex) fillAlpha = 1.0f;
|
||||
|
||||
// If clicked, set selected index to handle index
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
triangleIndex = i;
|
||||
}
|
||||
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) triangleIndex = i;
|
||||
|
||||
// If visible, draw DARKGRAY circle with varying alpha.
|
||||
if (fillAlpha > 0.0f)
|
||||
{
|
||||
Color fillColor = ColorAlpha(DARKGRAY, fillAlpha);
|
||||
|
||||
DrawCircleV(position, size, fillColor);
|
||||
}
|
||||
if (fillAlpha > 0.0f) DrawCircleV(position, handleRadius, ColorAlpha(DARKGRAY, fillAlpha));
|
||||
}
|
||||
|
||||
// Draw handle outline
|
||||
DrawCircleLinesV(position, size, BLACK);
|
||||
DrawCircleLinesV(position, handleRadius, BLACK);
|
||||
}
|
||||
|
||||
// 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();
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user