Introduction to OpenGL

Slides:



Advertisements
Similar presentations
Programming with OpenGL - Getting started - Hanyang University Han Jae-Hyek.
Advertisements

Chapter 2: Graphics Programming
Computer Graphics CSCE 441
David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
Drawing Geometric Objects
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
1 Lecture 4 Graphical primitives Rasterization: algorithmic approach Rasterization: geometric approach 2D discrete lines, triangles Discrete planes 3D.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
Introduction to OpenGL M. Ramanathan STTP CAD 2011Introduction to OpenGL.
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.
Programming in OpenGL Ryan Holmes CSE 570 February 12 th, 2003.
Lecture 5 - Programming with OpenGL
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.
Using OpenGL. 2 What is OpenGL? A software interface to graphics hardware It is a Graphics Rendering API (Application Programmer’s Interface) that is.
Modeling. Geometric Models  2D and 3D objects  Triangles, quadrilaterals, polygons  Spheres, cones, boxes  Surface characteristics  Color  Texture.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
CSC 461: Lecture 41 CSC461: Lecture 4 Introduction to OpenGL Objectives: Development of the OpenGL API OpenGL Architecture -- OpenGL as a state machine.
Graphics Architectures & OpenGL API Introduction Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac GPU  Precision, Power, Programmability –CPU: x60/decade, 6 GFLOPS,
Programming with OpenGL Part 1: Background
CSC 461: Lecture 3 1 CSC461 Lecture 3: Models and Architectures  Objectives –Learn the basic design of a graphics system –Introduce pipeline architecture.
State Management and Drawing Geometry Objects
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.
Image Synthesis Rabie A. Ramadan, PhD 1. 2 About my self Rabie A. Ramadan My website and publications
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
P RACTICING O PEN GL- P RIMITIVES. O PEN GL O UTPUT P RIMITIVES  Each geometric object is described by a set of vertices and the type of primitive to.
Intro to OpenGL Transformations CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
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.
Programming With OpenGL
What are Computer Graphics Basically anything that is on you Monitor – This includes the text that you will see Text isn’t Advanced Graphics But…. Understanding.
CSCE 441: Computer Graphics
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 15 Creating 3D Models.
OpenGL: Introduction #include main() { OpenWindow() ; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0,
2002 by Jim X. Chen: Drawing Geometric Models.1. Objectives –OpenGL drawing primitives and attributes –OpenGL polygon face.
Chapter 1 Graphics Systems and Models Models and Architectures.
Introduction to Graphics Programming. Graphics API.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Administrivia Back on track: canceling OpenGL lecture 2 Assignment 1
CSC Graphics Programming
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
Real-Time Rendering Geometry and Buffers
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
OpenGL Basics OpenGL’s primary function – Rendering
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Introduction to OpenGL

What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API A pipe-line to generate high-quality images composed of geometric and image primitives A state machine www.doom3.com

Light sources and Lighting Geometry and Models High-res details or Textures Camera and viewing

A 3D graphics API Separates code Opengl32.dll on Windows Vendors package their own version of this library with the graphics card Windows 2000 supports a software-only version of OpenGL 1.1 out of the box

A low-level 3D graphics API An interface to hardware The library knows how to interface with the card drivers to get the hardware to handle the graphics. Anything not done in hardware is done in software

A low-level 3D graphics API Primitive-based Objects consist of points, line-segments, and polygons OpenGL is not aware of any connections between primitives Exception The GLU libraries include quadric and NURBS “objects” that encapsulate primitives for you

A Pipeline Architecture

A state machine Functions are global and change the state of the OpenGL environment State can be pushed onto stacks and popped back off OpenGL properties remain as you set them until you set them again

OpenGL Is Not A modeling language Compiled directly into your code Object-oriented

Getting Started - Syntax OpenGL core functions are prefixed with gl OpenGL utility functions are prefixed with glu OpenGL typedef defined types are prefixed with GL OpenGL constants are all caps and prefixed with GL_

History of the 3D graphics industry Line drawings, hidden lines, parametric surfaces (B-splines…) Automated drafting & machining for car, airplane, and ships manufacturers 1970’s: Mainframes, Vector tubes (HP…) Software: Solids, (CSG), Ray Tracing, Z-buffer for hidden lines 1980s: Graphics workstations ($50K-$1M): Frame buffers, rasterizers , GL, Phigs VR: CAVEs and head-mounted displays CAD/CAM & GIS: CATIA, SDRC, PTC Sun, HP, IBM, SGI, E&S, DEC

