CENG477 Introduction to Computer Graphics Introduction to OpenGL, GLUT and GLUI.

Slides:



Advertisements
Similar presentations
SUJAN CHOWDHURY Lecturer, CSE, CUET
Advertisements

Department of nskinfo i-education
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
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
Computer Graphics CSCE 441
Pemrograman OpenGL Dasar
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
Computer Graphics (Fall 2008) COMS 4160, Lecture 9: OpenGL 1
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
Based on slides created by Edward Angel
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
Chapter 03: Graphics Primitives Course web page: Chapter #3.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Computer Graphics Bing-Yu Chen National Taiwan University.
1 Computer Graphics Dr. Amy Zhang Assistant Professor of Computer Science United International College.
CAP4730: Computational Structures in Computer Graphics Introduction to OpenGL.
COS 397 Computer Graphics Assoc. Prof. Svetla Boytcheva AUBG 2013 COS 397 Computer Graphics Practical Session №1 Introduction to OpenGL, GLFW and CG.
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
1 GLUT Callback functions Event-driven: Programs that use windows  Input/Output  Wait until an event happens and then execute some pre-defined functions.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Lecture 3 OpenGL.
Computer Graphics I, Fall 2010 Input and Interaction.
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
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.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
Computer Graphics Bing-Yu Chen National Taiwan University.
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
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
CSCE 441: Computer Graphics
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to Graphics Programming. Graphics API.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
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.
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 2: Complete Programs
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Drawing in the plane 455.
Introduction to OpenGL
Computer Graphics Practical Lesson 1-
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

CENG477 Introduction to Computer Graphics Introduction to OpenGL, GLUT and GLUI

What is OpenGL? “A software interface to graphics hardware” (Low Level) Graphics rendering API  high-quality color images composed of geometric and image primitives  window system independent  operating system independent

OpenGL/GLUT/GLU/GLUI OpenGL is the “core” library that is platform independent. GLUT is an auxiliary library that handles window creation, OS system calls (mouse buttons, movement, keyboard, etc), callbacks. GLU is an auxiliary library that handles a variety of graphics accessory functions such as setting up matrices for specific viewing orientations and projections, etc. GLUI is a GUI manager for GLUT.

OpenGL Basics Graphics Pipeline Rendering  Converting geometric/mathematical object descriptions into frame buffer values OpenGL can render:  Geometric primitives  Bitmaps and Images (Raster primitives)

Graphics Pipeline OpenGL commands specify how each step is performed The graphics pipeline is modeled as a state machine  Each pipeline step has parameters (attributes) to be set

OpenGL as a Renderer Geometric primitives  points, lines and polygons Image Primitives  images and bitmaps  separate pipeline for images and geometry linked through texture mapping Rendering depends on state  colors, materials, light sources, etc.

Preliminaries Header Files #include

GLUT Basics Application Structure  Configure and open window  Initialize OpenGL state  Register input callback functions render resize input: keyboard, mouse, etc.  Enter event processing loop

Sample Program #include void main(int argc, char** argv) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutInitWindowSize( 500,500 ); glutCreateWindow( “Simple” ); init(); glutDisplayFunc( display ); glutKeyboardFunc( key ); glutMainLoop(); }

#include void main(int argc, char** argv) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutInitWindowSize( 500,500 ); glutCreateWindow( “Simple” ); init(); glutDisplayFunc( display ); glutKeyboardFunc( key ); glutMainLoop(); } Specify the display Mode – RGB or color Index, single or double Buffer Sample Program

#include void main(int argc, char** argv) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutInitWindowSize( 500,500 ); glutCreateWindow( “Simple” ); init(); glutDisplayFunc( display ); glutKeyboardFunc( key ); glutMainLoop(); } Create a window Named “simple” with resolution 500 x 500 Sample Program

#include void main(int argc, char** argv) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutInitWindowSize( 500,500 ); glutCreateWindow( “Simple” ); init(); glutDisplayFunc( display ); glutKeyboardFunc( key ); glutMainLoop(); } Your OpenGL initialization code (Optional) Sample Program

