Sei sulla pagina 1di 29

OpenGL

for dummies…
hello.c
#include <GL/glut.h>
int main(int argc, char** argv)
{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);}
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void display(void)
{ glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();
glFlush ();
}
OpenGL’s rendering pipeline
Operations with vertices
Evaluators
Vertex Data Assembling primitives

Fragments operations

Rasterization
Display lists
Frame Buffer
Assembling textures

Pixel Data Operations with pixels


cube.c
cube.c
#include <GL/glut.h>
#include <stdlib.h>

int main(int argc, char** argv) Places the camera at (0,0,5)


Aims the camera lens towards (0,0,0)
{ glutInit(&argc, argv); Up-vector is (0,1,0)
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500); This defines a unique position and
glutInitWindowPosition (100, 100); orientation of the camera

glutCreateWindow (argv[0]);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop(); void display(void){
return 0; glClear (GL_COLOR_BUFFER_BIT);
} glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
Viewing gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0,
transformation 0.0, 0.0, 1.0, 0.0);
glScalef (1.0, 2.0, 1.0);
Modeling glutWireCube (1.0);
transformation glFlush ();
}
cube.c
#include <GL/glut.h>
#include <stdlib.h>

int main(int argc, char** argv) Defines left and right clipping planes,
Defines bottom and top clipping planes,
{ glutInit(&argc, argv); Defines zNear and zFar
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500); This defines a perpective matrix
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0; void reshape (int w, int h){
} glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
Projection glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
transformation glMatrixMode (GL_MODELVIEW);
}
glFrustrum

How to obtain an accurate


specification of the
parameters ?

http://www.meca.ucl.ac.be/~vl/teaching/meca2170/opengl/gl/frustum.html
cube.c
#include <GL/glut.h>
#include <stdlib.h>

int main(int argc, char** argv)

{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

void keyboard(unsigned char key, int x, int y)


{
switch (key) {
ESC allows the user to exit ! case 27: exit(0);
break; }
}
double.c
#include <GL/glut.h>
#include <stdlib.h>
static GLfloat spin = 0.0;
int main(int argc, char** argv)
double.c
{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
glutDisplayFunc(display); void display(void)
glutReshapeFunc(reshape);
glutMouseFunc(mouse); { glClear(GL_COLOR_BUFFER_BIT);
glutMainLoop(); glPushMatrix();
return 0; glRotatef(spin, 0.0, 0.0, 1.0);
} glColor3f(1.0, 1.0, 1.0);
glRectf(-25.0, -25.0, 25.0, 25.0);
glPopMatrix();
glutSwapBuffers();
}

void reshape (int w, int h)


{ glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
#include <GL/glut.h>
#include <stdlib.h>
static GLfloat spin = 0.0;
int main(int argc, char** argv)
double.c
{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (250, 250);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor (0.0, 0.0, 0.0, 0.0); void spinDisplay(void)
glShadeModel (GL_FLAT);
glutDisplayFunc(display); { spin = spin + 2.0;
glutReshapeFunc(reshape); if (spin > 360.0)
glutMouseFunc(mouse); spin = spin - 360.0;
glutMainLoop(); glutPostRedisplay();
return 0; }
}
void mouse(int button, int state, int x, int y)
{ switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) glutIdleFunc(spinDisplay);
break;
case GLUT_MIDDLE_BUTTON:
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN) glutIdleFunc(NULL);
break; default:
break; }
}
bezcurve.c
#include <GL/glut.h>
#include <stdlib.h>
GLfloat ctrlpoints[4][3] = {
{ -4.0, -4.0, 0.0}, { -2.0, 4.0, 0.0},
{2.0, -4.0, 0.0}, {4.0, 4.0, 0.0}};
bezcurve.c
int main(int argc, char** argv)
{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4,
&ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3); void display(void)
glutDisplayFunc(display);
{ int i;
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard); glClear(GL_COLOR_BUFFER_BIT);
glutMainLoop(); glColor3f(1.0, 1.0, 1.0);
return 0; glBegin(GL_LINE_STRIP); for (i =
} 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
glPointSize(5.0);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; i++)
glVertex3fv(&ctrlpoints[i][0]);
glEnd();
glFlush();
}
#include <GL/glut.h>
#include <stdlib.h>

