Accueil > > > AFFICHER/EFFACER LES FACES D'UN CUBE 3D EN OPENGL AVEC LE CLAVIER ET GLUT.
AFFICHER/EFFACER LES FACES D'UN CUBE 3D EN OPENGL AVEC LE CLAVIER ET GLUT.
Information sur la source
Description
Cette source permet d'afficher/effacer les faces d'un cube 3D en utilisant les touches du clavier. Le zip pour les lib/source/include et l'exe : http://ultimedbz.free.fr/3D.zip
Source
- ///////////*Les includes*/////////////
- #include <windows.h>
- #include <gl/gl.h>
- #include <gl/glut.h>
- #include "glTexFont.h" // Bibl. pour afficher du texte
- ///////////*////////////*/////////////
-
- ///////////*L/L de l'app.*////////////
- #define WIDTH 640 // Défini la largeur de la fenêtre
- #define HEIGHT 480 // Défini la longueur de la fenêtre
- ///////////*//////////////*////////////
-
- ///////////*Fonction*//////////////////
- int a=1, b=1, c=1, d=1, e=1, f=1; // Variable pour les faces/touches
- void clavier(unsigned char key, int x, int y); // La fonction pour les touches
- void Display();
- void Reshape(int w,int h);
- void Init();
- ///////////*////////*//////////////////
-
- ////////////////////////////////////////////////////////////////////////////
- int main( int argc, char *argv[ ])
- {
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
-
- glutInitWindowSize(WIDTH,HEIGHT); // Met la fenêtre en 640/480(voir plus haut)
- glutInitWindowPosition(50,50); // Position de demarrage de la fenêtre
- glutCreateWindow("Afficher/effacer les faces d'un cube 3D en OpenGl"); // Nom de la fenêtre
- fontLoad ("textures/font.tga"); // Charge le fichier "font.tga" dans le dossier "textures" pour le texte
-
- Init();
- glutDisplayFunc(Display);
- glutKeyboardFunc(clavier); // Clavier
- glutReshapeFunc(Reshape);
- glutMainLoop();
- return 0;
- }
-
- void Init()
- {
- glEnable(GL_DEPTH_TEST);
- }
-
- void Display() // Fonction display
- {
- glClearColor(0,0,0,0); // Selectionne la couleur noire (qui est celle par défaut)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Affiche le texte en bas à gauche
- fontColor (2.5, 0.7, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 5, "Tuto par Misugi"); // Affiche : Tuto par misugi
- fontColor (0.7, 0.7, 2.5); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 20, "%d,%d", WIDTH, HEIGHT); // Affiche : 640.480
- ///////////////////////////////////
-
- // Affiche le texte en haut à gauche
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 465, "Face 1 = touche num.1"); // Affiche : Face 1 = touche num.1
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 450, "Face 2 = touche num.2"); // Affiche : Face 2 = touche num.2
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 435, "Face 3 = touche num.3"); // Affiche : Face 3 = touche num.3
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 420, "Face 4 = touche num.4"); // Affiche : Face 4 = touche num.4
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 405, "Face 5 = touche num.5"); // Affiche : Face 5 = touche num.5
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 390, "Face 6 = touche num.6"); // Affiche : Face 6 = touche num.6
- fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
- fontSize (10); fontDrawString (5, 375, "Touche num.7 = effacer les faces"); // Affiche : Face 6 = touche num.6
- /////////////////////////////////////
-
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(4,2.5,4,0,0,0,0,1,0);
-
- glEnd();
- glFlush();
- }
-
- void Reshape(int w, int h) // Fonction Reshape
- {
- glViewport(0,0,w,h);
- glMatrixMode(GL_PROJECTION); // Choisit la matrice de projection
- glLoadIdentity();
- gluPerspective(45.0,(float) w/h,1.,10.);
- }
-
- void Face1() // Affice la Face 1
- {
- glBegin(GL_QUADS);
- // glColor3d pour la couleur de face
- glColor3d(1,0,0); // 0,0,0 = R.G.B = Red(rouge),Green(vert),Blue(bleue), ici la couleur est rouge
- glVertex3i(1,1,1);
- glVertex3i(1,-1,1);
- glVertex3i(-1,-1,1);
- glVertex3i(-1,1,1);
- a++; // Augmente d'une unité la variable "a"
- glEnd();
- glFlush();
- }
-
- void Face2() // Affice la Face 2
- {
- glBegin(GL_QUADS);
- glColor3d(0,1,0); // Couleur verte
- glVertex3i(1,1,-1);
- glVertex3i(1,-1,-1);
- glVertex3i(-1,-1,-1);
- glVertex3i(-1,1,-1);
- b++; // Augmente d'une unité la variable "b"
- glEnd();
- glFlush();
- }
-
- void Face3() // Affice la Face 3
- {
- glBegin(GL_QUADS);
- glColor3d(0,0,1); // Couleur bleue
- glVertex3i(1,1,1);
- glVertex3i(1,-1,1);
- glVertex3i(1,-1,-1);
- glVertex3i(1,1,-1);
- c++; // Augmente d'une unité la variable "c"
- glEnd();
- glFlush();
- }
-
- void Face4() // Affice la Face 4
- {
- glBegin(GL_QUADS);
- glColor3d(0,1,1); // Couleur verte et bleue
- glVertex3i(-1,1,1);
- glVertex3i(-1,-1,1);
- glVertex3i(-1,-1,-1);
- glVertex3i(-1,1,-1);
- d++; // Augmente d'une unité la variable "d"
- glEnd();
- glFlush();
- }
-
-
- void Face5() // Affice la Face 5
- {
- glBegin(GL_QUADS);
- glColor3d(1,1,0); // Couleur rouge et verte
- glVertex3i(-1,1,-1);
- glVertex3i(-1,1,1);
- glVertex3i(1,1,1);
- glVertex3i(1,1,-1);
- e++; // Augmente d'une unité la variable "e"
- glEnd();
- glFlush();
- }
-
-
- void Face6() // Affice la Face 6
- {
- glBegin(GL_QUADS);
- glColor3d(1,0,1); // Couleur rouge et bleue
- glVertex3i(-1,-1,-1);
- glVertex3i(-1,-1,1);
- glVertex3i(1,-1,1);
- glVertex3i(1,-1,-1);
- f++; // Augmente d'une unité la variable "f"
- glEnd();
- glFlush();
- }
-
- void UnFaceAll() // Fonction pour effacer les faces
- {
- glClearColor(0,0,0,0); // Selectionne la couleur noire (qui est celle par défaut)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- Display(); // Apelle la fonction Display() pour effacer les faces par le noir
- a--; // Diminue d'une unité la variable "a"
- b--; // Diminue d'une unité la variable "b"
- c--; // Diminue d'une unité la variable "c"
- d--; // Diminue d'une unité la variable "d"
- e--; // Diminue d'une unité la variable "e"
- f--; // Diminue d'une unité la variable "f"
- }
-
- void clavier(unsigned char key, int x, int y) // Fonction touche clavier
- {
- switch(key)
- {
- case 49: if(a=1) Face1(); // 49 = touche num 1, si "a" = 1 alors effectuer l'action Face1
- break;
-
- case 50: if(b=1) Face2(); // 50 = touche num 2, si "b" = 1 alors effectuer l'action Face2
- break;
-
- case 51: if(c=1) Face3(); // 51 = touche num 3, si "c" = 1 alors effectuer l'action Face3
- break;
-
- case 52: if(d=1) Face4(); // 52 = touche num 4, si "d" = 1 alors effectuer l'action Face4
- break;
-
- case 53: if(e=1) Face5(); // 53 = touche num 5, si "e" = 1 alors effectuer l'action Face5
- break;
-
- case 54: if(f=1) Face6(); // 54 = touche num 6, si "f" = 1 alors effectuer l'action Face6
- break;
-
- case 55: if(a=2, b=2, c=2, d=2, e=2, f=2) UnFaceAll(); // 55 = touche num 7, si "a/b/c/d/e/f" = 2 alors effectuer l'action UnFaceAll
- break;
- }
- }
- ////////////////////////////////////////////////////////////////
///////////*Les includes*/////////////
#include <windows.h>
#include <gl/gl.h>
#include <gl/glut.h>
#include "glTexFont.h" // Bibl. pour afficher du texte
///////////*////////////*/////////////
///////////*L/L de l'app.*////////////
#define WIDTH 640 // Défini la largeur de la fenêtre
#define HEIGHT 480 // Défini la longueur de la fenêtre
///////////*//////////////*////////////
///////////*Fonction*//////////////////
int a=1, b=1, c=1, d=1, e=1, f=1; // Variable pour les faces/touches
void clavier(unsigned char key, int x, int y); // La fonction pour les touches
void Display();
void Reshape(int w,int h);
void Init();
///////////*////////*//////////////////
////////////////////////////////////////////////////////////////////////////
int main( int argc, char *argv[ ])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowSize(WIDTH,HEIGHT); // Met la fenêtre en 640/480(voir plus haut)
glutInitWindowPosition(50,50); // Position de demarrage de la fenêtre
glutCreateWindow("Afficher/effacer les faces d'un cube 3D en OpenGl"); // Nom de la fenêtre
fontLoad ("textures/font.tga"); // Charge le fichier "font.tga" dans le dossier "textures" pour le texte
Init();
glutDisplayFunc(Display);
glutKeyboardFunc(clavier); // Clavier
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}
void Init()
{
glEnable(GL_DEPTH_TEST);
}
void Display() // Fonction display
{
glClearColor(0,0,0,0); // Selectionne la couleur noire (qui est celle par défaut)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Affiche le texte en bas à gauche
fontColor (2.5, 0.7, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 5, "Tuto par Misugi"); // Affiche : Tuto par misugi
fontColor (0.7, 0.7, 2.5); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 20, "%d,%d", WIDTH, HEIGHT); // Affiche : 640.480
///////////////////////////////////
// Affiche le texte en haut à gauche
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 465, "Face 1 = touche num.1"); // Affiche : Face 1 = touche num.1
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 450, "Face 2 = touche num.2"); // Affiche : Face 2 = touche num.2
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 435, "Face 3 = touche num.3"); // Affiche : Face 3 = touche num.3
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 420, "Face 4 = touche num.4"); // Affiche : Face 4 = touche num.4
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 405, "Face 5 = touche num.5"); // Affiche : Face 5 = touche num.5
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 390, "Face 6 = touche num.6"); // Affiche : Face 6 = touche num.6
fontColor (0.7, 2.5, 0.7); // Couleur du texte en RGB
fontSize (10); fontDrawString (5, 375, "Touche num.7 = effacer les faces"); // Affiche : Face 6 = touche num.6
/////////////////////////////////////
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(4,2.5,4,0,0,0,0,1,0);
glEnd();
glFlush();
}
void Reshape(int w, int h) // Fonction Reshape
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION); // Choisit la matrice de projection
glLoadIdentity();
gluPerspective(45.0,(float) w/h,1.,10.);
}
void Face1() // Affice la Face 1
{
glBegin(GL_QUADS);
// glColor3d pour la couleur de face
glColor3d(1,0,0); // 0,0,0 = R.G.B = Red(rouge),Green(vert),Blue(bleue), ici la couleur est rouge
glVertex3i(1,1,1);
glVertex3i(1,-1,1);
glVertex3i(-1,-1,1);
glVertex3i(-1,1,1);
a++; // Augmente d'une unité la variable "a"
glEnd();
glFlush();
}
void Face2() // Affice la Face 2
{
glBegin(GL_QUADS);
glColor3d(0,1,0); // Couleur verte
glVertex3i(1,1,-1);
glVertex3i(1,-1,-1);
glVertex3i(-1,-1,-1);
glVertex3i(-1,1,-1);
b++; // Augmente d'une unité la variable "b"
glEnd();
glFlush();
}
void Face3() // Affice la Face 3
{
glBegin(GL_QUADS);
glColor3d(0,0,1); // Couleur bleue
glVertex3i(1,1,1);
glVertex3i(1,-1,1);
glVertex3i(1,-1,-1);
glVertex3i(1,1,-1);
c++; // Augmente d'une unité la variable "c"
glEnd();
glFlush();
}
void Face4() // Affice la Face 4
{
glBegin(GL_QUADS);
glColor3d(0,1,1); // Couleur verte et bleue
glVertex3i(-1,1,1);
glVertex3i(-1,-1,1);
glVertex3i(-1,-1,-1);
glVertex3i(-1,1,-1);
d++; // Augmente d'une unité la variable "d"
glEnd();
glFlush();
}
void Face5() // Affice la Face 5
{
glBegin(GL_QUADS);
glColor3d(1,1,0); // Couleur rouge et verte
glVertex3i(-1,1,-1);
glVertex3i(-1,1,1);
glVertex3i(1,1,1);
glVertex3i(1,1,-1);
e++; // Augmente d'une unité la variable "e"
glEnd();
glFlush();
}
void Face6() // Affice la Face 6
{
glBegin(GL_QUADS);
glColor3d(1,0,1); // Couleur rouge et bleue
glVertex3i(-1,-1,-1);
glVertex3i(-1,-1,1);
glVertex3i(1,-1,1);
glVertex3i(1,-1,-1);
f++; // Augmente d'une unité la variable "f"
glEnd();
glFlush();
}
void UnFaceAll() // Fonction pour effacer les faces
{
glClearColor(0,0,0,0); // Selectionne la couleur noire (qui est celle par défaut)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Display(); // Apelle la fonction Display() pour effacer les faces par le noir
a--; // Diminue d'une unité la variable "a"
b--; // Diminue d'une unité la variable "b"
c--; // Diminue d'une unité la variable "c"
d--; // Diminue d'une unité la variable "d"
e--; // Diminue d'une unité la variable "e"
f--; // Diminue d'une unité la variable "f"
}
void clavier(unsigned char key, int x, int y) // Fonction touche clavier
{
switch(key)
{
case 49: if(a=1) Face1(); // 49 = touche num 1, si "a" = 1 alors effectuer l'action Face1
break;
case 50: if(b=1) Face2(); // 50 = touche num 2, si "b" = 1 alors effectuer l'action Face2
break;
case 51: if(c=1) Face3(); // 51 = touche num 3, si "c" = 1 alors effectuer l'action Face3
break;
case 52: if(d=1) Face4(); // 52 = touche num 4, si "d" = 1 alors effectuer l'action Face4
break;
case 53: if(e=1) Face5(); // 53 = touche num 5, si "e" = 1 alors effectuer l'action Face5
break;
case 54: if(f=1) Face6(); // 54 = touche num 6, si "f" = 1 alors effectuer l'action Face6
break;
case 55: if(a=2, b=2, c=2, d=2, e=2, f=2) UnFaceAll(); // 55 = touche num 7, si "a/b/c/d/e/f" = 2 alors effectuer l'action UnFaceAll
break;
}
}
////////////////////////////////////////////////////////////////
Conclusion
Vala tout dans le zip c'est ma première source.La librairie "glTexFont.lib" et l'include pour les textes sont ajoutés.
Historique
- 11 mai 2006 20:15:52 :
- Ajout de la source.
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
cube 3D en C [ par jdespe ]
Comment faire pour créer un cube en 3D en C et le faire pivoter, dans un code assez simple?Merci de la réponse.
Menu & Raccourci clavier [ par larion ]
Bonjour,Dans certain menu de windows, il est affiché "Alt+F4" à côté de "X Fermer".Comment fait-on afficher ce raccourci dans un menu ?Comment fait-on
Afficher puis cacher un treeview [ par abdoulax ]
Bon voilà, c tout simple ms je trouve poa !! Comment afficher un treeview puis le cacher ? J'arrive afficher le treeview avec cette instruction
PB remplissage 3D [ par MangaII ]
Hello tout le monde !J'ai un petit soucis ! j'ai un cube (3D), avec des points a l'intérieur (BCP, environ 10000). Et ces points sont répartie dans un
entree clavier a cacher [ par qsdfgh ]
bonjour, je vuex faire un prog ou il faut entrer un mot de passe mais je ne sais pas comment cacher les entrées par des etoiles (ou autre chos
Afficher des points en 3D [ par Fredchkek ]
Bonjour,Je suis à la recherche d'un bout de code (VC++) très simple qui permettrai d'afficher un point (un pixel ou un cercle) dans un
j'arrive pas à saisir les lettres au clavier et les afficher sous la SDL [ par oumal ]
si vous pouvez ,mes amis, m'aider je serai trés reconnaissante,en tout cas je sais bien que vous n'hésitez pas à rendre service à ceux qui en ont beso
Cube 3D + couleur + lumiere [ par jojomillenium ]
Bonjour, J'ai fait un cube en 3D unicolor, mon probleme c'est qu'en perspective je ne distingue pas les arretes (vous voyez ce que je veux dire?) J'ai
touché tapée par clavier [ par sidalilo ]
bonjour je veux afficher une touché tapée par le clavier en c++ builder 6 j'ai eu un code qui fait ca mais j'ai su pas comment l'utiliser voila ce cod
Connaitre le statut de window(cacher ou afficher) [ par gamemonde ]
bon bien le titre parle de lui meme j'utilise ShowWindow(hWndtype);mais comment connaitre si ma fenetre est soit cacher afficher minimiser agrandit...
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|