Programming with OpenGL Part 1: Background

Similar documents
Programming with OpenGL Part 1: Background

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Programming with OpenGL Part 3: Three Dimensions

Programming using OpenGL: A first Introduction

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

Computer Graphics. Bing-Yu Chen National Taiwan University

Graphics Programming

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 2: First Program

Drawing Primitives. OpenGL basics

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Introduction to OpenGL

API Background. Prof. George Wolberg Dept. of Computer Science City College of New York

Meshing and Geometry

LECTURE 02 OPENGL API

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Lecture 3 Advanced Computer Graphics (CS & SE )

CIS 441/541: Introduction to Computer Graphics Lecture 14: OpenGL Basics

Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:

Computer Graphics. OpenGL

Basic Graphics Programming

OPEN GL BACKGROUND. Objectives. Early History of APIs. SGI and GL. PHIGS and X. Modified from Angel 6e book slides

Lectures OpenGL Introduction

CS 4204 Computer Graphics

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

COMPUTER GRAPHICS LAB # 3

Assignment 1. Simple Graphics program using OpenGL

Introduction to Computer Graphics with OpenGL/GLUT

OpenGL Introduction Computer Graphics and Visualization

Computer Graphics (Basic OpenGL)

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

Computer Graphics, Chapt 08

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

Precept 2 Aleksey Boyko February 18, 2011

Today s Agenda. Basic design of a graphics system. Introduction to OpenGL

Announcements OpenGL. Computer Graphics. Spring CS4815

Lecture 4 of 41. Lab 1a: OpenGL Basics

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Basic Graphics Programming

Computer graphics MN1

Computer Graphics (CS 4731) OpenGL/GLUT(Part 1)

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

VR-programming tools (procedural) More VRML later in this course! (declarative)

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

CS 543 Lecture 1 (Part 3) Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

RECITATION - 1. Ceng477 Fall

Lecture 3. Understanding of OPenGL programming

CSC 8470 Computer Graphics. What is Computer Graphics?

COMP 371/4 Computer Graphics Week 1

1.2 Basic Graphics Programming

UNIT 7 LIGHTING AND SHADING. 1. Explain phong lighting model. Indicate the advantages and disadvantages. (Jun2012) 10M

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

CS 543 Lecture 1 (Part II): Intro to OpenGL and GLUT (Part I) Emmanuel Agu

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Computer Graphics Introduction to OpenGL

2D Drawing Primitives

Books, OpenGL, GLUT, GLUI, CUDA, OpenCL, OpenCV, PointClouds, and G3D

Computer graphic -- Programming with OpenGL I

OpenGL/GLUT Intro. Week 1, Fri Jan 12

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Graphics Pipeline & APIs

Books, OpenGL, GLUT, CUDA, OpenCL, OpenCV, PointClouds, G3D, and Qt

Duc Nguyen CSE 420 Computer Graphics 10/10/2018 Homework 1

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

CS 432 Interactive Computer Graphics

Computer Graphics Primitive Attributes

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

Rendering. Part 1 An introduction to OpenGL

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

C++ is Fun Part 13 at Turbine/Warner Bros.! Russell Hanson

Graphics Pipeline & APIs

3D computer graphics: geometric modeling of objects in the computer and rendering them

Draw the basic Geometry Objects. Hanyang University

CSE4030 Introduction to Computer Graphics

Using OpenGL with CUDA

Abel J. P. Gomes LAB. 1. INTRODUCTION TO OpenGL

Computer Graphics 1 Computer Graphics 1

Introduction to OpenGL. CSCI 4229/5229 Computer Graphics Fall 2012

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field


CS418 OpenGL & GLUT Programming Tutorial (I) Presented by : Wei-Wen Feng 1/30/2008

OpenGL refresher. Advanced Computer Graphics 2012

Computer Graphics. Chapter 3 Computer Graphics Software

OpenGL. Toolkits.

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

2. OpenGL -I. 2.1 What is OpenGL? Things OpenGL can do: -23-

by modifying the glutinitwindowsize() function you can change the screen size to whatever you please.

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Overview of Graphics Systems Hearn & Baker Chapter 2. Some slides are taken from Robert Thomsons notes.

Transcription:

Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico 1

Objectives Development of the OpenGL API OpenGL Architecture - OpenGL as a state machine Functions - Types - Formats Simple program 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 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) 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 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 6

OpenGL Evolution Controlled by an Architectural Review Board (ARB) - Members include SGI, Microsoft, Nvidia, HP, 3DLabs, IBM,. - Relatively stable (present version 2.0) Evolution reflects new hardware capabilities 3D texture mapping and texture objects Vertex programs - Allows for platform specific features through extensions 7

OpenGL Libraries OpenGL core library - 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 Links with window system - GLX for X window systems - WGL for Windows - AGL for Macintosh 8

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 9

Software Organization application program OpenGL Motif widget or similar GLX, AGL or WGL X, Win32, Mac O/S GLUT GLU GL software and/or hardware 10

