You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
9.5 KiB

1 year ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Draw a texture along a segmented curve
  4. *
  5. * Example originally created with raylib 4.5, last time updated with raylib 4.5
  6. *
  7. * Example contributed by Jeffery Myers and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2022-2024 Jeffery Myers and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. #include "rlgl.h"
  18. #include <math.h> // Required for: powf()
  19. #include <stdlib.h> // Required for: NULL
  20. //----------------------------------------------------------------------------------
  21. // Global Variables Definition
  22. //----------------------------------------------------------------------------------
  23. static Texture texRoad = { 0 };
  24. static bool showCurve = false;
  25. static float curveWidth = 50;
  26. static int curveSegments = 24;
  27. static Vector2 curveStartPosition = { 0 };
  28. static Vector2 curveStartPositionTangent = { 0 };
  29. static Vector2 curveEndPosition = { 0 };
  30. static Vector2 curveEndPositionTangent = { 0 };
  31. static Vector2 *curveSelectedPoint = NULL;
  32. //----------------------------------------------------------------------------------
  33. // Module Functions Declaration
  34. //----------------------------------------------------------------------------------
  35. static void DrawTexturedCurve(void);
  36. //------------------------------------------------------------------------------------
  37. // Program main entry point
  38. //------------------------------------------------------------------------------------
  39. int main()
  40. {
  41. // Initialization
  42. //--------------------------------------------------------------------------------------
  43. const int screenWidth = 800;
  44. const int screenHeight = 450;
  45. SetConfigFlags(FLAG_VSYNC_HINT | FLAG_MSAA_4X_HINT);
  46. InitWindow(screenWidth, screenHeight, "raylib [textures] examples - textured curve");
  47. // Load the road texture
  48. texRoad = LoadTexture("resources/road.png");
  49. SetTextureFilter(texRoad, TEXTURE_FILTER_BILINEAR);
  50. // Setup the curve
  51. curveStartPosition = (Vector2){ 80, 100 };
  52. curveStartPositionTangent = (Vector2){ 100, 300 };
  53. curveEndPosition = (Vector2){ 700, 350 };
  54. curveEndPositionTangent = (Vector2){ 600, 100 };
  55. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  56. //--------------------------------------------------------------------------------------
  57. // Main game loop
  58. while (!WindowShouldClose()) // Detect window close button or ESC key
  59. {
  60. // Update
  61. //----------------------------------------------------------------------------------
  62. // Curve config options
  63. if (IsKeyPressed(KEY_SPACE)) showCurve = !showCurve;
  64. if (IsKeyPressed(KEY_EQUAL)) curveWidth += 2;
  65. if (IsKeyPressed(KEY_MINUS)) curveWidth -= 2;
  66. if (curveWidth < 2) curveWidth = 2;
  67. // Update segments
  68. if (IsKeyPressed(KEY_LEFT)) curveSegments -= 2;
  69. if (IsKeyPressed(KEY_RIGHT)) curveSegments += 2;
  70. if (curveSegments < 2) curveSegments = 2;
  71. // Update curve logic
  72. // If the mouse is not down, we are not editing the curve so clear the selection
  73. if (!IsMouseButtonDown(MOUSE_LEFT_BUTTON)) curveSelectedPoint = NULL;
  74. // If a point was selected, move it
  75. if (curveSelectedPoint) *curveSelectedPoint = Vector2Add(*curveSelectedPoint, GetMouseDelta());
  76. // The mouse is down, and nothing was selected, so see if anything was picked
  77. Vector2 mouse = GetMousePosition();
  78. if (CheckCollisionPointCircle(mouse, curveStartPosition, 6)) curveSelectedPoint = &curveStartPosition;
  79. else if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6)) curveSelectedPoint = &curveStartPositionTangent;
  80. else if (CheckCollisionPointCircle(mouse, curveEndPosition, 6)) curveSelectedPoint = &curveEndPosition;
  81. else if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6)) curveSelectedPoint = &curveEndPositionTangent;
  82. //----------------------------------------------------------------------------------
  83. // Draw
  84. //----------------------------------------------------------------------------------
  85. BeginDrawing();
  86. ClearBackground(RAYWHITE);
  87. DrawTexturedCurve(); // Draw a textured Spline Cubic Bezier
  88. // Draw spline for reference
  89. if (showCurve) DrawSplineSegmentBezierCubic(curveStartPosition, curveEndPosition, curveStartPositionTangent, curveEndPositionTangent, 2, BLUE);
  90. // Draw the various control points and highlight where the mouse is
  91. DrawLineV(curveStartPosition, curveStartPositionTangent, SKYBLUE);
  92. DrawLineV(curveStartPositionTangent, curveEndPositionTangent, Fade(LIGHTGRAY, 0.4f));
  93. DrawLineV(curveEndPosition, curveEndPositionTangent, PURPLE);
  94. if (CheckCollisionPointCircle(mouse, curveStartPosition, 6)) DrawCircleV(curveStartPosition, 7, YELLOW);
  95. DrawCircleV(curveStartPosition, 5, RED);
  96. if (CheckCollisionPointCircle(mouse, curveStartPositionTangent, 6)) DrawCircleV(curveStartPositionTangent, 7, YELLOW);
  97. DrawCircleV(curveStartPositionTangent, 5, MAROON);
  98. if (CheckCollisionPointCircle(mouse, curveEndPosition, 6)) DrawCircleV(curveEndPosition, 7, YELLOW);
  99. DrawCircleV(curveEndPosition, 5, GREEN);
  100. if (CheckCollisionPointCircle(mouse, curveEndPositionTangent, 6)) DrawCircleV(curveEndPositionTangent, 7, YELLOW);
  101. DrawCircleV(curveEndPositionTangent, 5, DARKGREEN);
  102. // Draw usage info
  103. DrawText("Drag points to move curve, press SPACE to show/hide base curve", 10, 10, 10, DARKGRAY);
  104. DrawText(TextFormat("Curve width: %2.0f (Use + and - to adjust)", curveWidth), 10, 30, 10, DARKGRAY);
  105. DrawText(TextFormat("Curve segments: %d (Use LEFT and RIGHT to adjust)", curveSegments), 10, 50, 10, DARKGRAY);
  106. EndDrawing();
  107. //----------------------------------------------------------------------------------
  108. }
  109. // De-Initialization
  110. //--------------------------------------------------------------------------------------
  111. UnloadTexture(texRoad);
  112. CloseWindow(); // Close window and OpenGL context
  113. //--------------------------------------------------------------------------------------
  114. return 0;
  115. }
  116. //----------------------------------------------------------------------------------
  117. // Module Functions Definition
  118. //----------------------------------------------------------------------------------
  119. // Draw textured curve using Spline Cubic Bezier
  120. static void DrawTexturedCurve(void)
  121. {
  122. const float step = 1.0f/curveSegments;
  123. Vector2 previous = curveStartPosition;
  124. Vector2 previousTangent = { 0 };
  125. float previousV = 0;
  126. // We can't compute a tangent for the first point, so we need to reuse the tangent from the first segment
  127. bool tangentSet = false;
  128. Vector2 current = { 0 };
  129. float t = 0.0f;
  130. for (int i = 1; i <= curveSegments; i++)
  131. {
  132. t = step*(float)i;
  133. float a = powf(1.0f - t, 3);
  134. float b = 3.0f*powf(1.0f - t, 2)*t;
  135. float c = 3.0f*(1.0f - t)*powf(t, 2);
  136. float d = powf(t, 3);
  137. // Compute the endpoint for this segment
  138. current.y = a*curveStartPosition.y + b*curveStartPositionTangent.y + c*curveEndPositionTangent.y + d*curveEndPosition.y;
  139. current.x = a*curveStartPosition.x + b*curveStartPositionTangent.x + c*curveEndPositionTangent.x + d*curveEndPosition.x;
  140. // Vector from previous to current
  141. Vector2 delta = { current.x - previous.x, current.y - previous.y };
  142. // The right hand normal to the delta vector
  143. Vector2 normal = Vector2Normalize((Vector2){ -delta.y, delta.x });
  144. // The v texture coordinate of the segment (add up the length of all the segments so far)
  145. float v = previousV + Vector2Length(delta);
  146. // Make sure the start point has a normal
  147. if (!tangentSet)
  148. {
  149. previousTangent = normal;
  150. tangentSet = true;
  151. }
  152. // Extend out the normals from the previous and current points to get the quad for this segment
  153. Vector2 prevPosNormal = Vector2Add(previous, Vector2Scale(previousTangent, curveWidth));
  154. Vector2 prevNegNormal = Vector2Add(previous, Vector2Scale(previousTangent, -curveWidth));
  155. Vector2 currentPosNormal = Vector2Add(current, Vector2Scale(normal, curveWidth));
  156. Vector2 currentNegNormal = Vector2Add(current, Vector2Scale(normal, -curveWidth));
  157. // Draw the segment as a quad
  158. rlSetTexture(texRoad.id);
  159. rlBegin(RL_QUADS);
  160. rlColor4ub(255,255,255,255);
  161. rlNormal3f(0.0f, 0.0f, 1.0f);
  162. rlTexCoord2f(0, previousV);
  163. rlVertex2f(prevNegNormal.x, prevNegNormal.y);
  164. rlTexCoord2f(1, previousV);
  165. rlVertex2f(prevPosNormal.x, prevPosNormal.y);
  166. rlTexCoord2f(1, v);
  167. rlVertex2f(currentPosNormal.x, currentPosNormal.y);
  168. rlTexCoord2f(0, v);
  169. rlVertex2f(currentNegNormal.x, currentNegNormal.y);
  170. rlEnd();
  171. // The current step is the start of the next step
  172. previous = current;
  173. previousTangent = normal;
  174. previousV = v;
  175. }
  176. }