Graphics Programming - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Graphics Programming

Description:

Two-dimensional viewing as a special case of three ... glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500, 500) ... – PowerPoint PPT presentation

Number of Views:130
Avg rating:3.0/5.0
Slides: 43
Provided by: wsu6
Category:

less

Transcript and Presenter's Notes

Title: Graphics Programming


1
Graphics Programming
2
Objectives
  • Introduce a standard program structure
  • Two-dimensional viewing as a special case of
    three-dimensional viewing
  • Fundamental OpenGL primitives and their
    attributes
  • A more sophisticated 3D example
  • Introduce hidden-surface removal

3
Window-Based Programming
  • Event-driven programming

void main() // Skeleton of even-driven OpenGL
program initialize things Create a screen
window glutDisplayFunc ( myDisplay )
//register redraw( ) glutReshapeFunc (
myReshape ) //register reshape( )
glutMouseFunc ( myMouse ) //register mouse
action( ) glutKeyboardFunc ( myKeyboard )
//register keyboard action( ) initialize other
things glutMainLoop() //enter main loop all
of the callback functions are defined here
4
main.c
  • include ltGL/glut.hgt
  • int main(int argc, char argv)
  • glutInit(argc,argv)
  • glutInitDisplayMode(GLUT_SINGLEGLUT_RGB)
  • glutInitWindowSize(500,500)
  • glutInitWindowPosition(0,0)
  • glutCreateWindow("simple")
  • glutDisplayFunc(mydisplay)
  • init()
  • glutMainLoop()

5
init.c
  • void init()
  • glClearColor (0.0, 0.0, 0.0, 1.0)
  • glColor3f(1.0, 1.0, 1.0)
  • glMatrixMode (GL_PROJECTION)
  • glLoadIdentity ()
  • glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)

6
The Orthographic View
7
Coordinate Systems
  • Internally, OpenGL will convert to camera (eye)
    coordinates and later to screen coordinates
  • OpenGL uses some internal representations that
    usually are not visible to the application

OpenGL places a camera at the origin in object
space pointing in the negative z direction
8
Orthographic Viewing
  • In the orthographic view,

9
Transformations and Viewing
  • In OpenGL, projection is carried out by a
    projection matrix (transformation). We must set
    the matrix mode first
  • glMatrixMode (GL_PROJECTION)
  • Transformations incremental we start with an
    identity matrix and multiply it with a projection
    matrix
  • glLoadIdentity()
  • glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)

10
2D and 3D viewing
  • In glOrtho(left, right, bottom, top, near, far)
    the near and far distances are measured from the
    camera
  • If the application is in two dimensions

11
mydisplay.c
  • void mydisplay()
  • glClear(GL_COLOR_BUFFER_BIT)
  • glBegin(GL_POLYGON)
  • glVertex2f(-0.5, -0.5)
  • glVertex2f(-0.5, 0.5)
  • glVertex2f(0.5, 0.5)
  • glVertex2f(0.5, -0.5)
  • glEnd()
  • glFlush()

OpenGL displays polygons correctly if they are
12
OpenGL Primitives
13
Attributes
  • Attributes determine the appearance of objects

Arial Arial bold Arial italic
14
RGB color
  • Each color component is stored separately in the
    frame buffer
  • Usually 8 bits per component in buffer
  • Color values in glColor3f range from 0.0 (none)
    to 1.0 (all),
  • The values range from 0 to 255 in glColor3ub

15
Indexed Color
  • Colors are indices into tables of RGB values

16
Color in OpenGL
  • The color as set by glColor becomes part of the
    state and will be used until changed
  • Default is smooth shading
  • Alternative is flat shading

17
Example Sierpinski Gasket
  • Start with a triangle
  • Connect bisectors of sides and remove central
    triangle
  • Repeat 5 times

18
Gasket Program
  • include ltGL/glut.hgt
  • GLfloat v32-1.0, -0.58,1.0, -0.58,
  • 0.0, 1.15 / initial triangle /
  • int n / number of recursive steps /

/ display one triangle / void triangle(
GLfloat a, GLfloat b, GLfloat c)
glVertex2fv(a) glVertex2fv(b)
glVertex2fv(c)
19
Triangle Subdivision
  • void divide_triangle(GLfloat a, GLfloat b,
  • GLfloat c, int m)
  • point2 v0, v1, v2
  • int j
  • if(mgt0)
  • for(j0 jlt2 j) v0j(ajbj)/2
  • for(j0 jlt2 j) v1j(ajcj)/2
  • for(j0 jlt2 j) v2j(bjcj)/2
  • divide_triangle(a, v0, v1, m-1)
  • divide_triangle(c, v1, v2, m-1)
  • divide_triangle(b, v2, v0, m-1)
  • else( triangle(a,b,c) )
  • / draw triangle at end of recursion /

20
display and init Functions
  • void display()
  • glClear(GL_COLOR_BUFFER_BIT)
  • glBegin(GL_TRIANGLES)
  • divide_triangle(v0, v1, v2, n)
  • glEnd()
  • glFlush()
  • void myinit()
  • glMatrixMode(GL_PROJECTION)
  • glLoadIdentity()
  • gluOrtho2D(-2.0, 2.0, -2.0, 2.0)
  • glMatrixMode(GL_MODELVIEW)
  • glClearColor (1.0, 1.0, 1.0,1.0)
  • glColor3f(0.0,0.0,0.0)