#include void main(int argc, char** argv) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode( mode ); glutInitWindowSize( 500,500 ); glutCreateWindow( “Simple” ); init(); glutDisplayFunc( display ); glutKeyboardFunc(key); glutMainLoop(); } Register your call back functions Sample Program

#include int main(int argc, char** argv) { int mode = GLUT_RGB|GLUT_DOUBLE; glutInitDisplayMode(mode); glutInitWindowSize(500,500); glutCreateWindow(“Simple”); init(); glutDisplayFunc(display); glutKeyboardFunc(key); glutMainLoop(); } glutMainLoop() The program goes into an infinite loop waiting for events

OpenGL Initialization Set up whatever state you’re going to use void init( void ) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-10, 10, -10, 10, -10, 20); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /*glEnable( GL_LIGHT0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST );*/ }

GLUT Callback functions Event-driven: Programs that use windows  Input/Output  Wait until an event happens and then execute some pre-defined functions according to the user’s input Events – key press, mouse button press and release, window resize, etc.

GLUT Callback Functions Callback function : Routine to call when an event happens  Window resize or redraw  User input (mouse, keyboard)  Animation (render many frames) “Register” callbacks with GLUT  glutDisplayFunc( my_display );  glutIdleFunc( my_idle_func );  glutKeyboardFunc( my_key_events );  glutMouseFunc ( my_mouse );