bezcurve.c
GLfloat ctrlpoints[4][3] = {
{-4.0,-4.0, 0.0}, {-2.0, 4.0, 0.0},
{ 2.0,-4.0, 0.0}, { 4.0, 4.0, 0.0}};
int main(int argc, char** argv)
{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4,
&ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glutDisplayFunc(display);
glutReshapeFunc(reshape); void reshape(int w, int h)
glutKeyboardFunc(keyboard);
glutMainLoop(); { glViewport(0, 0, (GLsizei) w, (GLsizei) h);
return 0; glMatrixMode(GL_PROJECTION);
} glLoadIdentity();
if (w <= h)
glOrtho(-5.0, 5.0, 5.0*(GLfloat)h/(GLfloat)w,
5.0*(GLfloat)h/(GLfloat)w, -5.0, 5.0);
else
glOrtho(5.0*(GLfloat)w/(GLfloat)h,
5.0*(GLfloat)w/(GLfloat)h,
-5.0, 5.0,-5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
#include <GL/glut.h>
#include <stdlib.h>
GLUnurbsObj *nurb;
GLfloat knots[9] = {-3.0,-2.0,-1.0,0.0,1.0,2.0,3.0,4.0,5.0};
GLfloat ctrlpoints[12][3] = {
{-3.0,-3.0, 0.0},{-1.0, 3.0, 0.0},{ 1.0,-3.0, 0.0},

Et les
{ 3.0, 3.0, 0.0},{-3.0,-4.0, 0.0},{-2.0,-4.0, 0.0},
{-1.0,-4.0, 0.0},{ 0.0,-4.0, 0.0},{ 1.0,-4.0, 0.0},
{ 2.0,-4.0, 0.0},{ 3.0,-4.0, 0.0},{ 4.0,-4.0, 0.0}};
int main(int argc, char** argv)
{ glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
B-splines.
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4,
&ctrlpoints[0][0]);
glEnable(GL_MAP1_VERTEX_3);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMotionFunc(drag);
glutMainLoop();
return 0;
}
Nurbs = B-splines = Beziers !
Knots, pts de contrôle…
void display(void)
{ glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_STRIP);
for (i = 0; i <= 30; i++)
glEvalCoord1f((GLfloat) i/30.0);
glEnd();
glPointSize(8.0);
glBegin(GL_POINTS);
for (i = 0; i < 4; i++) {
glColor3f(1.0-0.1*(double)i,1.0-0.1*(double)i,2.0*(double)i);
glVertex3fv(&ctrlpoints[i][0]); }
glEnd();
nurb = gluNewNurbsRenderer();
gluBeginCurve(nurb);
gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 1.0);
gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse);
gluNurbsCurve(nurb,8,knots,3,&ctrlpoints[0][0],4,GL_MAP1_VERTEX_3);
gluEndCurve(nurb);
glFlush();
}
Joindre deux Nurbs…

nurb = gluNewNurbsRenderer();
gluBeginCurve(nurb);
gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 1.0);
gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse);
gluNurbsCurve(nurb,8,&knots[0],3,&ctrlpoints[0][0],4,GL_MAP1_VERTEX_3);
gluEndCurve(nurb);
glColor3f(1.0,0.0,0.0);
nurb = gluNewNurbsRenderer();
gluNurbsProperty(nurb, GLU_SAMPLING_TOLERANCE, 1.0);
gluNurbsProperty(nurb, GLU_DISPLAY_MODE, GLU_FILL);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_red_diffuse);
gluBeginCurve(nurb);
gluNurbsCurve(nurb,8,&knots[1],3,&ctrlpoints[1][0],4,GL_MAP1_VERTEX_3);
gluEndCurve(nurb);
Ce qui vous sera
demandé en 07-08

http://www.mema.ucl.ac.be/~comblen/meca2170/

Potrebbero piacerti anche