more playing with camera

This commit is contained in:
Volodymyr Patuta 2020-12-18 08:12:23 +01:00
parent a0236fafe0
commit aaa7a88730
1 changed files with 98 additions and 38 deletions

136
window.c
View File

@ -29,26 +29,40 @@ static void sortie(void);
static GLuint _screenId = 0;
/*!\brief une surface représentant une sphere */
static surface_t * _sphere = NULL;
static surface_t * _sun = NULL;
static surface_t * _mercury = NULL;
/* des variable d'états pour activer/désactiver des options de rendu */
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 _ycam = 0.0f;
static float _xcam = 0.0f;
static float _zcam = 10.0f;
static float _zcam = 10.0f;*/
static float _y = 0.0f;
typedef struct cam_t cam_t;
struct cam_t {
float x, z;
float theta;
};
static cam_t _cam = {0, 10, 0};
/*static float _y = 0.0f;
static float _x = 0.0f;
static float _z = 0.0f;
static float _v = 0.15f;*/
static int _wH = 600;
/*!\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, 640, 480, /* x, y, largeur, heuteur */
10, 10, 800, _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
@ -74,25 +88,30 @@ 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) {
GLuint id;
vec4 g = {0, 1, 0, 1};
GLuint id, id2;
/*vec4 g = {0, 1, 0, 1};*/
/* on créé nos trois type de surfaces */
_sphere = mkSphere(12, 12); /* ça fait 12x12x2 trianles ! */
_sun = mkSphere(12, 12); /* ça fait 12x12x2 trianles ! */
/* on change les couleurs de surfaces */
_sphere->dcolor = g;
/* on leur rajoute la même texture */
id = getTexFromBMP("images/2k-mars.bmp");
setTexId(_sphere, id);
/*_sun->dcolor = g; */
_mercury = mkSphere(12,12);
id = getTexFromBMP("images/2k-sun.bmp");
id2 = getTexFromBMP("images/2k-mercury.bmp");
setTexId(_sun, id);
setTexId(_mercury, id2);
/* si _use_tex != 0, on active l'utilisation de la texture pour les
* trois */
if(_use_tex) {
enableSurfaceOption(_sphere, SO_USE_TEXTURE);
enableSurfaceOption(_mercury, SO_USE_TEXTURE);
enableSurfaceOption(_sun, SO_USE_TEXTURE);
}
/* si _use_lighting != 0, on active l'ombrage */
if(_use_lighting) {
enableSurfaceOption(_sphere, SO_USE_LIGHTING);
enableSurfaceOption(_sun, SO_USE_LIGHTING);
enableSurfaceOption(_mercury, SO_USE_LIGHTING);
}
disableSurfaceOption(_sphere, SO_USE_COLOR);
disableSurfaceOption(_sun, SO_USE_COLOR);
disableSurfaceOption(_mercury, SO_USE_COLOR);
atexit(sortie);
}
@ -111,11 +130,18 @@ void draw(void) {
/* charger la matrice identité dans model-view */
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, _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);
/* la sphère est laissée au centre et tourne autour de son axe y */
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);
transform_n_raster(_sphere, nmv, projMat);
transform_n_raster(_sun, nmv, projMat);
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);
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*/
@ -125,65 +151,95 @@ 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;*/
switch(keycode) {
case GL4DK_UP:
_zcam -= 0.15f;
_cam.x += -0.1 * step * sin(_cam.theta);
_cam.z += -0.1 * step * cos(_cam.theta);
/*_zcam -= _v;*/
/*_z -= _v;*/
break;
case GL4DK_DOWN:
_zcam += 0.15f;
_cam.x += 0.1 * step * sin(_cam.theta);
_cam.z += 0.1 * step * cos(_cam.theta);
/*_zcam += _v;*/
/*_z += _v;*/
break;
case GL4DK_RIGHT:
_xcam += 0.15f;
_cam.theta += -0.01 * dtheta;
/*_xcam += _v;*/
/*_x += _v;*/
break;
case GL4DK_LEFT:
_xcam -= 0.15f;
_cam.theta -= -0.01 * dtheta;
/*_xcam -= _v;*/
/*_x -= _v;*/
break;
case GL4DK_w:
if (_y > 10.0f) {
/*if (_y > 36.0f) {
_y = -_y;
}
_y += 0.15f;
_y += _v;*/
break;
case GL4DK_a:
if (_x < -10.0f) {
/*if (_x < -36.0f) {
_x = -_x;
}
_x -= 0.15f;
_x -= _v;*/
break;
case GL4DK_s:
if (_y < -10.0f) {
/*if (_y < -36.0f) {
_y = -_y;
}
_y -= 0.15f;
_y -= _v;*/
break;
case GL4DK_d:
if (_x > 10.0f) {
/*if (_x > 36.0f) {
_x = -_x;
}
_x += 0.15f;
_x += _v;*/
break;
case GL4DK_MINUS:
/*if(_v <= 0.0) {
_v = 0.0f;
break;
}
_v -= 0.15f;*/
break;
case GL4DK_EQUALS:
/*_v += 0.15f;*/
break;
case GL4DK_t: /* 't' la texture */
_use_tex = !_use_tex;
if(_use_tex) {
enableSurfaceOption(_sphere, SO_USE_TEXTURE);
enableSurfaceOption(_sun, SO_USE_TEXTURE);
enableSurfaceOption(_mercury, SO_USE_TEXTURE);
} else {
disableSurfaceOption(_sphere, SO_USE_TEXTURE);
disableSurfaceOption(_sun, SO_USE_TEXTURE);
disableSurfaceOption(_mercury, SO_USE_TEXTURE);
}
break;
case GL4DK_c: /* 'c' utiliser la couleur */
_use_color = !_use_color;
if(_use_color) {
enableSurfaceOption(_sphere, SO_USE_COLOR);
enableSurfaceOption(_sun, SO_USE_COLOR);
enableSurfaceOption(_mercury, SO_USE_COLOR);
} else {
disableSurfaceOption(_sphere, SO_USE_COLOR);
disableSurfaceOption(_sun, SO_USE_COLOR);
disableSurfaceOption(_mercury, SO_USE_COLOR);
}
break;
case GL4DK_l: /* 'l' utiliser l'ombrage par la méthode Gouraud */
_use_lighting = !_use_lighting;
if(_use_lighting) {
enableSurfaceOption(_sphere, SO_USE_LIGHTING);
enableSurfaceOption(_sun, SO_USE_LIGHTING);
enableSurfaceOption(_mercury, SO_USE_LIGHTING);
} else {
disableSurfaceOption(_sphere, SO_USE_LIGHTING);
disableSurfaceOption(_sun, SO_USE_LIGHTING);
disableSurfaceOption(_mercury, SO_USE_LIGHTING);
}
break;
default: break;
@ -192,9 +248,13 @@ void key(int keycode) {
/*!\brief à appeler à la sortie du programme. */
void sortie(void) {
if(_sphere) {
freeSurface(_sphere);
_sphere = NULL;
if(_mercury) {
freeSurface(_mercury);
_mercury = NULL;
}
if(_sun) {
freeSurface(_sun);
_sun = NULL;
}
/* libère tous les objets produits par GL4Dummies, ici
* principalement les screen */