Lecture 5 - Programming with OpenGL

Slides:



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

Chapter 2: Graphics Programming
Graphics Pipeline.
Pemrograman OpenGL Dasar
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Programming with OpenGL Part 0: 3D API March 1, 2007.
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Models and Architectures Ed Angel Professor of Computer Science, Electrical and Computer.
CSC 461: Lecture 6 1 CSC461 Lecture 6: 2D Programming in OpenGL Objectives:  Fundamental OpenGL primitives  Attributes  Viewport.
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.
Demetriou/Loizidou – ACSC330 – Chapter 2 Graphics Programming Dr. Giorgos A. Demetriou Dr. Stephania Loizidou Himona Computer Science Frederick Institute.
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
CS 480/680 Computer Graphics Programming with Open GL Part 1: Background Dr. Frederick C Harris, Jr. Fall 2011.
Programming with OpenGL Part 1: Background
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
CSC 461: Lecture 3 1 CSC461 Lecture 3: Models and Architectures  Objectives –Learn the basic design of a graphics system –Introduce pipeline architecture.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
Scientific Visualization with OpenGL 22 February 2006.
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 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1Computer Graphics Lecture 4 - Models and Architectures John Shearer Culture Lab – space 2
OpenGL: Introduction Yanci Zhang Game Programming Practice.
Introduction to OpenGL Week 1 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico CS.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 1: Background Ed Angel Professor Emeritus.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
1 Chapter 2: Graphics Programming Bryson Payne CSCI 3600 NGCSU.
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 43 Computer Graphics Programming with OpenGL I.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Color and Color Models Angel 2.5 Angel: Interactive Computer Graphics5E © Addison-Wesley
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Angel: Interactive Computer Graphics5E © Addison- Wesley 2009 Image Formation Fundamental imaging notions Fundamental imaging notions Physical basis.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 4: Color and Attributes Isaac Gang University.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
Chapter 1 Graphics Systems and Models Models and Architectures.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Models and Architectures 靜宜大學 資訊工程系 蔡奇偉 副教授 2012.
Introduction to OpenGL (INF 250) Veronika Solteszova et al., UiB Dept. of Informatics,
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Models and Architectures
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 4: Color and Attributes
Programming with OpenGL Part 1: Background
Models and Architectures
Models and Architectures
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
Programming with OpenGL Part 4: Color and Attributes
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Lecture 5 - Programming with OpenGL Computer Graphics Lecture 5 - Programming with OpenGL John Shearer Culture Lab – space 2 john.shearer@ncl.ac.uk http://di.ncl.ac.uk/teaching/csc3201/ Computer Graphics 1 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 1

Objectives Development of the OpenGL API OpenGL Architecture Functions OpenGL as a state machine Functions Types Formats Fundamental OpenGL primitives Attributes Simple program Computer Graphics 2 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 2

Early History of APIs IFIPS (1973) formed two committees to come up with a standard graphics API Graphical Kernel System (GKS) 2D but contained good workstation model Core Both 2D and 3D GKS adopted as IS0 and later ANSI standard (1980s) GKS not easily extended to 3D (GKS-3D) Far behind hardware development Computer Graphics 3 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 3

PHIGS and X Programmers Hierarchical Graphics System (PHIGS) Arose from CAD community Database model with retained graphics (structures) X Window System DEC/MIT effort Client-server architecture with graphics PEX combined the two Not easy to use (all the defects of each) Computer Graphics 4 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 4

SGI and GL Silicon Graphics (SGI) revolutionized the graphics workstation by implementing the pipeline in hardware (1982) To access the system, application programmers used a library called GL With GL, it was relatively simple to program three dimensional interactive applications Computer Graphics 5 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 5

OpenGL The success of GL lead to OpenGL (1992), a platform-independent API that was Easy to use Close enough to the hardware to get excellent performance Focus on rendering Omitted windowing and input to avoid window system dependencies Computer Graphics 6 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 6