OpenGL Architecture Immediate Mode geometry pipeline Polynomial Evaluator Per Vertex Operations & Primitive Assembly CPU Display List Rasterization Per Fragment Operations Frame Buffer Pixel Operations Texture Memor y 11

Primitives - Points - Line Segments - Polygons Attributes Transformations - Viewing - Modeling Control (GLUT) Input (GLUT) Query OpenGL Functions 12

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 13

Lack of Object Orientation OpenGL is not object oriented so that there are multiple functions for a given logical function -glvertex3f -glvertex2i -glvertex3dv Underlying storage mode is the same Easy to create overloaded functions in C++ but issue is efficiency 14

OpenGL function format function name dimensions glvertex3f(x,y,z) belongs to GL library x,y,z are floats glvertex3fv(p) p is a pointer to an array 15

OpenGL #defines Most constants are defined in the include files gl.h, glu.h and glut.h - Note #include <GL/glut.h> should automatically include the others - Examples -glbegin(gl_polygon) -glclear(gl_color_buffer_bit) include files also define OpenGL data types: GLfloat, GLdouble,. 16

A Simple Program Generate a square on a solid background 17

simple.c #include <GL/glut.h> 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){ glutcreatewindow("simple"); glutdisplayfunc(mydisplay); glutmainloop(); } 18

Event Loop Note that the program defines a display callback function named mydisplay - Every glut program must have a display callback - The display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is opened - The main function ends with the program entering an event loop 19

Defaults simple.c is too simple Makes heavy use of state variable default values for - Viewing - Colors - Window parameters Next version will make the defaults more explicit 20

Notes on compilation See website and ftp for examples Unix/linux - Include files usually in /include/gl - Compile with lglut lglu lgl loader flags - May have to add L flag for X libraries - Mesa implementation included with most linux distributions - Check web for latest versions of Mesa and glut 21

Compilation on Windows Visual C++ - Get glut.h, glut32.lib and glut32.dll from web - Create a console application - Add opengl32.lib, glut32.lib, glut32.lib to project settings (under link tab) Borland C similar Cygwin (linux under Windows) - Can use gcc and similar makefile to linux - Use lopengl32 lglu32 lglut32 flags 22

Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico 23

Objectives Refine the first program - Alter the default values - Introduce a standard program structure Simple viewing - Two-dimensional viewing as a special case of three-dimensional viewing Fundamental OpenGL primitives Attributes 24

Program Structure Most OpenGL programs have a similar structure that consists of the following functions -main(): defines the callback functions opens one or more windows with the required properties enters event loop (last executable statement) -init(): sets the state variables Viewing Attributes - callbacks Display function Input and window functions 25

simple.c revisited In this version, we shall see the same output but we have defined all the relevant state values through function calls using the default values In particular, we set - Colors - Viewing conditions - Window properties 26

main.c #include <GL/glut.h> includes gl.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 27

GLUT functions glutinit allows application to get command line arguments and initializes system gluinitdisplaymode requests properties for the window (the rendering context) - RGB color - Single buffering - Properties logically ORed together glutwindowsize in pixels glutwindowposition from top-left corner of display glutcreatewindow create window with title simple glutdisplayfunc display callback glutmainloop enter infinite event loop 28

init.c void init() { glclearcolor (0.0, 0.0, 0.0, 1.0); black clear color opaque window glcolor3f(1.0, 1.0, 1.0); fill/draw with white glmatrixmode (GL_PROJECTION); glloadidentity (); glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); } viewing volume 29

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 30

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 31

Orthographic Viewing In the default orthographic view, points are projected forward along the z axis onto the plane z=0 z=0 z=0 32

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); 33

Two- and threedimensional 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 34

mydisplay.c 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(); } 35

OpenGL Primitives GL_POINTS GL_LINES GL_LINE_STRIP GL_POLYGON GL_LINE_LOOP GL_TRIANGLES GL_QUAD_STRIP GL_TRIANGLE_STRIP GL_TRIANGLE_FAN 36

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 nonsimple polygon nonconvex polygon 37

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 38

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 39

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 40

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 glcolor glvertex 41

Smooth Color 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 42

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

Programming with OpenGL Part 3: Three Dimensions Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico 44

Objectives Develop a more sophisticated threedimensional example - Sierpinski gasket: a fractal Introduce hidden-surface removal 45

Three-dimensional Applications In OpenGL, two-dimensional applications are a special case of three-dimensional graphics Going to 3D - Not much changes - Use glvertex3*( ) - Have to worry about the order in which polygons are drawn or use hidden-surface removal - Polygons should be simple, convex, flat 46

Sierpinski Gasket (2D) Start with a triangle Connect bisectors of sides and remove central triangle Repeat 47

Five subdivisions Example 48

