Download presentation
Presentation is loading. Please wait.
Published byStewart Lucas Modified over 8 years ago
1
UBI 516 Advanced Computer Graphics Two Dimensional Viewing ( Chapter 6 ) Aydın Öztürk ozturk@ube.ege.edu.tr http://www.ube.ege.edu.tr/~ozturk Two Dimensional Viewing ( Chapter 6 ) Aydın Öztürk ozturk@ube.ege.edu.tr http://www.ube.ege.edu.tr/~ozturk
2
Two Dimensional Viewing Any Cartesian coordinate system can be used to define a picture. Any Cartesian coordinate system can be used to define a picture. A view is selected by specifying a subarea of the total picture area. A view is selected by specifying a subarea of the total picture area. The picture parts within the selected areas are mapped onto specific areas of the device coordinates. The picture parts within the selected areas are mapped onto specific areas of the device coordinates. Any Cartesian coordinate system can be used to define a picture. Any Cartesian coordinate system can be used to define a picture. A view is selected by specifying a subarea of the total picture area. A view is selected by specifying a subarea of the total picture area. The picture parts within the selected areas are mapped onto specific areas of the device coordinates. The picture parts within the selected areas are mapped onto specific areas of the device coordinates.
3
Two Dimensional Viewing A world coordinate area selected for display is called a window. A world coordinate area selected for display is called a window. The window defines what is to be viewed. The window defines what is to be viewed. An area on a display device to which a window is mapped is called a viewport. An area on a display device to which a window is mapped is called a viewport. The viewport defines where it is to be displayed. The viewport defines where it is to be displayed. A world coordinate area selected for display is called a window. A world coordinate area selected for display is called a window. The window defines what is to be viewed. The window defines what is to be viewed. An area on a display device to which a window is mapped is called a viewport. An area on a display device to which a window is mapped is called a viewport. The viewport defines where it is to be displayed. The viewport defines where it is to be displayed.
4
Two Dimensional Viewing The mapping of part of a world- coordinate scene to device coordinates is referred to as viewing transformation The mapping of part of a world- coordinate scene to device coordinates is referred to as viewing transformation
5
Two Dimensional Viewing World Coordinates Viewing Coordinates
6
Viewing coordinate reference system X-world Y-world X-view Y-view
7
Window-to-viewport coordinate transformation X-viewport Y-viewport Y-view X-view Window Viewport Y-world X-world 0 1 1
8
2D viewing transformation pipeline Construct world coordinates Convert world coordinates to viewing coordinates Map viewing coordinates to normalized viewing coordinates Map Normalized viewport to device coordinates
9
Next Topic: Clipping We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport In general, this assumption will not hold: In general, this assumption will not hold: We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport We’ve been assuming that all primitives (lines, triangles, polygons) lie entirely within the viewport In general, this assumption will not hold: In general, this assumption will not hold:
10
Clipping Analytically calculating the portions of primitives within the viewport
11
Why illuminate before clipping? Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay Model World World Camera
12
Why World Camera before clipping? Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay Model World World Camera
13
Clip to what? View Window Eye position (focal point) Right Back Towards Up Viewing Frustum
14
Remind me why I care again Transform Illuminate Transform Clip Project Rasterize Model & Camera Parameters Rendering Pipeline FramebufferDisplay Model World World Camera
15
Why Clip? Bad idea to rasterize outside of framebuffer bounds Bad idea to rasterize outside of framebuffer bounds Also, don’t waste time scan converting pixels outside window Also, don’t waste time scan converting pixels outside window Bad idea to rasterize outside of framebuffer bounds Bad idea to rasterize outside of framebuffer bounds Also, don’t waste time scan converting pixels outside window Also, don’t waste time scan converting pixels outside window
16
Clipping The naïve approach to clipping lines: for each line segment for each edge of view_window for each edge of view_window find intersection point find intersection point pick “nearest” point pick “nearest” point if anything is left, draw it if anything is left, draw it What do we mean by “nearest”? How can we optimize this? The naïve approach to clipping lines: for each line segment for each edge of view_window for each edge of view_window find intersection point find intersection point pick “nearest” point pick “nearest” point if anything is left, draw it if anything is left, draw it What do we mean by “nearest”? How can we optimize this? A B C D
17
Trivial Accepts Big optimization: trivial accept/rejects Big optimization: trivial accept/rejects How can we quickly determine whether a line segment is entirely inside the viewport? How can we quickly determine whether a line segment is entirely inside the viewport? A: test both endpoints. A: test both endpoints. Big optimization: trivial accept/rejects Big optimization: trivial accept/rejects How can we quickly determine whether a line segment is entirely inside the viewport? How can we quickly determine whether a line segment is entirely inside the viewport? A: test both endpoints. A: test both endpoints.
18
Trivial Rejects How can we know a line is outside viewport? How can we know a line is outside viewport? A: if both endpoints on wrong side of same edge, can trivially reject line A: if both endpoints on wrong side of same edge, can trivially reject line How can we know a line is outside viewport? How can we know a line is outside viewport? A: if both endpoints on wrong side of same edge, can trivially reject line A: if both endpoints on wrong side of same edge, can trivially reject line
19
Clipping Lines To Viewport Discard segments of lines outside viewport Discard segments of lines outside viewport Trivially accept lines with both endpoints inside all edges of the viewport Trivially accept lines with both endpoints inside all edges of the viewport Trivially reject lines with both endpoints outside the same edge of the viewport Trivially reject lines with both endpoints outside the same edge of the viewport Otherwise, reduce to trivial cases by splitting into two segments Otherwise, reduce to trivial cases by splitting into two segments Discard segments of lines outside viewport Discard segments of lines outside viewport Trivially accept lines with both endpoints inside all edges of the viewport Trivially accept lines with both endpoints inside all edges of the viewport Trivially reject lines with both endpoints outside the same edge of the viewport Trivially reject lines with both endpoints outside the same edge of the viewport Otherwise, reduce to trivial cases by splitting into two segments Otherwise, reduce to trivial cases by splitting into two segments
20
Cohen-Sutherland Line Clipping Divide viewplane into regions defined by viewport edges Divide viewplane into regions defined by viewport edges Assign each region a 4-bit outcode: Assign each region a 4-bit outcode: Divide viewplane into regions defined by viewport edges Divide viewplane into regions defined by viewport edges Assign each region a 4-bit outcode: Assign each region a 4-bit outcode: 000000100001 1001 01010100 10001010 0110 y max x max
21
Cohen-Sutherland Line Clipping Assign an outcode to each vertex of line If both outcodes = 0, trivial accept If both outcodes = 0, trivial accept bitwise AND vertex outcodes together bitwise AND vertex outcodes together If result 0, trivial reject If result 0, trivial reject Assign an outcode to each vertex of line If both outcodes = 0, trivial accept If both outcodes = 0, trivial accept bitwise AND vertex outcodes together bitwise AND vertex outcodes together If result 0, trivial reject If result 0, trivial reject
22
Cohen-Sutherland Line Clipping If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded Pick an edge that the line crosses Pick an edge that the line crosses Intersect line with edge Intersect line with edge Discard portion on wrong side of edge and assign outcode to new vertex Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded Pick an edge that the line crosses Pick an edge that the line crosses Intersect line with edge Intersect line with edge Discard portion on wrong side of edge and assign outcode to new vertex Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests; repeat if necessary Apply trivial accept/reject tests; repeat if necessary
23
Cohen-Sutherland Line Clipping If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded Pick an edge that the line crosses Pick an edge that the line crosses Check against edges in same order each time Check against edges in same order each time –For example: top, bottom, right, left If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded If line cannot be trivially accepted or rejected, subdivide so that one or both segments can be discarded Pick an edge that the line crosses Pick an edge that the line crosses Check against edges in same order each time Check against edges in same order each time –For example: top, bottom, right, left A B D E C
24
Cohen-Sutherland Line Clipping Intersect line with edge Intersect line with edge A B D E C
25
Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests and repeat if necessary Discard portion on wrong side of edge and assign outcode to new vertex Apply trivial accept/reject tests and repeat if necessary Cohen-Sutherland Line Clipping A B D C
26
Solving Simultaneous Equations Equation of a line Slope-intercept (explicit equation): y = mx + b Slope-intercept (explicit equation): y = mx + b Implicit Equation: Ax + By + C = 0 Implicit Equation: Ax + By + C = 0 Parametric Equation: Line defined by two points, P 0 and P 1 Parametric Equation: Line defined by two points, P 0 and P 1 –P(t) = P 0 + (P 1 - P 0 ) t, where P is a vector [x, y] T –x(t) = x 0 + (x 1 - x 0 ) t –y(t) = y 0 + (y 1 - y 0 ) t Equation of a line Slope-intercept (explicit equation): y = mx + b Slope-intercept (explicit equation): y = mx + b Implicit Equation: Ax + By + C = 0 Implicit Equation: Ax + By + C = 0 Parametric Equation: Line defined by two points, P 0 and P 1 Parametric Equation: Line defined by two points, P 0 and P 1 –P(t) = P 0 + (P 1 - P 0 ) t, where P is a vector [x, y] T –x(t) = x 0 + (x 1 - x 0 ) t –y(t) = y 0 + (y 1 - y 0 ) t
27
Parametric Line Equation Describes a finite line Describes a finite line Works with vertical lines (like the viewport) Works with vertical lines (like the viewport) 0 ≤ t ≤ 1 0 ≤ t ≤ 1 –Defines line between P 0 and P 1 t < 0 t < 0 –Defines line before P 0 t > 1 t > 1 –Defines line after P 1 Describes a finite line Describes a finite line Works with vertical lines (like the viewport) Works with vertical lines (like the viewport) 0 ≤ t ≤ 1 0 ≤ t ≤ 1 –Defines line between P 0 and P 1 t < 0 t < 0 –Defines line before P 0 t > 1 t > 1 –Defines line after P 1
28
Parametric Lines and Clipping Define each line in parametric form: Define each line in parametric form: P 0 (t)…P n-1 (t) P 0 (t)…P n-1 (t) Define each edge of viewport in parametric form: Define each edge of viewport in parametric form: P L (t), P R (t), P T (t), P B (t) P L (t), P R (t), P T (t), P B (t) When using Cohen-Sutherland perform intersection calculation for appropriate viewport edge and line When using Cohen-Sutherland perform intersection calculation for appropriate viewport edge and line Define each line in parametric form: Define each line in parametric form: P 0 (t)…P n-1 (t) P 0 (t)…P n-1 (t) Define each edge of viewport in parametric form: Define each edge of viewport in parametric form: P L (t), P R (t), P T (t), P B (t) P L (t), P R (t), P T (t), P B (t) When using Cohen-Sutherland perform intersection calculation for appropriate viewport edge and line When using Cohen-Sutherland perform intersection calculation for appropriate viewport edge and line
29
Computing Intersections (Cyrus-Beck Algorithm) ● ● ● ● ● ● Edge E i PEiPEi P i (t)-PEiPEi P1P1 P0P0 N i ●[P(t) - P E i ] < 0 N i ●[P(t) - P E i ] = 0 N i ●[P(t) - P E i ] > 0 NiNi
30
Computing Intersections(cont.) The parametric equation : We have :
31
Example Edge E i P E i = (2,2) P 1 = ( 6, 5 ) P 0 = ( 0, 2 ) NiNi 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6
32
Clipping Polygons Clipping polygons is more complex than clipping the individual lines Clipping polygons is more complex than clipping the individual lines Input: polygon Input: polygon Output: original polygon, new polygon, or nothing Output: original polygon, new polygon, or nothing When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon? When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon? Clipping polygons is more complex than clipping the individual lines Clipping polygons is more complex than clipping the individual lines Input: polygon Input: polygon Output: original polygon, new polygon, or nothing Output: original polygon, new polygon, or nothing When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon? When can we trivially accept/reject a polygon as opposed to the line segments that make up the polygon?
33
What happens to a triangle during clipping? What happens to a triangle during clipping? Possible outcomes: Possible outcomes: What happens to a triangle during clipping? What happens to a triangle during clipping? Possible outcomes: Possible outcomes: triangle triangle Why Is Clipping Hard? triangle quad triangle 5-gon
34
A really tough case: Why Is Clipping Hard? concave polygon multiple polygons
35
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation
36
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
37
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
38
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
39
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
40
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
41
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
42
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
43
Sutherland-Hodgeman Clipping Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
44
Sutherland-Hodgeman Clipping: The Algorithm Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped Basic idea: Consider each edge of the viewport individually Consider each edge of the viewport individually Clip the polygon against the edge equation Clip the polygon against the edge equation After doing all planes, the polygon is fully clipped After doing all planes, the polygon is fully clipped
45
Sutherland-Hodgeman Clipping Input/output for algorithm: Input/output for algorithm: Input: list of polygon vertices in order Input: list of polygon vertices in order Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe) Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe) Note: this is exactly what we expect from the clipping operation against each edge Note: this is exactly what we expect from the clipping operation against each edge Input/output for algorithm: Input/output for algorithm: Input: list of polygon vertices in order Input: list of polygon vertices in order Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe) Output: list of clipped poygon vertices consisting of old vertices (maybe) and new vertices (maybe) Note: this is exactly what we expect from the clipping operation against each edge Note: this is exactly what we expect from the clipping operation against each edge
46
Sutherland-Hodgeman Clipping Sutherland-Hodgman basic routine: Go around polygon one vertex at a time Go around polygon one vertex at a time Current vertex has position p Current vertex has position p Previous vertex had position s, and it has been added to the output if appropriate Previous vertex had position s, and it has been added to the output if appropriate Sutherland-Hodgman basic routine: Go around polygon one vertex at a time Go around polygon one vertex at a time Current vertex has position p Current vertex has position p Previous vertex had position s, and it has been added to the output if appropriate Previous vertex had position s, and it has been added to the output if appropriate
47
Sutherland-Hodgeman Clipping Edge from s to p takes one of four cases: (Purple line can be a line or a plane) Edge from s to p takes one of four cases: (Purple line can be a line or a plane) insideoutside s p p output insideoutside s p no output insideoutside s p i output insideoutside s p i output p output
48
Sutherland-Hodgeman Clipping Four cases: 1.s inside plane and p inside plane –Add p to output –Note: s has already been added 3.s outside plane and p outside plane –Add nothing Four cases: 1.s inside plane and p inside plane –Add p to output –Note: s has already been added 3.s outside plane and p outside plane –Add nothing 2.s inside plane and p outside plane –Find intersection point i –Add i to output 4.s outside plane and p inside plane –Find intersection point i –Add i to output, followed by p 2.s inside plane and p outside plane –Find intersection point i –Add i to output 4.s outside plane and p inside plane –Find intersection point i –Add i to output, followed by p
49
Point-to-Plane test A very general test to determine if a point p is “inside” a plane P, defined by q and n: (p - q) n < 0: p inside PRemember: (p - q) n = 0: p on Pp n = |p| |n| cos ( ) (p - q) n > 0: p outside P = angle between p and n A very general test to determine if a point p is “inside” a plane P, defined by q and n: (p - q) n < 0: p inside PRemember: (p - q) n = 0: p on Pp n = |p| |n| cos ( ) (p - q) n > 0: p outside P = angle between p and n P n p q P n p q P n p q
50
Finding Line-Plane Intersections Use parametric definition of edge: L(t) = L 0 + (L 1 - L 0 )t If t = 0 then L(t) = L 0 If t = 0 then L(t) = L 0 If t = 1 then L(t) = L 1 If t = 1 then L(t) = L 1 Otherwise, L(t) is part way from L 0 to L 1 Otherwise, L(t) is part way from L 0 to L 1 Use parametric definition of edge: L(t) = L 0 + (L 1 - L 0 )t If t = 0 then L(t) = L 0 If t = 0 then L(t) = L 0 If t = 1 then L(t) = L 1 If t = 1 then L(t) = L 1 Otherwise, L(t) is part way from L 0 to L 1 Otherwise, L(t) is part way from L 0 to L 1
51
Finding Line-Plane Intersections Edge intersects plane P where E(t) is on P q is a point on P q is a point on P n is normal to P n is normal to P (L(t) - q) n = 0 t = [(q - L 0 ) n] / [(L 1 - L 0 ) n] The intersection point i = L(t) for this value of t The intersection point i = L(t) for this value of t Edge intersects plane P where E(t) is on P q is a point on P q is a point on P n is normal to P n is normal to P (L(t) - q) n = 0 t = [(q - L 0 ) n] / [(L 1 - L 0 ) n] The intersection point i = L(t) for this value of t The intersection point i = L(t) for this value of t
52
Line-Plane Intersections Note that the length of n doesn’t affect result: Note that the length of n doesn’t affect result: Again, lots of opportunity for optimization Again, lots of opportunity for optimization Note that the length of n doesn’t affect result: Note that the length of n doesn’t affect result: Again, lots of opportunity for optimization Again, lots of opportunity for optimization
53
Two Dimensional Viewing in OpenGL
54
Libraries OpenGL : Viewport functions, OpenGL : Viewport functions, GLU : Two-Dimensional clipping, GLU : Two-Dimensional clipping, GLUT : Handling display windows. GLUT : Handling display windows. OpenGL : Viewport functions, OpenGL : Viewport functions, GLU : Two-Dimensional clipping, GLU : Two-Dimensional clipping, GLUT : Handling display windows. GLUT : Handling display windows.
55
OpenGL Projection Mode : We must first select the projection matrix mode before any projection setting : We must first select the projection matrix mode before any projection setting : glMatrixMode ( GL_PROJECTION ); glLoadIdentity ( ); glMatrixMode ( GL_PROJECTION ); glLoadIdentity ( ); Remember: GL_MODELVIEW was the matrix mode for geometric transformations Remember: GL_MODELVIEW was the matrix mode for geometric transformations We must first select the projection matrix mode before any projection setting : We must first select the projection matrix mode before any projection setting : glMatrixMode ( GL_PROJECTION ); glLoadIdentity ( ); glMatrixMode ( GL_PROJECTION ); glLoadIdentity ( ); Remember: GL_MODELVIEW was the matrix mode for geometric transformations Remember: GL_MODELVIEW was the matrix mode for geometric transformations
56
GLU Clipping-Window Function For 2D Clipping : gluOrtho2D (xmin, xmax, ymin, ymax ); gluOrtho2D (xmin, xmax, ymin, ymax ); For 2D Clipping : gluOrtho2D (xmin, xmax, ymin, ymax ); gluOrtho2D (xmin, xmax, ymin, ymax ); World Coordinates
57
OpenGL Viewport Function For 2D Clipping : glViewport ( xmin, ymin, width, height ); glViewport ( xmin, ymin, width, height ); For 2D Clipping : glViewport ( xmin, ymin, width, height ); glViewport ( xmin, ymin, width, height ); World Coordinates
58
Creating a GLUT Display Window Remember homework-1 glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glClearColor ( red, green, blue, alpha ); glClearColor ( red, green, blue, alpha ); Remember homework-1 glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glClearColor ( red, green, blue, alpha ); glClearColor ( red, green, blue, alpha );
59
GLUT Display-Window Identifier We can use window-id to handle the window glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); winId=glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); winId=glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutDestroyWindow ( winId ); glutDestroyWindow ( winId ); glutSetWindow ( winId ); glutSetWindow ( winId ); current_winId = glutGetWindow ( ); current_winId = glutGetWindow ( ); We can use window-id to handle the window glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); winId=glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); winId=glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutDestroyWindow ( winId ); glutDestroyWindow ( winId ); glutSetWindow ( winId ); glutSetWindow ( winId ); current_winId = glutGetWindow ( ); current_winId = glutGetWindow ( );
60
Relocating and Resizing a GLUT Display Window In the main( ) : glutInit ( &argc, argv ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInitDisplayPosition ( xpos, ypos ); glutReshapeFunc( myReshape ); We can expand the window to fiil the screen : glutFullScreen ( ); glutInit ( &argc, argv ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInitDisplayPosition ( xpos, ypos ); glutReshapeFunc( myReshape ); We can expand the window to fiil the screen : glutFullScreen ( ); In the function define section : void myReshape( int width, int height ); void myReshape( int width, int height ); In the main( ) : glutInit ( &argc, argv ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInitDisplayPosition ( xpos, ypos ); glutReshapeFunc( myReshape ); We can expand the window to fiil the screen : glutFullScreen ( ); glutInit ( &argc, argv ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutInitDisplayPosition ( xpos, ypos ); glutReshapeFunc( myReshape ); We can expand the window to fiil the screen : glutFullScreen ( ); In the function define section : void myReshape( int width, int height ); void myReshape( int width, int height );
61
Managing Multiple GLUT Display Windows Minimize window glutSetWindow ( windowId );glutIconifyWindow ( ); or glutHideWindow ( ); glutSetWindow ( windowId );glutIconifyWindow ( ); or glutHideWindow ( ); Restore window : glutSetWindow ( windowId );glutShowWindow ( ); glutSetWindow ( windowId );glutShowWindow ( ); Swap windows : glutSetWindow ( windowId );glutPushWindow ( ); glutSetWindow ( windowId );glutPushWindow ( ); glutSetWindow ( windowId );glutPopWindow ( ); glutSetWindow ( windowId );glutPopWindow ( ); Minimize window glutSetWindow ( windowId );glutIconifyWindow ( ); or glutHideWindow ( ); glutSetWindow ( windowId );glutIconifyWindow ( ); or glutHideWindow ( ); Restore window : glutSetWindow ( windowId );glutShowWindow ( ); glutSetWindow ( windowId );glutShowWindow ( ); Swap windows : glutSetWindow ( windowId );glutPushWindow ( ); glutSetWindow ( windowId );glutPushWindow ( ); glutSetWindow ( windowId );glutPopWindow ( ); glutSetWindow ( windowId );glutPopWindow ( );
62
Other GLUT Functions Sub-windows Sub-windows glutSubWindow( windowId, xpos, ypos, width, height ); // windowId = 1,2,3,... glutSubWindow( windowId, xpos, ypos, width, height ); // windowId = 1,2,3,... Selecting cursor type Selecting cursor type glutSetCursor ( shape ); glutSetCursor ( shape ); –Shapes : GLUT_CURSOR_UP_DOWN GLUT_CURSOR_CYCLE GLUT_CURSOR_WAIT GLUT_CURSOR_DESTROY Sub-windows Sub-windows glutSubWindow( windowId, xpos, ypos, width, height ); // windowId = 1,2,3,... glutSubWindow( windowId, xpos, ypos, width, height ); // windowId = 1,2,3,... Selecting cursor type Selecting cursor type glutSetCursor ( shape ); glutSetCursor ( shape ); –Shapes : GLUT_CURSOR_UP_DOWN GLUT_CURSOR_CYCLE GLUT_CURSOR_WAIT GLUT_CURSOR_DESTROY
63
Viewing Objects in a GLUT Display Window In the main() : glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutDisplayFunc( myDisplay ); glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutDisplayFunc( myDisplay ); In Display Function : glutSwapBuffers ( ); // or glutFlush ( ); for display mode = GL_SINGLE glutSwapBuffers ( ); // or glutFlush ( ); for display mode = GL_SINGLE Calling display function to repaint the screen : glutPostRedisplay ( ); glutPostRedisplay ( ); In the main() : glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutDisplayFunc( myDisplay ); glutInit ( &argc, argv ); glutInitDisplayPosition ( xpos, ypos ); glutInitWindowSize ( width, height ); glutCreateWindow ( “Homework-1” ); glutInitDisplayMode ( GL_RGB | GL_DOUBLE ); glutDisplayFunc( myDisplay ); In Display Function : glutSwapBuffers ( ); // or glutFlush ( ); for display mode = GL_SINGLE glutSwapBuffers ( ); // or glutFlush ( ); for display mode = GL_SINGLE Calling display function to repaint the screen : glutPostRedisplay ( ); glutPostRedisplay ( );
64
Other GLUT Functions For animated re-displaying For animated re-displaying glutIdleFunction( myIdle ); // See Pong Hw glutIdleFunction( myIdle ); // See Pong Hw Getting information about system Getting information about system glutGet ( param ); glutGet ( param ); –Information parameters : GLUT_WINDOW_WIDTH, GLUT_WINDOW_HEIGHT, etc. For animated re-displaying For animated re-displaying glutIdleFunction( myIdle ); // See Pong Hw glutIdleFunction( myIdle ); // See Pong Hw Getting information about system Getting information about system glutGet ( param ); glutGet ( param ); –Information parameters : GLUT_WINDOW_WIDTH, GLUT_WINDOW_HEIGHT, etc.
65
Using GLUT Menus: Example [1/2] The following is called in function init. The following is called in function init. void make_main_menu() { glutCreateMenu(handle_main_menu); glutAddMenuEntry("Toggle eye color", 1); glutAddMenuEntry("Toggle mouth motion", 2); glutAddMenuEntry("Quit", 3); glutAttachMenu(GLUT_RIGHT_BUTTON); } Value passed to handler callback can be the same for multiple entries. Value passed to handler callback can be the same for multiple entries. The following is called in function init. The following is called in function init. void make_main_menu() { glutCreateMenu(handle_main_menu); glutAddMenuEntry("Toggle eye color", 1); glutAddMenuEntry("Toggle mouth motion", 2); glutAddMenuEntry("Quit", 3); glutAttachMenu(GLUT_RIGHT_BUTTON); } Value passed to handler callback can be the same for multiple entries. Value passed to handler callback can be the same for multiple entries.
66
Using GLUT Menus: Example [2/2] Here is part of the handler callback function Here is part of the handler callback function void handle_main_menu(int value) { switch (value) { case 1: // Toggle eye color blueeyes = !blueeyes; glutPostRedisplay(); break; … case 3: // Quit exit(0); break; case 3: // Quit exit(0); break; Menu handlers, like keyboard functions, typically consist of one switch statement. Menu handlers, like keyboard functions, typically consist of one switch statement. Here is part of the handler callback function Here is part of the handler callback function void handle_main_menu(int value) { switch (value) { case 1: // Toggle eye color blueeyes = !blueeyes; glutPostRedisplay(); break; … case 3: // Quit exit(0); break; case 3: // Quit exit(0); break; Menu handlers, like keyboard functions, typically consist of one switch statement. Menu handlers, like keyboard functions, typically consist of one switch statement.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.