cam y, mouse, mercury orital mouvement, pause

This commit is contained in:
Volodymyr Patuta 2020-12-20 12:28:30 +01:00
parent b47ad1a11d
commit 1ae25d4e14
1 changed files with 42 additions and 20 deletions

View File

@ -25,6 +25,7 @@ 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);
/*!\brief un identifiant pour l'écran (de dessin) */
static uint _screenId = 0;
@ -48,13 +49,15 @@ struct cam_t {
float theta;
};
static cam_t _cam = {0, 0, 50, 0};
static cam_t _cam = {0, 1.0f, 50, 0};
static float _v = 0.1f;
static int _wW = 1200, _wH = 1000;
static int _xm = 400, _ym = 300;
static int _pause = 0;
/*!\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 */
@ -74,6 +77,7 @@ int main(int argc, char ** argv) {
* 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 */
@ -90,7 +94,7 @@ void init(void) {
uint id, id2;
/*vec4 g = {0, 1, 0, 1};*/
/* on créé nos trois type de surfaces */
_sun = mkSphere(12, 12, 1); /* ç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, (1/227.0f) * 12.0f);
@ -135,26 +139,28 @@ void draw(void) {
/* 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), 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);
/*_cam.x = (_xm - (_wW >> 1)) / (float)_wW;*/
/*_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);
/* 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 */
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 */
rotate(nmv, a, 0.0f, 1.0f, 0.0f); // orbit mouvement
translate(nmv, 10.0f, 1.0f, 0.0f);
rotate(nmv, a, 0.0f, 1.0f, 0.0f);
rotate(nmv, a, 0.0f, 1.0f, 0.0f); // rotation
transform_n_raster(_mercury, 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);
a += 360 * dt;
if (_pause != 1)
a += 360 * dt;
}
/*!\brief intercepte l'événement clavier pour modifier les options. */
@ -162,26 +168,32 @@ void key(int keycode) {
double dtheta = M_PI, step = 5.0;
switch(keycode) {
case GL4DK_UP:
break;
case GL4DK_DOWN:
break;
case GL4DK_RIGHT:
break;
case GL4DK_LEFT:
break;
case GL4DK_w:
_cam.x += -_v * step * sin(_cam.theta);
_cam.z += -_v * step * cos(_cam.theta);
break;
case GL4DK_DOWN:
case GL4DK_s:
_cam.x += _v * step * sin(_cam.theta);
_cam.z += _v * step * cos(_cam.theta);
break;
case GL4DK_RIGHT:
_cam.theta += -_v * dtheta;
break;
case GL4DK_LEFT:
case GL4DK_a:
_cam.theta -= -_v * dtheta;
break;
case GL4DK_w:
break;
case GL4DK_s:
break;
case GL4DK_a:
break;
case GL4DK_d:
_cam.theta += -_v * dtheta;
break;
case GL4DK_p:
if (_pause == 0)
_pause = 1;
else
_pause = 0;
break;
case GL4DK_MINUS:
_v -= 0.001f;
@ -234,6 +246,16 @@ static void pmotion(int x, int y) {
_ym = y;
}
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;
if (button == GL4D_BUTTON_RIGHT)
_cam.y -= (_v/2) * dtheta;
if (button == GL4D_BUTTON_MIDDLE)
_cam.y = 1.0f;
}
/*!\brief à appeler à la sortie du programme. */
void sortie(void) {
if(_mercury) {