The gasket as a fractal Consider the filled area (black) and the perimeter (the length of all the lines around the filled triangles) As we continue subdividing - the area goes to zero - but the perimeter goes to infinity This is not an ordinary geometric object - It is neither two- nor three-dimensional It is a fractal (fractional dimension) object 49

Gasket Program #include <GL/glut.h> /* initial triangle */ GLfloat v[3][2]={{-1.0, -0.58}, {1.0, -0.58}, {0.0, 1.15}}; int n; /* number of recursive steps */ 50

Draw one triangle void triangle( GLfloat *a, GLfloat *b, GLfloat *c) /* display one triangle */ { glvertex2fv(a); glvertex2fv(b); glvertex2fv(c); } 51

Triangle Subdivision void divide_triangle(glfloat *a, GLfloat *b, GLfloat *c, int m) { /* triangle subdivision using vertex numbers */ point2 v0, v1, v2; int j; if(m>0) { for(j=0; j<2; j++) v0[j]=(a[j]+b[j])/2; for(j=0; j<2; j++) v1[j]=(a[j]+c[j])/2; for(j=0; j<2; j++) v2[j]=(b[j]+c[j])/2; divide_triangle(a, v0, v1, m-1); divide_triangle(c, v1, v2, m-1); divide_triangle(b, v2, v0, m-1); } else(triangle(a,b,c)); /* draw triangle at end of recursion */ } 52

display and init Functions void display() { glclear(gl_color_buffer_bit); glbegin(gl_triangles); divide_triangle(v[0], v[1], v[2], n); glend(); glflush(); } void myinit() { glmatrixmode(gl_projection); glloadidentity(); gluortho2d(-2.0, 2.0, -2.0, 2.0); glmatrixmode(gl_modelview); glclearcolor (1.0, 1.0, 1.0,1.0) glcolor3f(0.0,0.0,0.0); } 53

main Function int main(int argc, char **argv) { n=4; glutinit(&argc, argv); glutinitdisplaymode(glut_single GLUT_RGB); glutinitwindowsize(500, 500); glutcreatewindow( 2D Gasket"); glutdisplayfunc(display); myinit(); glutmainloop(); } 54

Efficiency Note By having the glbegin and glend in the display callback rather than in the function triangle and using GL_TRIANGLES rather than GL_POLYGON in glbegin, we call glbegin and glend only once for the entire gasket rather than once for each triangle 55

Moving to 3D We can easily make the program threedimensional by using GLfloat v[3][3] glvertex3f glortho But that would not be very interesting Instead, we can start with a tetrahedron 56

3D Gasket We can subdivide each of the four faces Appears as if we remove a solid tetrahedron from the center leaving four smaller tetrahedra 57

Example after 5 iterations 58

triangle code void triangle( GLfloat *a, GLfloat *b, GLfloat *c) { glvertex3fv(a); glvertex3fv(b); glvertex3fv(c); } 59

subdivision code void divide_triangle(glfloat *a, GLfloat *b, GLfloat *c, int m) { GLfloat v1[3], v2[3], v3[3]; int j; if(m>0) { for(j=0; j<3; j++) v1[j]=(a[j]+b[j])/2; for(j=0; j<3; j++) v2[j]=(a[j]+c[j])/2; for(j=0; j<3; j++) v3[j]=(b[j]+c[j])/2; divide_triangle(a, v1, v2, m-1); divide_triangle(c, v2, v3, m-1); divide_triangle(b, v3, v1, m-1); } else(triangle(a,b,c)); } 60

tetrahedron code void tetrahedron( int m) { glcolor3f(1.0,0.0,0.0); divide_triangle(v[0], v[1], v[2], m); glcolor3f(0.0,1.0,0.0); divide_triangle(v[3], v[2], v[1], m); glcolor3f(0.0,0.0,1.0); divide_triangle(v[0], v[3], v[1], m); glcolor3f(0.0,0.0,0.0); divide_triangle(v[0], v[2], v[3], m); } 61

Almost Correct Because the triangles are drawn in the order they are defined in the program, the front triangles are not always rendered in front of triangles behind them get this want this 62

Hidden-Surface Removal We want to see only those surfaces in front of other surfaces OpenGL uses a hidden-surface method called the z-buffer algorithm that saves depth information as objects are rendered so that only the front objects appear in the image 63

Using the z-buffer algorithm The algorithm uses an extra buffer, the z-buffer, to store depth information as geometry travels down the pipeline It must be - Requested in main.c glutinitdisplaymode (GLUT_SINGLE GLUT_RGB GLUT_DEPTH) - Enabled in init.c glenable(gl_depth_test) - Cleared in the display callback glclear(gl_color_buffer_bit GL_DEPTH_BUFFER_BIT) 64

Surface vs Volume Subdvision In our example, we divided the surface of each face We could also divide the volume using the same midpoints The midpoints define four smaller tetrahedrons, one for each vertex Keeping only these tetrahedrons removes a volume in the middle See text for code 65

Volume Subdivision 66