Event Queue Event queue Keyboard Mouse Window …. Mouse_callback() { …. { Keypress_callback() { …. { window_callback() { …. { MainLoop()

Rendering Callback Callback function where all our drawing is done glutDisplayFunc( my_display ); void my_display (void ) { glClear( GL_COLOR_BUFFER_BIT ); glBegin( GL_TRIANGLE ); glVertex3fv( v[0] ); glVertex3fv( v[1] ); glVertex3fv( v[2] ); glEnd(); }

Idle Callback Use for animation and continuous update glutIdleFunc( idle ); void idle( void ) { t += dt; glutPostRedisplay(); }

User Input Callbacks Process user input glutKeyboardFunc( my_key_events ); void my_key_events (char key, int x, int y ) { switch ( key ) { case ‘q’ : case ‘Q’ : exit ( EXIT_SUCCESS); break; case ‘r’ : case ‘R’ : rotate = GL_TRUE; break; }

Mouse Callback Captures mouse press and release events glutMouseFunc( my_mouse ); void myMouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { … }

Events in OpenGL EventExampleOpenGL Callback Function KeypressKeyDown KeyUp glutKeyboardFunc MouseleftButtonDown leftButtonUp glutMouseFunc MotionWith mouse press Without glutMotionFunc glutPassiveMotionFunc WindowMoving Resizing glutReshapeFunc SystemIdle Timer glutIdleFunc glutTimerFunc SoftwareWhat to draw glutDisplayFunc

OpenGL Geometric Primitives The geometry is specified by vertices. There are ten primitive types:

OpenGL Command Format

Vertices and Primitives Primitives are specified using glBegin( primType ); … glEnd();  primType determines how vertices are combined GLfloat red, green, blue; Glfloat coords[3]; glBegin( primType ); for ( i = 0; i < nVerts; ++i ) { glColor3f( red, green, blue ); glVertex3fv( coords ); } glEnd();

An Example void drawParallelogram( GLfloat color[] ) { glBegin( GL_QUADS ); glColor3fv( color ); glVertex2f( 0.0, 0.0 ); glVertex2f( 1.0, 0.0 ); glVertex2f( 1.5, ); glVertex2f( 0.5, ); glEnd(); }

Vertices and Primitives Points, GL_POINTS  individual points

Vertices and Primitives Lines, GL_LINES  pairs of vertices interpreted as individual line segments

Vertices and Primitives Line Strip, GL_LINE_STRIP  series of connected line segments

Vertices and Primitives Line Loop, GL_LINE_LOOP  Line strip with a segment added between last and first vertices

Vertices and Primitives Polygon, GL_POLYGON  boundary of a simple, convex polygon

Vertices and Primitives Triangles, GL_TRIANGLES  triples of vertices interpreted as triangles

Vertices and Primitives Triangle Strip, GL_TRIANGLE_STRIP  linked strip of triangles v0 v2 v1 v3 v4 v5 v6 v7

Vertices and Primitives Triangle Fan, GL_TRIANGLE_FAN  linked fan of triangles v0 v1 v2 v3 v4 v5

Vertices and Primitives Quads, GL_QUADS  quadruples of vertices interpreted as four-sided polygons

Vertices and Primitives Quad Strip, GL_QUAD_STRIP  linked strip of quadrilaterals v0 v1 v2 v3 v4 v5 v6 v7

Vertices and Primitives Vertices may be specified in 2D, 3D, or 4D. 2D coordinates are promoted to 3D by assigning a Z value of zero. 4D homogeneous coordinates are reduced to 3D by dividing x, y, and z by the w coordinate (if non-zero).

Vertices and Primitives Between glBegin/ glEnd, those opengl commands are allowed:  glVertex*() : set vertex coordinates  glColor*() : set current color  glIndex*() : set current color index  glNormal*() : set normal vector coordinates  glTexCoord*() : set texture coordinates

Vertices and Primitives  glMultiTexCoord*() : set texture coordinates for multitexturing  glEdgeFlag*() : control drawing of edges  glMaterial*() : set material properties  glArrayElement() : Extract array element data  glCallList(), glCallLists() : execute display list

GLUI GLUI is a GLUT-based C++ user interface library which provides controls such as  buttons,  checkboxes,  radio buttons,  spinners, etc. It is window-system independent, relying on GLUT to handle all system-dependent issues, such as window and mouse management.

GLUI Controls

GLUI - Simple Programming Interface GLUI provides default values for many parameters in the API and there are several ways to create a control: GLUI *glui;... glui->add_checkbox("Click me"); Adds a simple checkbox with the name "Click me" glui->add_checkbox("Click me", &state ); The variable state will now be automatically update to reflect the state of the checkbox (live variable). glui->add_checkbox( "Click me", &state, 17, callback_fn ); Now we have a live variable, plus a callback function will be invoked (and passed the value '17') whenever the checkbox changes state.

Usage for standalone GLUI windows Integrating GLUI with a new or existing GLUT application is very straightforward. The steps are: 1.Add the GLUI library to the link line (e.g., glui32.lib for Windows -lglui in Linux). 2.Include the file "glui.h" in all sources that will use the GLUI library. 3.Create your regular GLUT windows as usual. Make sure to store the window id of your main graphics window, so GLUI windows can later send it redisplay events: int window_id = glutCreateWindow( "Main gfx window" ); 4.Register your GLUT callbacks as usual (except the Idle callback, discussed below).

Usage for standalone GLUI windows 5.Register your GLUT idle callback (if any) with GLUI_Master (a global object which is already declared), to enable GLUI windows to take advantage of idle events without interfering with your application's idle events. If you do not have an idle callback, pass in NULL. GLUI_Master.set_glutIdleFunc( myGlutIdle ); or GLUI_Master.set_glutIdleFunc( NULL ); 6.In your idle callback, explicitly set the current GLUT window before rendering or posting a redisplay event. Otherwise the redisplay may accidently be sent to a GLUI window. void myGlutIdle( void ) { glutSetWindow(main_window); glutPostRedisplay(); }

Usage for standalone GLUI windows 7.Create a new GLUI window using GLUI *glui = GLUI_Master.create_glui( "name", flags, x, y ); Note that flags, x, and y are optional arguments. If they are not specified, default values will be used. GLUI provides default values for arguments whenever possible. 8.Add controls to the GLUI window. For example, we can add a checkbox and a quit button with: glui->add_checkbox( "Lighting", &lighting ); glui->add_button( "Quit", QUIT_ID, callback_func );

Usage for standalone GLUI windows 9.Let each GLUI window you've created know where its main graphics window is: glui->set_main_gfx_window( window_id ); 10.Invoke the standard GLUT main event loop, just as in any GLUT application: glutMainLoop(); Refer GLUI Manual on the usage for GLUI subwindows

What’s Next… Display Lists Viewing Lighting Texture Mapping …

References The OpenGL Programming Guide - The Redbook _1.0/ _1.0/ 3.