Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

140 řádky
5.3 KiB

před 11 měsíci
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
před 3 roky
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - Draw Textured Polygon
  4. *
  5. * Example originally created with raylib 3.7, last time updated with raylib 3.7
  6. *
  7. * Example contributed by Chris Camacho (@codifies) 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) 2021-2024 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "rlgl.h" // Required for: Vertex definition
  17. #include "raymath.h"
  18. #define MAX_POINTS 11 // 10 points and back to the start
  19. // Draw textured polygon, defined by vertex and texture coordinates
  20. void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointCount, Color tint);
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [textures] example - textured polygon");
  31. // Define texture coordinates to map our texture to poly
  32. Vector2 texcoords[MAX_POINTS] = {
  33. (Vector2){ 0.75f, 0.0f },
  34. (Vector2){ 0.25f, 0.0f },
  35. (Vector2){ 0.0f, 0.5f },
  36. (Vector2){ 0.0f, 0.75f },
  37. (Vector2){ 0.25f, 1.0f},
  38. (Vector2){ 0.375f, 0.875f},
  39. (Vector2){ 0.625f, 0.875f},
  40. (Vector2){ 0.75f, 1.0f},
  41. (Vector2){ 1.0f, 0.75f},
  42. (Vector2){ 1.0f, 0.5f},
  43. (Vector2){ 0.75f, 0.0f} // Close the poly
  44. };
  45. // Define the base poly vertices from the UV's
  46. // NOTE: They can be specified in any other way
  47. Vector2 points[MAX_POINTS] = { 0 };
  48. for (int i = 0; i < MAX_POINTS; i++)
  49. {
  50. points[i].x = (texcoords[i].x - 0.5f)*256.0f;
  51. points[i].y = (texcoords[i].y - 0.5f)*256.0f;
  52. }
  53. // Define the vertices drawing position
  54. // NOTE: Initially same as points but updated every frame
  55. Vector2 positions[MAX_POINTS] = { 0 };
  56. for (int i = 0; i < MAX_POINTS; i++) positions[i] = points[i];
  57. // Load texture to be mapped to poly
  58. Texture texture = LoadTexture("resources/cat.png");
  59. float angle = 0.0f; // Rotation angle (in degrees)
  60. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  61. //--------------------------------------------------------------------------------------
  62. // Main game loop
  63. while (!WindowShouldClose()) // Detect window close button or ESC key
  64. {
  65. // Update
  66. //----------------------------------------------------------------------------------
  67. // Update points rotation with an angle transform
  68. // NOTE: Base points position are not modified
  69. angle++;
  70. for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], angle*DEG2RAD);
  71. //----------------------------------------------------------------------------------
  72. // Draw
  73. //----------------------------------------------------------------------------------
  74. BeginDrawing();
  75. ClearBackground(RAYWHITE);
  76. DrawText("textured polygon", 20, 20, 20, DARKGRAY);
  77. DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f },
  78. positions, texcoords, MAX_POINTS, WHITE);
  79. EndDrawing();
  80. //----------------------------------------------------------------------------------
  81. }
  82. // De-Initialization
  83. //--------------------------------------------------------------------------------------
  84. UnloadTexture(texture); // Unload texture
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }
  89. // Draw textured polygon, defined by vertex and texture coordinates
  90. // NOTE: Polygon center must have straight line path to all points
  91. // without crossing perimeter, points must be in anticlockwise order
  92. void DrawTexturePoly(Texture2D texture, Vector2 center, Vector2 *points, Vector2 *texcoords, int pointCount, Color tint)
  93. {
  94. rlSetTexture(texture.id);
  95. // Texturing is only supported on RL_QUADS
  96. rlBegin(RL_QUADS);
  97. rlColor4ub(tint.r, tint.g, tint.b, tint.a);
  98. for (int i = 0; i < pointCount - 1; i++)
  99. {
  100. rlTexCoord2f(0.5f, 0.5f);
  101. rlVertex2f(center.x, center.y);
  102. rlTexCoord2f(texcoords[i].x, texcoords[i].y);
  103. rlVertex2f(points[i].x + center.x, points[i].y + center.y);
  104. rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
  105. rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
  106. rlTexCoord2f(texcoords[i + 1].x, texcoords[i + 1].y);
  107. rlVertex2f(points[i + 1].x + center.x, points[i + 1].y + center.y);
  108. }
  109. rlEnd();
  110. rlSetTexture(0);
  111. }