diff --git a/geometry.c b/geometry.c index a95db86..2c8282d 100644 --- a/geometry.c +++ b/geometry.c @@ -1,10 +1,10 @@ -/*!\file geometry.h +/*!\file geometry.h * * \brief quelques surfaces basiques sous forme polygonale : un plan * (quad), un cube et une sphere. * * \author Farès BELHADJ, amsi@up8.edu - * \date December 2, 2020. + * \date December 2, 2020. */ #include "moteur.h" #include @@ -27,7 +27,7 @@ surface_t * mkQuad(void) { const int order[] = { 0, 1, 2, 2, 1, 3 }; surface_t * s; /* on met du jaune partout */ - const vec4 color0 = { 1.0f, 1.0f, 0.0f, 1.0f }; + const vec4 color0 = { 1.0f, 1.0f, 0.0f, 1.0f }; triangle_t t[2]; int i, j, o; for(i = 0, o = 0; i < 2; ++i) @@ -82,7 +82,7 @@ surface_t * mkCube(void) { const int order[] = { 0, 1, 2, 2, 1, 3 }; surface_t * s; /* on met du vert-clair partout */ - const vec4 color0 = { 0.5f, 1.0f, 0.0f, 1.0f }; + const vec4 color0 = { 0.5f, 1.0f, 0.0f, 1.0f }; triangle_t t[12]; int i, j, k, o; for(i = 0, o = 0; i < 12; ++i) @@ -109,7 +109,7 @@ surface_t * mkSphere(int longitudes, int latitudes, double s) { double c2MPI_Long = 2.0 * M_PI / longitudes; double cMPI_Lat = M_PI / latitudes; /* on met du vert-clair partout */ - const vec4 color0 = { 0.5f, 1.0f, 0.0f, 1.0f }; + const vec4 color0 = { 0.5f, 1.0f, 0.0f, 1.0f }; int z, nz, x, nx, zw, nzw, k, n = 2 * longitudes * latitudes; assert(n); data = malloc((longitudes + 1) * (latitudes + 1) * sizeof *data); diff --git a/window.c b/window.c index 476f2c9..a1dfa9f 100644 --- a/window.c +++ b/window.c @@ -24,6 +24,7 @@ static void init(void); static void draw(void); static void key(int keycode); static void sortie(void); +static void pmotion(int x, int y); /*!\brief un identifiant pour l'écran (de dessin) */ static GLuint _screenId = 0; @@ -37,32 +38,29 @@ static int _use_tex = 1, _use_color = 0, _use_lighting = 1; /*!\brief on peut bouger la caméra vers le haut et vers le bas avec cette variable */ /*static float _ycam = 0.0f; -static float _xcam = 0.0f; -static float _zcam = 10.0f;*/ + static float _xcam = 0.0f; + static float _zcam = 10.0f;*/ typedef struct cam_t cam_t; struct cam_t { - float x, z; - float theta; + float x, y, z; + float theta; }; -static cam_t _cam = {0, 10, 0}; +static cam_t _cam = {0, 0, 50, 0}; -/*static float _y = 0.0f; -static float _x = 0.0f; -static float _z = 0.0f; +static float _v = 0.1f; -static float _v = 0.15f;*/ - -static int _wH = 600; +static int _wW = 1200, _wH = 1000; +static int _xm = 400, _ym = 300; /*!\brief paramètre l'application et lance la boucle infinie. */ int main(int argc, char ** argv) { /* tentative de création d'une fenêtre pour GL4Dummies */ if(!gl4duwCreateWindow(argc, argv, /* args du programme */ "Solar System", /* titre */ - 10, 10, 800, _wH, /* x, y, largeur, heuteur */ + 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 @@ -77,6 +75,7 @@ int main(int argc, char ** argv) { _screenId = gl4dpInitScreen(); /* mettre en place la fonction d'interception clavier */ 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 @@ -91,10 +90,10 @@ void init(void) { GLuint id, id2; /*vec4 g = {0, 1, 0, 1};*/ /* on créé nos trois type de surfaces */ - _sun = mkSphere(12, 12, 2); /* ça fait 12x12x2 trianles ! */ + _sun = mkSphere(12, 12, 1); /* ça fait 12x12x2 trianles ! */ /* on change les couleurs de surfaces */ /*_sun->dcolor = g; */ - _mercury = mkSphere(12,12, 0.3); + _mercury = mkSphere(12, 12, (1/227.0f) * 12.0f); id = getTexFromBMP("images/2k-sun.bmp"); id2 = getTexFromBMP("images/2k-mercury.bmp"); setTexId(_sun, id); @@ -118,10 +117,9 @@ void init(void) { /*!\brief la fonction appelée à chaque display. */ void draw(void) { static float a = 0.0f; - static Uint32 t0 = 0; - GLfloat dt = 0.0; - Uint32 t; - dt = ((t = SDL_GetTicks()) - t0) / 10000.0; + static double t0 = 0, t, dt; + t = gl4dGetElapsedTime(); + dt = (t - t0) / 10000.0; t0 = t; float mvMat[16], projMat[16], nmv[16]; /* effacer l'écran et le buffer de profondeur */ @@ -136,15 +134,20 @@ void draw(void) { MIDENTITY(mvMat); /* on place la caméra en arrière-haut, elle regarde le centre de la scène */ /*lookAt(mvMat, _xcam, _ycam, _zcam, _x, _y, _z, 0, 1, 0);*/ - lookAt(mvMat, _cam.x, 0.0, _cam.z, _cam.x - sin(_cam.theta), 0.0, _cam.z - cos(_cam.theta), 0.0, 1.0, 0.0); + /*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.y = 1.0 - (_ym - (_wH >> 1)) / (float)_wH; + /*_cam.x = (_xm - (_wW >> 1));*/ + lookAt(mvMat, _cam.x, 1.0, _cam.z, _cam.x - sin(_cam.theta), _cam.y, _cam.z - cos(_cam.theta), 0.0, 1.0, 0.0); + /* la sphère est laissée au centre et tourne autour de son axe y */ + static float sun_a = 0.0f; memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */ - translate(nmv, -3.0f, 0.0f, 0.0f); - rotate(nmv, a, 0.0f, 1.0f, 0.0f); + translate(nmv, 0.0f, 1.0f, 0.0f); + rotate(nmv, sun_a += dt * 360, 0.0f, 1.0f, 0.0f); transform_n_raster(_sun, nmv, projMat); memcpy(nmv, mvMat, sizeof nmv); /* copie mvMat dans nmv */ - translate(nmv, 3.0f, 0.0f, 0.0f); + translate(nmv, 10.0f, 1.0f, 0.0f); rotate(nmv, a, 0.0f, 1.0f, 0.0f); transform_n_raster(_mercury, nmv, projMat); /* déclarer qu'on a changé (en bas niveau) des pixels du screen */ @@ -156,66 +159,41 @@ void draw(void) { /*!\brief intercepte l'événement clavier pour modifier les options. */ void key(int keycode) { - double /*dt,*/ dtheta = M_PI, step = 5.0; - /*static double t0 = 0, t;*/ - /*dt = ((t = gl4dGetElapsedTime()) - t0) / 1000.0;*/ - /*t0 = t;*/ + double dtheta = M_PI, step = 5.0; switch(keycode) { case GL4DK_UP: - _cam.x += -0.1 * step * sin(_cam.theta); - _cam.z += -0.1 * step * cos(_cam.theta); - /*_zcam -= _v;*/ - /*_z -= _v;*/ + _cam.x += -_v * step * sin(_cam.theta); + _cam.z += -_v * step * cos(_cam.theta); break; case GL4DK_DOWN: - _cam.x += 0.1 * step * sin(_cam.theta); - _cam.z += 0.1 * step * cos(_cam.theta); - /*_zcam += _v;*/ - /*_z += _v;*/ + _cam.x += _v * step * sin(_cam.theta); + _cam.z += _v * step * cos(_cam.theta); break; case GL4DK_RIGHT: - _cam.theta += -0.01 * dtheta; - /*_xcam += _v;*/ - /*_x += _v;*/ + _cam.theta += -_v * dtheta; break; case GL4DK_LEFT: - _cam.theta -= -0.01 * dtheta; - /*_xcam -= _v;*/ - /*_x -= _v;*/ + _cam.theta -= -_v * dtheta; break; case GL4DK_w: - /*if (_y > 36.0f) { - _y = -_y; - } - _y += _v;*/ - break; - case GL4DK_a: - /*if (_x < -36.0f) { - _x = -_x; - } - _x -= _v;*/ break; case GL4DK_s: - /*if (_y < -36.0f) { - _y = -_y; - } - _y -= _v;*/ + break; + case GL4DK_a: break; case GL4DK_d: - /*if (_x > 36.0f) { - _x = -_x; - } - _x += _v;*/ break; case GL4DK_MINUS: - /*if(_v <= 0.0) { - _v = 0.0f; - break; + _v -= 0.001f; + if (_v < 0.001f){ + _v = 0.001f; } - _v -= 0.15f;*/ break; case GL4DK_EQUALS: - /*_v += 0.15f;*/ + _v += 0.001f; + if (_v > 0.5f){ + _v = 0.5f; + } break; case GL4DK_t: /* 't' la texture */ _use_tex = !_use_tex; @@ -232,7 +210,7 @@ void key(int keycode) { if(_use_color) { enableSurfaceOption(_sun, SO_USE_COLOR); enableSurfaceOption(_mercury, SO_USE_COLOR); - } else { + } else { disableSurfaceOption(_sun, SO_USE_COLOR); disableSurfaceOption(_mercury, SO_USE_COLOR); } @@ -242,7 +220,7 @@ void key(int keycode) { if(_use_lighting) { enableSurfaceOption(_sun, SO_USE_LIGHTING); enableSurfaceOption(_mercury, SO_USE_LIGHTING); - } else { + } else { disableSurfaceOption(_sun, SO_USE_LIGHTING); disableSurfaceOption(_mercury, SO_USE_LIGHTING); } @@ -251,6 +229,11 @@ void key(int keycode) { } } +static void pmotion(int x, int y) { + _xm = x; + _ym = y; +} + /*!\brief à appeler à la sortie du programme. */ void sortie(void) { if(_mercury) {