Академический Документы
Профессиональный Документы
Культура Документы
Computacin Grfica
The Course Presentation: Presentacin del Curso:
OpenGL OpenGL
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
Una imagen vale ms que mil palabras
Annimo
Universidad Nacional Mayor
de San Marcos
Facultad de Ingeniera de
Sistemas e Informtica
Deus EX
HalfLife
Vizx3D:
Herramienta
para
el
modelado visual 3D y la animacin.
GraphingCalc:
Programa
de
visualizacin matemtica 2D, 3D.
Que es OpenGL?
OpenGL es un Interfase de programacin de aplicaciones (API)
estndar. Es una biblioteca de trazado de grficos de alto rendimiento,
fcil de usar, intuitivo, portable, en el que el programador describe las
llamadas necesarias a funciones y comandos para conseguir una
escena, apariencia o efecto.
Qu no es OpenGL?
Un sistema de ventanas
Un manejador de eventos
Un sistema de animacin de objetos
Un sistema que controle la interaccin entre los
objetos
Un sistema de base de datos de objetos
tridimensionales
Etc, etc.
Caractersticas de OpenGL?
Portabilidad: OpenGL es por diseo independiente del hardware,
sistema operativo o sistema de ventanas.
Caractersticas de OpenGL?
Perceptiva a la red: Es posible separar la aplicacin OpenGL en
un servidor y un cliente que verdaderamente produzca los grficos.
Existe un protocolo para mover por la red los comandos OpenGL
entre el servidor y el cliente.
Funciones para crear grficos 2D y 3D: OpenGL tiene funciones
o primitivas con las que se podr realizar modelado 3D,
transformaciones, utilizacin de color e iluminacin, sombras,
mapeado de texturas, animacin, movimiento borroso, etc.
Ausencia de comandos para modelos complejos: no tiene
comandos para describir modelos complejos (mapas, pjaros,
molculas, etc) sino que tiene primitivas que permiten dibujar
puntos, lneas y polgonos.
Ejecucin Inmediata: Con OpenGL cualquier comando es
ejecutado inmediatamente.
Caractersticas de OpenGL?
Sintaxis Comn: Todos los comandos de OpenGL utilizan la
misma sintaxis. Todos ellos usan el prefijo gl y despus la palabra
que forma el comando con la primera letra en mayscula.
Caractersticas de OpenGL?
Mquina de estados: Las aplicaciones se pueden poner en varios
estados que permanecen en efecto hasta que se cambian. Por
ejemplo el color, se puede poner un color y dibujar un objeto, y ese
color seguir activo hasta que se ponga otro color.
OpenGL tiene las siguientes variables de estado:
Color
Vista actual
Transformaciones de proyecciones
Posiciones
Luces
Propiedades de material.
Algunas de estas variables de estado se pueden habilitar o
deshabilitar con los comandos glEnable() o glDisable(). Cada
variable de estado tiene un valor por defecto disponible en todo
momento a la consulta por parte del programador.
Mg. Johnny R. Avendao Q.
Caractersticas de OpenGL?
describe los objetos de la
escena
Variedad
de
lenguajes:
OpenGL puede ser invocado
desde cualquiera de los
siguientes lenguajes C, C++,
Fortran, Ada, Java.
Pipeline de renderizado:
Con OpenGL se debe seguir
un orden para la realizacin
de las operaciones graficas
necesarias para renderizar
una imagen en la pantalla.
describe
las
propiedades de la
escena que se
aplican sobre la
imagen tal y como
se representa en el
buffer
Caractersticas de OpenGL?
Tipos de datos propios: Todos los tipos de Opengl tienen el sufijo
gl y a continuacin el tipo de C correspondiente.
Funciones
Funciones
Funciones
Funciones
Funciones
Funciones
de
de
de
de
de
de
Primitivas (qu?)
Atributos (cmo?)
Visin (viewing)
Transformacin
Entrada
Control (sistema de ventanas, errores)
Descripcin
Gl
Gl.h
Opengl32.lib
Glu
Glu.h
Glu32.lib
Glut
Glut.h
Glut32.dll
Glut32.lib
(0, 1, 0)
(1, 1, 0)
(0, 0, 0)
(1, 0, 0)
glBegin(GL_POINTS);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 0, 0);
glEnd();
(0, 1, 0)
(1, 1, 0)
(0, 0, 0)
(1, 0, 0)
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 0, 0);
glEnd();
(0, 1, 0)
(1, 1, 0)
(0, 0, 0)
(1, 0, 0)
glBegin(GL_LINE_STRIP);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 0, 0);
glEnd();
(0, 1, 0)
(1, 1, 0)
(0, 0, 0)
(1, 0, 0)
glBegin(GL_LINE_LOOP);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 0, 0);
glEnd();
(0, 1, 0)
glBegin(GL_TRIANGLES);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 0, 0);
glEnd();
(1, 1, 0)
(1, 0, 0)
(0, 1, 0)
(1, 1, 0)
(0, 0, 0)
(1, 0, 0)
glBegin(GL_QUADS);
glVertex3f(0, 0, 0);
glVertex3f(0, 1, 0);
glVertex3f(1, 1, 0);
glVertex3f(1, 0, 0);
glEnd();
(0.5,1.5,0)
glBegin(GL_QUADS);
glVertex3f(0, 0, 0);
glVertex3f(-0.5, 1, 0);
glVertex3f(0.5, 1.5, 0);
glVertex3f(1.5, 1, 0);
glVertex3f(0, 1, 0);
glEnd();
Mg. Johnny R. Avendao Q.
(-0.5,1,0)
(1.5,1, 0)
(0, 0, 0)
(1, 0, 0)
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
Initializaciones
Todo programa de OpenGL que utilice GLUT
debe empezar inicializando el estado de la
mquina de estados de GLUT. Las funciones
de inicializacin de GLUT tienen el prefijo
glutInit.
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
Un programa en OpenGL
glutInitDisplayMode: define el
#include <stdlib.h>
#include <math.h>
modo en el que se tiene que dibujar la
#include <GL/glut.h>
ventana, como en otras funciones los
#define GL_PI 3.1416f
parmetros a utilizar se definen con
void init(void);
bit o mscaras de bits
void display(void);
void reshape(int,int);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Mg. Johnny R. Avendao Q.
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
glutInitWindowPosition: Nos
permite colocar la ventana en algn
punto determinado de la pantalla.
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
de la
Un programa en OpenGL
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h>
#define GL_PI 3.1416f
void init(void);
void display(void);
void reshape(int,int);
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
Se define el color con el que se borrara
void display(void)
el buffer al hacer un glClear(). Por
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
defecto el negro.
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
xc = rma * sin(ang);
yc = rma * cos(ang);
glVertex2f(xc,yc);
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
void display(void)
Las primitivas de OpenGL estn siempre
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
sombreadas, pero el modelo de sombreado
glClear(GL_COLOR_BUFFER_BIT);
puede ser plano (GL_FLAT) o suave
glPushMatrix();
// salva el estado actual de la matriz
(GL_SMOOTH)
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
xc = rma * sin(ang);
yc = rma * cos(ang);
glVertex2f(xc,yc);
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
void display(void)
{
Esta funcin especifica el color del pincel
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
glClear(GL_COLOR_BUFFER_BIT);
en formato RGB, el color es blanco.
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
xc = rma * sin(ang);
yc = rma * cos(ang);
glVertex2f(xc,yc);
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
void display(void)
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
Esta funcin resetea los buffer de color y
xc = rma * sin(ang);
yc = rma * cos(ang);
Profundidad, eliminando posibles
glVertex2f(xc,yc);
contenidos de ventanas anteriores.
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
void display(void)
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
xc = rma * sin(ang);
En OpenGL, los clculos de rotaciones,
yc = rma * cos(ang);
glVertex2f(xc,yc);
Texturas, translaciones, se realizan mediante
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
el clculo de matrices. Esta funcin crea una
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
copia de la matriz de modelado y la empuja
glVertex2f(x,y);
una posicin la pila de matrices.
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
Esta estructura es la encargada de dibujar el
{
objeto a visualizar. glBegin() comienza una
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
secuencia de vrtices con los que se construir
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
la primitiva. El tipo de primitivas viene dado
}
por el parmetro de glBegin(), en este caso
void display(void)
GL_POINTS se encarga de definir puntos.
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;glEnd se encarga simplemente de cerrar la
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
estructura.
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
xc = rma * sin(ang);
yc = rma * cos(ang);
glVertex2f(xc,yc);
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
void display(void)
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
Una vez realizadas los clculos sobre la matriz
xc = rma * sin(ang);
yc = rma * cos(ang);
de segundo plano, se coloca esta en la cima de
glVertex2f(xc,yc);
la pila de matrices.
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm* ang);
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0); //parametros: rojo, amarillo y azul, el cuarto es el parametro alpha
glShadeModel(GL_FLAT);
glColor3f(1.0,1.0,1.0);
}
void display(void)
{
GLfloat ang, rm = 1.0f, rma = 3*rm, x, y, xc, yc;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
// salva el estado actual de la matriz
glBegin(GL_POINTS);
for (ang = 0.0f; ang < 2 * GL_PI; ang += 0.01f)
{
xc = rma * sin(ang);
glFlush(): esta instruccion obliga a pintar y no
yc = rma * cos(ang);
glVertex2f(xc,yc);
espera a que el hardaware termine para
x = (rma - rm)*cos(ang) + rm*cos((rma - rm)/rm*
ang);
continuar
con el programa.
y = (rma - rm)*sin(ang) - rm*sin((rma - rm)/rm* ang);
glVertex2f(x,y);
}
glEnd();
glPopMatrix();
// reecupera el estado del matriz
glFlush();
}
Mg. Johnny R. Avendao Q.
Lic. John Ledgard Trujillo Trejo
Un programa en OpenGL
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Un programa en OpenGL
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Un programa en OpenGL
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Un programa en OpenGL
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Un programa en OpenGL
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-10.0,10.0,-10.0,10,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Bibliografa
1. Computer Graphics: Principles and Practice. Foley J., Van Dame A., Feiner S.,
Hughes J., Phillips R. Addison Wesley Publishing Company, Massachusetts.
1996
2. Curvas y superficies para modelado geomtrico. Cordero Valle J., Cortes
Parejo. Alfaomega Grupo Editor 2003
3. Fundamentals of Computer Aided Geometric Design. Hoschek J., Lasser D.
A.K. Peters Ltd. Wellesley Massachusetts. 1993
4. Grficas por computadora. Hearn D., Baker M.P. Prentice - Hall
Hispanoamericana. 1998
5. Introduction to Computing with Geometry Notes. Shene C.K. Department of
Computer Science. Michigan Technological University. 1997
www.cs.mtu.edu/~shene/COURSES/CS3621/NOTES/notes.html
6. OpenGL Silicon Graphics Inc. www.opengl.org
7. Programacin de graficos en 3D. M. Escribano. Add. Wesley.
8. Fundamentos de Dibujo en Ingeniera. Con una introduccin a las Graficas
por computadora interactiva para Diseo y Produccin - LUZADDER Warren
J. - DUFF Jon M.
Mg. Johnny R. Avendao Q.
Discusin, preguntas...
jueves, 03 de septiembre de