History of the 3D graphics industry PCs ($2K): Graphics boards, OpenGL, Java3D CAD+Videogames+Animations: AutoCAD, SolidWorks…, Alias-Wavefront Intel, many board vendors 2000s: Laptops, PDAs, Cell Phones: Parallel graphic chips Everything will be graphics, 3D, animated, interactive Nvidia, Sony, Nokia

Why OpenGL? Cross-platform. Better / easier to teach. Academically oriented textbooks, etc. Has existed long before other API’s. Hardware-based device drivers widely supported. Captures the low-level pipeline.

Other API’s? Microsoft’s Direct3D (DirectX) Java3D Also captures the low-level pipeline. I expect you to pick up a book and easily transition from OpenGL to Direct3D. Java3D A scenegraph-based API. Object oriented. Sits on top of OpenGL. Learning OpenGL will assist your understanding.

Other API’s PHIGS / PHIGS-Plus THE official standard (ANSI, ISO). National and international standards bodies could not keep pace with the rapid growth in graphics hardware functionality. Not necessarily interested in advancing the field. I was on the ANSI PHIGS-Plus committee in the late 1980’s.

Older API’s Display device dependent (different units / res) Window system dependent Operating system dependent (100, 50) (150, 100) Line(100,50,150,80) - device/lib 1 Moveto(100,50) - device/lib 2 Lineto(150,100) Without a standard API (such as OpenGL) - difficult to port

OpenGL Basics OpenGL’s primary functions OpenGL can render: Geometric description of objects. Composition or lay-out of objects. Color specification and lighting calculations Rasterization or sampling – calculating the pixel color and depth values from the above mathematical descriptions. OpenGL can render: Geometric primitives Bitmaps and Images (Raster primitives)

Computer Graphics v. OpenGL Object or model creation Data management / optimization Mapping from abstract of mathematical entities to low-level geometric primitives Specifying and controlling the environment (lighting, appearance, etc.) Dynamic or time-varying behavior. User-interaction / user interfaces for the above. Bottom-line: OpenGL is usually a small part of your application => porting not that hard.

Code Example A possible result void Display() {   glColor3f(1.0f, 1.0f, 0.0f );   glBegin(GL_POLYGON);     glVertex2f(-0.5f, -0.5f);     glVertex2f(-0.5f,  0.5f);     glVertex2f( 0.5f, 0.5f);     glVertex2f( 0.5f, -0.5f);   glEnd();   glFlush(); } …. What are the f’s for? Advise: Never use GL_POLYGON

Specifying Geometric primitives Primitives are specified using glBegin(primType); // define your vertices here … glEnd(); primType: GL_POINTS, GL_LINES, GL_TRIANGLES, GL_QUADS, …

OpenGL: Front/Back Rendering Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwise This is basically the right-hand rule Note that this still holds after perspective projection

OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd(): float v1[3], v2[3], v3[3], v4[3]; ... glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); The same vertex is used (sent, transformed, colored) many times (6 on average)

OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v4); glVertex3fv(v5); glEnd(); v0 v2 v1 v3 v4 v5 triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)

OpenGL: Triangle Fan The GL_TRIANGLE_FAN primitive is another way to reduce vertex redundancy: v0 v1 v2 v3 v4 v5 v6

OpenGL: Other Primitives You can draw other primitives using: GL_POINTS GL_LINES GL_LINE_STRIP GL_LINE_LOOP GL_QUADS …

Think about it: Why the redundancy? Primitive Types All primitives are specified by vertices: GL_LINE_STRIP GL_LINE_LOOP GL_POLYGON GL_LINES GL_POINTS GL_TRIANGLE_STRIP GL_QUAD_STRIP GL_TRIANGLES GL_QUADS GL_TRIANGLE_FAN Think about it: Why the redundancy?

Points in OpenGL p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_POINTS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Lines in OpenGL (1/3) Line Segments p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_LINES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Lines in OpenGL (2/3) Polylines – Line Strip p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_LINE_STRIP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Lines in OpenGL (3/3) Polylines – Line Loop p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_LINE_LOOP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons (1/2) Definition Simple Polygon Object that is closed as in a line loop, but that has an interior Simple Polygon No pair of edges of a polygon cross each other Simple Nonsimple

