Documentos de Académico
Documentos de Profesional
Documentos de Cultura
O
OpenGL
GL
Q é es OpenGL
Qué
• Librerías gráficas que facilitan el interfaz
con ell H
Hw gráfico
áfi
• Independiente del Hardware
• API sencilla, estable y compacta.
• Primitivas geométricas básicas
• No incluye gestión de ventanas, teclado,
etc.
Q é permite OpenGL
Qué
• Describir la geometría de los objetos a
partir
ti de
d las
l primitivas
i iti bá
básicas
i
• Situar los objetos y el punto de vista
• Cálculo de color, incluyendo iluminación,
texturas, etc.
• Generación de la imagen final
final, efectuando
p
operaciones de culling,
g z-buffer, etc.
Introd cción
Introducción
• OpenGL como máquina de estados
• Algunas propiedades mantienen su valor
hasta que se les asigne otro o se
desactive.
• glEnable/glDisable
• Valores por defecto
• Funciones de acceso al valor actual
Introd cción
Introducción
• Color actual.
• P t de
Punto d vista.
i t
• Transformaciones de proyección.
proyección
• Estilo de líneas y polígonos.
Introd cción
Introducción
• M
Modos
d d de dib
dibujar
j polígonos.
lí
• Posición y características de las fuentes
de iluminación.
• Propiedades de los materiales de los
objetos
Introd cción
Introducción
• Librerías:
Lib í
– http://www.opengl3d.org
– http://mesa3d.sourceforge.net
– http://www.sgi.com/software/opengl/glut.html
E tensiones OpenGL
Extensiones
• Son muy variadas y de diversa aplicación.
• Desarrolladas p por fabricantes de hardware: p
permiten
utilizar innovaciones introducidas en el hw gráfico
manteniendo simplicidad de programación de OpenGL.
• Librería básica de OpenGL casi no varía de una versión
a otra: cambia las extensiones que soporta.
• Tema importante: cómo incluirlas (existe una librería
llamada glew (http://glew.sourceforge.net/) que facilita la
carga de extensiones
• Dependiendo de la extensión hay que crear nuevas
constantes definir punteros a nuevas funciones ...
constantes,
E tensiones OpenGL
Extensiones
• Es importante estar seguros de que
nuestro hw gráfico soporte la extensión
extensión.
• Existen algunas cabeceras que simplifican
este proceso.
• Fragmento de unos cursos del siggraph:
http://www.mesa3d.org/brianp/sig97/exten.htm
p g p g
http://www.ii.uni.wroc.pl/~anl/cgfiles/Sig99AdvOpenGLnotes/node391.h
tml
E tensiones OpenGL
Extensiones
• openGl es mantenido por un grupo formado por
distintas empresas privadas y otros organismos.
organismos
• el comité encargado de mantener el estándar se
llama ARB (Architecture Review Board).
• página oficial del registro de todas la
extensiones de openGl (mantenido SGI):
htt //
http://oss.sgi.com/projects/ogl-sample/registry/
i / j t / l l / i t /
• Registro
g define: nomenclatura, g guías ppara crear
nuevas extensiones, etc.
E tensiones OpenGL
Extensiones
• Cada extensión es definida por su especificación, donde
explica para qué sirve y qué debes hacer para incluirla.
– Las extensiones GL son propias de openGL,
– Las extensiones GLX son propias de sistemas con X Windows
– Las
L extensiones
t i WGL son propias
i d de sistemas
i t Wi
Windows.
d
• Conceptos previos
• Dibujar
j p primitivas básicas
• Vista tridimensional
• Iluminación y materiales
Sinta is de OpenGL
Sintaxis
• Funciones con prefijo gl
• Constantes con prefijo GL_
• Algunas funciones con sufijo
– Número de parámetros
– Tipo de los parámetros
glColor3f()
lC l 3f()
g
glVertex3i()
()
Tipos de datos
Son equivalentes:
glVertex2i (1
(1, 3);
glVertex2f (1.0, 3.0);
Son equivalentes:
glColor3f (1.0, 1.0, 1.0);
void g
glClear ((GLbitfield mask);
)
Posibles Buffers:
GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT
GL_ACCUM_BUFFER_BIT
GL_STENCIL_BUFFER_BIT
• Valor por defecto: (0,0,0,0)
• Ejemplo:
j p
glClearColor (0.5, 0.5, 0.5, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
• Especificar color
void glColor3f(GLclampf red, GLclampf green, GLclampf blue);
Función
u c ó gglColor3f
Co o 3 () Color
Co o
glColor3f (1.0,
(1 0 0.0,
0 0 0.0);
0 0); rojo
glColor3f (1.0,
(1 0 1.0,
1 0 0.0);
0 0); amarillo
glColor3f (1.0,
(1 0 0.0,
0 0 1.0);
1 0); magenta
glColor3f (1.0,
(1 0 1.0,
1 0 1.0);
1 0); blanco
• Forzar a la Finalización del Dibujo.
void glFlush (void);
• Tamaño de punto
void g
glPointSize ((GLfloat size);
)
Por defecto size=1
• Rango de valores posibles
glGetFloatv (GL_POINT_SIZE_RANGE);
(GL POINT SIZE RANGE);
Líneas (GL_LINES)
(GL LINES)
Tiras de líneas
Líneas
• Grosor de la línea
void
id glLineWidth
lLi Width (GLfloat
(GLfl t width);
idth)
• Patrón de línea
void glLineStipple (Glint factor, Glshort
pattern);
tt )
Es necesario activarlo/desactivarlo
glEnable (GL_LINE_STIPPLE)
glDisable (GL_LINE_STIPPLE)
Líneas
//color de fondo
glClearColor(1.0, 1.0, 0.8, 1.0);
glClear(GL
g C ea (G _CO COLORO _BUFFER
U _BIT);
);
//color de dibujo
glColor3f (0.0, 0.0, 0.0);
g
//grosor de línea
glLineWidth(2.0);
//se activa el patrón de línea
glEnable(GL_LINE_STIPPLE);
for(i=1;i<7;i++){
glTranslatef(1.0,0.0,0.0);
glLineStipple(i,0x00FF);
glBegin (GL_LINES);
glVertex3f (0.0, -2.0, 0.0);
glVertex3f (0.0, 3.0, 0.0);
glEnd
lE d ()
();
}
glFlush();
Polígonos (Triáng
(Triángulos
los )
Orientación
Primiti as
Primitivas
Polígonos
• void glPolygonMode (GLenum face, GLenum mode);
Define el modo en q
que se dibujan
j los p
polígonos
g
Polígonos
• void glFrontFace (GLenum mode);
Define qué caras se consideran anteriores
anteriores.
GL_CCW (CounterClockWise). Por defecto
GL CW (ClockWise)
GL_CW (Cl kWi )
void glCullFace (GLenum mode);
• Se asignan a los
vértices
• IIndican
di lla orientación
i t ió
de la superficie
Vectores normales
• Transformaciones basadas en
multiplicaciones de matrices
• Tipos de matrices:
GL MODELVIEW Matriz
GL_MODELVIEW: M t i modelo-vista
d l i t
GL PROJECTION: Matriz proyección
GL_PROJECTION:
GL_TEXTURE: Matriz textura
Canal de transformaciones
Transformaciones de modelo
• Afectan a la matriz de modelo-vista
• glMatrixMode(GL_MODELVIEW);
– void g
glRotate{fd}
{ } ((TYPE angle,
g TYPE x,
TYPE y, TYPE z)
– void glTranslate{fd} (TYPE x,
x TYPE y,
y
TYPE z)
– void glScale{fd} (TYPE x, TYPE y, TYPE z)
Transformaciones de modelo
• Efecto acumulativo
Transformaciones de modelo
Código incorrecto
Transformaciones de modelo
glTranslate (0.0,
(0 0 0.0,
0 0 -5.0)
5 0) aplicada al
inicio es equivalente a situar el punto de
vista en el punto (0.0, 0.0, 5.0)
• Utilizando gluLookAt()
Transformaciones de vista
ista
void gluLookAt
(GLdouble eyex, GLdouble eyey, GLdouble eyez,
GLdouble centrox,, GLdouble centroy,
y, GLdouble
centroz, GLdouble upx, GLdouble upy, GLdouble
upz)
T
Transformaciones
f i de
d proyección
ió
• Define el tipo de proyección:
– Perspectiva
P ti o paralela
l l
• Afecta a la matriz GL
GL_PROJECTION
PROJECTION
glMatrixMode (GL_PROJECTION);
Pro ección Perspecti
Proyección Perspectiva
a
Pro ección perspecti
Proyección perspectiva
a
• void
id glFrustum
lF t (GLd bl lleft,
(GLdouble ft GLd
GLdouble
bl
g , GLdouble bottom,, GLdouble top,
right, p,
GLdouble near, GLdouble far)
• Operaciones
p afectan a la matriz activa
– void glLoadIdentity (void)
– void
id glLoadMatrix{fd}
lL dM t i {fd} (const
( t TYPE *m)
* )
– void g
glMultMatrix{fd}
{ } ((const TYPE *m))
Il minación
Iluminación
• Posición de la luz
GLfloat light
light_position[]
position[]={0
{0.0,
0, 0
0.0,
0, 0
0.0,
0, 1.0};
1 0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
• Dirección
Di ió ddell eje
j ddell cono d
de lluz
Glfloat spot_direction[] = {-1.0, -1.0, 0.0}
glLightfv(GL_LIGHT0,
lLi htf (GL LIGHT0 GL
GL_SPOT_DIRECTION,
SPOT DIRECTION spot_direction);
t di ti )
Creación de ffuentes
entes de lluz
• Intensidad de la luz g
global ambiente
• Posición del punto de vista: local o infinito
• Cálculos de iluminación a dos caras
Modelo de ililuminación
minación
• Además de las fuentes de luz, se puede
especificar
ifi una lluz global
l b l ambiente
bi t
glLightModeli(GL_LIGHT_MODEL_LOCAL_VI
EWER,, GL_TRUE);
);
Modelo de ililuminación
minación
glLightModeli(LIGHT_MODEL_TWO_SIDE,
GL TRUE);
GL_TRUE);
Materiales
• Similar a las luces
• void
id auxInitDisplayMode(GLbitfield
I i Di l M d (GLbi fi ld
mask)
• void auxInitPosition(GLint x, GLint y,
GLint width, Glint height)
– x,y
x y esquina superior izquierda
• void auxInitWindow(GLbyte *titleString)
Creación de una
na ventana
entana
• Ejemplo
auxInitDisplayMode(AUX_SINGLE
auxInitDisplayMode(AUX SINGLE |
AUX_RGBA);
auxInitPosition(100,100,250,250);
auxInitWindow(“Una ventana Opengl”);
Manejo de e
eventos
entos de ventana
entana
• Reescalado de ventana
void auxReshapeFunc
(void (*funcion)(Glsizei
( funcion)(Glsizei, GLsizei))
– button: AUX
AUX_LEFT_BUTTON,
LEFT BUTTON …
– mode: AUX_MOUSE_DOWN,…
– funcion: Función que se ejecutará cuando el
p
botón especificado p
por button esté en el
modo especificado por mode
Dib jar objetos simples
Dibujar
• void auxWireSphere(Gldouble radius)
void auxSolidSphere(Gldouble radius)
• void auxWireTorus(Gldouble
innerRadius, Gldouble outerRadius)
• void auxWireCylinder(Gldouble radius,
Gldouble height)
• void auxSolidTeapot(Gldouble size)
• Etc.
Proceso en backgro
background
nd
• void auxIdleFunc(void *func)
• void auxMainLoop p
(void (*displayFunc)(void))
• Ejemplo 1:
– Creación de una ventana
– Asignación de color de fondo
– Sin definir auxMainLoop()
– Sin definir auxReshapeFunc()
#include <windows.h>
#include <conio
<conio.h>
h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
return(0);
}
Comportamiento
• No hay un bucle de refresco
– La ventana se pinta una única vez al inicio de
j
la ejecución
– La ventana no se repinta en caso de
ocultación
• No existe un tratamiento del evento de
reescalado
Definición del b
bucle
cle principal
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
<gl\glaux h>
void
i CALLBACK display
i (void)
i
{
glClearColor(0.0, 0.0, 1.0, 1.0);
glClear(GL
g ( _COLOR_BUFFER_BIT);
);
glFlush();
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE |
AUX_RGB);
auxInitPosition (0, 0, 500, 500);
auxInitWindow ("Ventana
( Ventana azul
azul");
);
auxMainLoop(display);
return(0);
}
Ejemplo 2
• Dibujar un cubo
• Con proyección en perspectiva
• Definición de bucle principal
p p
• Definición de reescalado
Ejemplo 3
void CALLBACK funcionDePintado (void)
{
//color de fondo
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0 0, 0
glTranslatef(0.0, 0.0,
0, -5
5.0);
0);
//color de dibujo
glColor3f (0.0, 1.0, 0.0);
auxWireCube(2.0);
glFlush();
}
void CALLBACK funcionDeReescalado(GLsizei w, GLsizei h)
{
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGB);
auxInitPosition (0
(0, 0
0, 250
250, 250);
auxInitWindow ("cubo");
auxReshapeFunc(funcionDeReescalado);
auxMainLoop(funcionDePintado);
return(0);
}
Mantener el aspecto
p en el
reescalado
void CALLBACK funcionDeReescalado(GLsizei w,
GLsizei h)
{
glMatrixMode (GL
(GL_PROJECTION);
PROJECTION); //activar la
//matriz de proyeccion
glLoadIdentity ();
gluPerspective(90.0, (float)w/(float)h,
2.0, 7.0);
glViewport (0
(0, 0
0, w
w, h);
glMatrixMode (GL_MODELVIEW);
}
Ejemplo e
evento
ento teclado
GLfloat angulo=0.0;
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(angulo, 0.0, 0.0, 1.0);
//color de dibujo
glColor3f (0.0, 1.0, 0.0);
auxWireCube(2.0);
glFlush();
}
void CALLBACK funcionDeReescalado(GLsizei w, GLsizei h)
{
glMatrixMode (GL
(GL_PROJECTION);
PROJECTION);
glLoadIdentity ();
gluPerspective(90.0, (float)w/(float)h, 2.0, 7.0);
glMatrixMode (GL_MODELVIEW);
/* restaurar la matriz de modelo-vista como activa*/
}
void CALLBACK funcionRotar(){
angulo+=2.0;
if(angulo>=360) angulo=0.0;
f
funcionDePintado();
i D Pi t d ()
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE |
AUX_RGB);
auxInitPosition (0, 0, 250, 250);
auxInitWindow ("cubo");
( );
auxReshapeFunc(funcionDeReescalado);
auxKeyFunc(AUX_r, funcionRotar);
auxMainLoop(funcionDePintado);
M i L (f i D Pi t d )
return(0);
}
Ejemplo animación
//color de fondo
glClearColor(0.5, 0.5, 0.5, 1.0);
g
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glLoadIdentit ()
glTranslatef(0.0, 0.0, -5.0);
glRotatef(angulo,
g g 0.0, 0.0, 1.0);
//color de dibujo
glColo 3f (0.0,
glColor3f (0 0 1.0,
1 0 0.0);
0 0)
auxWireCube(2.0);
glFlush();
auxSwapBuffers();
}
void CALLBACK funcionDeReescalado(GLsizei
( w,
,
GLsizei h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(90.0, (float)w/(float)h, 2.0,
7 0);
7.0);
glViewport (0
(0, 0
0, w
w, h);
glMatrixMode (GL
(GL_MODELVIEW);
MODELVIEW);
/* restaurar la matriz de modelo-vista como
activa*/
}
void CALLBACK funcionIdle(){
angulo+=0.5;
angulo+=0 5;
if(angulo>=360) angulo=0.0;
Sl
Sleep(50);
(50)
funcionDePintado();
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX
(AUX_DOUBLE
DOUBLE |
AUX_RGB);
auxInitPosition
I itP iti (0
(0, 0
0, 250
250, 250)
250);
auxInitWindow ("cubo");
auxReshapeFunc(funcionDeReescalado);
auxIdleFunc(funcionIdle);
auxMainLoop(funcionDePintado);
return(0);
}
Librería GLUT
return 0;
}
Manejo de e
eventos
entos de ventana
entana
• Reescalado de ventana
void glutReshapeFunc(void (*func)(int
width int height));
width,
• Dibujar un cubo
• Con proyección en perspectiva
• Definición de bucle principal
p p
• Definición de reescalado
Ejemplo 3
void funcionDePintado (void)
{
//color de fondo
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0 0, 0
glTranslatef(0.0, 0.0,
0, -5
5.0);
0);
//color de dibujo
glColor3f (0.0, 1.0, 0.0);
glutWireCube(2.0);
glFlush();
}
void funcionDeReescalado(GLsizei w, GLsizei h)
{
}
int main(int argc, char** argv)
{
glutInit(&argc,
l tI it(& argv);
)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250,250);
glutInitWindowPosition(0,0);
glutCreateWindow("Cubo");
glutReshapeFunc(funcionDeReescalado);
l tR h F (f i D R l d )
glutDisplayFunc(funcionDePintado);
glutMainLoop();
return 0;
}
Mantener el aspecto
p en el
reescalado
void funcionDeReescalado(GLsizei w, GLsizei h)
{
glMatrixMode (GL_PROJECTION); //activar la
//matriz de proyeccion
glLoadIdentity ();
gluPerspective(90 0 (float)w/(float)h
gluPerspective(90.0, (float)w/(float)h,
2.0, 7.0);
– button: GLUT
GLUT_LEFT_BUTTON,
LEFT BUTTON
GLUT_MIDDLE_BUTTON o GLUT_RIGHT_BUTTON
– state: GLUT
GLUT_UPUP o GLUT
GLUT_DOWN
DOWN
– func: Función que se ejecutará cuando el botón
especificado
ifi d por b
button
tt esté
té en ell estado
t d
especificado por state
Ejemplo e
evento
ento teclado
GLfloat angulo=0.0;
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(angulo, 0.0, 0.0, 1.0);
//color de dibujo
glColor3f (0.0, 1.0, 0.0);
glutWireCube(2.0);
glFlush();
}
void funcionDeReescalado(GLsizei w, GLsizei h)
{
glMatrixMode (GL
(GL_PROJECTION);
PROJECTION);
glLoadIdentity ();
gluPerspective(90.0, (float)w/(float)h, 2.0, 7.0);
glMatrixMode (GL_MODELVIEW);
/* restaurar la matriz de modelo-vista como activa*/
}
void funcionRotar(unsigned char key
key, int xx, int y)
{
if(key== r )
if(key=='r')
{
angulo+=2.0;
l 20
if(angulo>=360) angulo=0.0;
funcionDePintado();
}
}
iintt main(int
i (i t argc, char**
h ** argv))
{
glutInit(&argc,
l tI it(& argv);
)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(250 250);
glutInitWindowSize(250,250);
glutInitWindowPosition(0,0);
glutCreateWindow( Cubo );
glutCreateWindow("Cubo");
glutReshapeFunc(funcionDeReescalado);
glutKeyboardFunc (funcionRotar);
glutDisplayFunc(funcionDePintado);
glutMainLoop();
return 0;
}
Proceso en backgro
background
nd
• void glutIdleFunc(void (*func)(void));
GLfloat angulo=0
angulo=0.0;
0;
//color de fondo
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
g y();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(angulo, 0.0, 0.0, 1.0);
//color de dibujo
glColor3f (0.0, 1.0, 0.0);
glutWireCube(2.0);
glFlush();
glutSwapBuffers();
}
void funcionDeReescalado(GLsizei w
w, GLsizei h)
{
g
glMatrixMode ((GL_PROJECTION);
_ )
glLoadIdentity ();
gluPerspective(90 0 (float)w/(float)h
gluPerspective(90.0, (float)w/(float)h, 2
2.0,
0 77.0);
0);
glMatrixMode (GL_MODELVIEW);
/* restaurar la matriz de modelo-vista
modelo vista como activa*/
}
void funcionIdle(){
angulo+=0.5;
angulo+=0 5;
if(angulo>=360)
( g ) angulo=0.0;
g
Sl
Sleep(50);
(50)
funcionDePintado();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
g
glutInitWindowSize(250,250);
( , );
glutInitWindowPosition(0,0);
glutCreateWindow( Cubo );
glutCreateWindow("Cubo");
glutReshapeFunc(funcionDeReescalado);
glutIdleFunc(funcionIdle);
glutDisplayFunc(funcionDePintado);
glutMainLoop();
return 0;
}
Test de profundidad
prof ndidad
void funcionDePintado (void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glFlush();
}
Sin test de profundidad
prof ndidad
Test de profundidad
prof ndidad
void inicializar(){
glEnable(GL_DEPTH_TEST);
}
void funcionDePintado (void)
( )
{
glClear(GL_COLOR_BUFFER_BIT
glClear(GL COLOR BUFFER BIT | GL_DEPTH_BUFFER_BIT);
GL DEPTH BUFFER BIT);
glLoadIdentity();
glTranslatef(0.0,
g ( 0.0, -5.0);
)
g
glFlush();
()
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE
l tI itDi l M d (GLUT SINGLE | GLUT
GLUT_RGB);
RGB)
glutInitWindowSize(250,250);
glutInitWindowPosition(0 0);
glutInitWindowPosition(0,0);
glutCreateWindow("Cubo");
glutReshapeFunc(funcionDeReescalado);
inicializar();
glutDisplayFunc(funcionDePintado);
glutMainLoop();
return 0;
}
Con test de profundidad
prof ndidad
Modelo de sombreado
void CALLBACK funcionDePintado
(void)
{
glClearColor(0.5,
lCl C l (0 5 0 0.5,
5 00.5,
5 11.0);
0)
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glShadeModel(GL_SMOOTH);
glBegin(GL_TRIANGLES);
glBegin(GL TRIANGLES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(-1.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
Modelo de sombreado
void CALLBACK funcionDePintado
(void)
{
glClearColor(0.5,
lCl C l (0 5 0 0.5,
5 00.5,
5 11.0);
0)
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glShadeModel(GL_FLAT);
glBegin(GL_TRIANGLES);
glBegin(GL TRIANGLES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f(-1.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
Zoom
• Modificar el zoom
• Aumentar el factor de zoom al presionar la
“Z”
• Disminuir el factor de zoom al presionar la
z
“z”
float zoom_factor=1.0;
GLsizei current_w, current_h;
//color de fondo
glClearColor(0 5 0
glClearColor(0.5, 0.5,
5 00.5,
5 11.0);
0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0 0 0
glTranslatef(0.0, 0.0,
0 -5.0);
5 0);
glShadeModel(GL_SMOOTH);
glBegin(GL_TRIANGLES);
glColor3f
lC l 3f (1
(1.0,
0 00.0,
0 00.0);
0)
glVertex3f(-1.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
void CALLBACK funcionDeReescalado(GLsizei w, GLsizei h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity
g y ();
gluPerspective(90.0 * zoom_factor, (float)w/(float)h, 2.0, 7.0);
current_w=w;
current h=h;
current_h=h;
glMatrixMode (GL_MODELVIEW);
/* restaurar la matriz de modelo
modelo-vista
vista como activa*/
}
void CALLBACK aumentarZoom(){ (){
zoom_factor-=0.15;
f
funcionDeReescalado(current_w,
i D R l d ( t
current_h);
);
}
return(0);
}
Zoom
Material e ililuminación
minación
#include <windows.h> glClearColor (0.0, 0.0, 0.0,
#include <gl\gl.h> 0.0);
#i l d
#include <
<gl\glu.h>
l\ l h> glShadeModel
lSh d M d l (GL_SMOOTH);
(GL SMOOTH)
#include <gl\glaux.h>
glMaterialfv(GL_FRONT,
GLsizei current_w,
current w current_h;
current h; GL SPECULAR mat_spec);
GL_SPECULAR, t )
glMaterialfv(GL_FRONT,
void inicializar(){ GL_SHININESS, mat_shin);
glLightfv(GL_LIGHT0,
lLi htf (GL LIGHT0
GLfloat mat_spec[]={1.0, 1.0, GL_POSITION, light_pos);
1.0, 1.0};
GLfloat mat_shin[]={50.0}; glLightfv(GL_LIGHT0,
GL DIFFUSE light
GL_DIFFUSE, light_dif);
dif);
GLfloat light_pos[]={1.0,
1.0, 1.0, 0.0};
GLfloat light_dif[]={1.0, glEnable(GL_LIGHTING);
0 0 0
0.0, 0.0,
0 1 1.0};
0}; glEnable(GL_LIGHT0);
0
}
void CALLBACK funcionDePintado
(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
a SolidSphere(1)
auxSolidSphere(1);
glFlush();
}
void CALLBACK funcionDeReescalado(GLsizei w, GLsizei h)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity
lL dId tit () ();
gluPerspective(45.0, (float)w/(float)h, 1.0, 10.0);
current_w=w;
t
current_h=h;
glMatrixMode (
g (GL_MODELVIEW);
);
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE |
AUX RGB)
AUX_RGB);
auxInitPosition (0, 0, 250, 250);
auxInitWindow ("esfera");
inicializar();
auxReshapeFunc(funcionDeReescalado);
auxMainLoop(funcionDePintado);
return(0);
}
Material e ililuminación
minación
Prácticas