Download presentation
Presentation is loading. Please wait.
Published byLoreen Jennings Modified over 6 years ago
1
Advanced Graphics Algorithms Ying Zhu Georgia State University
Lecture 04 Geometric objects & User Interaction
2
Outline Graphics primitives in OpenGL
Input devices and user interaction GLUT Programming event-driven input Animating interactive programs
3
Coordinate systems Each point and vector is defined in reference to a coordinate system Once we fix the origin of the coordinate system, we can represent all points unambiguously The same point may have different 3D coordinates in different coordinate systems Remember that part of the job of 3D graphics pipeline is to convert vertices from model space to 2D window space Each vertex with go through several coordinate systems along the pipeline
4
Coordinate systems A 3D coordinate system is defined by its three base vectors (x, y, and z axis) Get familiar with OpenGL’s coordinate system
5
Graphics primitives In computer graphics, we work with 3D models (geometry objects) 3D objects are described by their surfaces Can be thought of as being hollow Complex 3D objects either are composed of or can be approximated by flat convex polygons Curved surfaces are approximated by flat polygons Polygons can be specified through a set of 3D vertices
6
Graphics primitives The basic graphics primitives are points, lines, and polygons A polygon can be defined by an ordered set of vertices Graphics hardware is optimized for processing points and flat polygons Complex objects are eventually divided into triangular polygons (a process called tessellation) Because triangular polygons are always flat
7
Graphics primitives The basic geometric primitives can be described using three fundamental types: Scalars, points, and vectors Point: a location in the 3D space E.g. vertex, pixel Scalars: real numbers E.g. distance between two points Vector: direction or directed line segment No fixed position in 3D space E.g. surface normal, light direction, camera direction
8
Graphics Primitives In OpenGL, all geometry is specified by stating which type of object and then giving the vertices that define it glBegin(…), glEnd() glVertex[34][fdv] Three or four components (regular or homogeneous) Float, double or vector (eg float[3])
9
Graphics primitives in OpenGL
Example: triangle glBegin(GL_POLYGON); glVertex2f(0.0, 0.0); glVertex2f(0.0, 3.0); glVertex2f(3.0, 3.0); glVertex2f(4.0, 1.5); glVertex2f(3.0, 0.0); glEnd(); Other options: glBegin(GL_POINTS), glBegin(GL_LINES), glBegin(GL_POLYGONS), glBegin(GL_QUADS), etc.
10
Graphics Primitives in OpenGL
11
Graphics primitives in OpenGL
How to represent points and vector in OpenGL? Use array. Example GLfloat normals[][3] = {{0.0, 0.0, -1.0}, {0.0, 1.0, 0.0}, {-1.0, 0.0, 0.0}}; GLfloat light0_pos[4] = {0.9, 0.9, 2.25, 0.0};
12
Vertex Arrays and Display Lists
How to display large numbers of polygons efficiently? Vertex arrays (“Red Book”, Chapter 2) Display lists (“Red Book”, Chapter 7)
13
Vertex Arrays Why use vertex arrays?
Performance concern: OpenGL requires many function calls to render geometric primitives Redundant processing of vertices: many vertices are shared between adjacent polygons Vertex arrays allow you to specify lots of vertex-related data with just a few arrays Use few OpenGL function calls E.g. 20 vertices in a 20 sided polygon: one array and one function call
14
Vertex Arrays There are 3 steps to use vertex arrays
Activate up to 8 arrays Each storing a different type of data: coordinates, normals, colors, texture coordinates, etc. glEnableClientState(…) Specifying data for the arrays Specify a pointer E.g. glVertexPointer(…), glTexCoordPointer(…), etc.
15
Vertex Arrays Draw geometry with the data
Can obtain data from a single array element, from an ordered list of array elements, or from a sequence of array elements E.g. glArrayElement(), glDrawElements(), glDrawArrays(), etc.
16
Vertex Arrays “Red Book”, Example 2-10 glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(3, GL_FLOAT, 0, colors); glVertexPointer(2, GL_INT, 0, vertices); glBegin(GL_TRIANGLES); glArrayElement(2); glArrayElement(3); glArrayElement(5); glEnd(); es/redbook/varray.c
17
Display Lists Originally developed to take advantage of X Window’s client/server model Still useful on PC May improve performance of your OpenGL program A display list stores a group of OpenGL commands so that they can be used repeatedly just by calling the display list name. Display lists are usually optimized and store on graphics card
18
Immediate vs. Retained mode
“Immediate mode” rendering 3D primitives are rendered immediately when they are specified, without storing in a data structure Immediate mode is the default mode in OpenGL Sufficient for small programs “Retained” mode rendering 3D objects are stored in a data structure (e.g. display list) and rendered later Lots of opportunities for performance optimization Display list is a simple form of retained rendering Large scale 3D applications use scene graph
19
Why display lists are efficient?
When you create a display list, the OpenGL commands within are "precompiled“, optimized by graphics driver, and usually stored in the graphics card memory Can reuse the pre-calculated result rather than perform the same operations (e.g. matrix multiplications) repeatedly Get data from graphics memory instead of main memory (similar idea to Texture Object) Therefore, the execution of a display list is generally faster than the execution of the commands contained in it. E.g. A simple application with roughly 36,000 polygons, no texturing or lighting, running on a PIII at 450 Mhz and a GeForce with 32MB DDR: 153 fps (with DL) vs 55 fps (without DL)
20
Display List Example GLuint listName = glGenLists(1); // get a new name glNewList(listName, GL_COMPILE); // generate list glTranslatef(1.5, 0.0, 0.0); glColor3f(1.0, 0.0, 1.0); glBegin(GL_TRIANGLES); glVertex3f(0.0, 0.0, 0.0); … glEnd(); glEndList(); glCallList(listName); // draw one
21
When to use display list?
Encapsulate sequences of transformations (matrix operations) If the transformations don’t change from frame to frame Raster bitmaps and images (e.g. fonts) Lights, materials properties, and lighting models Put material definitions in display lists See teapots.c for example
22
Display list } 3 matrix multiplications per frame Display() { Init() {
… glTranslatef(…); glRotatef(…); Draw_something(); } 3 matrix multiplications per frame Init() { glNewList(1, GL_COMPILE); glTranslatef(…); glRotatef(…); Draw_something(); glEndlist(); } Display() { glCallList(1); … 1 matrix multiplication per frame
23
When not to use display lists?
For texture images, use texture objects instead of display lists Very small lists may not perform well since there are some overhead when executing a list It may not be worth it to use display lists for objects with less than several hundreds of polygons.
24
When not to use display lists?
Remember you cannot change the parameters in a display list once it’s created and its content cannot be read Only use the OpenGL calls with constant parameters in the display list If you use a variable in a display list, then the value at the time of creation is stored. Subsequent changes to that variable won’t affect display list E.g. if you want to use keyboard to translate an object, don’t put that glTranslatef() in display list
25
Vertex Arrays vs. Display Lists
Use vertex arrays if the properties of your geometry primitives will be changed at run time E.g. the color or texture may change from frame to frame Use display lists if the properties of your geometry primitives will NOT be changed at run time You may still move the geometry primitives defined in display lists
26
Vertex Arrays vs. Display Lists
Use vertex arrays for dynamic objects Use display lists of static objects Read the following code examples at examples/redbook/ list.c, torus.c, teapots.c
27
How to develop GUI for 3D applications?
Many game companies develop their own GUI library on Windows Relatively few people use .NET For cross platform GUI GLUT (for very simple GUI), FLTK ( Qt GTK WxWidgets ( open source, cross-platform GUI API Crazy Eddie’s GUI System ( Designed for games, works great with OGRE
28
Examples from Crazy Eddie’s GUI
29
GLUT window management
GLUT provides some window management functions: glutCreateWindow, glutDestroyWindow glutPostRedisplay glutSwapBuffers glutPositionWindow, glutReshapeWindow glutFullScreen glutShowWindow, glutHideWindow, etc. See /spec3.html for details
30
GLUT menu management GLUT provides simple menu management functions
glutCreateMenu glutSetMenu, glutGetMenu glutDestroyMenu glutAddMenuEntry glutAddSubMenu glutAttachMenu, glutDetachMenu, etc. See /spec3.html for details
31
GLUT Menu Example sub_menu = glutCreateMenu(size_menu);
glutAddMenuEntry(“increase square size”, 2); glutAddMenuEntry(“decrease square size”, 3); glutCreateMenu(top_menu); glutAddMenuEntry(“quit”, 1); glutAddSubMenu(“Resize”, sub_menu); glutAttachMenu(GLUT_RIGHT_BUTTON);
32
How to read from input devices?
Keyboard/mouse: use GLUT, DirectX, X Window, etc. Joystick: DirectX, X Window Unconventional input devices: write program that directly talks to device drivers, or use commercial tools
33
Input Modes Input devices contain a trigger which can be used to send a signal to the operating system Button on mouse Pressing or releasing a key When triggered, input devices return information (their measure) to the system Mouse returns position information Keyboard returns ASCII code
34
Request Mode Input provided to program only when user triggers the device Typical of keyboard input Can erase (backspace), edit, correct until enter (return) key (the trigger) is depressed
35
Event Mode Most systems have more than one input device, each of which can be triggered at an arbitrary time by a user Each trigger generates an event whose measure is put in an event queue which can be examined by the user program
36
Event Types Window: resize, expose, iconify
Mouse: click one or more buttons Motion: move mouse Keyboard: press or release a key Idle: nonevent Define what should be done if no other event is in queue
37
Callbacks Programming interface for event-driven input
Define a callback function for each type of event the graphics system recognizes This user-supplied function is executed when the event occurs GLUT example: glutMouseFunc(mymouse) mouse callback function
38
GLUT callbacks GLUT recognizes a subset of the events recognized by any particular window system (Windows, X Window, Macintosh) Can register callback functions to handle those events glutDisplayFunc glutMouseFunc glutReshapeFunc glutKeyFunc glutIdleFunc glutMotionFunc, glutPassiveMotionFunc See spec3.html for details
39
GLUT Event Loop Remember that the last line in main.c for a program using GLUT must be glutMainLoop(); which puts the program in an infinite event loop In each pass through the event loop, GLUT looks at the events in the queue for each event in the queue, GLUT executes the appropriate callback function if one is defined if no callback is defined for the event, the event is ignored
40
The display callback The display callback is executed whenever GLUT determines that the window should be refreshed, for example When the window is first opened When the window is reshaped When a window is exposed When the user program decides it wants to change the display In main.c glutDisplayFunc(mydisplay) identifies the function to be executed Every GLUT program must have a display callback
41
Posting redisplays Many events may invoke the display callback function Can lead to multiple executions of the display callback on a single pass through the event loop We can avoid this problem by instead using glutPostRedisplay(); which sets a flag. GLUT checks to see if the flag is set at the end of the event loop If set then the display callback function is executed
42
Animation = Redraw + Swap
When we redraw the display through the display callback, we usually start by clearing the window with glClear()and then draw the next frame Instead of one color buffer, we use two Front Buffer: one that is displayed but not written to Back Buffer: one that is written to but not altered
43
Animation = Redraw + Swap
Program then requests a double buffer in main.c glutInitDisplayMode(GL_RGB | GL_DOUBLE) At the end of the display callback buffers are swapped void mydisplay() { glClear() /* draw graphics here */ glutSwapBuffers() }
44
Use idle callback for animation
The idle callback is executed whenever there are no events in the event queue glutIdleFunc(myidle) Useful for automatic animations. For example: void myidle() { /* change something */ t += dt glutPostRedisplay(); } Void mydisplay() { glClear(); /* draw something that depends on t */ glutSwapBuffers();
45
Use idle callback for animation
void main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320,320); glutCreateWindow(""); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutReshapeFunc(changeSize); glEnable(GL_DEPTH_TEST); glutMainLoop(); }
46
Use idle callback for animation
// A triangle is automatically rotated void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glRotatef(angle,0.0,1.0,0.0); glBegin(GL_TRIANGLES); glVertex3f(-0.5,-0.5,0.0); glVertex3f(0.5,0.0,0.0); glVertex3f(0.0,0.5,0.0); glEnd(); glPopMatrix(); glutSwapBuffers(); // Increase the angle for the next frame angle++; } See for details.
47
A Simple OpenGL Program (1)
#include <GL/glut.h> void display(void) { /* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); /* draw white polygon (rectangle) with corners at * (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ glColor3f (1.0, 1.0, 1.0); glBegin(GL_POLYGON); glVertex3f (0.25, 0.25, 0.0); glVertex3f (0.75, 0.25, 0.0); glVertex3f (0.75, 0.75, 0.0); glVertex3f (0.25, 0.75, 0.0); glEnd();
48
A Simple OpenGL Program (2)
/* start processing buffered OpenGL routines */ glFlush (); } void init (void) { /* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0); /* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
49
A Simple OpenGL Program (3)
/* Declare initial window size, position, and display mode (single buffer and RGBA). Open window with "hello" in its title bar. Call initialization routines. Register callback function to display graphics. Enter main loop and process events. */ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100); glutCreateWindow ("hello"); init (); glutDisplayFunc(display); glutMainLoop(); return 0; /* ANSI C requires main to return int. */ }
50
Readings OpenGL Programming Guide OpenGL Programming Guide v1.1
Chapter 2 and 7 OpenGL Programming Guide v1.1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.