Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

168 linhas
7.9 KiB

  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.2 (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. //------------------------------------------------------------------------------------
  16. // Module Functions Declaration
  17. //------------------------------------------------------------------------------------
  18. void DrawSphereBasic(Color color); // Draw sphere without any matrix transformation
  19. //------------------------------------------------------------------------------------
  20. // Program main entry point
  21. //------------------------------------------------------------------------------------
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. const float sunRadius = 4.0f;
  29. const float earthRadius = 0.6f;
  30. const float earthOrbitRadius = 8.0f;
  31. const float moonRadius = 0.16f;
  32. const float moonOrbitRadius = 1.5f;
  33. InitWindow(screenWidth, screenHeight, "raylib [models] example - rlgl module usage with push/pop matrix transformations");
  34. // Define the camera to look into our 3d world
  35. Camera camera = { 0 };
  36. camera.position = (Vector3){ 16.0f, 16.0f, 16.0f };
  37. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  38. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  39. camera.fovy = 45.0f;
  40. camera.type = CAMERA_PERSPECTIVE;
  41. SetCameraMode(camera, CAMERA_FREE);
  42. float rotationSpeed = 0.2f; // General system rotation speed
  43. float earthRotation = 0.0f; // Rotation of earth around itself (days) in degrees
  44. float earthOrbitRotation = 0.0f; // Rotation of earth around the Sun (years) in degrees
  45. float moonRotation = 0.0f; // Rotation of moon around itself
  46. float moonOrbitRotation = 0.0f; // Rotation of moon around earth in degrees
  47. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  48. //--------------------------------------------------------------------------------------
  49. // Main game loop
  50. while (!WindowShouldClose()) // Detect window close button or ESC key
  51. {
  52. // Update
  53. //----------------------------------------------------------------------------------
  54. UpdateCamera(&camera);
  55. earthRotation += (5.0f*rotationSpeed);
  56. earthOrbitRotation += (365/360.0f*(5.0f*rotationSpeed)*rotationSpeed);
  57. moonRotation += (2.0f*rotationSpeed);
  58. moonOrbitRotation += (8.0f*rotationSpeed);
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. BeginDrawing();
  63. ClearBackground(RAYWHITE);
  64. BeginMode3D(camera);
  65. rlPushMatrix();
  66. rlScalef(sunRadius, sunRadius, sunRadius); // Scale Sun
  67. DrawSphereBasic(GOLD); // Draw the Sun
  68. rlPopMatrix();
  69. rlPushMatrix();
  70. rlRotatef(earthOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Earth orbit around Sun
  71. rlTranslatef(earthOrbitRadius, 0.0f, 0.0f); // Translation for Earth orbit
  72. rlRotatef(-earthOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Earth orbit around Sun inverted
  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(-moonOrbitRotation, 0.0f, 1.0f, 0.0f); // Rotation for Moon orbit around Earth inverted
  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. rlBegin(RL_TRIANGLES);
  110. rlColor4ub(color.r, color.g, color.b, color.a);
  111. for (int i = 0; i < (rings + 2); i++)
  112. {
  113. for (int j = 0; j < slices; j++)
  114. {
  115. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
  116. sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
  117. cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
  118. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
  119. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  120. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
  121. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)),
  122. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  123. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices)));
  124. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)),
  125. sinf(DEG2RAD*(270+(180/(rings + 1))*i)),
  126. cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices)));
  127. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)),
  128. sinf(DEG2RAD*(270+(180/(rings + 1))*(i))),
  129. cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices)));
  130. rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)),
  131. sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))),
  132. cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices)));
  133. }
  134. }
  135. rlEnd();
  136. }