OpenGL Evolution Originally controlled by an Architectural Review Board (ARB) Members included SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,……. Relatively stable Though recent changes have caused some disruption Evolution reflects new hardware capabilities 3D texture mapping and texture objects Vertex programs Allows for platform specific features through extensions ARB replaced by Kronos Computer Graphics 7 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 7

Kronos The Khronos Group was founded in January 2000 by a number of leading media-centric companies, including 3Dlabs, ATI, Discreet, Evans & Sutherland, Intel, NVIDIA, SGI and Sun Microsystems, dedicated to creating open standard APIs to enable the authoring and playback of rich media on a wide variety of platforms and devices. Standards include: OpenGL (2D & 3D graphics) COLLADA (digital asset exchange) glFX (run-time effects API for OpenGL) OpenGL ES (Embedded 3D graphics) … Computer Graphics 8 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 8

OpenGL Libraries OpenGL core library OpenGL Utility Library (GLU) OpenGL32.dll on Windows GL on most unix/linux systems (libGL.a) OpenGL Utility Library (GLU) Provides functionality in OpenGL core but avoids having to rewrite code Links with window system GLX for X window systems WGL for Windows AGL for Macintosh Computer Graphics 9 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 9

GLUT OpenGL Utility Toolkit (GLUT) Provides functionality common to all window systems Open a window Get input from mouse and keyboard Menus Event-driven Code is portable but GLUT lacks the functionality of a good toolkit for a specific platform No slide bars Computer Graphics 10 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 10

Software Organization GLUT GLU GL GLX, AGL or WGL X, Win32, Mac O/S software and/or hardware application program OpenGL Motif widget or similar Computer Graphics 11 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 11

OpenGL Architecture geometry pipeline CPU Computer Graphics 12 Immediate Mode Per Vertex Operations & Primitive Assembly Polynomial Evaluator Display List Per Fragment Operations Frame Buffer CPU Rasterization Texture Memory Pixel Operations Computer Graphics 12 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 12

OpenGL Functions Primitives Attributes Transformations Points Line Segments Polygons Attributes Transformations Viewing Modeling Control (GLUT / org.lwjgl.opengl ) Input (GLUT / org.lwjgl.input ) Query Computer Graphics 13 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 13

OpenGL State OpenGL is a state machine OpenGL functions are of two types Primitive generating Can cause output if primitive is visible How vertices are processed and appearance of primitive are controlled by the state State changing Transformation functions Attribute functions Computer Graphics 14 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 14

Lack of Object Orientation OpenGL is not object oriented so that there are multiple functions for a given logical function glVertex3f – 3 FLOATS glVertex2i – 2 INTEGERS glVertex3dv a vector (pointer to) 3 DOUBLES (in C/C++ glVertexPointer(int size, int stride, java.nio.FloatBuffer pointer) Underlying storage mode is the same Easy to create overloaded functions but issue is efficiency The OpenGL API is a C API, but in practice most implementations (NVidia, AMD/ATI) are written in C++. Computer Graphics 15 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 15

OpenGL function format function name dimensions glVertex3f(x,y,z) x,y,z are floats belongs to GL library glVertex3fv(p) p is a pointer to an array Computer Graphics 16 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 16

OpenGL defines Most constants are defined in the package org.lwjgl.opengl.GL11 Examples glBegin(GL_POLYGON) glClear(GL_COLOR_BUFFER_BIT) include files or org.lwjgl.opengl.GL11 package in LWJGL, also define OpenGL data types: GLfloat, GLdouble,…. Computer Graphics 17 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 17

Coordinate Systems The units in glVertex are determined by the application and are called object or problem coordinates The viewing specifications are also in object coordinates and it is the size of the viewing volume that determines what will appear in the image Internally, OpenGL will convert to camera (eye) coordinates and later to screen coordinates OpenGL also uses some internal representations that usually are not visible to the application Computer Graphics 18 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 18

OpenGL Camera OpenGL places a camera at the origin in object space pointing in the negative z direction The default viewing volume is a box centered at the origin with a side of length 2 Computer Graphics 19 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 19

Orthographic Viewing In the default orthographic view, points are projected forward along the z axis onto the plane z=0 Computer Graphics 20 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 20

Transformations and Viewing In OpenGL, projection is carried out by a projection matrix (transformation) There is only one set of transformation functions so we must set the matrix mode first glMatrixMode (GL_PROJECTION) Transformation functions are incremental so we start with an identity matrix and alter it with a projection matrix that gives the view volume glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); Computer Graphics 21 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 21

