petite horloge en 3D avec camera tournante utilisant glut
Source / Exemple :
/*****************************************************************************/
/* */
/* fichier : main.c version : V1.0 */
/* projet : clock date : 05/05/2004 */
/* par : aerith */
/* */
/* clock 3D */
/* */
/*****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "glut.h"
#define PI 3.14159265358979323846
void Draw(void);
GLvoid Reshape(GLsizei w, GLsizei h);
void InitGL();
void GestionSpecial(int key, int x, int y) ;
int Win;
float AngX = 0, AngY = 0;
int main(int argc, char *argv[], char *envp[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
Win = glutCreateWindow("Clock 3D");
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
glutSpecialFunc(GestionSpecial);
InitGL();
glutMainLoop();
return 0;
}
void GestionSpecial(int key, int x, int y)
{
float p = 0.1;
switch (key)
{
case GLUT_KEY_UP :
AngX -= p;
break;
case GLUT_KEY_DOWN :
AngX += p;
break;
case GLUT_KEY_RIGHT :
AngY -= p;
break;
case GLUT_KEY_LEFT :
AngY += p;
break;
}
}
void InitGL()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMatrixMode(GL_MODELVIEW);
}
GLvoid Reshape(GLsizei w, GLsizei h)
{
if (h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,1.0f,200.0f);
glMatrixMode(GL_MODELVIEW);
}
void Draw(void)
{
time_t Time;
char StrTime[24], Nom[4], Moi[4];
int Jou, Heu, Min, Sec, Ann, i, CamDist = 50;
int LightPos[4] = {0, 0, CamDist, 0};
GLfloat LightDif[4] = {1.0, 1.0, 0.0, 1.0};
GLfloat LightSpec[4] = {0.0, 0.0, 1.0, 1.0};
float z = CamDist*cos(AngY)*cos(AngX);
float x = CamDist*sin(AngY)*cos(AngX);
float y = CamDist*sin(AngX);
time(&Time);
strcpy(StrTime, ctime(&Time));
strcpy(Nom, strtok(StrTime, " :"));
strcpy(Moi, strtok(NULL, " :"));
Jou = atoi(strtok(NULL, " :"));
Heu = atoi(strtok(NULL, " :"));
Min = atoi(strtok(NULL, " :"));
Sec = atoi(strtok(NULL, " :"));
Ann = atoi(strtok(NULL, " :"));
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
LightPos[0] = x;
LightPos[1] = y;
LightPos[2] = z;
gluLookAt(x, y, z,
0, 0, 0,
0, 1, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLightiv(GL_LIGHT0,GL_POSITION,LightPos);
glLightfv(GL_LIGHT0,GL_DIFFUSE,LightDif);
glLightfv(GL_LIGHT0,GL_SPECULAR,LightSpec);
//horloge
glPushMatrix();
glutSolidSphere(0.5,24,24);
glPopMatrix();
for(i = 0; i < 12; i++)
{
glPushMatrix();
glTranslated(17*sin(2*PI/12*i),17*cos(2*PI/12*i),0);
glutSolidSphere(0.5,12,12);
glPopMatrix();
}
for(i = 0; i < 60; i++)
{
glPushMatrix();
glTranslated(17*sin(2*PI/60*i),17*cos(2*PI/60*i),0);
glutSolidSphere(0.25,12,12);
glPopMatrix();
}
// heures
glPushMatrix();
glTranslated(10*sin(2*PI/12*Heu+2*PI/60*Min/12+2*PI/60*Sec/60/12),10*cos(2*PI/12*Heu+2*PI/60*Min/12+2*PI/60*Sec/60/12),0);
glutSolidSphere(1,12,12);
glPopMatrix();
// minutes
glPushMatrix();
glTranslated(13*sin(2*PI/60*Min+2*PI/60*Sec/60),13*cos(2*PI/60*Min+2*PI/60*Sec/60),0);
glutSolidSphere(0.75,12,12);
glPopMatrix();
// secondes
glPushMatrix();
glTranslated(15*sin(2*PI/60*Sec),15*cos(2*PI/60*Sec),0);
glutSolidSphere(0.5,12,12);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
Conclusion :
aucun bug connu
code portable en theorie
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.