IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

GLUT Discussion :

deplacer un cube avec clavier


Sujet :

GLUT

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    18
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 18
    Par défaut deplacer un cube avec clavier
    bonjour,

    J'ai beau cherché partout mais je n'ai rien trouvé.
    J'aurais voulu savoir comment peut-on déplacer un cube à l'aide du clavier dessiné sur opengl comme dans cet exemple ci dessous avec la fonction glutKeyboardFunc ou glutSpecialFunc sachant que j'ai paramétré et initialisé GLUT?
    Je ne vois pas comment sélectionner ce cube sans nom.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
     
    void Draw()
    {
     
    glClear(GL_COLOR_BUFFER_BIT);
    glClear (GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT); 	//Efface le frame buffer et le Z-buffer
    glMatrixMode(GL_MODELVIEW); 	//Choisit la matrice MODELVIEW
    glLoadIdentity(); 	//Réinitialise la matrice
    gluLookAt(5,5,5,0,0,0,0,1,0);
     
    //Placer ici tout le code de transformation et de dessin
    glBegin (GL_QUADS);
    glColor3ub(255,255,0);// COULEUR JAUNE
        glVertex3d(1,1,1);
        glVertex3d(1,1,-1);
        glVertex3d(-1,1,-1);
        glVertex3d(-1,1,1);
     
    glColor3ub(255,0,0);// COULEUR ROUGE
        glVertex3d(1,-1,1);
        glVertex3d(1,-1,-1);
        glVertex3d(1,1,-1);
        glVertex3d(1,1,1);
     
    glColor3ub(162,230,212);// COULEUR BLEUE
        glVertex3d(-1,-1,1);
        glVertex3d(-1,-1,-1);
        glVertex3d(1,-1,-1);
        glVertex3d(1,-1,1);
     
    glColor3ub(255,255,0); //COULEUR JAUNE
        glVertex3d(-1,1,1);
        glVertex3d(-1,1,-1);
        glVertex3d(-1,-1,-1);
        glVertex3d(-1,-1,1);
     
    glColor3ub(0,255,0); //COULEUR VERTE
        glVertex3d(1,1,-1);
        glVertex3d(1,-1,-1);
        glVertex3d(-1,-1,-1);
        glVertex3d(-1,1,-1);
     
    glColor3ub(0,255,0); //COULEUR VERTE
        glVertex3d(1,-1,1);
        glVertex3d(1,1,1);
        glVertex3d(-1,1,1);
        glVertex3d(-1,-1,1);
     
        glEnd();
     
    glutSwapBuffers();
    	//Attention : pas SwapBuffers(DC) !
     
    glutPostRedisplay();
    	//Demande de recalculer la scène
    }

  2. #2
    Membre émérite Avatar de MatRem
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    750
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 750
    Par défaut
    Il faut que tu paramètrises ton affichage par une position (x,y,z), que tu mets à jour dans ton callback clavier.

  3. #3
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    18
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 18
    Par défaut
    merci de m'avoir répondu.
    Mais je nage un peu là il n'y aurait-il pas un tutorial là dessus?
    Si non, pourrais tu me donner un exemple afin que je puisse mieux comprendre?

    Merci d'avance,

  4. #4
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    18
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 18
    Par défaut
    en fait j'ai ce code mais je n'arrive pas à faire déplacer à gauche,à droite,en haut et en bas.Comment faire?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
     
    #include <windows.h>		// Header File For Windows
    #include <gl\glut.h>
     
     
    bool keys[256];
    bool skeys[256];
    float deep = -6.0f;		//profondeur
    float xrot = 0.0f;		//rotation sur l'axe x
    float yrot = 0.0f;		//rotation sur l'axe y
    float xspeed = 0.0f;	//vitesse de rotation x
    float yspeed = 0.0f;	//vitesse de rotation y
     
     
    void glutResize(int width, int height)
    {
    	glViewport(0, 0, width, height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
          /* modify this line to change perspective values */
    	gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
     
    int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
    {
    	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
    	glClearDepth(1.0f);									// Depth Buffer Setup
    	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
    	glMatrixMode(GL_PROJECTION);
       // Set the persepctive.  View angel 45, aspect ration 1, near plance 1
       // and the far clipping plane at 200.  The last two mean to start drawing
       // from 1 unit in front of you up to 200 units far.
       gluPerspective(45, 1.3, 0.1, 200.0);
     
       // Change to the modelview matrix.
       glMatrixMode(GL_MODELVIEW);
       glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	//Cette fonction permet d'effacer
    											//l'écran avec la couleur spécifiée
     
     
    	return TRUE;										// Initialization Went OK
    }
     
     
    void DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
    {
    	if (skeys[GLUT_KEY_PAGE_UP])
    	{
    		deep -= 0.02f;
    	}
    	if (skeys[GLUT_KEY_PAGE_DOWN])
    	{
    		deep += 0.02f;
    	}
    	if (skeys[GLUT_KEY_LEFT])
    		yspeed -= 0.005f;
    	if (skeys[GLUT_KEY_RIGHT])
    		yspeed += 0.005f;
    	if (skeys[GLUT_KEY_UP])
    		xspeed -= 0.005f;
    	if (skeys[GLUT_KEY_DOWN])
    		xspeed += 0.005f;
    	if (keys['r'])
    	{
    		xrot = 0.0f;
    		yrot = 0.0f;
    		xspeed = 0.0f;
    		yspeed = 0.0f;
    	}
     
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
    	glLoadIdentity();		//on 'reset' la matrice de l'écran: origine au centre de la fenetre
     
    	glTranslatef( 0.0f, 0.0f, deep);		//translation (zoom)
    	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    	glRotatef(xrot, 1.0f, 0.0f, 0.0f);
     
    	glBegin(GL_TRIANGLES);
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
     
    	glBegin(GL_QUADS);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
     
    	xrot += xspeed;
    	yrot += yspeed;
     
    	glutSwapBuffers();		//changement des buffers
    	glutPostRedisplay();	//reaffiche le plan
    }
     
    void keydown(unsigned char key, int x, int y)
    {
    	keys[key] = true;
    	switch(key)
    	{
    	case VK_ESCAPE: exit(0); break;
    	}
    }
     
    void keyup(unsigned char key, int x, int y)
    {
    	keys[key] = false;
    }
     
    void specdown(int key, int x, int y)
    {
    	skeys[key] = true;
    	switch(key)
    	{
    	case GLUT_KEY_F1:
    		glutFullScreen();
     
    	}
    }
     
    void specup(int key, int x, int y)
    {
    	skeys[key] = false;
    	switch(key)
    	{
     
    	}
    }
     
    int main(int arg, char **argc)
    {
    	//ici, on va initialiser les propriétés de la fenetre que l'on va créer
    	//commen on va afficher (le '|' permet d'utiliser plusieurs éléments dans
    	//un seul argument grace au code binaire: chaque élément a le '1' dans une rangéé,
    	//et avec '|' (or), plusieurs rangées auront des 1 pour avoir plusieurs fonctionnalités
    	glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE );
    	//ici, on définit les coordonnées de la position de la fenetre dans l'écran
        glutInitWindowPosition( 0, 0 );
    	//ici, la taille de la fenetre
        glutInitWindowSize( 800, 600 );
     
    	//ici on initialize le tout par rapport aux ressources (args) et on crée la fenetre
    	glutInit(&arg, argc);
    	glutCreateWindow("première fenetre glut");
     
    	//ici, on appelle la fonction InitGL(), que l'on va implémenter plus tard
    	// et qui va contenir toutes les initialisations de l'opengl: emplacement des textures,
    	//des 'lights', du 'blending' etc...
    	InitGL();
     
    	//ici, on dit à glut quelles fonction il devra utiliser dans la boucle principale (glutMainLoop())
    	//en mettant comme argument le pointeur de la fonction écrite. Elles seront appelées dans la boucle
    	//principale quand il y en aura besoin
    	glutDisplayFunc(DrawGLScene);	//fonction d'affichage: c'est le code principal de votre programme
    	glutReshapeFunc(glutResize);	//appelée en cas de 'resize' de la fenetre
    	glutKeyboardFunc(keydown);		//appelée quand l'utilisateur appuie sur une touche normale
    	glutKeyboardUpFunc(keyup);		//relache la touche normale
    	glutSpecialFunc(specdown);		//appuie sur une touche spéciale (F1, flèches...)
    	glutSpecialUpFunc(specup);		//relache la touche spéciale
    	glutMainLoop();				//boucle du jeu
     
    	return 1;
    }

  5. #5
    Membre très actif
    Profil pro
    Responsable technique
    Inscrit en
    Février 2006
    Messages
    366
    Détails du profil
    Informations personnelles :
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Responsable technique

    Informations forums :
    Inscription : Février 2006
    Messages : 366
    Par défaut
    Utilise la fonction gltranslated.

    Etape 1:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    //En variables globales
    double translation_x,translation_y,translation_z;
    translation_x=translation_y=translation_z=0.0;
    Etape 2:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    //Dans ta fonction de draw
    glPushMatrix();
    glTranslated(translation_x,translation_y,translation_z);
    glBegin();
    ...//Code de ton cube
    glEnd();
    glPopMatrix();
    Etape 3:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    //Dans une des fonction de callback keydown du clavier
     
    switch (key)
     
    case GLUT_KEY_UP
    y+=0.5;
    case GLUT_KEY_DOWN
    y-=0.5;
    case GLUT_KEY_LEFT
    x-=0.5;
    case GLUT_KEY_RIGHT
    x+=0.5;

  6. #6
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    18
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 18
    Par défaut
    Je te remercie mais eh oui encore un truc j'ai bien inséré les codes et j'ai bien compilé mais le dessin ne s'affiche pas.
    Qu'est ce qui pourrait faire ça?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    #include <windows.h>		// Header File For Windows
    #include <gl\glut.h>
     
     
    bool keys[256];
    bool skeys[256];
    float deep = -6.0f;		//profondeur
    float xrot = 0.0f;		//rotation sur l'axe x
    float yrot = 0.0f;		//rotation sur l'axe y
    float xspeed = 0.0f;	//vitesse de rotation x
    float yspeed = 0.0f;	//vitesse de rotation y
    double translation_x,translation_y,translation_z;
     
     
     
    void glutResize(int width, int height)
    {
    	glViewport(0, 0, width, height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
          /* modify this line to change perspective values */
    	gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    }
     
    int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
    {
    	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
    	glClearDepth(1.0f);									// Depth Buffer Setup
    	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
    	glMatrixMode(GL_PROJECTION);
       // Set the persepctive.  View angel 45, aspect ration 1, near plance 1
       // and the far clipping plane at 200.  The last two mean to start drawing
       // from 1 unit in front of you up to 200 units far.
       gluPerspective(45, 1.3, 0.1, 200.0);
     
       // Change to the modelview matrix.
       glMatrixMode(GL_MODELVIEW);
       glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	//Cette fonction permet d'effacer
    											//l'écran avec la couleur spécifiée
     
     
    	return TRUE;										// Initialization Went OK
    }
     
     
    void DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
    {
    	if (skeys[GLUT_KEY_PAGE_UP])
    	{
    		deep -= 0.02f;
    	}
    	if (skeys[GLUT_KEY_PAGE_DOWN])
    	{
    		deep += 0.02f;
    	}
    	if (skeys[GLUT_KEY_LEFT])
    		yspeed -= 0.005f;
    	if (skeys[GLUT_KEY_RIGHT])
    		yspeed += 0.005f;
    	if (skeys[GLUT_KEY_UP])
    		xspeed -= 0.005f;
    	if (skeys[GLUT_KEY_DOWN])
    		xspeed += 0.005f;
    	if (keys['r'])
    	{
    		xrot = 0.0f;
    		yrot = 0.0f;
    		xspeed = 0.0f;
    		yspeed = 0.0f;
    	}
     
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
    	glLoadIdentity();		//on 'reset' la matrice de l'écran: origine au centre de la fenetre
        glPushMatrix ();
    	glTranslated(translation_x,translation_y,translation_z);		//translation (zoom)
    	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    	glRotatef(xrot, 1.0f, 0.0f, 0.0f);
     
    	glBegin(GL_TRIANGLES);
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
     
    	glBegin(GL_QUADS);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
    glPopMatrix ();
     
    	xrot += xspeed;
    	yrot += yspeed;
     
    	glutSwapBuffers();		//changement des buffers
    	glutPostRedisplay();	//reaffiche le plan
    }
     
    void keydown(unsigned char key, int x, int y)
    {
    	switch (key)
    {
    case GLUT_KEY_UP :
    translation_y+=0.5;
    case GLUT_KEY_DOWN :
    translation_y-=0.5;
    case GLUT_KEY_LEFT :
    translation_x-=0.5;
    case GLUT_KEY_RIGHT :
    translation_x+=0.5;
    }
    }
     
    void keyup(unsigned char key, int x, int y)
    {
    	keys[key] = false;
    }
     
    void specdown(int key, int x, int y)
    {
    	skeys[key] = true;
    	switch(key)
    	{
    	case GLUT_KEY_F1:
    		glutFullScreen();
     
    	}
    }
     
    void specup(int key, int x, int y)
    {
    	skeys[key] = false;
    	switch(key)
    	{
     
    	}
    }
     
    int main(int arg, char **argc)
    {
    	//ici, on va initialiser les propriétés de la fenetre que l'on va créer
    	//commen on va afficher (le '|' permet d'utiliser plusieurs éléments dans
    	//un seul argument grace au code binaire: chaque élément a le '1' dans une rangéé,
    	//et avec '|' (or), plusieurs rangées auront des 1 pour avoir plusieurs fonctionnalités
    	glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA | GLUT_MULTISAMPLE );
    	//ici, on définit les coordonnées de la position de la fenetre dans l'écran
        glutInitWindowPosition( 0, 0 );
    	//ici, la taille de la fenetre
        glutInitWindowSize( 800, 600 );
     
    	//ici on initialize le tout par rapport aux ressources (args) et on crée la fenetre
    	glutInit(&arg, argc);
    	glutCreateWindow("première fenetre glut");
     
    	//ici, on appelle la fonction InitGL(), que l'on va implémenter plus tard
    	// et qui va contenir toutes les initialisations de l'opengl: emplacement des textures,
    	//des 'lights', du 'blending' etc...
    	InitGL();
     
    	//ici, on dit à glut quelles fonction il devra utiliser dans la boucle principale (glutMainLoop())
    	//en mettant comme argument le pointeur de la fonction écrite. Elles seront appelées dans la boucle
    	//principale quand il y en aura besoin
    	glutDisplayFunc(DrawGLScene);	//fonction d'affichage: c'est le code principal de votre programme
    	glutReshapeFunc(glutResize);	//appelée en cas de 'resize' de la fenetre
    	glutKeyboardFunc(keydown);		//appelée quand l'utilisateur appuie sur une touche normale
    	glutKeyboardUpFunc(keyup);		//relache la touche normale
    	glutSpecialFunc(specdown);		//appuie sur une touche spéciale (F1, flèches...)
    	glutSpecialUpFunc(specup);		//relache la touche spéciale
    	glutMainLoop();//boucle du jeu
    	translation_x=translation_y=translation_z=0.0;
     
    	return 1;
    }

  7. #7
    Membre émérite Avatar de MatRem
    Profil pro
    Inscrit en
    Décembre 2002
    Messages
    750
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2002
    Messages : 750
    Par défaut
    Il y a pas mal de petit soucis dans ton code, j'en ai fait une correction (c++)...

    Regarde les commentaires. A mon avis tu aussi des problèmes dans la gestion de tes touches.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    #include <GL/glut.h>
    #include <cstdlib>
     
     
    bool   keys[256];   ///Pas optimal
    bool   skeys[256];  ///Pas optimal
    float  deep = -6.0f;
    float  xrot = 0.0f;
    float  yrot = 0.0f;
    float  xspeed = 0.0f;
    float  yspeed = 0.0f;
     
    ///Ne pas oublier d'intialiser
    double translation_x =0, translation_y=0, translation_z=-10;
     
    void glutResize(int width, int height)
    {
    	glViewport(0, 0, width, height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
    	gluPerspective(45.0, static_cast<float>(width)/height, 1.0, 300.0);
     
    	/// Ne sert à rien
    /*	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();*/
    }
     
    void InitGL(GLvoid)
    {
    	/// Par défaut déjà à smooth
    	//glShadeModel(GL_SMOOTH);
     
    	glClearDepth(1.0f);
       glClearColor(0.0f, 0.0f, 0.0f, 0.5f);        // Couleur d'effacement
    	glEnable(GL_DEPTH_TEST);
    	glDepthFunc(GL_LEQUAL);
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     
    	/// Ne sert à rien
    	/*glMatrixMode(GL_PROJECTION);
    	gluPerspective(45, 1.3, 0.1, 200.0);*/
    }
     
     
    void DrawGLScene(GLvoid)
    {
    	/// Il vaudrait mieux gérer les touches directement dans les fonctions de touches, et sans passer par des tableaux.
    	if (skeys[GLUT_KEY_PAGE_UP])
    		deep -= 0.02f;
    	if (skeys[GLUT_KEY_PAGE_DOWN])
    		deep += 0.02f;
    	if (skeys[GLUT_KEY_LEFT])
    		yspeed -= 0.005f;
    	if (skeys[GLUT_KEY_RIGHT])
    		yspeed += 0.005f;
    	if (skeys[GLUT_KEY_UP])
    		xspeed -= 0.005f;
    	if (skeys[GLUT_KEY_DOWN])
    		xspeed += 0.005f;
    	if (keys[static_cast<int>('r')])
    	{
    		xrot = 0.0f;
    		yrot = 0.0f;
    		xspeed = 0.0f;
    		yspeed = 0.0f;
    	}
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	glMatrixMode(GL_MODELVIEW); ///Autant l'avoir ici plutôt qu'en haut
    	glLoadIdentity();
     
    	glPushMatrix ();
     
    	glTranslated(translation_x, translation_y, translation_z);
    	glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    	glRotatef(xrot, 1.0f, 0.0f, 0.0f);
     
    	glBegin(GL_TRIANGLES);
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
     
    	glBegin(GL_QUADS);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
     
    	glPopMatrix ();
     
    	xrot += xspeed;
    	yrot += yspeed;
     
    	glutSwapBuffers();
    	glutPostRedisplay();
    }
     
    void keydown(unsigned char key, int x, int y)
    {
    	/// Ne pas oublier les break
    	switch (key)
    	{
    		case GLUT_KEY_UP :
    			translation_y+=0.5;
    			break;
    		case GLUT_KEY_DOWN :
    			translation_y-=0.5;
    			break;
    		case GLUT_KEY_LEFT :
    			translation_x-=0.5;
    			break;
    		case GLUT_KEY_RIGHT :
    			translation_x+=0.5;
    			break;
    	}
    }
     
    void keyup(unsigned char key, int x, int y)
    {
    	keys[key] = false;
    }
     
    void specdown(int key, int x, int y)
    {
    	skeys[key] = true;
    	switch(key)
    	{
    	case GLUT_KEY_F1:
    		glutFullScreen();
    		break;
    	}
    }
     
    void specup(int key, int x, int y)
    {
    	skeys[key] = false;
     
    	/// Vide
    	/*switch(key)
    	{
     
    	}*/
    }
     
    int main(int arg, char **argc)
    {
    	glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA /*| GLUT_MULTISAMPLE*/ );
    	glutInitWindowPosition( 0, 0 );
    	glutInitWindowSize( 800, 600 );
     
    	glutInit(&arg, argc);
    	glutCreateWindow("première fenetre glut");
     
    	InitGL();
     
    	glutDisplayFunc(DrawGLScene);
    	glutReshapeFunc(glutResize);
    	glutKeyboardFunc(keydown);
    	glutKeyboardUpFunc(keyup);
    	glutSpecialFunc(specdown);
    	glutSpecialUpFunc(specup);
    	glutMainLoop();
     
    	/// Ne sert à rien
    	//translation_x=translation_y=translation_z=0.0;
    }
    ps: ton code affichait bien quelquechose, mais derrière le point de vue, avec z=-10, ça règle le problème.

  8. #8
    Membre averti
    Inscrit en
    Juillet 2007
    Messages
    18
    Détails du profil
    Informations forums :
    Inscription : Juillet 2007
    Messages : 18
    Par défaut
    merci j'ai bien rectifié le code, le dessin a réapparu. J'ai enlevé aussi la rotation et la vitesse de rotation.Par contre le dessin ne bouge pas quand j'appuie sur les touches haut bas gauche droite, je vois pas quel est le souci?!
    Quand ça marchera je m'en souviendrai ça c'est certain!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    #include <windows.h>		// Header File For Windows
    #include <gl\glut.h>
     
     
    bool keys[256];
    bool skeys[256];
    double translation_x = 0,translation_y = 0, translation_z = -10;
     
     
     
    void glutResize(int width, int height)
    {
    	glViewport(0, 0, width, height);
    	glMatrixMode(GL_PROJECTION);
    	glLoadIdentity();
          /* modify this line to change perspective values */
    	gluPerspective(45.0, (float)width/(float)height, 1.0, 300.0);
     
    }
     
    int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
    {
    		glClearDepth(1.0f);									// Depth Buffer Setup
    	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
     
     
       // Change to the modelview matrix.
       glMatrixMode(GL_MODELVIEW);
       glClearColor(0.0f, 0.0f, 0.0f, 0.5f);	//Cette fonction permet d'effacer
    											//l'écran avec la couleur spécifiée
     
     
    	return TRUE;										// Initialization Went OK
    }
     
     
    void DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
    {
     
     
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear Screen And Depth Buffer
    	glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();		//on 'reset' la matrice de l'écran: origine au centre de la fenetre
        glPushMatrix ();
    	glTranslated(translation_x,translation_y,translation_z);		//translation (zoom)
     
    	glBegin(GL_TRIANGLES);
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
     
    		glColor3f(1.0f, 0.0f, 0.0f);	glVertex3f( 0.0f, 1.0f, 0.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
     
    	glBegin(GL_QUADS);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f( 1.0f,-1.0f, 1.0f);
    		glColor3f(0.1f, 0.1f, 0.1f);	glVertex3f( 1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 0.0f, 1.0f);	glVertex3f(-1.0f,-1.0f,-1.0f);
    		glColor3f(0.0f, 1.0f, 0.0f);	glVertex3f(-1.0f,-1.0f, 1.0f);
    	glEnd();
    glPopMatrix ();
     
     
    	glutSwapBuffers();		//changement des buffers
    	glutPostRedisplay();	//reaffiche le plan
    }
     
    void keydown(unsigned char key, int x, int y)
    {
    	switch (key)
    {
    case GLUT_KEY_UP :
    translation_y+=0.5;
    break;
    case GLUT_KEY_DOWN :
    translation_y-=0.5;
    break;
    case GLUT_KEY_LEFT :
    translation_x-=0.5;
    break;
    case GLUT_KEY_RIGHT :
    translation_x+=0.5;
    break;
    }
    }
     
    void keyup(unsigned char key, int x, int y)
    {
    	keys[key] = false;
    }
     
    void specdown(int key, int x, int y)
    {
    	skeys[key] = true;
    	switch(key)
    	{
    	case GLUT_KEY_F1:
    		glutFullScreen();
     
    	}
    }
     
    void specup(int key, int x, int y)
    {
    	skeys[key] = false;
     
    }
     
    int main(int arg, char **argc)
    {
    	//ici, on va initialiser les propriétés de la fenetre que l'on va créer
    	//commen on va afficher (le '|' permet d'utiliser plusieurs éléments dans
    	//un seul argument grace au code binaire: chaque élément a le '1' dans une rangéé,
    	//et avec '|' (or), plusieurs rangées auront des 1 pour avoir plusieurs fonctionnalités
    	glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
    	//ici, on définit les coordonnées de la position de la fenetre dans l'écran
        glutInitWindowPosition( 0, 0 );
    	//ici, la taille de la fenetre
        glutInitWindowSize( 800, 600 );
     
    	//ici on initialize le tout par rapport aux ressources (args) et on crée la fenetre
    	glutInit(&arg, argc);
    	glutCreateWindow("première fenetre glut");
     
    	//ici, on appelle la fonction InitGL(), que l'on va implémenter plus tard
    	// et qui va contenir toutes les initialisations de l'opengl: emplacement des textures,
    	//des 'lights', du 'blending' etc...
    	InitGL();
     
    	//ici, on dit à glut quelles fonction il devra utiliser dans la boucle principale (glutMainLoop())
    	//en mettant comme argument le pointeur de la fonction écrite. Elles seront appelées dans la boucle
    	//principale quand il y en aura besoin
    	glutDisplayFunc(DrawGLScene);	//fonction d'affichage: c'est le code principal de votre programme
    	glutReshapeFunc(glutResize);	//appelée en cas de 'resize' de la fenetre
    	glutKeyboardFunc(keydown);		//appelée quand l'utilisateur appuie sur une touche normale
    	glutKeyboardUpFunc(keyup);		//relache la touche normale
    	glutSpecialFunc(specdown);		//appuie sur une touche spéciale (F1, flèches...)
    	glutSpecialUpFunc(specup);	    //relache la touche spéciale
    	glutMainLoop();//boucle du jeu
     
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [FLASH] Déplacement des objets avec clavier ???
    Par 3adoula dans le forum Flash
    Réponses: 1
    Dernier message: 07/01/2006, 01h03
  2. Réponses: 2
    Dernier message: 29/11/2005, 13h38
  3. Cube avec 8 Vertex + textures
    Par mister3957 dans le forum DirectX
    Réponses: 2
    Dernier message: 23/11/2005, 23h31
  4. Decision Cube avec Oracle
    Par Leclair2000 dans le forum Bases de données
    Réponses: 1
    Dernier message: 16/11/2004, 15h38
  5. SoftIce avec clavier Azerty
    Par - Robby - dans le forum x86 32-bits / 64-bits
    Réponses: 6
    Dernier message: 01/10/2003, 14h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo