No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

138 líneas
5.2 KiB

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