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.

174 lines
8.0 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-2022 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 };
  40. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  41. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  42. camera.fovy = 45.0f;
  43. camera.projection = CAMERA_PERSPECTIVE;
  44. SetCameraMode(camera, CAMERA_FREE);
  45. float rotationSpeed = 0.2f; // General system rotation speed
  46. float earthRotation = 0.0f; // Rotation of earth around itself (days) in degrees
  47. float earthOrbitRotation = 0.0f; // Rotation of earth around the Sun (years) in degrees
  48. float moonRotation = 0.0f; // Rotation of moon around itself
  49. float moonOrbitRotation = 0.0f; // Rotation of moon around earth in degrees
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. UpdateCamera(&camera);
  58. earthRotation += (5.0f*rotationSpeed);
  59. earthOrbitRotation += (365/360.0f*(5.0f*rotationSpeed)*rotationSpeed);
  60. moonRotation += (2.0f*rotationSpeed);
  61. moonOrbitRotation += (8.0f*rotationSpeed);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. BeginMode3D(camera);
  68. rlPushMatrix();
  69. rlScalef(sunRadius, sunRadius, sunRadius); // Scale Sun
  70. DrawSphereBasic(GOLD); // Draw the Sun
  71. rlPopMatrix();
  72. rlPushMatrix();
  73. rlRotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Earth orbit around Sun
  74. rlTranslatef(earthOrbitRadius, 0.0f, 0.0f); // Translation for Earth orbit
  75. rlPushMatrix();
  76. rlRotatef(earthRotation, 0.25, 1.0, 0.0); // Rotation for Earth itself
  77. rlScalef(earthRadius, earthRadius, earthRadius);// Scale Earth
  78. DrawSphereBasic(BLUE); // Draw the Earth
  79. rlPopMatrix();
  80. rlRotatef(moonOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon orbit around Earth
  81. rlTranslatef(moonOrbitRadius, 0.0f, 0.0f); // Translation for Moon orbit
  82. rlRotatef(moonRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon itself
  83. rlScalef(moonRadius, moonRadius, moonRadius); // Scale Moon
  84. DrawSphereBasic(LIGHTGRAY); // Draw the Moon
  85. rlPopMatrix();
  86. // Some reference elements (not affected by previous matrix transformations)
  87. DrawCircle3D((Vector3){ 0.0f, 0.0f, 0.0f }, earthOrbitRadius, (Vector3){ 1, 0, 0 }, 90.0f, Fade(RED, 0.5f));
  88. DrawGrid(20, 1.0f);
  89. EndMode3D();
  90. DrawText("EARTH ORBITING AROUND THE SUN!", 400, 10, 20, MAROON);
  91. DrawFPS(10, 10);
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. CloseWindow(); // Close window and OpenGL context
  98. //--------------------------------------------------------------------------------------
  99. return 0;
  100. }
  101. //--------------------------------------------------------------------------------------------
  102. // Module Functions Definitions (local)
  103. //--------------------------------------------------------------------------------------------
  104. // Draw sphere without any matrix transformation
  105. // NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
  106. void DrawSphereBasic(Color color)
  107. {
  108. int rings = 16;
  109. int slices = 16;
  110. // Make sure there is enough space in the internal render batch
  111. // buffer to store all required vertex, batch is reseted if required
  112. rlCheckRenderBatchLimit((rings + 2)*slices*6);
  113. rlBegin(RL_TRIANGLES);
  114. rlColor4ub(color.r, color.g, color.b, color.a);
  115. for (int i = 0; i < (rings + 2); i++)
  116. {
  117. for (int j = 0; j < slices; j++)
  118. {
  119. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
  120. sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
  121. cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
  122. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
  123. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  124. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
  125. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)),
  126. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  127. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices)));
  128. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
  129. sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
  130. cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
  131. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)),
  132. sinf(DEG2RAD*(270+(180/(rings + 1))*(i))),
  133. cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices)));
  134. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
  135. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  136. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
  137. }
  138. }
  139. rlEnd();
  140. }