Introduction to 2D Graphics

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

CS 450: COMPUTER GRAPHICS REVIEW: INTRODUCTION TO COMPUTER GRAPHICS SPRING 2016 DR. MICHAEL J. REALE

CS451Real-time Rendering Pipeline

Graphics Programming. Computer Graphics, VT 2016 Lecture 2, Chapter 2. Fredrik Nysjö Centre for Image analysis Uppsala University

Pipeline Operations. CS 4620 Lecture Steve Marschner. Cornell CS4620 Spring 2018 Lecture 11

Ciril Bohak. - INTRODUCTION TO WEBGL

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

CHAPTER 1 Graphics Systems and Models 3

Programming shaders & GPUs Christian Miller CS Fall 2011

3D Programming. 3D Programming Concepts. Outline. 3D Concepts. 3D Concepts -- Coordinate Systems. 3D Concepts Displaying 3D Models

3D GRAPHICS. design. animate. render

Pipeline Operations. CS 4620 Lecture 14

Real-Time Rendering (Echtzeitgraphik) Michael Wimmer

WebGL A quick introduction. J. Madeira V. 0.2 September 2017

CS 4620 Program 3: Pipeline

Lecture 13: OpenGL Shading Language (GLSL)

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Lab 2-3D Transformations and Vertex Shaders

WebGL (Web Graphics Library) is the new standard for 3D graphics on the Web, designed for rendering 2D graphics and interactive 3D graphics.

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

The Rendering Pipeline (1)

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

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

The Graphics Pipeline and OpenGL I: Transformations!

CS 418: Interactive Computer Graphics. Introduction. Eric Shaffer

1 20 pts Graphics Pipeline short answer

Computer Graphics Seminar

Spring 2011 Prof. Hyesoon Kim

2: Introducing image synthesis. Some orientation how did we get here? Graphics system architecture Overview of OpenGL / GLU / GLUT

Computer Graphics (CS 543) Lecture 1 (Part 1): Introduction to Computer Graphics

Rendering Objects. Need to transform all geometry then

OpenGL SUPERBIBLE. Fifth Edition. Comprehensive Tutorial and Reference. Richard S. Wright, Jr. Nicholas Haemel Graham Sellers Benjamin Lipchak

2D rendering takes a photo of the 2D scene with a virtual camera that selects an axis aligned rectangle from the scene. The photograph is placed into

Lecture 2. Shaders, GLSL and GPGPU

Graphics Pipeline 2D Geometric Transformations

Models and Architectures

The Importance of Matrices in the DirectX API. by adding support in the programming language for frequently used calculations.

PROFESSIONAL. WebGL Programming DEVELOPING 3D GRAPHICS FOR THE WEB. Andreas Anyuru WILEY. John Wiley & Sons, Ltd.

Game Architecture. 2/19/16: Rasterization

COMP Preliminaries Jan. 6, 2015

CS475/CS675 - Computer Graphics. OpenGL Drawing

Visual HTML5. Human Information Interaction for Knowledge Extraction, Interaction, Utilization, Decision making HI-I-KEIUD

Rasterization and Graphics Hardware. Not just about fancy 3D! Rendering/Rasterization. The simplest case: Points. When do we care?

Computer graphics 2: Graduate seminar in computational aesthetics

TSBK03 Screen-Space Ambient Occlusion

IMAGE-BASED RENDERING AND ANIMATION

CS 130 Exam I. Fall 2015

Game Programming with. presented by Nathan Baur

Rendering. Converting a 3D scene to a 2D image. Camera. Light. Rendering. View Plane

Introduction. What s New in This Edition

Spring 2009 Prof. Hyesoon Kim

CS 432 Interactive Computer Graphics

Today. Parity. General Polygons? Non-Zero Winding Rule. Winding Numbers. CS559 Lecture 11 Polygons, Transformations

Computer Graphics - Treasure Hunter

Shaders. Slide credit to Prof. Zwicker

Cornell CS4620 Fall 2011!Lecture Kavita Bala (with previous instructors James/Marschner) Cornell CS4620 Fall 2011!Lecture 1.