21
main Function
  • int main(int argc, char argv)
  • n4
  • glutInit(argc, argv)
  • glutInitDisplayMode(GLUT_SINGLEGLUT_RGB)
  • glutInitWindowSize(500, 500)
  • glutCreateWindow(2D Gasket")
  • glutDisplayFunc(display)
  • myinit()
  • glutMainLoop()

22
3D Applications
  • In OpenGL, two-dimensional applications are a
    special case of three-dimensional graphics
  • Going to 3D
  • Not much changes
  • Use glVertex3( )
  • Have to worry about the order in which polygons
    are drawn or use hidden-surface removal

23
3D Gasket

24
triangle code
  • void triangle( GLfloat a, GLfloat b, GLfloat
    c)
  • glVertex3fv(a)
  • glVertex3fv(b)
  • glVertex3fv(c)

25
subdivision
  • void divide_triangle(GLfloat a, GLfloat b,
  • GLfloat c, int m)
  • GLfloat v13, v23, v33
  • int j
  • if(mgt0)
  • for(j0 jlt3 j) v1j(ajbj)/2
  • for(j0 jlt3 j) v2j(ajcj)/2
  • for(j0 jlt3 j) v3j(bjcj)/2
  • divide_triangle(a, v1, v2, m-1)
  • divide_triangle(c, v2, v3, m-1)
  • divide_triangle(b, v3, v1, m-1)
  • else(triangle(a,b,c))

26
tetrahedron
  • void tetrahedron( int m)
  • glColor3f(1.0,0.0,0.0)
  • divide_triangle(v0, v1, v2, m)
  • glColor3f(0.0,1.0,0.0)
  • divide_triangle(v3, v2, v1, m)
  • glColor3f(0.0,0.0,1.0)
  • divide_triangle(v0, v3, v1, m)
  • glColor3f(0.0,0.0,0.0)
  • divide_triangle(v0, v2, v3, m)

27
Hidden-Surface Removal

get this
want this
28
Hidden-Surface Removal
  • We want to see only those surfaces in front of
    other surfaces

29
Using the z-buffer algorithm
  • The algorithm
  • in main.c
  • in init.c,
  • in the display callback

30
Viewport
  • Simple rendering pipeline
  • The need of viewport transformation

31
World Window and Viewport
  • World window
  • Viewport

32
Viewport
  • Do not have use the entire window for the image
    glViewport(x,y,w,h)
  • Values in pixels (screen coordinates)

33
Window-to-Viewport Mapping
  • (x, y) in world window should be mapped to
    (sx,sy) in viewport proportionally.
  • Mapping is done internally in OpenGL

34
Window-to-Viewport Mapping
  • Every vertex used in window to define an object
    should be mapped to viewport

35
Window-to-Viewport Mapping
36
Example
The window has (W.l, W.r, W.b, W.t) (0, 2.0, 0,
1.0), and the viewport has (V.l, V.r, V.b, V.t)
(40, 400, 60, 300). We compute
37
Resizing without Distortion
  • To prevent from distortion, a viewport with the
    same aspect ratio of world window is required.
  • aspect ratio width / height

glViewport ( 0, 0, W, W/R )
glViewport ( 0, 0, HR, H )
38
Resizing without Distortion
glutReshapeFunc ( myReshape ) // Making a
matched viewport void myReshape ( GLsizei W,
GLsizei H ) //use (global)window aspect
ratio if ( RgtW/H ) setViewport( 0, W, 0, W/R
) else setViewport( 0, HR, 0, H )
39
Use Mapping in OpenGL
//---setWindow --- void setWindow ( GLdouble
left, Gldouble right,
GLdouble bottom, GLdouble top )
glMatrixMode(GL_PROJECTION)
glLoadIdentity() gluOrtho2D(left, right,
bottom, top) //---- setViewport ----- void
setViewport ( GLdouble left, Gldouble right,
GLdouble bottom, GLdouble top )
glViewport(left, bottom, right left,
top - bottom)
40
Example Tiling
  • //set a fixed window
  • setWindow ( 0, 640.0, 0, 440.0 )
  • for (int i0 ilt5 i) //for each column
  • for (int j0 jlt5 j) //for each row
  • //set the next viewport
  • glViewport ( i64, j44, 64, 44 )
  • //draw it again
  • drawPolylineFile( dino.dat )

41
Example Tiling
  • for (int i 0i lt5i)
  • for (int j 0j lt5j)
  • if ((ij)2 0) //if (ij)is even
  • //right-side-up window
  • setWindow(0.0,640.0,0.0,440.0)
  • else
  • //upside-down window
  • setWindow(0.0,640.0,440.0,0.0)
  • //set the next viewport
  • glViewport(i 64,j 44,64,44)
  • drawPolylineFile("dino.dat")//draw it again

42
Zooming and Panning
  • Zooming scale the world window with respect to
    the center of it with viewport fixed.
  • Panning shift the world window with viewport
    fixed.
Write a Comment
User Comments (0)
About PowerShow.com