shapes_rlgl_triangle.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - rlgl triangle
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Robin (@RobinsAviary) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Robin (@RobinsAviary)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "rlgl.h"
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. SetConfigFlags(FLAG_MSAA_4X_HINT);
  29. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - rlgl triangle");
  30. // Starting postions and rendered triangle positions
  31. Vector2 startingPositions[3] = {{ 400.0f, 150.0f }, { 300.0f, 300.0f }, { 500.0f, 300.0f }};
  32. Vector2 trianglePositions[3] = { startingPositions[0], startingPositions[1], startingPositions[2] };
  33. // Currently selected vertex, -1 means none
  34. int triangleIndex = -1;
  35. bool linesMode = false;
  36. float handleRadius = 8.0f;
  37. SetTargetFPS(60);
  38. //--------------------------------------------------------------------------------------
  39. // Main game loop
  40. while (!WindowShouldClose()) // Detect window close button or ESC key
  41. {
  42. // Update
  43. //----------------------------------------------------------------------------------
  44. if (IsKeyPressed(KEY_SPACE)) linesMode = !linesMode;
  45. // Check selected vertex
  46. for (unsigned int i = 0; i < 3; i++)
  47. {
  48. // If the mouse is within the handle circle
  49. if (CheckCollisionPointCircle(GetMousePosition(), trianglePositions[i], handleRadius) &&
  50. IsMouseButtonDown(MOUSE_BUTTON_LEFT))
  51. {
  52. triangleIndex = i;
  53. break;
  54. }
  55. }
  56. // If the user has selected a vertex, offset it by the mouse's delta this frame
  57. if (triangleIndex != -1)
  58. {
  59. Vector2 *position = &trianglePositions[triangleIndex];
  60. Vector2 mouseDelta = GetMouseDelta();
  61. position->x += mouseDelta.x;
  62. position->y += mouseDelta.y;
  63. }
  64. // Reset index on release
  65. if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) triangleIndex = -1;
  66. // Enable/disable backface culling (2-sided triangles, slower to render)
  67. if (IsKeyPressed(KEY_LEFT)) rlEnableBackfaceCulling();
  68. if (IsKeyPressed(KEY_RIGHT)) rlDisableBackfaceCulling();
  69. // Reset triangle vertices to starting positions and reset backface culling
  70. if (IsKeyPressed(KEY_R))
  71. {
  72. trianglePositions[0] = startingPositions[0];
  73. trianglePositions[1] = startingPositions[1];
  74. trianglePositions[2] = startingPositions[2];
  75. rlEnableBackfaceCulling();
  76. }
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(RAYWHITE);
  82. if (linesMode)
  83. {
  84. // Draw triangle with lines
  85. rlBegin(RL_LINES);
  86. // Three lines, six points
  87. // Define color for next vertex
  88. rlColor4ub(255, 0, 0, 255);
  89. // Define vertex
  90. rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
  91. rlColor4ub(0, 255, 0, 255);
  92. rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
  93. rlColor4ub(0, 255, 0, 255);
  94. rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
  95. rlColor4ub(0, 0, 255, 255);
  96. rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
  97. rlColor4ub(0, 0, 255, 255);
  98. rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
  99. rlColor4ub(255, 0, 0, 255);
  100. rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
  101. rlEnd();
  102. }
  103. else
  104. {
  105. // Draw triangle as a triangle
  106. rlBegin(RL_TRIANGLES);
  107. // One triangle, three points
  108. // Define color for next vertex
  109. rlColor4ub(255, 0, 0, 255);
  110. // Define vertex
  111. rlVertex2f(trianglePositions[0].x, trianglePositions[0].y);
  112. rlColor4ub(0, 255, 0, 255);
  113. rlVertex2f(trianglePositions[1].x, trianglePositions[1].y);
  114. rlColor4ub(0, 0, 255, 255);
  115. rlVertex2f(trianglePositions[2].x, trianglePositions[2].y);
  116. rlEnd();
  117. }
  118. // Render the vertex handles, reacting to mouse movement/input
  119. for (unsigned int i = 0; i < 3; i++)
  120. {
  121. // Draw handle fill focused by mouse
  122. if (CheckCollisionPointCircle(GetMousePosition(), trianglePositions[i], handleRadius))
  123. DrawCircleV(trianglePositions[i], handleRadius, ColorAlpha(DARKGRAY, 0.5f));
  124. // Draw handle fill selected
  125. if (i == triangleIndex) DrawCircleV(trianglePositions[i], handleRadius, DARKGRAY);
  126. // Draw handle outline
  127. DrawCircleLinesV(trianglePositions[i], handleRadius, BLACK);
  128. }
  129. // Draw controls
  130. DrawText("SPACE: Toggle lines mode", 10, 10, 20, DARKGRAY);
  131. DrawText("LEFT-RIGHT: Toggle backface culling", 10, 40, 20, DARKGRAY);
  132. DrawText("MOUSE: Click and drag vertex points", 10, 70, 20, DARKGRAY);
  133. DrawText("R: Reset triangle to start positions", 10, 100, 20, DARKGRAY);
  134. EndDrawing();
  135. //----------------------------------------------------------------------------------
  136. }
  137. // De-Initialization
  138. //--------------------------------------------------------------------------------------
  139. CloseWindow(); // Close window and OpenGL context
  140. //--------------------------------------------------------------------------------------
  141. return 0;
  142. }