Introduction to OpenGL Programming

Slides:



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

Computer Graphics - Graphics Programming -
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
© 2004, Tom Duff and George Ledin Jr1 Lectures OpenGL Introduction By Tom Duff Pixar Animation Studios Emeryville, California and George Ledin Jr Sonoma.
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
 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.
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.
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.
CSE 494/598 Intro to Applied Computer Graphics Anshuman Razdan DCST AR's Web Page AR's Web Page
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
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.
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.
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
Lecture 3 OpenGL.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
 “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.
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.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
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.
Intro to OpenGL (Version 2) Geb Thomas. Setting Up GLUT You will need GLUT for opening windows We can use the version made by Nate Robins: –
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
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.
Graphics: Conceptual Model
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
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.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
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.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
Basic Program with OpenGL and GLUT
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
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
Programming with OpenGL Part 1: Background
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Introduction to OpenGL Programming 2D Graphics

What is OpenGL OpenGL is a software API to graphics hardware. designed as a streamlined, hardware-independent interface to be implemented on many different hardware platforms Intuitive, procedural interface with C, C++, Java, Perl, Python, … bindings No windowing commands! No high-level commands for describing models of three-dimensional objects The OpenGL Utility Library (GLU) provides many of the modeling features, such as quadric surfaces (spheres, cylinders, …) and NURBS curves and surfaces

What Is OpenGL? A software interface to graphics hardware. The interface consists of about 250 commands (functions) to specify the objects and operations needed to produce 2D and 3D graphics OpenGL geometric primitives include points, lines, polylines, and polygons. There is specific support for triangle and quadrilateral polynomials Has texture mapping support.

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 In 3D computer graphics, the terms graphics pipeline or rendering pipeline most commonly refer to the current state of the art method of rasterization-based rendering as supported by commodity graphics hardware[1]. The graphics pipeline typically accepts some representation of a three-dimensional scene as an input and results in a 2D raster image as output. OpenGL and Direct3D are two notable graphics pipeline models accepted as widespread industry standards.

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 Rendering is the process of generating an image from a model, by means of computer programs. The model is a description of three dimensional objects in a strictly defined language or data structure. It would contain geometry, viewpoint, texture, lighting, and shading information. The image is a digital image or raster graphics image. The term may be by analogy with an "artist's rendering" of a scene. 'Rendering' is also used to describe the process of calculating effects in a video editing file to produce final video output

OpenGL Evolution Controlled by an Architectural Review Board – in 2006 transferred to the Khronos Group. Members include SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,……. Relatively stable. Just released version 3.0 demonstrated at SIGGRAPH 2008. Evolution reflects new hardware capabilities Allows for platform specific features through extensions

OpenGL Libraries OpenGL core library OpenGL Utility Library (GLU) OpenGL32 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 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

Software Organization This diagram demonstrates the relationship between OpenGL GLU and windowing APIs. API Hierarchy OpenGL applications use the window system’s window, input, and event mechanism GLU supports quadrics, NURBS, complex polygons, matrix utilities, and more GLX: OpenGL Extension to the X Window System – functions begin with glX WGL: Microsoft Windows-to-OpenGL interface with functions beginning with wgl AGL: Apple GL, functions begin with agl GDI is a Microsoft Windows interface for representing graphical objects and transmitting them to output devices such as monitors and printers. Xlib is an X Window System protocol client library in the C programming language. It contains functions for interacting with an X server.

Classic Rendering Pipeline Modeling Transformation Lighting Projection Viewing 3D Geometric Primitives Image Clipping Scan Conversion

OpenGL as a state machine Put OpenGL into states (modes). Projection and viewing matrix Color and material properties Lights and shading Line and polygon drawing modes … GL State Variables- can be set and queried by OpenGL. Remains unchanged until the next change. 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

OpenGL Syntax functions have prefix gl and initial capital letters for each word glClearColor(), glEnable(), glPushMatrix() … glu for GLU functions gluLookAt(), gluPerspective() … constants begin with GL_, use all capital letters GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW … Extra letters in some commands indicate the number and type of variables glColor3f(), glVertex3f() … OpenGL data types GLfloat, GLdouble, GLint, GLenum, …

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

OpenGL Syntax OpenGL defines its own types which correspond to C types: GLbyte signed char GLint int or long GLfloat float The C++ compiler may generate a warning/error and need a cast.

OpenGL Syntax Examples Example: Setting the current color using glColor. Colors may have 3 components (RGB) or 4 components (RGBA). Think of A (or alpha) as opacity. Floating point - color component values range from 0 to 1 glColor3f(0.0, 0.5, 1.0); This is 0% Red, 50% Green, 100% Blue; glColor4f(0.0, 0.5, 1.0, 0.3); This is 0% Red, 50% Green, 100% Blue, 30% Opacity GLfloat color[4] = { 0.0, 0.5, 1.0, 0.3 }; glColor4fv(color); Again, 0% Red, 50% Green, 100% Blue, 30% Opacity

OpenGL Syntax Examples Unsigned byte – color component values range from 0 to 255 (same as C’s unsigned char). glColor3ub (0, 127, 255); This is: 0% Red, 50% Green, 100% Blue glColor4ub (0, 127, 255, 76); This is 0% Red, 50% Green, 100% Blue, 30% Opacity . . .

