added moon + mouse X control and no more Y mouse control

This commit is contained in:
Volodymyr Patuta 2020-12-23 16:34:52 +01:00
parent 33d32936b2
commit 82b0f7df59
1 changed files with 54 additions and 38 deletions

View File

@ -35,6 +35,7 @@ static surface_t * _sun = NULL;
static surface_t * _planet[9] = {NULL, NULL, NULL,
NULL, NULL, NULL,
NULL, NULL, NULL};
static surface_t * _moon = NULL;
/* des variable d'états pour activer/désactiver des options de rendu */
static int _use_tex = 1, _use_color = 0, _use_lighting = 1;
@ -50,7 +51,7 @@ static cam_t _cam = {0, 1.0f, 10, 0};
static float _v = 0.1f;
static int _wW = 1200, _wH = 1000;
static int _wW = 1200, _wH = 900;
static int _xm = 400, _ym = 300;
static int _pause = 0;
@ -89,10 +90,11 @@ int main(int argc, char ** argv) {
/*!\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;
uint id[9], sun_id, i, moon_id;
/*vec4 g = {0, 1, 0, 1};*/
/* on créé nos trois type de surfaces */
_sun = mkSphere(12, 12); /*ça fait 12x12x2 trianles !*/
_moon = mkSphere(12, 12);
/* on change les couleurs de surfaces */
/*_sun->dcolor = g; */
for(i = 0; i < 9; ++i) {
@ -108,23 +110,28 @@ void init(void) {
id[6] = getTexFromBMP("images/2k-uranus.bmp");
id[7] = getTexFromBMP("images/2k-neptune.bmp");
id[8] = getTexFromBMP("images/pluto.bmp");
moon_id = getTexFromBMP("images/2k-moon.bmp");
setTexId(_sun, sun_id);
setTexId(_moon, moon_id);
for(i = 0; i < 9; ++i)
setTexId(_planet[i], id[i]);
/* si _use_tex != 0, on active l'utilisation de la texture pour les
* trois */
if(_use_tex) {
enableSurfaceOption(_sun, SO_USE_TEXTURE);
enableSurfaceOption(_moon, SO_USE_TEXTURE);
for(i = 0; i < 9; ++i)
enableSurfaceOption(_planet[i], SO_USE_TEXTURE);
}
/* si _use_lighting != 0, on active l'ombrage */
if(_use_lighting) {
enableSurfaceOption(_sun, SO_USE_LIGHTING);
enableSurfaceOption(_moon, SO_USE_LIGHTING);
for(i = 0; i < 9; ++i)
enableSurfaceOption(_planet[i], SO_USE_LIGHTING);
}
disableSurfaceOption(_sun, SO_USE_COLOR);
disableSurfaceOption(_moon, SO_USE_COLOR);
for(i = 0; i < 9; ++i)
disableSurfaceOption(_planet[i], SO_USE_COLOR);
atexit(sortie);
@ -153,12 +160,13 @@ void draw(void) {
/*lookAt(mvMat, _cam.x, 0.0, _cam.z, _cam.x - sin(_cam.theta), 1.0f - _ym / 200.0f, _cam.z - cos(_cam.theta), 0.0, 1.0, 0.0);*/
/*_cam.x = (_xm - (_wW >> 1)) / (float)_wW;*/
// Y ----> 1.0 - (_ym - (_wH >> 1)) / (float)_wH * 5
/*_cam.y = 1.0 - (_ym - (_wH >> 1)) / 300.0f;*/
lookAt(mvMat, _cam.x, _cam.y, _cam.z, _cam.x - sin(_cam.theta), 1.0 - (_ym - (_wH >> 1)) / 300.0f, _cam.z - cos(_cam.theta), 0.0, 1.0, 0.0);
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);
// scales https://www.jpl.nasa.gov/infographics/infographic.view.php?id=10749
// https://www.exploratorium.edu/ronh/solar_system/scale.pdf
/* la sphère est laissée au centre et tourne autour de son axe y */
// SUN
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
rotate(nmv, (a / 24.5f), 0.0f, 1.0f, 0.0f);
@ -179,7 +187,7 @@ void draw(void) {
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, (a / 243.75f), 0.1773f, -1.0f, 0.0f); // rotation anti-clockwise
rotate(nmv, (a / 243.75f), -0.1773f, -1.0f, 0.0f); // rotation anti-clockwise
transform_n_raster(_planet[1], nmv, projMat);
// EARTH
@ -187,15 +195,23 @@ void draw(void) {
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, a, 0.234f, 1.0f, 0.0f);
rotate(nmv, a, -0.234f, 1.0f, 0.0f);
transform_n_raster(_planet[2], nmv, projMat);
// MOON
memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */
rotate(nmv, (a / 365.2425f), 0.0f, 1.0f, 0.0f);
translate(nmv, 11.0f, 1.0f, 0.0f);
rotate(nmv, a, 0.0f, 1.0f, 0.0f);
/*scale(nmv, (1/108.0f) * 12.0f / 30, (1/108.0f) * 12.0f / 30, (1/108.0f) * 12.0f / 30);*/
transform_n_raster(_moon, 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, a / 1.0416f, 0.252f, 1.0f, 0.0f);
rotate(nmv, a / 1.0416f, -0.252f, 1.0f, 0.0f);
transform_n_raster(_planet[3], nmv, projMat);
// JUPITER
@ -203,7 +219,7 @@ void draw(void) {
rotate(nmv, a / (12 * 365.2425f), 0.0f, 1.0f, 0.0f);
translate(nmv, 52.0f, 1.0f, 0.0f);
scale(nmv, (1/9.7f) * 12.0f, (1/9.7f) * 12.0f, (1/9.7f) * 12.0f);
rotate(nmv, a / 0.416f, 0.031f, 1.0f, 0.0f);
rotate(nmv, a / 0.416f, -0.031f, 1.0f, 0.0f);
transform_n_raster(_planet[4], nmv, projMat);
// SATURN
@ -211,7 +227,7 @@ void draw(void) {
rotate(nmv, a / (30 * 365.2425f), 0.0f, 1.0f, 0.0f);
translate(nmv, 95.4f, 1.0f, 0.0f);
scale(nmv, (1/11.4f) * 12.0f, (1/11.4f) * 12.0f, (1/11.4f) * 12.0f);
rotate(nmv, a / 0.4583f, 0.267f, 1.0f, 0.0f);
rotate(nmv, a / 0.4583f, -0.267f, 1.0f, 0.0f);
transform_n_raster(_planet[5], nmv, projMat);
// URANUS
@ -227,15 +243,15 @@ void draw(void) {
rotate(nmv, a / (165 * 365.2425f), 0.0f, 1.0f, 0.0f);
translate(nmv, 301.0f, 1.0f, 0.0f);
scale(nmv, (1/27.7f) * 12.0f, (1/27.7f) * 12.0f, (1/27.7f) * 12.0f);
rotate(nmv, a / 0.6f, 0.283f, 1.0f, 0.0f);
rotate(nmv, a / 0.6f, -0.283f, 1.0f, 0.0f);
transform_n_raster(_planet[7], 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, 394.0f, 1.0f, 0.0f);
scale(nmv, (1/500.0f) * 12.0f, (1/500.0f) * 12.0f, (1/500.0f) * 12.0f);
rotate(nmv, a / 6.4, 0.119f, 1.0f, 0.0f);
rotate(nmv, a / 6.4, -0.119f, 1.0f, 0.0f);
transform_n_raster(_planet[8], nmv, projMat);
/* déclarer qu'on a changé (en bas niveau) des pixels du screen */
@ -249,6 +265,7 @@ void draw(void) {
/*!\brief intercepte l'événement clavier pour modifier les options. */
void key(int keycode) {
double dtheta = M_PI, step = 5.0;
int i;
switch(keycode) {
case GL4DK_UP:
_cam.y += 5 * dtheta;
@ -271,10 +288,10 @@ void key(int keycode) {
_cam.z += _v * step * cos(_cam.theta);
break;
case GL4DK_a:
_cam.theta -= -_v * dtheta;
_cam.theta += _v * step;
break;
case GL4DK_d:
_cam.theta += -_v * dtheta;
_cam.theta -= _v * step;
break;
case GL4DK_p:
if (_pause == 0)
@ -283,13 +300,13 @@ void key(int keycode) {
_pause = 0;
break;
case GL4DK_MINUS:
_v -= 0.001f;
if (_v < 0.001f){
_v = 0.001f;
_v -= 0.01f;
if (_v < 0.01f){
_v = 0.01f;
}
break;
case GL4DK_EQUALS:
_v += 0.001f;
_v += 0.01f;
if (_v > 0.5f){
_v = 0.5f;
}
@ -302,42 +319,36 @@ void key(int keycode) {
_use_tex = !_use_tex;
if(_use_tex) {
enableSurfaceOption(_sun, SO_USE_TEXTURE);
enableSurfaceOption(_planet[0], SO_USE_TEXTURE);
enableSurfaceOption(_planet[1], SO_USE_TEXTURE);
enableSurfaceOption(_planet[2], SO_USE_TEXTURE);
for(i = 0; i < 9; ++i)
enableSurfaceOption(_planet[i], SO_USE_TEXTURE);
} else {
disableSurfaceOption(_sun, SO_USE_TEXTURE);
disableSurfaceOption(_planet[0], SO_USE_TEXTURE);
disableSurfaceOption(_planet[1], SO_USE_TEXTURE);
disableSurfaceOption(_planet[2], 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);
enableSurfaceOption(_planet[0], SO_USE_COLOR);
enableSurfaceOption(_planet[1], SO_USE_COLOR);
enableSurfaceOption(_planet[2], SO_USE_COLOR);
for(i = 0; i < 9; ++i)
enableSurfaceOption(_planet[i], SO_USE_COLOR);
} else {
disableSurfaceOption(_sun, SO_USE_COLOR);
disableSurfaceOption(_planet[0], SO_USE_COLOR);
disableSurfaceOption(_planet[1], SO_USE_COLOR);
disableSurfaceOption(_planet[2], 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);
enableSurfaceOption(_planet[0], SO_USE_LIGHTING);
enableSurfaceOption(_planet[1], SO_USE_LIGHTING);
enableSurfaceOption(_planet[2], SO_USE_LIGHTING);
for(i = 0; i < 9; ++i)
enableSurfaceOption(_planet[i], SO_USE_LIGHTING);
} else {
disableSurfaceOption(_sun, SO_USE_LIGHTING);
disableSurfaceOption(_planet[0], SO_USE_LIGHTING);
disableSurfaceOption(_planet[1], SO_USE_LIGHTING);
disableSurfaceOption(_planet[2], SO_USE_LIGHTING);
for(i = 0; i < 9; ++i)
disableSurfaceOption(_planet[i], SO_USE_LIGHTING);
}
break;
default: break;
@ -347,14 +358,15 @@ void key(int keycode) {
static void pmotion(int x, int y) {
_xm = x;
_ym = y;
_cam.theta = -(_xm - (_wW >> 1)) / (float)_wW*10;
}
static void mouse(int button, int state, int x, int y) {
double dtheta = M_PI;
if (button == GL4D_BUTTON_LEFT)
_cam.y += (_v/2) * dtheta;
_cam.y += 10 * dtheta;
if (button == GL4D_BUTTON_RIGHT)
_cam.y -= (_v/2) * dtheta;
_cam.y -= 10 * dtheta;
if (button == GL4D_BUTTON_MIDDLE)
_cam.y = 1.0f;
}
@ -366,6 +378,10 @@ void sortie(void) {
freeSurface(_sun);
_sun = NULL;
}
if(_moon) {
freeSurface(_moon);
_moon = NULL;
}
for(i = 0; i < 9; ++i) {
if(_planet[i]){
freeSurface(_planet[i]);