Projection matrix & Modelview matrix OpenGL uses two matrices (projection & modelview) to make life simpler. A single matrix could be used (as two matrices can be multiplied together to create just one). In most scenarios the projection the user/programmer wants is rarely changed, so the projection matrix is used for this and isn't changed often The modelview matrix is used to transform the position, orientation, etc of objects in the real world and so is changed very frequently (when objects move, but more importantly, it is different for each object). Computer Graphics 22 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 22

GL_PROJECTION glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1, 1, -1, 1, -1.0, 1.0); glTranslate( 100, 100, 100 ); glRotateF( 45, 1, 0, 0 ); Really means: GL_PROJECTION_MATRIX = IDENTITY * ORTHOGRAPHIC_MATRIX * TRANSLATION_MATRIX * ROTATION_MATRIX Computer Graphics 23 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 23

GL_MODELVIEW Computer Graphics 24 glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslate( 100, 100, 100 ); glRotateF( 45, 1, 0, 0 ); glPushMatrix(); glTranslate( carx, cary, carz ); // draw car here, by specifying various gl vertices glPopMatrix(); glTranslate( battleshipx, battleshipy, battleshipz ); // draw battleship here, by specifying various gl vertices car_vertice_matrix = projection_ortho_matrix * projection_translation * car_translation_matrix battleship_vertice_matrix = projection_ortho_matrix * projection_translation * battleship_translation_matrix Computer Graphics 24 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 24

Two- and three-dimensional viewing In glOrtho(left, right, bottom, top, near, far) the near and far distances are measured from the camera Two-dimensional vertex commands place all vertices in the plane z=0 If the application is in two dimensions, we can use the function gluOrtho2D(left, right,bottom,top) In two dimensions, the view or clipping volume becomes a clipping window Computer Graphics 25 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 25

OpenGL Primitives Any missing? GL_POINTS GL_POLYGON GL_LINE_STRIP GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN Any missing? Computer Graphics 26 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 26

Polygon Issues Computer Graphics 27 OpenGL will only display polygons correctly that are Simple: edges cannot cross Convex: All points on line segment between two points in a polygon are also in the polygon Flat: all vertices are in the same plane User program can check if above true OpenGL will produce output if these conditions are violated but it may not be what is desired Triangles satisfy all conditions nonsimple polygon nonconvex polygon Computer Graphics 27 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 27

Attributes Attributes are part of the OpenGL state and determine the appearance of objects Color (points, lines, polygons) Size and width (points, lines) Stipple pattern (lines, polygons) Polygon mode Display as filled: solid color or stipple pattern Display edges Display vertices Computer Graphics 28 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 28

RGB color Each color component is stored separately in the frame buffer Usually 8 bits per component in buffer Note in glColor3f the color values range from 0.0 (none) to 1.0 (all), whereas in glColor3ub the values range from 0 to 255 Computer Graphics 29 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 29

Indexed Color Colors are indices into tables of RGB values Requires less memory indices usually 8 bits not as important now Memory inexpensive Need more colors for shading Computer Graphics 30 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 30

Color and State The color as set by glColor becomes part of the state and will be used until changed Colors and other attributes are not part of the object but are assigned when the object is rendered We can create conceptual vertex colors by code such as glColor glVertex Computer Graphics 31 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 31

Smooth Color glShadeModel Default is smooth shading OpenGL interpolates vertex colors across visible polygons Alternative is flat shading Color of first vertex determines fill color glShadeModel (GL_SMOOTH) or GL_FLAT Computer Graphics 32 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 32

Viewports Do not have use the entire window for the image: glViewport(x,y,w,h) Values in pixels (screen coordinates) Computer Graphics 33 Computer Graphics - Newcastle University Computer Graphics - Newcastle University 33