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.

172 lines
8.2 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - rlgl module usage with push/pop matrix transformations
  4. *
  5. * NOTE: This example uses [rlgl] module functionality (pseudo-OpenGL 1.1 style coding)
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 4.0
  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) 2018-2024 Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "rlgl.h"
  17. #include <math.h> // Required for: cosf(), sinf()
  18. //------------------------------------------------------------------------------------
  19. // Module Functions Declaration
  20. //------------------------------------------------------------------------------------
  21. void DrawSphereBasic(Color color); // Draw sphere without any matrix transformation
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. const float sunRadius = 4.0f;
  32. const float earthRadius = 0.6f;
  33. const float earthOrbitRadius = 8.0f;
  34. const float moonRadius = 0.16f;
  35. const float moonOrbitRadius = 1.5f;
  36. InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl module usage with push/pop matrix transformations");
  37. // Define the camera to look into our 3d world
  38. Camera camera = { 0 };
  39. camera.position = (Vector3){ 16.0f, 16.0f, 16.0f }; // Camera position
  40. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  41. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  42. camera.fovy = 45.0f; // Camera field-of-view Y
  43. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  44. float rotationSpeed = 0.2f; // General system rotation speed
  45. float earthRotation = 0.0f; // Rotation of earth around itself (days) in degrees
  46. float earthOrbitRotation = 0.0f; // Rotation of earth around the Sun (years) in degrees
  47. float moonRotation = 0.0f; // Rotation of moon around itself
  48. float moonOrbitRotation = 0.0f; // Rotation of moon around earth in degrees
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. UpdateCamera(&camera, CAMERA_ORBITAL);
  57. earthRotation += (5.0f*rotationSpeed);
  58. earthOrbitRotation += (365/360.0f*(5.0f*rotationSpeed)*rotationSpeed);
  59. moonRotation += (2.0f*rotationSpeed);
  60. moonOrbitRotation += (8.0f*rotationSpeed);
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginDrawing();
  65. ClearBackground(RAYWHITE);
  66. BeginMode3D(camera);
  67. rlPushMatrix();
  68. rlScalef(sunRadius, sunRadius, sunRadius); // Scale Sun
  69. DrawSphereBasic(GOLD); // Draw the Sun
  70. rlPopMatrix();
  71. rlPushMatrix();
  72. rlRotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Earth orbit around Sun
  73. rlTranslatef(earthOrbitRadius, 0.0f, 0.0f); // Translation for Earth orbit
  74. rlPushMatrix();
  75. rlRotatef(earthRotation, 0.25, 1.0, 0.0); // Rotation for Earth itself
  76. rlScalef(earthRadius, earthRadius, earthRadius);// Scale Earth
  77. DrawSphereBasic(BLUE); // Draw the Earth
  78. rlPopMatrix();
  79. rlRotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon orbit around Earth
  80. rlTranslatef(moonOrbitRadius, 0.0f, 0.0f); // Translation for Moon orbit
  81. rlRotatef(moonRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon itself
  82. rlScalef(moonRadius, moonRadius, moonRadius); // Scale Moon
  83. DrawSphereBasic(LIGHTGRAY); // Draw the Moon
  84. rlPopMatrix();
  85. // Some reference elements (not affected by previous matrix transformations)
  86. DrawCircle3D((Vector3){ 0.0f, 0.0f, 0.0f }, earthOrbitRadius, (Vector3){ 1, 0, 0 }, 90.0f, Fade(RED, 0.5f));
  87. DrawGrid(20, 1.0f);
  88. EndMode3D();
  89. DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, MAROON);
  90. DrawFPS(10, 10);
  91. EndDrawing();
  92. //----------------------------------------------------------------------------------
  93. }
  94. // De-Initialization
  95. //--------------------------------------------------------------------------------------
  96. CloseWindow(); // Close window and OpenGL context
  97. //--------------------------------------------------------------------------------------
  98. return 0;
  99. }
  100. //--------------------------------------------------------------------------------------------
  101. // Module Functions Definitions (local)
  102. //--------------------------------------------------------------------------------------------
  103. // Draw sphere without any matrix transformation
  104. // NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
  105. void DrawSphereBasic(Color color)
  106. {
  107. int rings = 16;
  108. int slices = 16;
  109. // Make sure there is enough space in the internal render batch
  110. // buffer to store all required vertex, batch is reseted if required
  111. rlCheckRenderBatchLimit((rings + 2)*slices*6);
  112. rlBegin(RL_TRIANGLES);
  113. rlColor4ub(color.r, color.g, color.b, color.a);
  114. for (int i = 0; i < (rings + 2); i++)
  115. {
  116. for (int j = 0; j < slices; j++)
  117. {
  118. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
  119. sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
  120. cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
  121. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
  122. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  123. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
  124. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)),
  125. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  126. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices)));
  127. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
  128. sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
  129. cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
  130. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)),
  131. sinf(DEG2RAD*(270+(180/(rings + 1))*(i))),
  132. cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices)));
  133. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
  134. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  135. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
  136. }
  137. }
  138. rlEnd();
  139. }