Basic Graphics Programming

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

Basic Graphics Programming

1/12/11. Basic Graphics Programming. What is OpenGL. OpenGL is cross-platform. How does OpenGL work. How does OpenGL work (continued) The result

1.2 Basic Graphics Programming

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

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

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

Exercise 1 Introduction to OpenGL

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

Lecture 4 of 41. Lab 1a: OpenGL Basics

Computer Graphics (Basic OpenGL)

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

Precept 2 Aleksey Boyko February 18, 2011

Introduction to Computer Graphics with OpenGL/GLUT

Lectures OpenGL Introduction

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Programming using OpenGL: A first Introduction

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

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


Computer graphics MN1

Graphics Programming

Programming with OpenGL Part 1: Background

RECITATION - 1. Ceng477 Fall

Introduction to OpenGL Week 1

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

OpenGL refresher. Advanced Computer Graphics 2012

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

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

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Lecture 3. Understanding of OPenGL programming

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

Interaction. CSCI 480 Computer Graphics Lecture 3

Introduction to OpenGL: Part 2

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

Computer Graphics. Bing-Yu Chen National Taiwan University

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

Display Lists in OpenGL

CS 4204 Computer Graphics

Computer Graphics Introduction to OpenGL

Computer Graphics. OpenGL

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

Using OpenGL with CUDA

Computer Graphics 1 Computer Graphics 1

Lecture 3 Advanced Computer Graphics (CS & SE )

Introduction to OpenGL

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

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

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

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

Graphics Hardware and OpenGL

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

OpenGL. Toolkits.

COMPUTER GRAPHICS LAB # 3

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

Interaction. CSCI 420 Computer Graphics Lecture 3

Introduction to OpenGL

SOURCES AND URLS BIBLIOGRAPHY AND REFERENCES

Interaction Computer Graphics I Lecture 3

Computer Graphics, Chapt 08

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

Computer Graphics Primitive Attributes

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

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

COMP 371/4 Computer Graphics Week 1

CS Computer Graphics: OpenGL, Continued

CS Computer Graphics: OpenGL, Continued

Programming with OpenGL Part 3: Three Dimensions

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

Lecture 2 2D transformations Introduction to OpenGL

Drawing Primitives. OpenGL basics

2 Transformations and Homogeneous Coordinates

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Assignment 1. Simple Graphics program using OpenGL

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

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

Computer Graphics Course 2005

Lectures Display List

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

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

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

Rendering. Part 1 An introduction to OpenGL

Rendering Pipeline/ OpenGL

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

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

OpenGL Introduction Computer Graphics and Visualization

Announcements OpenGL. Computer Graphics. Spring CS4815

Class of Algorithms. Visible Surface Determination. Back Face Culling Test. Back Face Culling: Object Space v. Back Face Culling: Object Space.

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

CSC 8470 Computer Graphics. What is Computer Graphics?

Chapter 13 Selection and Feedback

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

Intro to OpenGL III. Don Fussell Computer Science Department The University of Texas at Austin

OpenGL JOGL. OpenGL & JOGL. Shaoting Zhang, or Tony. September 12, 2007

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

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

Visualizing Molecular Dynamics

6. Make use of glviewport() to display two sine curves on the same screen, one on the

Graphics Pipeline & APIs

Transcription:

CSCI 480 Computer Graphics Lecture 2 Basic Graphics Programming January 11, 2012 Jernej Barbic University of Southern California http://www-bcf.usc.edu/~jbarbic/cs480-s12/ Graphics Pipeline OpenGL API Primitives: Lines, Polygons Attributes: Color Example [Angel Ch. 2] 1

What is OpenGL A low- level graphics library (API) for 2D and 3D interac>ve graphics. Descendent of GL (from SGI) First version in 1992; now: 4.2 (2012) Managed by Khronos Group (non- profit consor>um) API is governed by Architecture Review Board (part of Khronos) 2

Where is OpenGL used CAD Virtual reality Scien>fic visualiza>on Flight simula>on Video games 3

Graphics library (API) Intermediary between applica>ons and graphics hardware Other popular APIs: Direct3D (MicrosoU) OpenGL ES (embedded devices) X3D (successor of VRML) 4

OpenGL is cross-platform Same code works with liyle/no modifica>ons Implementa>ons: Windows, Mac, Linux: ships with the OS Linux: Mesa, a freeware implementa>on. 5

How does OpenGL work From the programmer s point of view: 1. Specify geometric objects 2. Describe object properties Color How objects reflect light 6

How does OpenGL work (continued) 3. Define how objects should be viewed where is the camera what type of camera 4. Specify light sources where, what kind 5. Move camera or objects around for animation 7

The result the result 8

OpenGL is a state machine State variables: color, camera position, light position, material properties... These variables (the state) then apply to every subsequent drawing command. They persist until set to new values by the programmer. 9

OpenGL Library Organization GL (Graphics Library): core graphics capabilities GLU (OpenGL Utility Library): utilities on top of GL GLUT (OpenGL Utility Toolkit): input and windowing 10

Graphics Pipeline Primitives+ material properties Translate Rotate Scale Is it visible on screen? 3D to 2D Convert to pixels Shown on the screen (framebuffer) 11

OpenGL uses immediate-mode rendering Application generates stream of geometric primitives (polygons, lines) System draws each one into the framebuffer Entire scene redrawn anew every frame Compare to: off-line rendering (e.g., Pixar Renderman, ray tracers) 12

The pipeline is implemented by OpenGL, graphics driver and the graphics hardware OpenGL programmer does not need to implement the pipeline. However, pipeline is reconfigurable if needed shaders 13

