800 lines
23 KiB
C
800 lines
23 KiB
C
/*!\file window.c
|
|
* \brief Utilisation du raster "maison" pour finaliser le pipeline de
|
|
* rendu 3D. Ici on peut voir les géométries disponibles.
|
|
* \author Farès BELHADJ, amsi@up8.edu
|
|
* \date December 4, 2020.
|
|
* \todo pour les étudiant(e)s : changer la variation de l'angle de
|
|
* rotation pour qu'il soit dépendant du temps et non du framerate
|
|
*/
|
|
#include <assert.h>
|
|
/* inclusion des entêtes de fonctions de gestion de primitives simples
|
|
* de dessin. La lettre p signifie aussi bien primitive que
|
|
* pédagogique. */
|
|
#include <GL4D/gl4dp.h>
|
|
/* inclure notre bibliothèque "maison" de rendu */
|
|
#include "moteur.h"
|
|
|
|
/* inclusion des entêtes de fonctions de création et de gestion de
|
|
* fenêtres système ouvrant un contexte favorable à GL4dummies. Cette
|
|
* partie est dépendante de la bibliothèque SDL2 */
|
|
#include <GL4D/gl4duw_SDL2.h>
|
|
#include <SDL_mixer.h>
|
|
|
|
/* protos de fonctions locales (static) */
|
|
static void init(void);
|
|
static void draw(void);
|
|
static void key(int keycode);
|
|
static void sortie(void);
|
|
static void pmotion(int x, int y);
|
|
static void mouse(int button, int state, int x, int y);
|
|
static void goto_obj(float * mvMat);
|
|
static void move_to(float posx, float posy , float posz);
|
|
static int get_sign(float x, float y);
|
|
|
|
/*!\brief un identifiant pour l'écran (de dessin) */
|
|
static uint _screenId = 0;
|
|
|
|
// 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,
|
|
NULL, NULL, NULL, NULL,
|
|
NULL, NULL};
|
|
|
|
/* des variable d'états pour activer/désactiver des options de rendu */
|
|
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;
|
|
};
|
|
|
|
static cam_t _cam = {0, 1.0f, 10, 0};
|
|
|
|
// movement speed.
|
|
static float _v = 0.1f;
|
|
|
|
// window size.
|
|
static int _wW = 1200, _wH = 900;
|
|
|
|
// middle of the window.
|
|
static int _xm = 600, _ym = 450;
|
|
|
|
static int _pause = 0; // boolean value for the pause.
|
|
static int _movement = 1; // boolean to allow movement.
|
|
static int _overview = 0; // boolean to toggle overview (view from the top).
|
|
static int _p = -1; // the object (sun, planets, pluto) number to move.
|
|
static float _s = 1.0f; // multiplier for angle (for speeding planets movement and rotation).
|
|
static float _a = 0.0f; // rotation angle.
|
|
static float _r = 0.0f;
|
|
|
|
static Mix_Chunk * bsound = NULL; // background sound.
|
|
|
|
/*!\brief paramètre l'application et lance la boucle infinie. */
|
|
int main(int argc, char ** argv) {
|
|
if (SDL_Init(SDL_INIT_AUDIO) == -1) {
|
|
fprintf(stderr, "SDL_Init: %s\n,", Mix_GetError());
|
|
exit(3);
|
|
}
|
|
/* tentative de création d'une fenêtre pour GL4Dummies */
|
|
if(!gl4duwCreateWindow(argc, argv, /* args du programme */
|
|
"Solar System", /* titre */
|
|
10, 10, _wW, _wH, /* x, y, largeur, heuteur */
|
|
GL4DW_SHOWN) /* état visible */) {
|
|
/* ici si échec de la création souvent lié à un problème d'absence
|
|
* de contexte graphique ou d'impossibilité d'ouverture d'un
|
|
* contexte OpenGL (au moins 3.2) */
|
|
return 1;
|
|
}
|
|
/* Pour forcer la désactivation de la synchronisation verticale */
|
|
SDL_GL_SetSwapInterval(0);
|
|
init();
|
|
/* création d'un screen GL4Dummies (texture dans laquelle nous
|
|
* pouvons dessiner) aux dimensions de la fenêtre */
|
|
_screenId = gl4dpInitScreen();
|
|
/* mettre en place la fonction d'interception clavier */
|
|
gl4duwMouseFunc(mouse);
|
|
gl4duwKeyDownFunc(key);
|
|
gl4duwPassiveMotionFunc(pmotion);
|
|
/* mettre en place la fonction de display */
|
|
gl4duwDisplayFunc(draw);
|
|
/* boucle infinie pour éviter que le programme ne s'arrête et ferme
|
|
* la fenêtre immédiatement */
|
|
|
|
gl4duwMainLoop();
|
|
return 0;
|
|
}
|
|
|
|
/*!\brief init de nos données, spécialement les trois surfaces
|
|
* utilisées dans ce code */
|
|
void init(void) {
|
|
uint id[9], sun_id, i, moon_id[18];
|
|
int flags, initted;
|
|
|
|
flags = MIX_INIT_MP3;
|
|
initted = Mix_Init(flags);
|
|
|
|
if ((initted & flags) != flags) {
|
|
fprintf(stderr, "Mix_Init: Failed to init required mp3 support!\n");
|
|
fprintf(stderr, "Mix_Init: %s\n,", Mix_GetError());
|
|
exit(2);
|
|
}
|
|
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024) == -1) {
|
|
fprintf(stderr, "Mix_OpenAudio: %s\n,", Mix_GetError());
|
|
exit(4);
|
|
}
|
|
if (bsound == NULL)
|
|
bsound = Mix_LoadWAV("./space.wav");
|
|
if (Mix_PlayChannel(-1, bsound, 0) < 0)
|
|
fprintf(stderr, "Mix_PlayChannel: %s\n", Mix_GetError());
|
|
|
|
// create all spheres.
|
|
_sun = mkSphere(12, 12); /*ça fait 12x12x2 triangles !*/
|
|
for(i = 0; i < 18; ++i) {
|
|
_moon[i] = mkSphere(12, 12);
|
|
if (i < 9)
|
|
_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");
|
|
id[2] = getTexFromBMP("images/2k-earth-daymap.bmp");
|
|
id[3] = getTexFromBMP("images/2k-mars.bmp");
|
|
id[4] = getTexFromBMP("images/2k-jupiter.bmp");
|
|
id[5] = getTexFromBMP("images/2k-saturn.bmp");
|
|
id[6] = getTexFromBMP("images/2k-uranus.bmp");
|
|
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");
|
|
moon_id[3] = getTexFromBMP("images/moons/io.bmp");
|
|
moon_id[4] = getTexFromBMP("images/moons/europa.bmp");
|
|
moon_id[5] = getTexFromBMP("images/moons/ganymede.bmp");
|
|
moon_id[6] = getTexFromBMP("images/moons/callisto.bmp");
|
|
moon_id[7] = getTexFromBMP("images/moons/enceladus.bmp");
|
|
moon_id[8] = getTexFromBMP("images/moons/dione.bmp");
|
|
moon_id[9] = getTexFromBMP("images/moons/rhea.bmp");
|
|
moon_id[10] = getTexFromBMP("images/moons/titan.bmp");
|
|
moon_id[11] = getTexFromBMP("images/moons/iapetus.bmp");
|
|
moon_id[12] = getTexFromBMP("images/moons/ariel.bmp");
|
|
moon_id[13] = getTexFromBMP("images/moons/umbriel.bmp");
|
|
moon_id[14] = getTexFromBMP("images/moons/titania.bmp");
|
|
moon_id[15] = getTexFromBMP("images/moons/oberon.bmp");
|
|
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);
|
|
if(_use_tex)
|
|
enableSurfaceOption(_sun, SO_USE_TEXTURE);
|
|
if(_use_lighting)
|
|
enableSurfaceOption(_sun, SO_USE_LIGHTING);
|
|
disableSurfaceOption(_sun, SO_USE_COLOR);
|
|
for(i = 0; i < 18; ++i) {
|
|
setTexId(_moon[i], moon_id[i]);
|
|
disableSurfaceOption(_moon[i], SO_USE_COLOR);
|
|
if(_use_tex)
|
|
enableSurfaceOption(_moon[i], SO_USE_TEXTURE);
|
|
if(_use_lighting)
|
|
enableSurfaceOption(_moon[i], SO_USE_LIGHTING);
|
|
if (i < 9) {
|
|
setTexId(_planet[i], id[i]);
|
|
disableSurfaceOption(_planet[i], SO_USE_COLOR);
|
|
if(_use_tex)
|
|
enableSurfaceOption(_planet[i], SO_USE_TEXTURE);
|
|
if(_use_lighting)
|
|
enableSurfaceOption(_planet[i], SO_USE_LIGHTING);
|
|
}
|
|
}
|
|
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; // 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.09f);
|
|
}
|
|
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: // 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: // 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: // 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: // 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: // 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: // 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: // 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: // 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: // 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: // 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);
|
|
break;
|
|
}
|
|
}
|
|
|
|
/*!\brief la fonction appelée à chaque display. */
|
|
void draw(void) {
|
|
static double t0 = 0, t, dt;
|
|
t = gl4dGetElapsedTime();
|
|
dt = (t - t0) / 1000.0;
|
|
t0 = t;
|
|
float mvMat[16], projMat[16], nmv[16], cpy[16];
|
|
/* effacer l'écran et le buffer de profondeur */
|
|
gl4dpClearScreen();
|
|
clearDepth();
|
|
/* des macros facilitant le travail avec des matrices et des
|
|
* vecteurs se trouvent dans la bibliothèque GL4Dummies, dans le
|
|
* fichier gl4dm.h */
|
|
/* charger un frustum dans projMat */
|
|
MFRUSTUM(projMat, -0.005f, 0.005f, -0.005f, 0.005f, 0.01f, 1000.0f);
|
|
/* charger la matrice identité dans model-view */
|
|
MIDENTITY(mvMat);
|
|
|
|
// Y ----> 1.0 - (_ym - (_wH >> 1)) / (float)_wH * 5
|
|
if (_movement)
|
|
lookAt(mvMat, _cam.x, _cam.y, _cam.z, _cam.x - sin(_cam.theta), 1.0, _cam.z - cos(_cam.theta), 0.0, 1.0, 0.0);
|
|
else
|
|
goto_obj(mvMat);
|
|
|
|
// SUN
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, (_r / 24.5f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 0.0f, 1.0f, 0.0f);
|
|
scale(nmv, 2,2,2);
|
|
transform_n_raster(_sun, nmv, projMat);
|
|
|
|
// MERCURY
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, (_a / 87.97f), 0.0f, 1.0f, 0.0f); // orbit mouvement
|
|
translate(nmv, 4.2f, 1.0f, 0.0f);
|
|
scale(nmv, (1/227.0f) * 12.0f, (1/227.0f) * 12.0f, (1/227.0f) * 12.0f);
|
|
rotate(nmv, (_r / 59.0f), 0.0f, 1.0f, 0.0f); // rotation
|
|
transform_n_raster(_planet[0], nmv, projMat);
|
|
|
|
// VENUS
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, (_a / 224.7f), 0.0f, 1.0f, 0.0f); // orbit mouvement
|
|
translate(nmv, 7.9f, 1.0f, 0.0f);
|
|
scale(nmv, (1/113.0f) * 12.0f, (1/113.0f) * 12.0f, (1/113.0f) * 12.0f);
|
|
rotate(nmv, (_r / 243.75f), -0.1773f, -1.0f, 0.0f); // rotation anti-clockwise
|
|
transform_n_raster(_planet[1], nmv, projMat);
|
|
|
|
// EARTH
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, (_a / 365.2425f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 10.0f, 1.0f, 0.0f);
|
|
scale(nmv, (1/108.0f) * 12.0f, (1/108.0f) * 12.0f, (1/108.0f) * 12.0f);
|
|
rotate(nmv, _r, -0.234f, 1.0f, 0.0f);
|
|
transform_n_raster(_planet[2], nmv, projMat);
|
|
|
|
// Moon
|
|
translate(nmv, 4.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/5.0f) , (1/5.0f), (1/5.0f));
|
|
transform_n_raster(_moon[0], nmv, projMat);
|
|
|
|
// MARS
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, (_a / 686.98f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 15.2f, 1.0f, 0.0f);
|
|
scale(nmv, (1/208.0f) * 12.0f, (1/208.0f) * 12.0f, (1/208.0f) * 12.0f);
|
|
rotate(nmv, _r / 1.0416f, -0.252f, 1.0f, 0.0f);
|
|
transform_n_raster(_planet[3], nmv, projMat);
|
|
|
|
memcpy(cpy, nmv, sizeof cpy);
|
|
|
|
// Phobos
|
|
rotate(nmv, _r * 4.3f, 0.0f, 0.1f, 0.0f);
|
|
translate(nmv, 10.0f, 1.0f, 0.0f);
|
|
scale(nmv, (1/5.0f), (1/5.0f), (1/5.0f));
|
|
transform_n_raster(_moon[1], nmv, projMat);
|
|
|
|
// Deimos
|
|
rotate(cpy, _r / 1.5f, 0.0f, 0.1f, 0.0f);
|
|
translate(cpy, 25.0f, 1.0f, 0.0f);
|
|
scale(cpy, (1/6.0f), (1/6.0f), (1/6.0f));
|
|
transform_n_raster(_moon[2], cpy, projMat);
|
|
|
|
// JUPITER
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, _a / (12 * 365.2425f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 30.0f, 1.0f, 0.0f);
|
|
scale(nmv, (1/9.7f) * 12.0f, (1/9.7f) * 12.0f, (1/9.7f) * 12.0f);
|
|
rotate(nmv, _r / 0.416f, -0.031f, 1.0f, 0.0f);
|
|
transform_n_raster(_planet[4], nmv, projMat);
|
|
|
|
memcpy(cpy, nmv, sizeof cpy);
|
|
|
|
// 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));
|
|
transform_n_raster(_moon[3], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Europa
|
|
rotate(nmv, _r / 2.0f, 0.0f, 0.1f, 0.0f);
|
|
translate(nmv, 4.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/13.2f), (1/13.2f), (1/13.2f));
|
|
transform_n_raster(_moon[4], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Ganymede
|
|
rotate(nmv, _r / 3.0f, 0.0f, 0.1f, 0.0f);
|
|
translate(nmv, 5.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/9.0f), (1/9.0f), (1/9.0f));
|
|
transform_n_raster(_moon[5], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Callisto
|
|
rotate(nmv, _r / 5.0f, 0.0f, 0.1f, 0.0f);
|
|
translate(nmv, 6.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/10.0f), (1/10.0f), (1/10.0f));
|
|
transform_n_raster(_moon[6], nmv, projMat);
|
|
|
|
// SATURN
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, _a / (30 * 365.2425f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 50.4f, 1.0f, 0.0f);
|
|
scale(nmv, (1/11.4f) * 12.0f, (1/11.4f) * 12.0f, (1/11.4f) * 12.0f);
|
|
rotate(nmv, _r / 0.4583f, -0.267f, 1.0f, 0.0f);
|
|
transform_n_raster(_planet[5], nmv, projMat);
|
|
|
|
memcpy(cpy, nmv, sizeof cpy);
|
|
|
|
// Enceladus
|
|
rotate(nmv, _r * 5.0f, 0.0f, 0.1f, 0.0f);
|
|
translate(nmv, 2.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/20.0f), (1/20.0f), (1/20.0f));
|
|
transform_n_raster(_moon[7], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Dione
|
|
rotate(nmv, _r * 4.0f, 0.0f, 0.01f, 0.0f);
|
|
translate(nmv, 3.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/18.0f), (1/18.0f), (1/18.0f));
|
|
transform_n_raster(_moon[8], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Rhea
|
|
rotate(nmv, _r * 3.0f, 0.0f, 0.01f, 0.0f);
|
|
translate(nmv, 4.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/15.0f), (1/15.0f), (1/15.0f));
|
|
transform_n_raster(_moon[9], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Titan
|
|
rotate(nmv, _r / 4.0f, 0.0f, 0.01f, 0.0f);
|
|
translate(nmv, 6.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/10.0f), (1/10.0f), (1/10.0f));
|
|
transform_n_raster(_moon[10], nmv, projMat);
|
|
|
|
memcpy(nmv, cpy, sizeof nmv);
|
|
|
|
// Iapetus
|
|
rotate(nmv, _r / 6.0f, 0.0f, 0.01f, 0.0f);
|
|
translate(nmv, 7.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/14.0f), (1/14.0f), (1/14.0f));
|
|
transform_n_raster(_moon[11], nmv, projMat);
|
|
|
|
// URANUS
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, _a / (84 * 365.2425f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 65.0f, 1.0f, 0.0f);
|
|
scale(nmv, (1/26.8f) * 12.0f, (1/26.8f) * 12.0f, (1/26.8f) * 12.0f);
|
|
rotate(nmv, _r / 0.7083f, -0.978f, 0.0f, 0.0f);
|
|
transform_n_raster(_planet[6], nmv, projMat);
|
|
|
|
memcpy(cpy, nmv, sizeof cpy);
|
|
|
|
// Ariel
|
|
rotate(nmv, _r , 0.01f, 0.0f, 0.0f);
|
|
translate(nmv, 0.0f, 3.0f, 0.0f);
|
|
scale(nmv, (1/20.0f), (1/20.0f), (1/20.0f));
|
|
transform_n_raster(_moon[12], nmv, projMat);
|
|
|
|
memcpy (nmv, cpy, sizeof nmv);
|
|
|
|
// Umbriel
|
|
rotate(nmv, _r / 2.0f, 0.01f, 0.0f, 0.0f);
|
|
translate(nmv, 0.0f, 4.0f, 0.0f);
|
|
scale(nmv, (1/20.0f), (1/20.0f), (1/20.0f));
|
|
transform_n_raster(_moon[13], nmv, projMat);
|
|
|
|
memcpy (nmv, cpy, sizeof nmv);
|
|
|
|
// Titania
|
|
rotate(nmv, _r / 3.0f, 0.01f, 0.0f, 0.0f);
|
|
translate(nmv, 0.0f, 5.0f, 0.0f);
|
|
scale(nmv, (1/17.0f), (1/17.0f), (1/17.0f));
|
|
transform_n_raster(_moon[14], nmv, projMat);
|
|
|
|
memcpy (nmv, cpy, sizeof nmv);
|
|
|
|
// Oberon
|
|
rotate(nmv, _r / 5.0f, 0.01f, 0.0f, 0.0f);
|
|
translate(nmv, 0.0f, 6.0f, 0.0f);
|
|
scale(nmv, (1/17.0f), (1/17.0f), (1/17.0f));
|
|
transform_n_raster(_moon[15], nmv, projMat);
|
|
|
|
// NEPTUNE
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, _a / (165 * 365.2425f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 75.0f, 1.0f, 0.0f);
|
|
scale(nmv, (1/27.7f) * 12.0f, (1/27.7f) * 12.0f, (1/27.7f) * 12.0f);
|
|
rotate(nmv, _r / 0.6f, -0.283f, 1.0f, 0.0f);
|
|
transform_n_raster(_planet[7], nmv, projMat);
|
|
|
|
// Triton
|
|
rotate(nmv, _r / 5.0f, 0.0f, 0.01f, 0.0f);
|
|
translate(nmv, 3.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/10.0f), (1/10.0f), (1/10.0f));
|
|
transform_n_raster(_moon[16], nmv, projMat);
|
|
|
|
// PLUTO
|
|
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
|
|
rotate(nmv, _a / (248 * 365.2425f), 0.0f, 1.0f, 0.0f);
|
|
translate(nmv, 85.4f, 1.0f, 0.0f);
|
|
scale(nmv, (1/500.0f) * 12.0f, (1/500.0f) * 12.0f, (1/500.0f) * 12.0f);
|
|
rotate(nmv, _r / 6.4, -0.119f, 1.0f, 0.0f);
|
|
transform_n_raster(_planet[8], nmv, projMat);
|
|
|
|
// Charon
|
|
rotate(nmv, _r / 5.0f, 0.0f, 0.01f, 0.0f);
|
|
translate(nmv, 3.0f, 0.0f, 0.0f);
|
|
scale(nmv, (1/13.0f), (1/13.0f), (1/13.0f));
|
|
transform_n_raster(_moon[16], nmv, projMat);
|
|
|
|
/* déclarer qu'on a changé (en bas niveau) des pixels du screen */
|
|
gl4dpScreenHasChanged();
|
|
/* fonction permettant de raffraîchir l'ensemble de la fenêtre*/
|
|
gl4dpUpdateScreen(NULL);
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
/*!\brief intercepte l'événement clavier pour modifier les options. */
|
|
void key(int keycode) {
|
|
double step = 5.0;
|
|
int i;
|
|
switch(keycode) {
|
|
case GL4DK_UP:
|
|
if (_movement)
|
|
_cam.y += 5;
|
|
break;
|
|
case GL4DK_DOWN:
|
|
if (_movement)
|
|
_cam.y -= 5;
|
|
break;
|
|
case GL4DK_RIGHT:
|
|
if (!_pause)
|
|
_s += 30.0f;
|
|
break;
|
|
case GL4DK_LEFT:
|
|
if (!_pause)
|
|
_s -= 30.0f;
|
|
break;
|
|
case GL4DK_w:
|
|
if (_movement) {
|
|
_cam.x += -_v * step * sin(_cam.theta);
|
|
_cam.z += -_v * step * cos(_cam.theta);
|
|
}
|
|
break;
|
|
case GL4DK_s:
|
|
if (_movement) {
|
|
_cam.x += _v * step * sin(_cam.theta);
|
|
_cam.z += _v * step * cos(_cam.theta);
|
|
}
|
|
break;
|
|
case GL4DK_a:
|
|
if (_movement)
|
|
_cam.theta += step * 0.01;
|
|
break;
|
|
case GL4DK_d:
|
|
if (_movement)
|
|
_cam.theta -= step * 0.01;
|
|
break;
|
|
case GL4DK_p:
|
|
if (_pause == 0)
|
|
_pause = 1;
|
|
else
|
|
_pause = 0;
|
|
break;
|
|
case GL4DK_MINUS:
|
|
_v -= 0.01f;
|
|
if (_v < 0.01f){
|
|
_v = 0.01f;
|
|
}
|
|
break;
|
|
case GL4DK_EQUALS:
|
|
_v += 0.01f;
|
|
if (_v > 0.5f){
|
|
_v = 0.5f;
|
|
}
|
|
break;
|
|
case GL4DK_o:
|
|
if (_movement) {
|
|
if (!_overview) {
|
|
_overview = !_overview;
|
|
_cam.x = 0.0f;
|
|
_cam.y = 200.0f;
|
|
_cam.z = 0.0f;
|
|
_cam.theta = 0.0f;
|
|
} else {
|
|
_overview = !_overview;
|
|
_cam.x = 0.0f;
|
|
_cam.y = 1.0f;
|
|
_cam.z = 10.0f;
|
|
_cam.theta = 0.0f;
|
|
}
|
|
}
|
|
break;
|
|
case GL4DK_r:
|
|
_a = _r;
|
|
_p = -1;
|
|
_pause = 0;
|
|
_movement = 1;
|
|
break;
|
|
case GL4DK_q:
|
|
exit(0);
|
|
break;
|
|
case GL4DK_0:
|
|
_p = 0;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_1:
|
|
_p = 1;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_2:
|
|
_p = 2;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_3:
|
|
_p = 3;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_4:
|
|
_p = 4;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_5:
|
|
_p = 5;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_6:
|
|
_p = 6;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_7:
|
|
_p = 7;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_8:
|
|
_p = 8;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_9:
|
|
_p = 9;
|
|
/*_pause = 1;*/
|
|
_movement = 0;
|
|
_cam.x = (int)_cam.x;
|
|
_cam.y = (int)_cam.y;
|
|
_cam.z = (int)_cam.z;
|
|
break;
|
|
case GL4DK_t: /* 't' la texture */
|
|
_use_tex = !_use_tex;
|
|
if(_use_tex) {
|
|
enableSurfaceOption(_sun, SO_USE_TEXTURE);
|
|
for(i = 0; i < 18; ++i)
|
|
enableSurfaceOption(_moon[i], SO_USE_TEXTURE);
|
|
for(i = 0; i < 9; ++i)
|
|
enableSurfaceOption(_planet[i], SO_USE_TEXTURE);
|
|
} else {
|
|
disableSurfaceOption(_sun, SO_USE_TEXTURE);
|
|
for(i = 0; i < 18; ++i)
|
|
disableSurfaceOption(_moon[i], SO_USE_TEXTURE);
|
|
for(i = 0; i < 9; ++i)
|
|
disableSurfaceOption(_planet[i], SO_USE_TEXTURE);
|
|
}
|
|
break;
|
|
case GL4DK_c: /* 'c' utiliser la couleur */
|
|
_use_color = !_use_color;
|
|
if(_use_color) {
|
|
enableSurfaceOption(_sun, SO_USE_COLOR);
|
|
for(i = 0; i < 18; ++i)
|
|
enableSurfaceOption(_moon[i], SO_USE_COLOR);
|
|
for(i = 0; i < 9; ++i)
|
|
enableSurfaceOption(_planet[i], SO_USE_COLOR);
|
|
} else {
|
|
disableSurfaceOption(_sun, SO_USE_COLOR);
|
|
for(i = 0; i < 18; ++i)
|
|
disableSurfaceOption(_moon[i], SO_USE_COLOR);
|
|
for(i = 0; i < 9; ++i)
|
|
disableSurfaceOption(_planet[i], SO_USE_COLOR);
|
|
}
|
|
break;
|
|
case GL4DK_l: /* 'l' utiliser l'ombrage par la méthode Gouraud */
|
|
_use_lighting = !_use_lighting;
|
|
if(_use_lighting) {
|
|
enableSurfaceOption(_sun, SO_USE_LIGHTING);
|
|
for(i = 0; i < 18; ++i)
|
|
enableSurfaceOption(_moon[i], SO_USE_LIGHTING);
|
|
for(i = 0; i < 9; ++i)
|
|
enableSurfaceOption(_planet[i], SO_USE_LIGHTING);
|
|
} else {
|
|
disableSurfaceOption(_sun, SO_USE_LIGHTING);
|
|
for(i = 0; i < 18; ++i)
|
|
disableSurfaceOption(_moon[i], SO_USE_LIGHTING);
|
|
for(i = 0; i < 9; ++i)
|
|
disableSurfaceOption(_planet[i], SO_USE_LIGHTING);
|
|
}
|
|
break;
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
// handler of mouse motion.
|
|
static void pmotion(int x, int y) {
|
|
if (_movement){
|
|
_xm = x;
|
|
_ym = y;
|
|
_cam.theta = -(_xm - (_wW >> 1)) / (float)_wW*10;
|
|
}
|
|
}
|
|
|
|
// handler of mouse buttons.
|
|
static void mouse(int button, int state, int x, int y) {
|
|
if (_movement){
|
|
double dtheta = M_PI;
|
|
if (button == GL4D_BUTTON_LEFT)
|
|
_cam.y += dtheta;
|
|
if (button == GL4D_BUTTON_RIGHT)
|
|
_cam.y -= dtheta;
|
|
if (button == GL4D_BUTTON_MIDDLE)
|
|
_cam.y = 1.0f;
|
|
}
|
|
}
|
|
|
|
/*!\brief à appeler à la sortie du programme. */
|
|
void sortie(void) {
|
|
int i;
|
|
Mix_CloseAudio();
|
|
Mix_Quit();
|
|
if(_sun) {
|
|
freeSurface(_sun);
|
|
_sun = NULL;
|
|
}
|
|
for(i = 0; i < 18; ++i) {
|
|
if(_moon[i]){
|
|
freeSurface(_moon[i]);
|
|
_moon[i] = NULL;
|
|
}
|
|
if(i < 9) {
|
|
if(_planet[i]){
|
|
freeSurface(_planet[i]);
|
|
_planet[i] = NULL;
|
|
}
|
|
}
|
|
}
|
|
if (bsound)
|
|
Mix_FreeChunk(bsound);
|
|
bsound = NULL;
|
|
/* libère tous les objets produits par GL4Dummies, ici
|
|
* principalement les screen */
|
|
gl4duClean(GL4DU_ALL);
|
|
}
|