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 | //------------------------------------------------------------------------------------ |
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.type = 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 |
134 | void DrawSphereBasic(Color color) |
135 | { |
136 | int rings = 16; |
137 | int slices = 16; |
138 | |
139 | rlBegin(RL_TRIANGLES); |
140 | rlColor4ub(color.r, color.g, color.b, color.a); |
141 | |
142 | for (int i = 0; i < (rings + 2); i++) |
143 | { |
144 | for (int j = 0; j < slices; j++) |
145 | { |
146 | rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)), |
147 | sinf(DEG2RAD*(270+(180/(rings + 1))*i)), |
148 | cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices))); |
149 | rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)), |
150 | sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))), |
151 | cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices))); |
152 | rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*(j*360/slices)), |
153 | sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))), |
154 | cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*(j*360/slices))); |
155 | |
156 | rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*i))*sinf(DEG2RAD*(j*360/slices)), |
157 | sinf(DEG2RAD*(270+(180/(rings + 1))*i)), |
158 | cosf(DEG2RAD*(270+(180/(rings + 1))*i))*cosf(DEG2RAD*(j*360/slices))); |
159 | rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*sinf(DEG2RAD*((j+1)*360/slices)), |
160 | sinf(DEG2RAD*(270+(180/(rings + 1))*(i))), |
161 | cosf(DEG2RAD*(270+(180/(rings + 1))*(i)))*cosf(DEG2RAD*((j+1)*360/slices))); |
162 | rlVertex3f(cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*sinf(DEG2RAD*((j+1)*360/slices)), |
163 | sinf(DEG2RAD*(270+(180/(rings + 1))*(i+1))), |
164 | cosf(DEG2RAD*(270+(180/(rings + 1))*(i+1)))*cosf(DEG2RAD*((j+1)*360/slices))); |
165 | } |
166 | } |
167 | rlEnd(); |
168 | } |
169 | |