representação de imagensmotta/dim102/opengl.pdf · opengl 35t56 – sala 3e3 bruno motta de...
Post on 20-Aug-2020
2 Views
Preview:
TRANSCRIPT
DIM102 1
OpenGL
35T56 – Sala 3E3Bruno Motta de CarvalhoDIMAp – Sala 15 – Ramal 227
DIM1022
Introdução
Definição História Portabilidade
DIM1023
Vantagens
Rápida Simples Robusta Padrão aberto Multiplataforma Mecanismo de incorporação de
extensões ao padrão
DIM1024
Bibliotecas OpenGL
Sistema de janelas pode ser trocado
Existem implementações para variantes de Unix (IRIX, Solaris, Linux, etc.), OS/2, Windows, BSD, MAC OS
DIM1025
Bibliotecas OpenGL
DIM1026
Pipeline da OpenGL
DIM1027
Máquina de Estados da OpenGL Máquina de estados Setando estados
Cor Normal Coordinadas de texturas Modo de desenho Modo de matriz
DIM1028
Opções em OpenGL
As funções glEnable() e glDisable() funcionalidades da OpenGLGL_DEPTH_TESTGL_CULL_FACEGL_DITHERGL_BLENDetc.
DIM1029
Tipos de dados
Nomes das funções denotam argumentos
GLbyte, GLshort glVertex{2,3,4}{sifd}[v]()
glVertex3i(Glint x, Glint y, Glint z);glVertex3fv(Glgloat *v);
GLbyte, Glshort, Glint, Glfloat, GLdouble
DIM10210
Primitivas Geométricas Desenhase primitivas
usandose glBegin() e glEnd()
Tipos de primitivas: GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_POLYGON, GL_QUADS, etc.
DIM10211
Vértices
Usase glVertex3f(GLfloat x, GLfloat y, GLfloat z) Alternativa: glVertex3fv(const Glfloat *v) Variantes: glVertex2, glVertex3 e
glVertex4, e shorts, ints, floats, doubles Arrays (OpenGL 1.1)
DIM10212
Normais
Usase glNormal3f(GLfloat x, GLfloat y, GLfloat z) Alternativa: glNormal3fv(const Glfloat *v) Variantes: glVertex2, glVertex3 e glVertex4,
e bytes, shorts, ints, floats, doubles
DIM10213
Cores
Usase glColor3f(GLfloat x, GLfloat y, GLfloat z) Alternativa: glColor3fv(const Glfloat *v) Variantes: glColor3 e glColor4, e bytes,
unsigned bytes, shorts, ints, floats, doubles
DIM10214
Transformações
Coord.objetos
Matriz dosModelos
Matriz daProjeção
Matriz doViewport
Coord.camera
Coord.Normalizadas
Coord.display
DIM10215
Transformações
TranslaçãoglTranslationf(GLfloat x, GLfloat y, GLfloat z)
RotaçãoglRotationf(Glfloat angulo, GLfloat x, GLfloat
y, GLfloat z)Ângulo é dado em graus e (x,y,z) é o eixo da
rotação Escala
glScalef(GLGLfloat x, GLfloat y, GLfloat z)
DIM10216
Projeção Ortogonal Comando glOrtho utiliza seis parâmetros que
definem um volume de visão. glOrtho(left,right,bottom,top,near,far);
DIM10217
Projeção Perspectiva Comando glFrustum, define o volume de visão
em forma de tronco de pirâmide (frustum). glFrustrum(left,right,bottom,top,near,far);
DIM10218
Exemplo
glBegin(GL_POLYGON); glTranslatef(4.5,7.0,0); glRotatef(40,0,1,0); glScalef(1,1,1); glColor3f(1,0,0); glNormal3f(0,0,1); glVertex3f(-0.5, 0.5,0.5); glVertex3f( 0.5, 0.5,0.5); glVertex3f( 0.5,-0.5,0.5); glVertex3f(-0.5,-0.5,0.5);glEnd();
DIM10219
Visibilidade
OpenGL só suporta Zbuffering glEnable(GL_DEPTH_TEST) glClear(GL_DEPTH_BUFFER)
DIM10220
Iluminação Em OpenGL a luz é dividida em quatro
componentes independentes: Ambiente: resultado da luz refletida no ambiente;
é uma luz que vem de todas as direções Difusa: luz que vem de uma direção, atinge a
superfície e é refletida em todas as direções Especular: luz que vem de uma direção e tende a
ser refletida numa única direção Emissiva: simula a luz que se origina em um
objeto
DIM10221
Luzes e Colorização glLightfv(GLenum light, GLenum pname,GLfloat
*param) glEnable(GL_LIGHTING) glEnable(GL_LIGHT0) glShadeModel(GLenum mode), onde mode pode
serGL_FLAT FLAT shadingGL_SMOOTH GOURAUD shading
DIM10222
Mapeamento de Texturas Crie um objeto de textura
Especifique um bitmap da textura Escolha como aplicar a textura Ligue o mapeamento de texturas Desenha a cena, enviando as
coordenadas geométricas e as coordenadas de textura
DIM10223
Extensões
Mecanismo utilizado para se incluir novas funcionalidades a OpenGL
São propostas por fabricantes e podem ser adotadas pela ARB posteriormente
Prefixos denotam os proponentes, HP, SUN, NV, SGI, WIN, INTEL, etc.
Função glGetString é usada para se determinar quais extensões estão disponíveis
DIM10224
Interação com o Usuário
Interação com o usuário é feita através de outras ferramentas, como GLUT, Qt, GTK, etc.
OpenGL foi desenvolvida para ser independente de plataformas
Diferentes SOs tratam eventos de formas diferentes
DIM10225
/#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <unistd.h> // needed to sleep
#define ESCAPE 27
int window; /* The number of our GLUT window */
float rtri = 0.0f, rquad = 0.0f; /* rotation angles for the triangle and quadrilateral. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW);
}
void ReSizeGLScene(int Width, int Height)
{
if (Height==0) Height=1; // Prevent A Divide By Zero If The Window Is Too Small
glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
Exemplo
DIM10226
void DrawGLScene()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View
glTranslatef(1.5f,0.0f,6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glRotatef(rtri,0.0f,1.0f,0.0f); // Rotate The Pyramid On The Y axis
// draw a pyramid (in smooth coloring mode)
glBegin(GL_POLYGON); // start drawing a pyramid
// front face of pyramid
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top of triangle (front)
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
glVertex3f(1.0f,1.0f, 1.0f); // left of triangle (front)
glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
glVertex3f(1.0f,1.0f, 1.0f); // right of traingle (front)
// right face of pyramid
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f( 1.0f,1.0f, 1.0f); // Left Of Triangle (Right)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,1.0f, 1.0f); // Right Of Triangle (Right)
// back face of pyramid
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f( 1.0f,1.0f, 1.0f); // Left Of Triangle (Back)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(1.0f,1.0f, 1.0f); // Right Of Triangle (Back)
// left face of pyramid.
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(1.0f,1.0f,1.0f); // Left Of Triangle (Left)
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(1.0f,1.0f, 1.0f); // Right Of Triangle (Left)
glEnd(); // Done Drawing The Pyramid
Exemplo
DIM10227
glLoadIdentity(); // make sure we're no longer rotated.
glTranslatef(1.5f,0.0f,7.0f); // Move Right 3 Units, and back into the screen 7
glRotatef(rquad,1.0f,1.0f,1.0f); // Rotate The Cube On X, Y, and Z
// draw a cube (6 quadrilaterals)
glBegin(GL_QUADS); // start drawing the cube.
// top of cube
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Blue
glVertex3f( 1.0f, 1.0f,1.0f); // Top Right Of The Quad (Top)
glVertex3f(1.0f, 1.0f,1.0f); // Top Left Of The Quad (Top)
glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
// bottom of cube
glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 1.0f,1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(1.0f,1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(1.0f,1.0f,1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,1.0f,1.0f); // Bottom Right Of The Quad (Bottom)
// front of cube
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(1.0f,1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,1.0f, 1.0f); // Bottom Right Of The Quad (Front)
// back of cube.
glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 1.0f,1.0f,1.0f); // Top Right Of The Quad (Back)
glVertex3f(1.0f,1.0f,1.0f); // Top Left Of The Quad (Back)
glVertex3f(1.0f, 1.0f,1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,1.0f); // Bottom Right Of The Quad (Back)
// left of cube
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(1.0f, 1.0f,1.0f); // Top Left Of The Quad (Left)
glVertex3f(1.0f,1.0f,1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(1.0f,1.0f, 1.0f); // Bottom Right Of The Quad (Left)
// Right of cube
glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,1.0f,1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Cube
rtri+=15.0f; // Increase The Rotation Variable For The Pyramid
rquad=15.0f; // Decrease The Rotation Variable For The Cube
// swap the buffers to display, since double buffering is used.
glutSwapBuffers();
}
Exemplo
DIM10228
/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this call */
usleep(100);
/* If escape is pressed, kill everything. */
if (key == ESCAPE)
{
/* shut down our window */
glutDestroyWindow(window);
/* exit the program...normal termination. */
exit(0);
}
}
int main(int argc, char **argv)
{
/* Initialize GLUT state glut will take any command line arguments that pertain to it or
X Windows look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&argc, argv);
/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffered for automatic clipping */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA |
GLUT_DEPTH);
/* get a 640 x 480 window */
glutInitWindowSize(640, 480);
/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);
/* Open a window */
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99");
/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&DrawGLScene);
/* Go fullscreen. This is as soon as possible. */
glutFullScreen();
/* Even if there are no events, redraw our gl scene. */
glutIdleFunc(&DrawGLScene);
/* Register the function called when our window is resized. */
glutReshapeFunc(&ReSizeGLScene);
/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&keyPressed);
/* Initialize our window. */
InitGL(640, 480);
/* Start Event Processing Engine */
glutMainLoop();
return 1;
}
Exemplo
DIM10229
Exemplo
top related