Graficacion Para Dummies
Transcript of Graficacion Para Dummies
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
Vertex Data
Pixel Data
EvaluatorsOperations with vertices
Assembling primitives
Operations with pixels
Fragments operations
Assembling textures
Rasterization
Frame BufferDisplay lists
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;
}
cube.c
void display(void){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0); glScalef (1.0, 2.0, 1.0);
glutWireCube (1.0);
glFlush ();
}
Viewing
transformation
Modeling
transformation
Places the camera at (0,0,5)
Aims the camera lens towards (0,0,0)
Up-vector is (0,1,0)
This defines a unique position and
orientation of the camera
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 reshape (int w, int h){
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
Projection
transformation
Defines left and right clipping planes,
Defines bottom and top clipping planes,
Defines zNear and zFar
This defines a perpective matrix
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
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: exit(0);
break; }
}
ESC allows the user to exit !
#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;
}
double.c
double.c#include <GL/glut.h>#include <stdlib.h>
static GLfloat spin = 0.0;
int main(int argc, char** argv)
{ 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);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
void display(void)
{ glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
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)
{ 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);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}
double.c
void spinDisplay(void)
{ spin = spin + 2.0;
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay();
}
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
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}};
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);
glutKeyboardFunc(keyboard);
glutMainLoop();return 0;
}
void display(void)
{ int i;
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(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>
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);
glutKeyboardFunc(keyboard);
glutMainLoop();return 0;
}
bezcurve.c
void reshape(int w, int h)
{ glViewport(0, 0, (GLsizei) w, (GLsizei) h);
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();
}
Et les
B-splines.
#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},
{ 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);
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/