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