models_rlgl_solar_system.c 7.9 KB

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