Ce programme dessine un cube et utilise la fonction glRotate() pour le faire tourner, la fonction OnKey() maitrise le camera movement et un simple eclairage ....
PS: mon second prog =)
Source / Exemple :
#include <windows.h>
#include <gl\glut.h>
// variable for camera Distance
float Distance = -30.0f;
//float g_LightPosition[] = {0, 10, (Distance)+30};
float g_bLight = true;
typedef unsigned char uchar;
void RenderScene (void)
{
// angle of revolution
static float fElect1 = 0.0f;
//clears screen with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Loads Identity
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// variable for the z-axis in glTranslate
// controlled by the camera distance
glTranslatef(0.0f, 0.0f, Distance);
// Rotation controlled by variable (angle of revolution)
glRotatef(fElect1, 1.0f, 0.0f, 0.0f);
glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
glBegin(GL_QUADS);
// Front face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(2.5f, 2.5f, 2.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(2.5f, -2.5f, 2.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, -2.5f, 2.5f);
glColor3f(1.0f, 1.0f ,0.0f);
glVertex3f(-2.5, 2.5f ,2.5f);
// Back Face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(2.5f, 2.5f, -2.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(2.5f, -2.5f, -2.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, -2.5f, -2.5f);
glColor3f(1.0f, 1.0f ,0.0f);
glVertex3f(-2.5, 2.5f ,-2.5f);
// Right-Side Face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(2.5f, 2.5f, 2.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(2.5f, -2.5f, 2.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(2.5f, -2.5, -2.5f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(2.5f, 2.5f, -2.5f);
//Left Face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-2.5f, 2.5f, 2.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-2.5f, -2.5f, 2.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, -2.5, -2.5f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-2.5f, 2.5f, -2.5f);
// Top Face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(2.5f, 2.5f, 2.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(2.5f, 2.5f, -2.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, 2.5f, -2.5);
glColor3f(1.0f,1.0f, 0.0f);
glVertex3f(-2.5f, 2.5f, 2.5f);
//Bottom Face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(2.5f, -2.5f, 2.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(2.5f, -2.5f, -2.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-2.5f, -2.5f, -2.5);
glColor3f(1.0f,1.0f, 0.0f);
glVertex3f(-2.5f, -2.5f, 2.5f);
glEnd();
// Rotate by 10 degrees
fElect1 += 0.5f;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Swap the buffers
glutSwapBuffers();
// Update our scene
glutPostRedisplay();
}
void SetupRC( void )
{
// Clearing color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
GLfloat ambient[] = {0.8f, 0.8f, 0.8f, 1.0f};
// Enable depth test
glEnable(GL_DEPTH_TEST);
glEnable( GL_LIGHTING );
glLightModelfv( GL_LIGHT_MODEL_AMBIENT, ambient);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
}
void ChangeSize (GLsizei w, GLsizei h)
{
// Aspect ratio variable
GLfloat fAspect = 0.0f;
// prevent a divide by zero
if ( h == 0 )
h = 1;
// Sets the viewport to window dimensions
glViewport(0, 0, w, h);
// calculates the aspect ration
fAspect = (GLfloat)w / (GLfloat)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Perspective with angle 70 degrees, aspect ration
// Near and Far clip
gluPerspective(70.0f, fAspect, 1.0f, 400.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OnKey( uchar Key, int X, int Y )
{
// Key input for switching zoom IN/OUT
switch( Key )
{
case 'i': case 'I':
Distance += 1.0f;
break;
case 'o': case 'O':
Distance -= 1.0f;
break;
}
}
void main (void)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB );
glutCreateWindow("Teapot");
SetupRC();
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(OnKey);
glutMainLoop();
}
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.