OpenGL Basics.

Slides:



Advertisements
Similar presentations
SUJAN CHOWDHURY Lecturer, CSE, CUET
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…
Lecture 3 Graphics Pipeline and Graphics Software
Chapter 2: Graphics Programming
Pemrograman OpenGL Dasar
OpenGL Basics Donghui Han. Assignment Grading Visual Studio Glut Files of four types needed: – Source code:.cpp,.h – Executable file:.exe (build in release.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Interactive 3D Graphics and Virtual Reality Introduction to OpenGL concepts Session 2.
CSE 494/598 Intro to Applied Computer Graphics Anshuman Razdan DCST AR's Web Page AR's Web Page
OpenGL (II). How to Draw a 3-D object on Screen?
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.
Development of Interactive 3D Virtual World Applications
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
Write a Simple Program with OpenGL & GLUT. Books and Web Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible.
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.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
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.
2 COEN Computer Graphics I Introductions n Brad Grantham lecturer lab dude n Dave Shreiner lecturer slave driver.
Programming with OpenGL Part 1: Background
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Lecture 3 OpenGL.
Write a Simple Program with OpenGL & GLUT. Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible
 “OpenGL (Open Graphics Library) is a standard specification defining a cross- language cross-platform API for writing applications that produce 2D 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.
Modeling with OpenGL Practice with OpenGL transformations.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
OpenGL Viewing and Modeling Transformation Geb Thomas Adapted from the OpenGL Programming Guidethe OpenGL Programming Guide.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
Projections. Viewports Windows can have separate viewports void glViewport(GLint x, GLint y, GLsizei width, GLsizei height ) x, y - Specify the lower.
Graphics: Conceptual Model
Chap 3 Viewing and Transformation
CSCE 441: Computer Graphics
Introduction to OpenGL Programming
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Chap 2 Write a Simple OpenGL Program. Preparing 1/2 environment : Microsoft Visual C 、 Microsoft Visual C++.Net Also need : GLUT
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
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 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 LAB III.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Basic Program with OpenGL and GLUT
The Human Visual System vs The Pinhole camera
Computer Graphics Lecture 33
“Computer Science is no more about computers than astronomy is about telescopes.” Professor Edsger Dijkstra.
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.
Drawing in the plane 455.
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

OpenGL Basics

OpenGL – Open Graphics Library What it is a s/w interface to graphics h/w mid-level, device-independent, portable graphics subroutine package developed primarily by SGI 2D/3D graphics, lower-level primitives (polygons) does not include low-level I/O management basis for higher-level libraries/toolkits

The camera analogy

OpenGL libraries GL – gl.h – Opengl32.lib Provides basic commands for graphics drawing GLU (OpenGL Utility Library) – glu.h – glu32.lib Uses GL commands for performing compound graphics like viewing orientation and projection specification polygon tessellations, surface rendering etc. GLUT (OpenGL Utility Toolkit) – glut.h – glut.lib is a window system-independent toolkit for user interaction built on top of OpenGL and WGL (Windows) or GLX (Linux). System-specific OpenGl extensions GLX : for X window system (Linux/Unix) WGL: for Windows 95/98/2000/NT AGL : Apple Macintosh system

OpenGL conventions Functions in OpenGL start with gl Most functions use just gl (e.g., glColor()) Functions starting with glu are utility functions (e.g., gluLookAt()) Note that GLU functions can always be composed entirely from core GL functions Functions starting with glut are from the GLUT library.

OpenGL conventions Function names indicate argument type and number Functions ending with f take floats Functions ending with i take ints Functions ending with b take bytes Functions ending with ub take unsigned bytes Functions that end with v take an array. Examples glColor3f() takes 3 floats glColor4fv() takes an array of 4 floats

Graphics Pipeline

Step 1: Modeling Transform Vertices of an object are define in it’s own co-ordinate system (Object Space) A scene is composed of different objects and some times multiple copy of the same object Modeling transforms places all the objects in a world co-ordinate system (World Space) Model Transforms must be specified before specifying the vertices of an object glRotatef(30, 1, 0, 0); glVertex3d(20, 30, 10); Basic Modeling Transforms are: Translate : glTranslate{f|d}(x,y,z) Rotate : glRotate{f|d}(,x,y,z) Scale : glScale{f|d}(x,y,z) Object Space World Space Instantiate & Modeling Transform

Step 2: Viewing Transform A model can be viewed from different angles. Viewing Transform specify following information about the viewer: eye position head up Look at direction Eye Space

Step 2: Viewing Transform void gluLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx, GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy, GLdouble upz);