Windowing with OpenGL OpenGL is independent of any specific window system OpenGL can be used with different window systems X windows (GLX) MFC (WGL) … GLUT provide a portable API for creating window and interacting with I/O devices MFC Microsoft Foundation Class Library and X windows are libraries that wrap portions of the Windows API into C++ classes and/or functions.

GLUT Developed by Mark Kilgard Hides the complexities of differing window system APIs Default user interface for class projects Glut routines have prefix glut glutCreateWindow() … Has very limited GUI interface Glui is the C++ extension of glut that provides buttons, checkboxes, radio buttons, etc.

Glut Routines Initialization: glutInit() processes (and removes) command­line arguments that may be of interest to glut and the window system and does general initialization of Glut and OpenGL Must be called before any other glut routines Display Mode: The next procedure, glutInitDisplayMode(), performs initializations informing OpenGL how to set up the frame buffer. Display Mode Meaning GLUT_RGB Use RGB colors GLUT_RGBA Use RGB plus alpha (for transparency) GLUT_INDEX Use indexed colors (not recommended) GLUT_DOUBLE Use double buffering (recommended) GLUT_SINGLE Use single buffering (not recommended) GLUT_DEPTH Use depth­buffer (for hidden surface removal.)

Glut Routines Window Setup glutInitWindowSize(int width, int height) glutInitWindowPosition(int x, int y) glutCreateWindow(char* title)

A Simple Program Generate a square on a solid background

simple.c #include <GL/glut.h> int main(int argc, char** argv) void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); } int main(int argc, char** argv) glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop();

Closer Look at the main() includes gl.h and glu.h #include <GL/glut.h> int main(int argc, char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("simple"); glutDisplayFunc(mydisplay); init(); glutMainLoop(); } define window properties display callback set OpenGL state enter event loop

init() black clear color opaque window fill/draw with white viewport void init() { glClearColor (0.0, 0.0, 0.0, 1.0); glColor3f(1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluOrtho2D(-1.0, 1.0, -1.0, 1.0); } opaque window fill/draw with white viewport

Callbacks Virtually all interactive graphics programs are event driven Glut uses callbacks to handle events Windows system invokes a particular procedure when an event of particular type occurs. MOST IMPORTANT: display event Signaled when window first displays and whenever portions of the window reveals from blocking window glutDisplayFunc(void (*func)(void)) registers the display callback function Running the program: glutMainLoop() Main event loop. Never exit()

More Callbacks glutReshapeFunc(void (*func)(int w, int h)) indicates what action should be taken when the window is resized. glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)) and glutMouseFunc(void (*func)(int button, int state, int x, int y)) allow you to link a keyboard key or a mouse button with a routine that's invoked when the key or mouse button is pressed or released. glutMotionFunc(void (*func)(int x, int y)) registers a routine to call back when the mouse is moved while a mouse button is also pressed. glutMouseFunc(void (*func)(int button, int state, int x, int y)) registers a function that's to be executed if a mouse button event occurs. The argument button can be GLUT_LEFT_BUTTON or GLUT_RIGHT_BUTTON. The argument state can be GLUT_UP or GLUT_DOWN. The arguments x and y indicated the mouse cursor position when the button was clicked.

Basic Drawing in OpenGL We have learned how to create a window Simple 2D drawing No lighting and shading OpenGL coordinate system has different origin from the window system Uses lower left corner instead of upper left corner as origin

OpenGL Drawing Steps in the display function Clear the window Set drawing attributes Send drawing commands Flush the buffer

Clear the Window glClear(GL_COLOR_BUFFER_BIT) clears the frame buffer by overwriting it with the background color. Background color is a state set by glClearColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a) in the init().

Drawing Attributes: Color glColor3f(GLfloat r, GLfloat g, GLfloat b) sets the drawing color glColor3d(), glColor3ui() can also be used Remember OpenGL is a state machine Once set, the attribute applies to all subsequent defined objects until it is set to some other value glColor3fv() takes a flat array as input

Drawing Commands Simple Objects glRectf() Complex Objects Use construct glBegin(mode) and glEnd() and a list of vertices in between glBegin(mode) glVertex(v0); glVertex(v1); ... glEnd();

Drawing Attributes Besides glVertex() commands, other attributes commands can also be used between glBegin() and glEnd(), e.g. glColor3f(). There are more drawing attributes than color Point size: glPointSize() Line width: glLinewidth() Dash or dotted line: glLineStipple() Polygon pattern: glPolygonStipple() …

Polygon Issues 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 nonconvex polygon nonsimple polygon

Projection and Viewport

Orthographic projection Orthographic projection used for 2D drawing, Perspective project often used for 3D drawing 2D Viewing: Orthographic View gluOrtho2D(left, right, bottom, top) Specifies the coordinates of 2D region to be projected into the viewport. Any drawing outside the region will be automatically clipped away. z=0

Viewports Do not have to use the entire window for the image: glViewport(x,y,w,h) Values in pixels (screen coordinates)

Window to Viewport mapping Aspect Ratio: Height/Width If the aspect ratio of the window Is different from that of the viewport, the picture will be distorted.

Display Callback revisited: void mydisplay() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_POLYGON); glVertex2f(-0.5, -0.5); glVertex2f(-0.5, 0.5); glVertex2f(0.5, 0.5); glVertex2f(0.5, -0.5); glEnd(); glFlush(); }