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

Similar documents
Lecture 4 of 41. Lab 1a: OpenGL Basics

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Lecture 2 2D transformations Introduction to OpenGL

Information Coding / Computer Graphics, ISY, LiTH. OpenGL! ! where it fits!! what it contains!! how you work with it 11(40)

Programming using OpenGL: A first Introduction

Introduction to Computer Graphics with OpenGL/GLUT

RECITATION - 1. Ceng477 Fall

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

Computer Graphics 1 Computer Graphics 1

Exercise 1 Introduction to OpenGL

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

CS 4204 Computer Graphics

Rendering. Part 1 An introduction to OpenGL

Lectures OpenGL Introduction

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

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

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

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

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

Computer Graphics. OpenGL

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

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

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

GLUT. What is OpenGL? Introduction to OpenGL and GLUT

OpenGL Introduction Computer Graphics and Visualization

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Computer graphics MN1

Computer Graphics, Chapt 08

COMP 371/4 Computer Graphics Week 1

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

OpenGL. Toolkits.

Lecture 3 Advanced Computer Graphics (CS & SE )

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

Computer Graphics Course 2005

Drawing Primitives. OpenGL basics

Image Processing. Geometry Processing. Reading: (Not really covered in our text. See Sects 18.1, 18.2.) Overview: Display

Programming with OpenGL Part 1: Background

OpenGL refresher. Advanced Computer Graphics 2012

Precept 2 Aleksey Boyko February 18, 2011

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

Programming with OpenGL Part 1: Background

Assignment 1. Simple Graphics program using OpenGL

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

Basic Graphics Programming

Computer Graphics (Basic OpenGL)

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

Computer Graphics Introduction to OpenGL

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

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

Computer graphic -- Programming with OpenGL I

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

Graphics Programming

Computer Graphics Primitive Attributes

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

Computer Graphics. Chapter 3 Computer Graphics Software

An Overview GLUT GLSL GLEW

Introduction to OpenGL

Basic Graphics Programming

Introduction to OpenGL

Programming of Graphics

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

An Interactive Introduction to OpenGL Programming

OpenGL/GLUT Intro. Week 1, Fri Jan 12

to OpenGL Introduction Pipeline Graphics pipeline OpenGL pipeline OpenGL syntax Modeling Arrays Conclusion 1 Introduction Introduction to OpenGL

CS Computer Graphics: Intro to OpenGL

CS Computer Graphics: Intro to OpenGL

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

Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

LECTURE 02 OPENGL API

C OMPUTER G RAPHICS Thursday

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

Draw the basic Geometry Objects. Hanyang University

OpenGL and GLUT. COMP413 Computer Graphics. School of Computer Sci. & Eng. Kyungpook National University, Korea. Spring Semester, 2016

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

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

CSE 167: Introduction to Computer Graphics Lecture #5: Visibility, OpenGL

Lecture 3. Understanding of OPenGL programming

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

cs123 Lab 3 OpenGL Part One 1 Introduction 2 OpenGL Basics 2.2 The Rendering Pipeline 2.1 The CPU and the GPU 2.3 Basic Syntax of OpenGL commands

Graphics and Visualization

Lecture 10: Input, Interaction & callbacks. CITS 3003 Graphics & Animation

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

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

Interaction. CSCI 480 Computer Graphics Lecture 3

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Graphics Pipeline & APIs

Image Rendering. Rendering can be divided into three sub-problems. Image Formation The hidden surface problem visibility determination steps

1 (Practice 1) Introduction to OpenGL

CS 432 Interactive Computer Graphics

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

Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

Announcements OpenGL. Computer Graphics. Spring CS4815

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

Graphics Pipeline & APIs

