Presentation is loading. Please wait.

Presentation is loading. Please wait.

Basic OpenGL programming, Geometric objects & User Interaction

Similar presentations


Presentation on theme: "Basic OpenGL programming, Geometric objects & User Interaction"— Presentation transcript:

1 Basic OpenGL programming, Geometric objects & User Interaction
CSC4820/6820 Computer Graphics Algorithms Ying Zhu Georgia State University Basic OpenGL programming, Geometric objects & User Interaction

2 Outline Graphics primitives in OpenGL
Input devices and user interaction GLUT Programming event-driven input Animating interactive programs

3 Set up OpenGL programming environment
Copy the files: 1. Download freeglut (if you are using VS 2008) or freeglut (if you are using VS 2005) 2. Go to folder "freeglut \VisualStudio2008" and open the "freeglut" solution file in Visual Studio Build the project. Under the newly created "Debug" folder, you'll find freeglut.lib and freeglut.dll.

4 Set up OpenGL programming environment
5. Copy freeglut.dll to "c:\windows\system32" 6. Go to folder "freeglut-2.6.0\include" and copy freeglut.h, freeglut_ext.h, and freeglut_std.h to "C:\Program Files\Microsoft Visual Studio 9.0\VC\include\GL". If the "GL" folder doesn't exist, create it.

5 Set up OpenGL programming environment
Download GLEW from Follow the installation instructions at Include glew.h in your OpenGL code glew.h must be placed before freeglut.h.

6 Set up OpenGL programming environment
Set up Visual Studio 2008: 1. In Visual Studio 2008, create a "Win32 Console Application" In the "Win32 Application Wizard", under "Application Settings", check "Empty project". (The default is "Precompiled header". You don't need it.) 3. After the project is created, right click on the project name and select "Properties". 4. In the "Property Pages" window, click Linker --> Input, and add the following files to the "Additional Dependencies" line: opengl32.lib glu32.lib freeglut.lib (Note that opengl32.lib and glu32.lib come with Visual Studio 2008.)

7 Set up OpenGL programming environment
You can find plenty of OpenGL samples at and Drag and drop any OpenGL programs to the "Source Files" node under the project you created in VS Whenever you see #include <GL/glut.h> change it to #include <GL/freeglut.h>

8 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

9 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

10 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

11 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

12 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

13 Graphics Primitives in OpenGL

14 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};

15 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

16 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 Java + JOGL WxWidgets ( open source, cross-platform GUI API See OpenGL Wiki for a list of tools

17 GLUT window management
GLUT provides some window management functions: glutCreateWindow, glutDestroyWindow glutPostRedisplay glutSwapBuffers glutPositionWindow, glutReshapeWindow glutFullScreen glutShowWindow, glutHideWindow, etc. See for details

18 GLUT menu management GLUT provides simple menu management functions
glutCreateMenu glutSetMenu, glutGetMenu glutDestroyMenu glutAddMenuEntry glutAddSubMenu glutAttachMenu, glutDetachMenu, etc. See for details

19 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);

20 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

21 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

22 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

23 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

24 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

25 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

26 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 for details

27 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

28 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

29 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

30 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

31 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() }

32 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();

33 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(); }

34 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.


Download ppt "Basic OpenGL programming, Geometric objects & User Interaction"

Similar presentations


Ads by Google