Polygons (2/2) Convexity If all points on the line segment between any two points inside the object, or on its boundary, are inside the object p1 p2 Convex Objects

Polygons in OpenGL (1/6) Polygon p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_POLYGON); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (2/6) Quadrilaterals p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_QUADS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (3/6) Quadstrip p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_QUAD_STRIP); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p0); glVertex2fv(p4); glVertex2fv(p7); glVertex2fv(p5); glVertex2fv(p6); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (4/6) Triangles p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_TRIANGLES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (5/6) Triangle Strip p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_TRIANGLE_STRIP); glVertex2fv(p0); glVertex2fv(p7); glVertex2fv(p1); glVertex2fv(p6); glVertex2fv(p2); glVertex2fv(p5); glVertex2fv(p3); glVertex2fv(p4); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (6/6) Triangle Fan p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_TRIANGLE_FAN); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Attributes Properties that determines How to render a geometric primitive Color, thickness, pattern of filling, etc. Color Three color theory Red Blue Green M Y C Cyan Yellow Magenta G B R Additive Color Subtractive Color Color Solid

OpenGL’s State Machine All rendering attributes are encapsulated in the OpenGL State rendering styles shading lighting texture mapping Each time OpenGL processes a vertex, it uses data stored in its internal state tables to determine how the vertex should be transformed, lit, textured or any of OpenGL’s other modes.

Manipulating OpenGL State Appearance is controlled by current state for each ( primitive to render ) { update OpenGL state render primitive } Manipulating vertex attributes is the most common way to manipulate state glColor*() / glIndex*() glNormal*() glTexCoord*() The general flow of any OpenGL rendering is to set up the required state, then pass the primitive to be rendered, and repeat for the next primitive. In general, the most common way to manipulate OpenGL state is by setting vertex attributes, which include color, lighting normals, and texturing coordinates.

Controlling current state Setting State glPointSize( size ); glLineStipple( repeat, pattern ); glShadeModel( GL_SMOOTH ); Enabling Features glEnable( GL_LIGHTING ); glDisable( GL_TEXTURE_2D ); Setting OpenGL state usually includes modifying the rendering attribute, such as loading a texture map, or setting the line width. Also for some state changes, setting the OpenGL state also enables that feature ( like setting the point size or line width ). Other features need to be turned on. This is done using glEnable(), and passing the token for the feature, like GL_LIGHT0 or GL_POLYGON_STIPPLE.

Simple Example Void DrawBlueQuad( ) { glColor3f(0.0f, 0.0f, 1.0f); glBegin(GL_QUADS); glVertex2f(0.0f, 0.0f); glVertex2f(1.0f, 0.0f); glVertex2f(1.0f, 1.0f); glVertex2f(0.0f, 1.0f); glEnd(); } This type of operation is called immediate-mode rendering; Each command happens immediately Although you may not see the result if you use double buffering Things get drawn into the back buffer Then buffers are swapped

OpenGL Command Formats glVertex2f(x, y) number of Components/ Dimensions 2 – (x,y) 3 – (x,y,z) 4 – (x,y,z,w) b – byte ub – unsigned byte s – short us – unsigned short i – int ui – unsigned int f – float d – double Add ‘v’ for vector form glVertex2fv(v) No method overloading in C or FORTRAN Internally everything is usually a float - I think.

OpenGL: Specifying Color Can specify other properties such as color To produce a single aqua-colored triangle: glColor3f(0.1, 0.5, 1.0); glVertex3fv(v0); glVertex3fv(v1); glVertex3fv(v2); To produce a smoothly shaded triangle: glColor3f(1, 0, 0); glVertex3fv(v0); glColor3f(0, 1, 0); glVertex3fv(v1); glColor3f(0, 0, 1); glVertex3fv(v2); In OpenGL, colors can also have a fourth component  (opacity or 1-transparency) Generally want  = 1.0 (opaque);

Window system independent OpenGL is window system independent No window management functions – create windows, resize windows, event handling, etc This is to ensure the application’s portability Creates some headaches though – a pure OpenGL program won’t work anywhere.

More APIs are needed X window system: GLX Apple Macintosh: AGL Microsoft Windows: WGL Additional libraries are needed to create Graphical User Interface (GUI) elements, such as sliders, buttons, menus, etc. Problem – you need to learn and implement them all to write truly portable software