models_rlgl_solar_system.c 8.2 KB

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