void drawdot(glint x, GLint y) { glbegin(gl_points); glvertex2i(x,y); glend();

Transcription:

Introduction to OpenGL CSCI 4229/5229 Computer Graphics Fall 2012

OpenGL by Example Learn OpenGL by reading nehe.gamedev.net Excellent free tutorial Code available for many platforms and languages OpenGL: A Primer (3ed) by Edward Angel Short and sweet OpenGL Programming Guide (Red Book) Free older editions as PDF OpenGL Superbible Theory and Applications

What is OpenGL? Sometimes called a language, actually an Application Programming Interface (API) Specification is controlled by OpenGL Architecture Review Board (ARB) Multiple implementations by different vendors Mesa & FreeGLUT free implementations OpenGL just does real time graphics Need GLX/WGL/AGL for windowing and input Limited font support (in GLUT) No sound, printing, etc. support

OpenGL Versions 1.0 Initial release (1992) 1.1 Major upgrade (1997) Lastest version on some Windows system 1.2 Improves textures (1998) 1.3-1.5 Incremental improvements (2001-2003) 2.0 Relaxes restrictions, adds shader (2004) 2.1-2.3 Incremental improvement (2006-7) 3.0 Support advanced hardware features (2008) 3.1-3.3 Improved shaders (2009) 4.0 Merge desktop and devices (2010) 4.1-4.3 Additional shaders

OpenGL Deprecation I will teach you OpenGL 2.0 Feature rich Flat learning curve OpenGL Core Profile concentrates on rendering Improved execution time performance User must provide deprecated functionality Steepens the learning curve Deprecated features in Compatibility Profile Increases reliance on third party libraries Adds development time until tools mature

OpenGL APIs Languages C, C++, C# FORTRAN Java Perl Python Ada Packages Qt (QGLWidget) SDL Many others

OpenGL and Friends From OpenGL: A Primer

OpenGL on X11 From OpenGL: A Primer

GLU: OpenGL Utility Higher Level and Convenience Functions Projections Creating texture maps NURBS, quadrics, tessalation Predefined objects (sphere, cylinder, teapot) Collections of calls for convenience Standard with all OpenGL implementations

GLUT: GL Utility Toolkit Provides access to OS and Window System Open windows and setting size and capabilities Register and triggers callbacks Keyboard and mouse interaction Elementary fonts Not part of OpenGL, but provides a portable abstraction of the OS FreeGLUT OpenGLUT Alternatives: SDL, Qt,...

Header Files and Libraries Usually you only need #define GL_GLEXT_PROTOTYPES #include <GL/glut.h> Header file locations /usr/include/gl on most systems Linking may only need -l glut -l GLU Special cases Some unices put OpenGL in /usr/x11r6 OS/X separates GL and GLUT Windows differs depending on the compiler

OpenGL Naming Convention gldosomethingxy() DoSomething is the name of the function X is 2 or 3 or 4 for the dimension y is for the the variable type b GLbyte (signed char) 8 bit s GLshort (signed short) 16 bit i GLint (signed int) 32 bit ub GLubyte (unsigned char) 8 bit us GLushort (unsigned short) 16 bit ui GLuint (unsigned int) 32 bit f GLfloat (float) 32 bit d GLdouble (double) 64 bit

OpenGL Naming Example Vertex glvertex3i(0, 0, 1) glvertex2d(27.34, 88.12) glvertex3dv(array) Few functions return a value Most functions created by name mangling Constants are GL_SOMETHING Variable types are GLsomething

GLUT and GLU Naming Functions glutdosomething gludosomething Constants GLUT_SOMETHING GLU_SOMETHING You can always tell by the name which API supplies a function or constant Avoid things starting with glx, wgl & agl

GLUT: GL Utility Toolkit Supplies interface to OS Windowing Interaction Hello World in GLUT (well sorta) int main(int argc,char* argv[ ]) { glutinit(&argc,argv); glutcreatewindow( Hello World ); glutdisplayfunc(display); glutmainloop(); }

Completing Hello World Draw a triangle #include <GL/glut.h> void display() { glclear(gl_color_buffer_bit); glbegin(gl_polygon); glvertex2f(0.0,0.5); glvertex2f(0.5,-0.5); glvertex2f(-0.5,-0.5); glend(); glflush(); }

Compile, link and run cc -o helloworld helloworld.c -lglut Heavily relies on defaults Window Viewport Projection Color

Event Driven Programming Don't call us, we'll call you register callbacks corresponding to events similar to interrupt driven programs DO NOT explicitly call display() request redisplay using glutpostredisplay() NEVER call sleep() use global/static variables and wall time for timing use gluttimerfunc() for regular events Return control as soon as possible

glbegin(type) Types of Objects GL_POINTS points GL_LINES lines between pairs of points GL_LINE_STRIP series of line segments GL_LINE_LOOP closed GL_LINE_STRIP GL_POLYGON simple polygon GL_TRIANGLES triangles between triples of points GL_TRIANGLE_STRIP series of triangles GL_TRIANGLE_FAN fan of triangles Set coordinates with glvertex glend()

Qualifiers glpointsize(float size) POINT size in pixels (default 1) gllinewidth(float width) LINE width in pixels (default 1) gllinestipple(int factor,unsigned short pattern) LINE type Requires glenable(gl_line_stipple)

Default is RGB color Color X11 TrueColor R,G,B 0-1 or integer range glcolor3f(1.0, 0.0.0.0) glcolor3b(127, 0, 0); glcolor3ub(255, 0, 0); glcolor3fv(rgbarray); Color can also contain transparency (alpha) glcolor4f(1.0, 0.0. 0.0, 0.5); Default alpha=1 (opaque) Stays in effect until you change color

X11 Direct Color Indexed Color Based on a colormap Set color using glindexi(27) Need to load colors into color map using glutsetcolor() Use RGB color unless hardware constraine Deprecated in OpenGL 3 since it really is obsolete

Displaying a scene Register using glutdisplayfunc() glclear() Draw Something glflush() glutswapbuffers() Schedule using glutpostredisplay()

Transformations Transformation apply to everything that follows Transformations are cumulative Call glloadidentity() in display() Primitive operations glloadidentity(); gltranslate[fd](dx, dy, dz) glscale[fd](sx, Sy, Sz) glrotate[fd](angle, Ux, Uy, Uz) Compatibility profile in OpenGL4 still useful

gltranslate[fd](dx, dy, dz); Move an object in three dimensions Allows you to easily produce multiple copies of an object Always takes 3D coordinates (float or double)

glscale[fd](sx, Sy, Sz) Change the scale along the axes Multiplicative factors S <1 shrink S >1 expand Negative values creates mirror image Allows you to easily create multiple copies of the same type at different sizes

glrotate[fd](angle, Ux, Uy, Uz) Rotates around the origin and axis (Ux,Uy,Uz) Angle is measured in degrees The axis can be a primary axis (X,Y,Z) but may be axis Allows you to create multiple copies of the same object viewed from different sides, or to view the scene from different positions

Temporary Transformations glpushmatrix() Saves the current transformation glpopmatrix() Resets the transformation to what it was when you did the push Allows you to build complex transformations and then get them back

Compound Transformations Rotate angle around the point (X,Y,Z) and axis (Ux,Uy,Uz) gltranslated(-x,-y,-z) glrotated(angle,ux,uy,uz) gltranslated(x,y,z) OpenGL does this intelligently

Projections Orthographic glortho(left,right,bottom,top,near,far) Same size regardless of distance Easiest to use Perspective glfrustrum(left,right,bottom,top,near,far) Closer objects are bigger GLU convenience functions gluperspective(fov,aspect,znear,zfar) glulookat(ex,ey,ez, Cx,Cy,Cz, Ux,Uy,Uz)

Text OpenGL provides only hooks for fonts Stroked fonts Lines and fills write the characters Bitmap (raster) fonts Characters are raster images Orientation, size, etc. treated just like any other drawing elements

Text using GLUT glutbitmapcharacter(glut_fonttype,ch) Single charcter Limited font selection glrasterpos3d(x,y,z) Sets position to write text in (x,y,z) coordinates glwindowpos2i(x,y) Sets position to write text in pixels coordinates

Display Registering Callbacks glutdisplayfunc() Draw the scene glutreshapefunc() Window resized glutidlefunc() Nothing more scheduled User input glutkeyboardfunc() Key pressed glutspecialfunc() Special key pressed glutmousefunc() Mouse button glutmotionfunc() Mouse motion Many more

Keyboard Input special(int key,int x,int y) Cursor keys GLUT_KEY_LEFT, GLUT_KEY_UP,... Function keys GLUT_KEY_Fx Basically anything not an ASCII key keyboard(char ch,int x,int y) Regular keystrokes (x,y) is the mouse position in pixels

Setting Modes glutinitdisplaymode Interfaces with the window manager to get the right kind of window (BE CAREFUL ABOUT DEFAULTS) glenable() & gldisable() Switches OpenGL into various modes GL_DEPTH_TEST GL_ALPHA_TEST GL_CULL_FACE GL_LIGHTING Different modes for different objects

Checking for Errors OpenGL fails silently Functions do not return an error code glgeterror() must be called explicitly to check for errors A black screen is a sure signal of an error