WebGL and GLSL Basics. CS559 Fall 2015 Lecture 10 October 6, 2015

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

CGT520 Transformations

S U N G - E U I YO O N, K A I S T R E N D E R I N G F R E E LY A VA I L A B L E O N T H E I N T E R N E T

2D/3D Geometric Transformations and Scene Graphs

Today. Rendering pipeline. Rendering pipeline. Object vs. Image order. Rendering engine Rendering engine (jtrt) Computergrafik. Rendering pipeline

Core Graphics and OpenGL ES. Dr. Sarah Abraham

CS 465 Program 4: Modeller

We assume that you are familiar with the following:

Culling. Computer Graphics CSE 167 Lecture 12

Lecture 6 of 41. Scan Conversion 1 of 2: Midpoint Algorithm for Lines and Ellipses

Lecture 6 of 41. Scan Conversion 1 of 2: Midpoint Algorithm for Lines and Ellipses

Drawing Fast The Graphics Pipeline

Reading. Hierarchical Modeling. Symbols and instances. Required: Angel, sections , 9.8. Optional:

Adaptive Point Cloud Rendering

Computer Graphics Fundamentals. Jon Macey

We will use WebGL 1.0. WebGL 2.0 is now being supported by most browsers but requires a better GPU so may not run on older computers or on most cell

Introduction to Computer Graphics with WebGL

The Application Stage. The Game Loop, Resource Management and Renderer Design

EECE 478. Learning Objectives. Learning Objectives. Rasterization & Scenes. Rasterization. Compositing

Announcements. Submitting Programs Upload source and executable(s) (Windows or Mac) to digital dropbox on Blackboard

CS559 Computer Graphics Fall 2015

Drawing Fast The Graphics Pipeline

6.837 Introduction to Computer Graphics Final Exam Tuesday, December 20, :05-12pm Two hand-written sheet of notes (4 pages) allowed 1 SSD [ /17]

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

CS 130 Exam I. Fall 2015

The Graphics Pipeline and OpenGL I: Transformations!

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

This is the vector graphics "drawing" technology with its technical and creative beauty. SVG Inkscape vectors

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

CS4621/5621 Fall Computer Graphics Practicum Intro to OpenGL/GLSL

Rasterization Overview

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

CSE 167: Introduction to Computer Graphics Lecture #10: View Frustum Culling

RenderMonkey 1.6. Natalya Tatarchuk ATI Research

WebGL and GLSL Basics. CS559 Fall 2016 Lecture 14 October

CSE 167: Introduction to Computer Graphics Lecture #5: Rasterization. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2015

CMSC427 Transformations II: Viewing. Credit: some slides from Dr. Zwicker

Drawing in 3D (viewing, projection, and the rest of the pipeline)

Drawing in 3D (viewing, projection, and the rest of the pipeline)

Computer Graphics CS 543 Lecture 13a Curves, Tesselation/Geometry Shaders & Level of Detail

CS452/552; EE465/505. Clipping & Scan Conversion

CS123 INTRODUCTION TO COMPUTER GRAPHICS. Describing Shapes. Constructing Objects in Computer Graphics 1/15

Drawing Fast The Graphics Pipeline

Transcription:

Introduction to 2D Graphics Using OpenGL 1/32

Why Learn About OpenGL? A well-known industry standard for real-time 2D and 3D computer graphics Available on most platforms Desktop operating systems, mobile devices (OpenGL ES*, e.g., iphone), browsers (WebGL) Older (OpenGL 1.0) API provides features for rapid prototyping; newer API (OpenGL 2.0 and newer) provides more flexibility and control * ES is for Embedded Systems Many old features available in new API as deprecated functionality We will use the new API exclusively 2/32

Why Learn 2D first? A good stepping stone towards 3D many issues much easier to understand in 2D no need to simulate lights, cameras, the physics of light interacting with objects, etc. intro to modeling vs. rendering and other notions get used to rapid prototyping in OpenGL, both of designs and concepts 2D is still really important and the most common use of computer graphics, e.g. in UI/UX, documents, browsers 3/32

