diff --git a/README.md b/README.md index 71ff3f9..3cc1ade 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ $ make && ./solar-system * `0` à `9` pour visualiser en grand chaque astre. * `R` pour reprendre le contrôle (recover) du point précédant. * `O` pour toggle mode overview (vue de haut). +* `Q` pour quitter. * `L` pour désactiver/activer la lumière. * `C` pour activer/désactiver l'utilisation de la couleur. diff --git a/window.c b/window.c index 3109d82..5fcc011 100644 --- a/window.c +++ b/window.c @@ -33,12 +33,15 @@ static int get_sign(float x, float y); /*!\brief un identifiant pour l'écran (de dessin) */ static uint _screenId = 0; -/*!\brief une surface représentant une sphere */ +// surface representing the Sun. static surface_t * _sun = NULL; + +// surface array representing 8 planets + pluto (dwarf planet). static surface_t * _planet[9] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; +// surface array representing 18 moons of the planets. static surface_t * _moon[18] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, @@ -50,6 +53,7 @@ static int _use_tex = 1, _use_color = 0, _use_lighting = 1; typedef struct cam_t cam_t; +// camera structure, borrowed from sample3d_01-1.6 of GL4Dummies samples. struct cam_t { float x, y, z; float theta; @@ -57,17 +61,21 @@ struct cam_t { static cam_t _cam = {0, 1.0f, 10, 0}; +// movement speed. static float _v = 0.1f; +// window size. static int _wW = 1200, _wH = 900; -static int _xm = 400, _ym = 300; -static int _pause = 0; -static float _s = 1.0f; -static int _movement = 1; -static int _p = -1; -static float _a = 0.0f; -static int _overview = 0; +// middle of the window. +static int _xm = 600, _ym = 450; + +static int _pause = 0; // boolean value for the pause. +static float _s = 1.0f; // multiplier for angle (for speeding planets movement and rotation). +static int _movement = 1; // boolean to allow movement. +static int _p = -1; // the object (sun, planets, pluto) number to move. +static float _a = 0.0f; // rotation angle. +static int _overview = 0; // boolean to toggle overview (view from the top). /*!\brief paramètre l'application et lance la boucle infinie. */ int main(int argc, char ** argv) { @@ -103,8 +111,9 @@ int main(int argc, char ** argv) { * utilisées dans ce code */ void init(void) { uint id[9], sun_id, i, moon_id[18]; - - _sun = mkSphere(12, 12); /*ça fait 12x12x2 trianles !*/ + + // create all spheres. + _sun = mkSphere(12, 12); /*ça fait 12x12x2 triangles !*/ for(i = 0; i < 18; ++i) { _moon[i] = mkSphere(12, 12); } @@ -112,6 +121,7 @@ void init(void) { _planet[i] = mkSphere(12, 12); } + // get all textures. sun_id = getTexFromBMP("images/2k-sun.bmp"); id[0] = getTexFromBMP("images/2k-mercury.bmp"); id[1] = getTexFromBMP("images/2k-venus-surface.bmp"); @@ -123,6 +133,7 @@ void init(void) { id[7] = getTexFromBMP("images/2k-neptune.bmp"); id[8] = getTexFromBMP("images/pluto.bmp"); // https://upload.wikimedia.org/wikipedia/commons/4/4f/Moons_of_solar_system_v7.jpg + // most of the biggest moons are here. moon_id[0] = getTexFromBMP("images/2k-moon.bmp"); moon_id[1] = getTexFromBMP("images/moons/phobos.bmp"); moon_id[2] = getTexFromBMP("images/moons/deimos.bmp"); @@ -142,6 +153,7 @@ void init(void) { moon_id[16] = getTexFromBMP("images/moons/triton.bmp"); moon_id[17] = getTexFromBMP("images/moons/charon.bmp"); + // set texture to an object id and set all options. setTexId(_sun, sun_id); for(i = 0; i < 18; ++i) setTexId(_moon[i], moon_id[i]); @@ -170,73 +182,76 @@ void init(void) { atexit(sortie); } +// get sign for further move calculation. static int get_sign(float x, float y) { if (x > y) return 1; return -1; } +// move to the coordinate. static void move_to(float posx, float posy , float posz){ - float epsilon = 0.1f; - if (fabsf(_cam.x - posx) > epsilon){ - _cam.x = _cam.x - (get_sign(_cam.x, posx) * 0.2f); + float epsilon = 0.1f; // precision for comparing + if (fabsf(_cam.x - posx) > epsilon) { + _cam.x = _cam.x - (get_sign(_cam.x, posx) * 0.09f); } - if (fabsf(_cam.y - posy) > epsilon){ - _cam.y = _cam.y - (get_sign(_cam.y, posy) * 0.2f); + if (fabsf(_cam.y - posy) > epsilon) { + _cam.y = _cam.y - (get_sign(_cam.y, posy) * 0.09f); } - if (fabsf(_cam.z - posz) > epsilon){ - _cam.z = _cam.z - (get_sign(_cam.z, posz) * 0.2f); + if (fabsf(_cam.z - posz) > epsilon) { + _cam.z = _cam.z - (get_sign(_cam.z, posz) * 0.09f); } } +// go to a choosen object (planet, star, pluto(dwarf)) static void goto_obj(float * mvMat) { switch (_p) { - case 1: - _a = 0; + case 1: // MERCURY + _a = 0; // reset angle to 0. lookAt(mvMat, _cam.x, _cam.y, _cam.z, 4.2f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(4.2f, 1.0f, 0.5f); break; - case 2: + case 2: // VENUS _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 7.9f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(7.9f, 1.0f, 1.0f); break; - case 3: + case 3: // EARTH _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 10.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(10.0f, 1.0f, 1.0f); break; - case 4: + case 4: // MARS _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 15.2f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(15.2f, 1.0f, 0.5f); break; - case 5: + case 5: // JUPITER _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 30.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(30.0f, 1.0f, 5.0f); break; - case 6: + case 6: // SATURN _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 50.4f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(50.4f, 1.0f, 5.0f); break; - case 7: + case 7: // NEPTUNE _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 65.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(65.0f, 1.0f, 5.0f); break; - case 8: + case 8: // URANUS _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 75.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(75.0f, 1.0f, 4.0f); break; - case 9: + case 9: // PLUTO _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 85.4f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(85.4f, 1.0f, 0.1f); break; - case 0: + case 0: // SUN _a = 0; lookAt(mvMat, _cam.x, _cam.y, _cam.z, 0, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f); move_to(0.0f, 1.0f, 10.0f); @@ -340,7 +355,7 @@ void draw(void) { // Io rotate(nmv, r * 3.0f, 0.0f, 0.1f, 0.0f); translate(nmv, 3.0f, 0.0f, 0.0f); - scale(nmv, (1/11.2f), (1/11.2f), (1/11.2f)); // 1/11.2f -> Jupiter is bigger then earth 11.2x / 50 io ~= moon size. + scale(nmv, (1/11.2f), (1/11.2f), (1/11.2f)); transform_n_raster(_moon[3], nmv, projMat); memcpy(nmv, cpy, sizeof nmv); @@ -487,7 +502,7 @@ void draw(void) { gl4dpScreenHasChanged(); /* fonction permettant de raffraîchir l'ensemble de la fenêtre*/ gl4dpUpdateScreen(NULL); - if (_pause != 1){ + if (!_pause){ _a += ((360.0 * dt) / 60) * _s; // 360 in 1 minute so 1 day = 1 min r += ((360.0 * dt) / 60) * _s; // 360 in 1 minute so 1 day = 1 min } @@ -574,7 +589,8 @@ void key(int keycode) { _pause = 0; _movement = 1; break; - case GL4DK_z: + case GL4DK_q: + exit(0); break; case GL4DK_0: _p = 0; @@ -708,6 +724,7 @@ void key(int keycode) { } } +// handler of mouse motion. static void pmotion(int x, int y) { if (_movement){ _xm = x; @@ -716,6 +733,7 @@ static void pmotion(int x, int y) { } } +// handler of mouse buttons. static void mouse(int button, int state, int x, int y) { if (_movement){ double dtheta = M_PI;