Vous êtes sur la page 1sur 11

//////////////////////////////////////////////////////

// Example #1: Draw Lines along major axis


//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #2: Translate Model View Origin
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslated(0.5,-0.5,0);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);

glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #3: Rotate along Z - axis
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotated(45,0,0,1);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}

//////////////////////////////////////////////////////
// Example #4: Scale X,Y,Z-axis
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glScaled(0.3,0.7,0.4);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glColor3f(0,1,0);
glVertex3f(0,0,0);
glVertex3f(0,1,0);
glColor3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(0,0,1);
glEnd();
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #5: Draw a Tea Pot at origin
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.5);
glFlush();
}
int main(void)
{

glutCreateWindow("This is the window title");


glutDisplayFunc(Display);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #6: Rotate Tea Pot 45 degree aroung y axis
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.5);
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(45,0,1,0);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #7: Rotate Tea Pot 45 degree (1,1,1)
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.5);
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
glRotated(45,1,1,1);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #8: Push Poping Model View Matrices
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotated(45,1,1,1);
glutWireTeapot(0.3);
glPopMatrix();

// Push ModelView matrix

glPushMatrix();
glTranslated(0.5,0.5,0);
glutWireTeapot(0.3);
glPopMatrix();
glPushMatrix();
glScaled(0.5,1,0);
glTranslated(-1,0.5,0);
glutWireTeapot(0.4);
glPopMatrix();
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #9: Select Viewing Volume as Orthographic Projection
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glutWireTeapot(0.5);
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2,2,-2,2,-2,2);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #10: Select Prepective Projection Matrix
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.9);
glFlush();
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1,3);
glTranslated(0,0,-2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(45,0,1,0);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #11: Timer Call Back
//////////////////////////////////////////////////////
#include <stdio.h>
#include <windows.h>
#include "glut.h"
void Display(void)

{
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.5);
glFlush();
}
void Timer(int extra)
{
Beep(1000,100);
glutTimerFunc(1000,Timer,0);
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutTimerFunc(1000,Timer,0);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #12: Rotate the teapot (flickering)
//////////////////////////////////////////////////////
#include <stdio.h>
#include "glut.h"
int GAngle=0;
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotated(GAngle,0,1,0);
glutWireTeapot(0.5);
GAngle = GAngle +1;
glFlush();
}
void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}
int main(void)
{
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutTimerFunc(0,Timer,0);
glutMainLoop();
return 0;
}

//////////////////////////////////////////////////////
// Example #13: Double Buffering
//////////////////////////////////////////////////////
#include <stdio.h>
#include <math.h>
#include "glut.h"
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glutWireTeapot(0.5);
glFlush();
// Swap to back buffer after back buffer drawn
glutSwapBuffers();
}
int main(void)
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #14: Rotate the teapot with double buffering
//////////////////////////////////////////////////////
#include <stdio.h>
#include "glut.h"
int GAngle=0;
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotated(GAngle,0,1,0);
glutWireTeapot(0.5);
GAngle = GAngle +1;
glFlush();
glutSwapBuffers();
}
void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}

int main(void)
{
// Init to double buffering
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutTimerFunc(0,Timer,0);
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////
// Example #15: Rotate the teapot with Perspective Projection
// Notice the Cliping of the teapot against near/far planes
//////////////////////////////////////////////////////
#include <stdio.h>
#include "glut.h"
int GAngle=0;
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotated(GAngle,0,1,0);
glutWireTeapot(0.9);
GAngle = GAngle +1;
glFlush();
glutSwapBuffers();
}
void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}
int main(void)
{
// Init to double buffering
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutTimerFunc(0,Timer,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1,3);
glTranslated(0,0,-2);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}

//////////////////////////////////////////////////////
// Example #16: Planet-Moon System with push/pop
//////////////////////////////////////////////////////
#include <stdio.h>
#include "glut.h"
int GAngle=0;
// Draw Sphere at Origin
void DrawPlanetMoon(float radius,int angle)
{
glPushMatrix();
glRotated(angle,0,1,0);
glPushMatrix();
glTranslated(radius,0,0);
glutWireSphere(radius,10,10);
glPopMatrix();
glPushMatrix();
glTranslated(-2*radius,0,0);
glutWireSphere(radius/2,10,10);
glPopMatrix();
glPopMatrix();
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
DrawPlanetMoon(0.4,GAngle++);
glFlush();
glutSwapBuffers();
}
void Timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}
int main(void)
{
// Init to double buffering
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutCreateWindow("This is the window title");
glutDisplayFunc(Display);
glutTimerFunc(0,Timer,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,1,3);
glTranslated(0,0,-2);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();

return 0;
}

Vous aimerez peut-être aussi