Step 3: Normalize & Clip Normalize View volume within an unit cube. Remove Primitives that are not in Normalized view volume Eye Space Clipping Space After clipping lighting effects and textures are applied to the primitives

Step 4: Projection Maps 3D-coordinates to 2D-image coordinates Types Clipping Space Image Space Projection Parallel Perspective Projection Types

Step 4: Projection Perspective Projection

Step 4: Projection Perspective Projection

Step 4: Projection Parallel Projection

Step 5: Rasterization Projected image (vertices) in image space has fractional x and y co-ordinates values But raster scan device can only display pixels at integer co-ordinates Image Space Screen Rasterization Some Algorithms: DDA (Digital Differential Analyzer Brasenham’s Algorithm Midpoint Algorithm

Step 6: Viewport Transformation Maps rasterized 2D images onto graphical device Screen Space Device Space

Step 6: Viewport Transformation void glViewport(GLint x, GLint y, GLsizei width, GLsizei height); gluPerspective(fovy, 1.0, near, far); glViewport(0, 0, 400, 400); gluPerspective(fovy, 1.0, near, far); glViewport(0, 0, 400, 200);

Graphics Pipeline Graphics Primitives transform matrix Modeling Object Space transform matrix Modeling Transform World Space Eye, lookat, headup Viewing Transform Eye Space Parallel or Perspective volume Normalize & Clip Clipping Space material, lights, color Shading & Texture Image Space Projection projection matrix viewport location Image on Screen Viewport Transform Device Space Image in Internal Buffer Rasteri- zation Screen Space Scan conversion

Primitives Primitives: Points, Lines & Polygons Each object is specified by a set Vertices Grouped together by glBegin & glEnd glBegin(type) glVertex*( ) … glEnd( ); type can have 10 possible values

Primitive Types Polygon must be: Simple No-holes inside Convex GL_LINE V0 V1 V2 V3 V5 V4 GL_LINE_STRIP V0 V1 V2 V3 V5 V4 GL_LINE_LOOP V0 V1 V2 V3 V5 V4 Polygon must be: Simple No-holes inside Convex Non-convex Complex P1 P2 GL_POINTS V0 V1 V2 V3 V5 V4 GL_POLYGON V0 V1 V2 V3 V4

Primitive Types Order of Vertex rendering 012, 213, 234, 435 GL_TRIANGLE V0 V1 V2 V3 V4 V5 V6 V7 V8 GL_TRIANGLE_STRIP V0 V1 V2 V3 V4 V5 Order of Vertex rendering 012, 213, 234, 435 GL_TRIANGLE_FAN V0 V1 V2 V3 V4 V5 012, 023 , 034, 045 GL_QUAD V0 V1 V2 V3 V4 V5 V6 V7 GL_QUAD_STRIP V0 V1 V2 V3 V4 V5 V6 V7 0132, 2354, 4576

Files Required for GLUT: Configuring OpenGL in Visual C++ Files Required for GLUT: glut32.dll glut.h glut32.lib

Sample Program

Specify Canvas Color Must always remember to clear canvas before drawing glClearColor( r , g , b , α ) specify the color to clear the canvas to should generally set α to be 0 (i. e., fully transparent) this is a state variable, and can be done only once glClear( GL_ COLOR_ BUFFER_ BIT) actually clears the screen glClear clears such as the depth buffer GL_ DEPTH_ BUFFER_ BIT but we’re not using it right now

Redrawing Window void glFlush(void); void glFinish(void); Forces previously issued OpenGL commands to begin execution It returns before the execution ends. glutSwapBuffers() automatically calls glFlush() For single buffer display function should end with this command void glFinish(void); Forces previously issued OpenGL commands to complete This command doesn’t return until all effects from previous commands are fully realized. void glutPostRedisplay(void); Causes the currently registered display function to be called at the next available opportunity.

Initializing GLUT Void glutInit( int argc, char **argv) initialize glut, process command line arguments such as -geometry, -display etc. void glutInitDisplayMode(unsigned int mode) Mode for later glutCreateWindow() call mode is a bit-wised Ored combination of Either GLUT_RGBA or GLUT_INDEX Either GLUT_SINGLE or GLUT_DOUBLE One or more GLUT_DEPTH, GLUT_STENCIL, GLUT_ACCUM buffers default:RGBA & SINGLE

Initializing GLUT void glutInitWindowPosition(int x, int y) Initial location of window void glutInitWindowSize(int width, int height) Initial size of window int glutCreateWindow(char *name) Called after Init, Displaymode, Position and Size calls Window will not appear until glutMainLoop is called Return value is a unique identifier for the window

Each application has its Event driven approach void glutMainLoop(void); enters the GLUT event processing loop. should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered. While (TRUE) { e=getNextEvent(); switch (e) { case (MOUSE_EVENT): call registered MouseFunc break; case (RESIZE_EVENT): call registered ReshapeFunc … } Event Queue Keyboard Callback Mouse Display OS MainLoop Each application has its Own event queue

Callback Functions void glutDisplayFunc(void (*func) (void)) Specifies the function that’s called whenever the window is initially opened The content of the window is needed to be redrawn glutPostRedisplay() is explicitly called. void glutReshapeFunc( void (*func)(int width, int height)); The window is resized or moved The function should perform following tasks Call glViewPort(0,0,width, height); // default behavior Redefine projection matrix to match aspect ratio of image & view port

Callback Functions void glutKeyboardFunc( void (* func)(unsigned int key, int x, int y) ); Specifies the function that’s called whenever a key that generates an ASCII character is pressed. The key callback parameter is the generated ASCII value. The x and y callback parameters indicate the location of the mouse when the key was pressed.

Callback Functions void glutMouseFunc( void (* func)(int button, int state, int x, int y)); Specifies the function that’s called whenever a mouse button is pressed or released. button callback parameter is one of GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON state callback parameter is either GLUT_UP GLUT_DOWN The x and y callback parameters indicate the location of the mouse when the event occurred.

Animation( Motion = Redraw+Swap )

Animation( Motion = Redraw+Swap )

Animation( Motion = Redraw+Swap )

Transformation in OpenGL OpenGL uses 3 stacks to maintain transformation matrices: Model & View transformation matrix stack Projection matrix stack Texture matrix stack You can load, push and pop the stack The top most matrix from each stack is applied to all graphics primitive until it is changed M N Graphics Primitives (P) Output N•M•P Model-View Matrix Stack Projection Matrix Stack

Translation – 2D x’ = x + dx y’ = y + dy

Transformations and OpenGL® Each time an OpenGL transformation M is called the current MODELVIEW matrix C is altered: glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0); Note: v is any vertex placed in rendering pipeline v’ is the transformed vertex from v.

Matrix Operation

Thinking About Transformations There is a World Coordinate System where: All objects are defined Transformations are in World Coordinate space Two Different Views As a Global System Objects moves but coordinates stay the same Think of transformation in reverse order as they appear in code As a Local System Objects moves and coordinates move with it Think of transformation in same order as they appear in code

Order of Transformation T•R glLoadIdentity(); glMultiMatrixf( T); glMultiMatrixf( R); draw_ the_ object( v); v’ = ITRv Global View Rotate Object Then Translate Local View Translate Object Then Rotate Effect is same, but perception is different

Order of Transformation R•T glLoadIdentity(); glMultiMatrixf( R); glMultiMatrixf( T); draw_ the_ object( v); v’ = ITRv Global View Translate Object Then Rotate Local View Rotate Object Then Translate Effect is same, but perception is different

Thank You