Basic OpenGL programming, Geometric objects & User Interaction

Slides:



Advertisements
Similar presentations
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Chapter 2: Graphics Programming
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
InteractionHofstra University1 Graphics Programming Input and Interaction.
InteractionHofstra University1 Graphics Programming Input and Interaction.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
Based on slides created by Edward Angel
CSC461 Lecture 9: GLUT Callbacks Objectives Introduce double buffering for smooth animations Programming event input with GLUT.
Reference1. [OpenGL course slides by Rasmus Stenholt]
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
Fundamentals of Computer Graphics Part 3 prof.ing.Václav Skala, CSc. University of West Bohemia Plzeň, Czech Republic ©2002 Prepared with Angel,E.: Interactive.
1Computer Graphics Input and Interaction Lecture 8 John Shearer Culture Lab – space 2
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 3.
Lecture 3 OpenGL.
Computer Graphics I, Fall 2010 Input and Interaction.
CS 480/680 Computer Graphics Programming with Open GL Part 7: Input and Interaction Dr. Frederick C Harris, Jr. Fall 2011.
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
1 Input and Interaction. 2 Objectives Introduce the basic input devices ­Physical Devices ­Logical Devices ­Input Modes Event-driven input Introduce double.
University of New Mexico
NoufNaief.net TA: Nouf Al-harbi.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Input and Interaction Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
Introduction to Graphics Programming. Graphics API.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics -practical- Lecture 6. (visual c++) open gl library To use open GL with VC++ we add these files:- 1)Glut.h C:\program files\ Microsoft.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Review GLUT Callback Functions
CSC8820 Advanced Graphics Algorithms
Programming with OpenGL Part 1: Background
Working with Callbacks
Computer Graphics Lecture 33
CS5500 Computer Graphics March 2, 2006.
CSC461 Lecture 8: Input Devices
Programming with OpenGL Part 2: Complete Programs
Advanced Graphics Algorithms Ying Zhu Georgia State University
Introduction to Computer Graphics with WebGL
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Starting to draw dealing with Windows which libraries? clipping
Advanced Menuing, Introduction to Picking
גרפיקה ממוחשבת: מבוא ל-OpenGL
Input and Interactions
Working with Callbacks
Programming with OpenGL Part 1: Background
Introduction to OpenGL
Isaac Gang University of Mary Hardin-Baylor
Fundamentals of Computer Graphics Part 3
Input and Interaction Ed Angel
University of New Mexico
Input and Interaction Ed Angel
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Starting to draw dealing with Windows which libraries? clipping
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Input and Interaction Ed Angel Professor Emeritus of Computer Science,
Presentation transcript:

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

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

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

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.

Set up OpenGL programming environment Download GLEW from http://glew.sourceforge.net/ Follow the installation instructions at http://glew.sourceforge.net/install.html Include glew.h in your OpenGL code glew.h must be placed before freeglut.h.

Set up OpenGL programming environment Set up Visual Studio 2008: 1. In Visual Studio 2008, create a "Win32 Console Application". 2. 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.)

Set up OpenGL programming environment You can find plenty of OpenGL samples at http://www.opengl.org/resources/code/samples/redbook/ and http://www.opengl.org/code/category/C19. Drag and drop any OpenGL programs to the "Source Files" node under the project you created in VS 2008. Whenever you see #include <GL/glut.h> change it to #include <GL/freeglut.h>

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

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

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

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

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

Graphics Primitives in OpenGL

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

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

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 (www.fltk.org) Qt Java + JOGL WxWidgets (http://www.wxwidgets.org/), open source, cross-platform GUI API See OpenGL Wiki for a list of tools

GLUT window management GLUT provides some window management functions: glutCreateWindow, glutDestroyWindow glutPostRedisplay glutSwapBuffers glutPositionWindow, glutReshapeWindow glutFullScreen glutShowWindow, glutHideWindow, etc. See http://freeglut.sourceforge.net/ for details

GLUT menu management GLUT provides simple menu management functions glutCreateMenu glutSetMenu, glutGetMenu glutDestroyMenu glutAddMenuEntry glutAddSubMenu glutAttachMenu, glutDetachMenu, etc. See http://freeglut.sourceforge.net/ for details

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

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

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

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

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

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

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

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 http://freeglut.sourceforge.net/ for details

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

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

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

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

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

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

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

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 http://www.lighthouse3d.com/opengl/glut/index.php3?4 for details.