22*
33* raylib [models] example - rlgl module usage with push/pop matrix transformations
44*
5- * This example uses [rlgl] module funtionality (pseudo-OpenGL 1.1 style coding)
5+ * NOTE: This example uses [rlgl] module functionality (pseudo-OpenGL 1.1 style coding)
66*
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)
7+ * Example originally created with raylib 2.5, last time updated with raylib 4.0
98*
10- * Copyright (c) 2018 Ramon Santamaria (@raysan5)
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)
1113*
1214********************************************************************************************/
1315
1416const r = require ( 'raylib' )
15-
1617// ------------------------------------------------------------------------------------
1718// Module Functions Declaration
1819// ------------------------------------------------------------------------------------
19- function DrawSphereBasic ( color ) {
20- const rings = 16
21- const slices = 16
22-
23- r . rlBegin ( r . RL_TRIANGLES )
24- r . rlColor4ub ( color . r , color . g , color . b , color . a )
25-
26- for ( let i = 0 ; i < ( rings + 2 ) ; i ++ ) {
27- for ( let j = 0 ; j < slices ; j ++ ) {
28- r . rlVertex3f ( Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * Math . sin ( r . DEG2RAD * ( j * 360 / slices ) ) ,
29- Math . sin ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) ,
30- Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * Math . cos ( r . DEG2RAD * ( j * 360 / slices ) ) )
31- r . rlVertex3f ( Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * Math . sin ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) ,
32- Math . sin ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) ,
33- Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * Math . cos ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) )
34- r . rlVertex3f ( Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * Math . sin ( r . DEG2RAD * ( j * 360 / slices ) ) ,
35- Math . sin ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) ,
36- Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * Math . cos ( r . DEG2RAD * ( j * 360 / slices ) ) )
37-
38- r . rlVertex3f ( Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * Math . sin ( r . DEG2RAD * ( j * 360 / slices ) ) ,
39- Math . sin ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) ,
40- Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * Math . cos ( r . DEG2RAD * ( j * 360 / slices ) ) )
41- r . rlVertex3f ( Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i ) ) ) * Math . sin ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) ,
42- Math . sin ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i ) ) ) ,
43- Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i ) ) ) * Math . cos ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) )
44- r . rlVertex3f ( Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * Math . sin ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) ,
45- Math . sin ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) ,
46- Math . cos ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * Math . cos ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) )
47- }
48- }
49- r . rlEnd ( )
50- }
5120
5221// ------------------------------------------------------------------------------------
5322// Program main entry point
@@ -64,39 +33,38 @@ const earthOrbitRadius = 8.0
6433const moonRadius = 0.16
6534const moonOrbitRadius = 1.5
6635
67- r . InitWindow ( screenWidth , screenHeight , 'raylib [models] example - rlgl module usage with push/pop matrix transformations' )
36+ r . InitWindow ( screenWidth , screenHeight , 'node raylib [models] example - rlgl module usage with push/pop matrix transformations' )
6837
69- // Define the camera to look into our 3d world
70- const camera = r . Camera ( )
71- camera . position = r . Vector3 ( 16 , 16 , 16 )
72- camera . target = r . Vector3 ( 0 , 0 , 0 )
73- camera . up = r . Vector3 ( 0 , 1 , 0 )
74- camera . fovy = 45
75- camera . type = r . CAMERA_PERSPECTIVE
76- camera . projection = r . CAMERA_PERSPECTIVE
38+ const camera = {
39+ position : { x : 16 , y : 16 , z : 16 } ,
40+ target : { x : 0 , y : 0 , z : 0 } ,
41+ up : { x : 0 , y : 1 , z : 0 } ,
42+ fovy : 45 ,
43+ projection : r . CAMERA_PERSPECTIVE
44+ }
7745
7846r . SetCameraMode ( camera , r . CAMERA_FREE )
7947
8048const rotationSpeed = 0.2 // General system rotation speed
8149
82- let earthRotation = 0 // Rotation of earth around itself (days) in degrees
83- let earthOrbitRotation = 0 // Rotation of earth around the Sun (years) in degrees
50+ let earthRotation = 0.0 // Rotation of earth around itself (days) in degrees
51+ let earthOrbitRotation = 0.0 // Rotation of earth around the Sun (years) in degrees
8452let moonRotation = 0.0 // Rotation of moon around itself
85- let moonOrbitRotation = 0 // Rotation of moon around earth in degrees
53+ let moonOrbitRotation = 0.0 // Rotation of moon around earth in degrees
8654
8755r . SetTargetFPS ( 60 ) // Set our game to run at 60 frames-per-second
8856// --------------------------------------------------------------------------------------
8957
9058// Main game loop
91- while ( ! r . WindowShouldClose ( ) ) { // Detect window close button or ESC key
59+ while ( ! r . WindowShouldClose ( ) ) {
9260 // Update
9361 // ----------------------------------------------------------------------------------
9462 r . UpdateCamera ( camera )
9563
96- earthRotation += ( 5 * rotationSpeed )
97- earthOrbitRotation += ( 365 / 360 * ( 5 * rotationSpeed ) * rotationSpeed )
98- moonRotation += ( 2 * rotationSpeed )
99- moonOrbitRotation += ( 8 * rotationSpeed )
64+ earthRotation += ( 5.0 * rotationSpeed )
65+ earthOrbitRotation += ( 365 / 360.0 * ( 5.0 * rotationSpeed ) * rotationSpeed )
66+ moonRotation += ( 2.0 * rotationSpeed )
67+ moonOrbitRotation += ( 8.0 * rotationSpeed )
10068 // ----------------------------------------------------------------------------------
10169
10270 // Draw
@@ -115,7 +83,6 @@ while (!r.WindowShouldClose()) { // Detect window close button or ESC key
11583 r . rlPushMatrix ( )
11684 r . rlRotatef ( earthOrbitRotation , 0.0 , 1.0 , 0.0 ) // Rotation for Earth orbit around Sun
11785 r . rlTranslatef ( earthOrbitRadius , 0.0 , 0.0 ) // Translation for Earth orbit
118- r . rlRotatef ( - earthOrbitRotation , 0.0 , 1.0 , 0.0 ) // Rotation for Earth orbit around Sun inverted
11986
12087 r . rlPushMatrix ( )
12188 r . rlRotatef ( earthRotation , 0.25 , 1.0 , 0.0 ) // Rotation for Earth itself
@@ -126,15 +93,14 @@ while (!r.WindowShouldClose()) { // Detect window close button or ESC key
12693
12794 r . rlRotatef ( moonOrbitRotation , 0.0 , 1.0 , 0.0 ) // Rotation for Moon orbit around Earth
12895 r . rlTranslatef ( moonOrbitRadius , 0.0 , 0.0 ) // Translation for Moon orbit
129- r . rlRotatef ( - moonOrbitRotation , 0.0 , 1.0 , 0.0 ) // Rotation for Moon orbit around Earth inverted
13096 r . rlRotatef ( moonRotation , 0.0 , 1.0 , 0.0 ) // Rotation for Moon itself
13197 r . rlScalef ( moonRadius , moonRadius , moonRadius ) // Scale Moon
13298
13399 DrawSphereBasic ( r . LIGHTGRAY ) // Draw the Moon
134100 r . rlPopMatrix ( )
135101
136102 // Some reference elements (not affected by previous matrix transformations)
137- r . DrawCircle3D ( r . Vector3 ( 0.0 , 0.0 , 0.0 ) , earthOrbitRadius , r . Vector3 ( 1 , 0 , 0 ) , 90.0 , r . Fade ( r . RED , 0.5 ) )
103+ r . DrawCircle3D ( { x : 0 , y : 0 , z : 0 } , earthOrbitRadius , { x : 1 , y : 0 , z : 0 } , 90.0 , r . Fade ( r . RED , 0.5 ) )
138104 r . DrawGrid ( 20 , 1.0 )
139105
140106 r . EndMode3D ( )
@@ -150,3 +116,48 @@ while (!r.WindowShouldClose()) { // Detect window close button or ESC key
150116// --------------------------------------------------------------------------------------
151117r . CloseWindow ( ) // Close window and OpenGL context
152118// --------------------------------------------------------------------------------------
119+
120+ // --------------------------------------------------------------------------------------------
121+ // Module Functions Definitions (local)
122+ // --------------------------------------------------------------------------------------------
123+
124+ // Draw sphere without any matrix transformation
125+ // NOTE: Sphere is drawn in world position ( 0, 0, 0 ) with radius 1.0f
126+ function DrawSphereBasic ( color ) {
127+ const rings = 16
128+ const slices = 16
129+
130+ // Make sure there is enough space in the internal render batch
131+ // buffer to store all required vertex, batch is reseted if required
132+ r . rlCheckRenderBatchLimit ( ( rings + 2 ) * slices * 6 )
133+
134+ r . rlBegin ( r . RL_TRIANGLES )
135+ r . rlColor4ub ( color . r , color . g , color . b , color . a )
136+ const cosf = Math . cos
137+ const sinf = Math . sin
138+ r . DEG2RAD = Math . PI / 180
139+ for ( let i = 0 ; i < ( rings + 2 ) ; i ++ ) {
140+ for ( let j = 0 ; j < slices ; j ++ ) {
141+ r . rlVertex3f ( cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * sinf ( r . DEG2RAD * ( j * 360 / slices ) ) ,
142+ sinf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) ,
143+ cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * cosf ( r . DEG2RAD * ( j * 360 / slices ) ) )
144+ r . rlVertex3f ( cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * sinf ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) ,
145+ sinf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) ,
146+ cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * cosf ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) )
147+ r . rlVertex3f ( cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * sinf ( r . DEG2RAD * ( j * 360 / slices ) ) ,
148+ sinf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) ,
149+ cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * cosf ( r . DEG2RAD * ( j * 360 / slices ) ) )
150+
151+ r . rlVertex3f ( cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * sinf ( r . DEG2RAD * ( j * 360 / slices ) ) ,
152+ sinf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) ,
153+ cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * i ) ) * cosf ( r . DEG2RAD * ( j * 360 / slices ) ) )
154+ r . rlVertex3f ( cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i ) ) ) * sinf ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) ,
155+ sinf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i ) ) ) ,
156+ cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i ) ) ) * cosf ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) )
157+ r . rlVertex3f ( cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * sinf ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) ,
158+ sinf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) ,
159+ cosf ( r . DEG2RAD * ( 270 + ( 180 / ( rings + 1 ) ) * ( i + 1 ) ) ) * cosf ( r . DEG2RAD * ( ( j + 1 ) * 360 / slices ) ) )
160+ }
161+ }
162+ r . rlEnd ( )
163+ }
0 commit comments