Graphics Platforms (1/4) Applications that only write pixels are rare Application Model (AM) is the data being represented by a rendered image manipulated by user interaction with the application typically a hierarchical model, with components built from lower-level components Graphics Platform is intermediary between App and platform rendering and interaction handling 4/32

Graphics Platforms (2/4) Graphics Platform runs in conjunction with window manager Determines what section of the screen is allocated to the application Handles chrome (title bar, resize handles); client area is controlled by application 5/32

Graphics Platforms (3/4) Typically, AM uses client area for: user interface to collect input to the AM display some representation of AM in the viewport This is usually called the scene, in the context of both 2D and 3D applications Scene is rendered by the scene generator, which is typically separate from the UI generator, which renders rest of UI 6/32

CS123 INTRODUCTION TO COMPUTER GRAPHICS Graphics Platforms (4/4) Early raster graphics packages/libraries/platforms RamTek library 1981, Apple QuickDraw 1984 Microsoft's Graphics Display Interface (GDI 1990, now GDI+), Java.awt.Graphics2D Earliest packages usually had these characteristics: geometric primitives/shapes, appearance attributes specified in attribute bundles (a.k.a. graphical contexts / brushes ) applied modally rather than in a parameter list for each primitive (too many parameters for that) integer coordinates map directly to screen pixels on output device immediate mode (no record kept of display commands) no built-in functions for applying transforms to primitives no built-in support for component hierarchy (no composite shapes) Early packages were little more than assembly languages for display device 7/32

Problems with Early Graphics Platforms (1/3) Geometric Scalability Integer coordinates mapped to display pixels affects apparent size of image: large on low-res display & small on high-res display Application needs flexible internal coordinate representation floating point is essential float to fixed conversion required; actually a general mapping 8/32

Problems with Early Graphics Platforms (2/3) Display updates To perform operations on objects in scene, application must keep list of all primitives and their attributes (along with application-specific data) Some updates are transitory feedback animations, only a display change Consider an interior-design layout application when user picks up an object and drags to new location, object follows cursor movement interim movements do not relate to data changes in application model, purely visual changes application model only updated when user drops object (releases mouse button) in immediate mode, application must re-specify entire scene each time cursor moves Alternatively, use a retained mode platform, which will store an internal representation of all objects in scene called a display model to distinguish it from application model from which it is derived 9/32

Problems with Early Graphics Platforms (3/3) Interaction Consider a simple clock example: User clicks minute hand, location must be mapped to relevant application object; called pick correlation Developer responsible for pick correlation (usually some kind of "point-inbounding box rectangle" test based on pick coordinates) find top-most object at clicked location may need to find entire composite object hierarchy from lowest-level primitive to highest level composite e.g., triangle -> hand -> clock Solution: retained mode can do pick correlation, as it has a representation of scene 10/32

Modern Graphics Platforms (1/2) Device-independent floating point coordinate system packages convert application-space" to "device-space" coordinates Specification of hierarchy support building scenes as hierarchy of objects, using transforms (scale, rotate, translate) to place children into parents' coordinate systems support manipulating composites as coherent objects Smart Objects (Widgets, etc.) graphic objects have innate behaviors and interaction responses e.g., button that automatically highlights itself when cursor is over it 11/32

Immediate Retained CS123 INTRODUCTION TO COMPUTER GRAPHICS Modern Graphics Platforms (2/2) GUI Platforms: Layout Managers and Smart Controls Templates and Reusability WPF Cocoa JavaFX Qt SVG Floating-point Coordinates GDI+ Quartz HTML5 canvas Integer Pixel Coordinates QuickDraw Orig. GDI 12/32

Immediate Mode Vs Retained Mode Immediate Mode (OpenGL, Vulkan, Microsoft s DirectX, Apple s Metal) Application model: stores both geometric information and non-geometric information in Application Database Platform keeps no record of primitives that compose scene Vulkan was originally the next version of OpenGL (code name OpenGL Next) but was eventually released as its own API. Both are maintained by The Khronos Group. 13/32