Graphics Pipeline Efficiently implementable in hardware (but not in software) Each stage can employ multiple specialized processors, working in parallel, busses between stages #processors per stage, bus bandwidths are fully tuned for typical graphics use Latency vs throughput 14

Vertices Vertices in world coordinates void glvertex3f(glfloat x, GLfloat y, GLfloat z) Vertex (x, y, z) is sent down the pipeline. Function call then returns. Use GLtype for portability and consistency glvertex{234}{sfid}[v](type coords) 15

Transformer Transformer in world coordinates Must be set before object is drawn! glrotatef(45.0, 0.0, 0.0, -1.0); glvertex2f(1.0, 0.0); Complex [Angel Ch. 4] 16

Clipper Mostly automatic (must set viewport) 17

Projector Complex transformation [Angel Ch. 5] Orthographic Perspective 18

Rasterizer Interesting algorithms [Angel Ch. 7] To window coordinates Antialiasing 19

Primitives Specified via vertices General schema glbegin(type); glvertex3f(x1, y1, z1);... glvertex3f(xn, yn, zn); glend(); type determines interpretation of vertices Can use glvertex2f(x,y) in 2D 20

Example: Draw Square Outline Type = GL_LINE_LOOP glbegin(gl_line_loop); glvertex3f(0.0, 0.0, 0.0); glvertex3f(1.0, 0.0, 0.0); glvertex3f(1.0, 1.0, 0.0); glvertex3f(0.0, 1.0, 0.0); glend(); Calls to other functions are allowed between glbegin(type) and glend(); 21

Points and Line Segments glbegin (GL_POINTS); glvertex3f( ); glvertex3f( ); glend(); Draw points 22

Polygons Polygons enclose an area Rendering of area (fill) depends on attributes All vertices must be in one plane in 3D 23

Polygon Restrictions OpenGL Polygons must be simple OpenGL Polygons must be convex (a) simple, but not convex (c) convex (b) non-simple 24

Why Polygon Restrictions? Non-convex and non-simple polygons are expensive to process and render Convexity and simplicity is expensive to test Behavior of OpenGL implementation on disallowed polygons is undefined Some tools in GLU for decomposing complex polygons (tessellation) Triangles are most efficient 25

Polygon Strips Efficiency in space and time Reduces visual artefacts Polygons have a front and a back, possibly with different attributes! 26

Attributes: color, shading and reflection properties Part of the OpenGL state Set before primitives are drawn Remain in effect until changed! 27

Physics of Color Electromagnetic radiation Can see only tiny piece of the spectrum 28

Color Filters Eye can perceive only 3 basic colors Computer screens designed accordingly Amplitude R G B Wavelength [nanometer] Source: Vos & Walraven 29

Color Spaces RGB (Red, Green, Blue) Convenient for display Can be unintuitive (3 floats in OpenGL) HSV (Hue, Saturation, Value) Hue: what color Saturation: how far away from gray Value: how bright Other formats for movies and printing 30

RGB vs HSV Gimp Color Picker B G G V R B R R G B H S 31

Example: Drawing a shaded polygon Initialization: the main function int main(int argc, char** argv) { glutinit(&argc, argv); glutinitdisplaymode (GLUT_DOUBLE GLUT_RGB); glutinitwindowsize (500, 500); glutinitwindowposition (100, 100); glutcreatewindow (argv[0]); init ();... 32

GLUT Callbacks Window system independent interaction glutmainloop processes events... glutdisplayfunc(display); glutreshapefunc(reshape); glutkeyboardfunc (keyboard); glutmainloop(); return 0; } 33

Initializing Attributes Separate in init function void init(void) { glclearcolor (0.0, 0.0, 0.0, 0.0); } /* glshademodel (GL_FLAT); */ glshademodel (GL_SMOOTH); 34

The Display Callback The routine where you render the object Install with glutdisplayfunc(display) void display(void) { glclear (GL_COLOR_BUFFER_BIT); /* clear buffer */ setupcamera(); /* set up the camera */ triangle (); /* draw triangle */ glutswapbuffers (); /* force display */ } 35

Drawing In world coordinates; remember state! void triangle(void) { glbegin (GL_TRIANGLES); glcolor3f (1.0, 0.0, 0.0); /* red */ glvertex2f (5.0, 5.0); glcolor3f (0.0, 1.0, 0.0); /* green */ glvertex2f (25.0, 5.0); glcolor3f (0.0, 0.0, 1.0); /* blue */ glvertex2f (5.0, 25.0); glend(); } 36

The Image glshademodel(gl_flat) color of last vertex glshademodel(gl_smooth) each vertex separate color smoothly interpolated 37

Flat vs Smooth Shading Flat Shading Smooth Shading 38

Projection Mapping world to screen coordinates void reshape(int w, int h) { glviewport (0, 0, (GLsizei) w, (GLsizei) h); glmatrixmode (GL_PROJECTION); glloadidentity (); if (w <= h) gluortho2d (0.0, 30.0, 0.0, 30.0 * (GLfloat) h/(glfloat) w); else gluortho2d (0.0, 30.0 * (GLfloat) w/(glfloat) h, 0.0, 30.0); glmatrixmode(gl_modelview); } 39

Orthographic Projection 2D and 3D versions glortho2d(left, right, bottom, top) In world coordinates! 40

Viewport Determines clipping in window coordinates glviewport(x, y, w, h) 41

Summary 1. 2. 3. 4. 5. A Graphics Pipeline The OpenGL API Primitives: vertices, lines, polygons Attributes: color Example: drawing a shaded triangle 42