Immediate Mode Vs Retained Mode Retained Mode (WPF, SVG, most game engines) Application model in app and Display model in platform Display model contains information that defines geometry to be viewed Display model is a geometric subset of Application model (typically a scene graph) Simple drawing application does not need Application model (e.g., clock example) No right answer on which to use context-dependent tradeoffs (see Chapter 16) 14/32

OpenGL (1/3) Immediate-mode graphics API No display model, application must direct OpenGL to draw primitives Implemented in C, also works in C++ Bindings available for many other programming languages Cross-platform Also available on mobile (OpenGL ES ) and in the browser (WebGL) Different platforms provide glue code for initializing OpenGL within the desktop manager (e.g. GLX, WGL) Labs and projects for CS123 use Qt library to abstract this glue code away 15/32

OpenGL (2/3) Created by Silicon Graphics Inc. (SGI, http://sgi.com) in 1992, now managed by the non-profit Khronos Group (http://khronos.org) Originally aimed to allow any OpenGL program to run on a variety of graphics hardware devices Invented when fixed-function hardware was the norm Techniques were implemented in the hardware; OpenGL calls sent commands to the hardware to activate / configure different features Now supports programmable hardware the common industry practice today Modern graphics cards are miniature, highly parallel computers themselves, with many-core GPUs, onboard RAM, etc. GPUs are a large collection of highly parallel high speed arithmetic units; several thousand cores! GPUs run simple programs (called shaders ): take in vertices and other data and output a color value for an individual pixel. GLSL, (O)GL Shader Language, is C-like language, controls arithmetic pipelines Other shader languages: (DirectX) High-Level Shader Language, RenderMan Shading Language for offline rendering Implement new features in shaders instead of waiting for hardware vendors to support them in h/w Your final project (typically a team project) will involve writing your choice of shaders (learn in labs) 16/32

OpenGL (3/3) Fixed-function API provides features that make it easier to prototype e.g., the OGL library implements much of the linear algebra needed to move objects on the screen GL utility library ( GLU ) provides additional high-level utilities Programmable API implements most of the fixed-function API for backwards compatibility, but uses shaders for implementation Only true for desktop; must use shaders exclusively to program with OpenGL ES 2.0+ or WebGL We will use GLM (OpenGL Mathematics) to do our linear algebra instead of using the Fixed-function API 17/32

Shaders In future labs and your final project you will write your own shaders, but for now we will provide shaders for you. Various types of input to shaders Attributes are provided per-vertex Uniforms are provided per-object; have the same value for a group of vertices OpenGL has many built in types including vectors and matrices To provide this input you must provide an identifier ( location ) of the Attribute or Uniform glgetattriblocation for attributes glgetuniformlocation for uniforms Labs will cover details on how to use these functions 18/32

Representing Shapes 3D shapes are usually represented as a collection of vertices that make up triangles or quads OpenGL uses triangles Other methods include 3D voxels, polynomial splines, etc. We can use triangles to build arbitrary polygons, and approximate smooth shapes. A complex polygon made of triangle primitives An approximate circle made of triangle primitives 19/32

Coordinate Systems (1/3) Cartesian coordinates in math, engineering typically modeled as floating point typically X increasing right, Y increasing up Display (physical) coordinates integer only typically X increasing right, Y increasing down 1 unit = 1 pixel But we want to be insulated from physical display (pixel) coordinates OpenGL is the intermediary 20/32

Coordinate Systems (2/3) OpenGL Coordinates (which it maps to the window) Choose a convention For us: X increases right, Y increases up Units are based on the size of the window or screen Visible area stretches to fill window Units correlate to percentage of window size, don t correspond to physical units or pixels Define coordinate system using the projection matrix. Supply it to shader as a uniform variable (the term projection matrix will become clear) Note: 3D glm functions still work in the special case of 2D just use our defaults glm::mat4 projectionmat; // Our projection matrix is a 4x4 matrix projectionmat = glm::ortho(-1, // X coordinate of left edge 1, // X coordinate of right edge -1, // Y coordinate of bottom edge 1, // Y coordinate of top edge 1, // Z coordinate of the near plane -1); // Z coordinate of the far plane 21/32

Coordinate Systems (3/3) Two choices on how to think Draw everything in OpenGL coordinate system This can get very inconvenient! Choose your own abstract coordinate system natural for your app (in nanometers, lightyears, ), then specify all app s primitives to OpenGL using your coordinates. Must also specify a transformation to map the application coordinates to OpenGL coordinates Transformation usually means a change - scale, rotate, and/or translate or a combination of changes Application Coordinates Display OGL Coordinates 22/32

Winding Order Order is important: vertices must be specified in counter-clockwise order relative to the viewer. Otherwise nothing shows up! Winding order determines whether a triangle s normal vector is facing in front or behind it. Triangles facing the wrong way will be invisible! Counter-clockwise winding consistent with the right-hand rule GLfloat vertexdata[] = { -.7, -.7,.7, -.7,.7,.7, -.7,.7, }; GLfloat vertexdata[] = { -.7, -.7, -.7,.7,.7,.7,.7, -.7, }; N NX 23/32

Transformations (1/3) Use GLM to do linear algebra for building (hierarchical) models that constitute the scene (aka the world ) supplying two matrices to OGL to control where and how the scene is to appear (see OGL 3D lecture) More about the significance of these matrices in viewing lectures; for now only use the model matrix which is used to position objects in the scene For the following examples assume we are already keeping track of the model matrix initialized like this: glm::mat4 model = glm::mat4(1.0); //creates a 4x4 identity matrix with 1 s in the main diagonal, //identity matrix doesn t change the position of an object 24/32

Transformations (2/3) Geometric Transformations in 2D (note the z-coordinate is 0) Original Translate model *= glm::translate(.1,.1, 0); Original Rotate model *= glm::rotate(-45, glm::vec3(0, 0, 1)); Original Scale model *= glm::scale(2, 2, 1); Positive angles rotate counter-clockwise, here about the origin (i.e., Z-axis as vector) 25/32

Transformations (3/3) Transformations can be composed (combined by multiplying matrices) but are NOT commutative, so proper order is vital model *= glm::rotate(-90, glm::vec3(0, 0, 1)); model *= glm::scale(2, 1, 1); model *= glm::scale(2, 1, 1); model *= glm::rotate(-90, glm::vec3(0, 0, 1)); 26/32

Animation (1/3) Rapidly displaying sequence of images to create an illusion of movement Flipbook (https://www.youtube.com/watch?v=csj0lajqbrm) Keyframe animation: specify keyframes, computer interpolates (e.g., ball bouncing) Flipbook Keyframe Animation 27/32

Animation (2/3) Idea: Move objects incrementally every time we render Example: Animating the hands of a clock Given the number of seconds elapsed, how many degrees should we rotate the seconds hand? need to convert from seconds to degrees Idea: Use rotations around the clock as a common conversion factor Seconds per revolution: 60 Degrees per revolution: 360 Every time we render, we need to recalculate the position of the hands 28/32

Animation (3/3) //Example in code float secondselapsed =...; // num seconds since last render const float SECONDS_PER_REVOLUTION = 60; const float DEGREES_PER_REVOLUTION = 360; secondsangle += -1 // Turn clockwise * secondselapsed // Δt * DEGREES_PER_REVOLUTION // Turn 360 degrees... / SECONDS_PER_REVOLUTION; //... every 60 seconds 29/32

Book Sections Preface, Intro as useful background Chapter 2 while written in terms of MSFT s WPF, a retained-mode library, the concepts carry over to OGL. Useful to know about HTML/XML style syntax, given its prominence, but don t worry about the syntactic details 30/32

OpenGL Basics Lab (1/2) An intro to OpenGL data structures to represent vertex geometry that will be held this week Generate 2D graphics and learn the modern OpenGL pipeline 31/32

OpenGL Basics Lab (2/2) First lab available now It s an important foundation The OpenGL 3D lecture will make more sense and your life will be much easier if you come to lab Don t miss it Reminder: you can get your labs checked off by a TA at hours as well 32/32