Core Graphics and OpenGL ES. Dr. Sarah Abraham

Similar documents
Stanford CS193p. Developing Applications for ios. Spring CS193p. Spring 2016

Announcements. Lab 2 is due next Monday (Sept 25 th ) by 11:59 PM. Late policy is 10% of lab total per day late. So -7.5 points per day late for lab 2

Custom Drawing & Animation. CS 442: Mobile App Development Michael Saelee

Stanford CS193p. Developing Applications for ios. Winter CS193p. Winter 2017

CHAPTER 1 Graphics Systems and Models 3

Transformations. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

Custom Drawing & Animation. CS 442: Mobile App Development Michael Saelee

Affine Transformation. Edith Law & Mike Terry

GEOMETRIC TRANSFORMATIONS AND VIEWING

Software Engineering. ò Look up Design Patterns. ò Code Refactoring. ò Code Documentation? ò One of the lessons to learn for good design:

CS230 : Computer Graphics Lecture 4. Tamar Shinar Computer Science & Engineering UC Riverside

2D transformations: An introduction to the maths behind computer graphics

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY CS2401 COMPUTER GRAPHICS QUESTION BANK

UNIT 2 2D TRANSFORMATIONS

3D Mathematics. Co-ordinate systems, 3D primitives and affine transformations

Direct Rendering. Direct Rendering Goals

Graphics Pipeline 2D Geometric Transformations

03 Vector Graphics. Multimedia Systems. 2D and 3D Graphics, Transformations

Stanford CS193p. Developing Applications for ios. Fall Stanford CS193p. Fall 2011

Object Representation Affine Transforms. Polygonal Representation. Polygonal Representation. Polygonal Representation of Objects

CS451Real-time Rendering Pipeline

Stanford CS193p. Developing Applications for ios. Winter CS193p! Winter 2015

Institutionen för systemteknik

Lecture 11: Output 2. Fall UI Design and Implementation 1

Computer Graphics Seminar

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

(a) rotating 45 0 about the origin and then translating in the direction of vector I by 4 units and (b) translating and then rotation.

2D/3D Geometric Transformations and Scene Graphs

Affine Transformations. Transforming shape models Combining affine transformations Scene graphs, interactor trees Hit tests on transformed shapes

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

Vector Algebra Transformations. Lecture 4

2D and 3D Transformations AUI Course Denbigh Starkey

INKSCAPE BASICS. 125 S. Prospect Avenue, Elmhurst, IL (630) elmhurstpubliclibrary.org. Create, Make, and Build

Points and lines, Line drawing algorithms. Circle generating algorithms, Midpoint circle Parallel version of these algorithms

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

CS602 MCQ,s for midterm paper with reference solved by Shahid

Computing Visibility. Backface Culling for General Visibility. One More Trick with Planes. BSP Trees Ray Casting Depth Buffering Quiz

AQA GCSE Further Maths Topic Areas

Project report Augmented reality with ARToolKit

Computer Graphics 7: Viewing in 3-D

Graphics and Interaction Transformation geometry and homogeneous coordinates

Computer Graphics. Instructor: Oren Kapah. Office Hours: T.B.A.

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

CS602- Computer Graphics Solved MCQS From Midterm Papers. MIDTERM EXAMINATION Spring 2013 CS602- Computer Graphics

Merits of QT for developing Imaging Applications UI

Graphics and Interaction Rendering pipeline & object modelling

CS475/675: Computer Graphics End-Semester Examination (Solutions), Fall 2016

Topics and things to know about them:

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

Lets assume each object has a defined colour. Hence our illumination model is looks unrealistic.

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

Adobe Flash Course Syllabus

1 Attempt any three of the following: 1 5 a. What is Computer Graphics? How image is to be display on Video Display Device?

CS 464 Review. Review of Computer Graphics for Final Exam

12 APPLYING EFFECTS. Lesson overview

Math background. 2D Geometric Transformations. Implicit representations. Explicit representations. Read: CS 4620 Lecture 6

Computer Graphics I Lecture 11

This work is about a new method for generating diffusion curve style images. Although this topic is dealing with non-photorealistic rendering, as you

Graphics (Output) Primitives. Chapters 3 & 4

Adobe Photoshop Sh S.K. Sublania and Sh. Naresh Chand

Lecture 5 Demo Code: FaceIt MVC and Gestures

Game Architecture. 2/19/16: Rasterization

Spring 2009 Prof. Hyesoon Kim

Rendering Objects. Need to transform all geometry then

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Computer Graphics

End-Term Examination

Lecture 4: Transformations and Matrices. CSE Computer Graphics (Fall 2010)

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

Computer Graphics. Chapter 5 Geometric Transformations. Somsak Walairacht, Computer Engineering, KMITL

the gamedesigninitiative at cornell university Lecture 14 2D Sprite Graphics

6 Using Adobe illustrator: advanced

COMP30019 Graphics and Interaction Rendering pipeline & object modelling

Lecture outline. COMP30019 Graphics and Interaction Rendering pipeline & object modelling. Introduction to modelling

Cs602-computer graphics MCQS MIDTERM EXAMINATION SOLVED BY ~ LIBRIANSMINE ~

Class Meeting 05 (Lecture 04) Objectives for this class meeting. Conduct vote on basic style of game for class project

CS 4204 Computer Graphics

Development of Computer Graphics

Scalable Vector Graphics (SVG) vector image World Wide Web Consortium (W3C) defined with XML searched indexed scripted compressed Mozilla Firefox

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4620/8626 Computer Graphics Spring 2014 Homework Set 1 Suggested Answers

12 APPLYING EFFECTS. Lesson overview

Computer Graphics. Chapter 4 Attributes of Graphics Primitives. Somsak Walairacht, Computer Engineering, KMITL 1

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

Representing 2D Transformations as Matrices

Computer Graphics Lecture 2

IMAGE-BASED RENDERING AND ANIMATION

CSE528 Computer Graphics: Theory, Algorithms, and Applications

CS 381 Computer Graphics, Fall 2012 Midterm Exam Solutions. The Midterm Exam was given in class on Tuesday, October 16, 2012.

2D Image Transforms Computer Vision (Kris Kitani) Carnegie Mellon University

Computer Science 426 Midterm 3/11/04, 1:30PM-2:50PM

How to draw and create shapes

CS 381 Computer Graphics, Fall 2008 Midterm Exam Solutions. The Midterm Exam was given in class on Thursday, October 23, 2008.

SFU CMPT 361 Computer Graphics Fall 2017 Assignment 2. Assignment due Thursday, October 19, 11:59pm

Buffers, Textures, Compositing, and Blending. Overview. Buffers. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E.

Transforms. COMP 575/770 Spring 2013

SRM INSTITUTE OF SCIENCE AND TECHNOLOGY

The Rasterization Pipeline

CS 130 Exam I. Fall 2015

Module 13C: Using The 3D Graphics APIs OpenGL ES

Curves & Splines. Assignment #3. Overview & Objectives. Due Dates. CPSC 453 Fall 2018 University of Calgary

Stanford CS193p. Developing Applications for ios. Fall CS193p. Fall

Transcription:

Core Graphics and OpenGL ES Dr. Sarah Abraham University of Texas at Austin CS329e Fall 2018

Core Graphics Apple s vector-drawing framework Previously known as Quartz or Quartz2D Includes handling for: Geometric data such as points, vectors, shapes etc Functions for rendering pixels to screen

Vector Drawings Vectors defined mathematically (rather than by pixel) Allows for continuous scaling and additional manipulation More robust than bitmap (raster) graphics Built from geometric primitives like points, lines, curves, and shapes Points define lines and curves, lines and curves define shapes etc

Vector Example

Vectorization vs Rasterization

Vector Advantages: Infinite scaling (not resolution dependent) Smaller file size Used most frequently for line-art with flat, uniform coloring Logos, letterhead, fonts etc Bitmap Advantages: Capture gradations and complex composition A lot of detail at high resolution Used most frequently for graphics used at-resolution Photographs, scanned artwork, pixel-based art etc

Using Computer Graphics Graphics libraries require a graphics context Graphics context describes the state of world in which you are drawing 1. User tells graphics context how and where to draw 2. User specifies what to draw 3. Graphic context draws according to specification

What does the Graphics Context need to know? Graphic context identifies draw destination, coordinate system Destination can be screen, printer, PDF file, etc Graphics context maintains global information about current drawing attributes Transforms, line style, fill style, font style, etc Associated with a window and view in ios

Example: CGRect CGRect objects define location and dimensions of rectangles stroke() and fill() functions control appearance of object s line and body properties UIColor.white.setFill() rectangle.fill().setfill() defines fill color for current graphics context.fill() draws rectangle geometry with fill properties in current graphics context

Getting to the Screen In general, lots of things are being drawn to the screen via the graphics context Multiple objects can overlap Objects can have different levels of transparency How does the screen display all objects correctly?

Frame Buffer Contains final pixel values (RGB) to be displayed on the screen Must account for: Device aspect ratio and resolution Anti-aliasing

Quartz Compositor Context passes 2D content to Quartz Compositor Quartz Compositor combines all context content into rasterized frame buffer for display

Core Graphics Framework Classes prefixed with CG CGRect, CGFloat, CGLayer etc All CG calls executed in associated CGContext Bitmap graphics, PDF graphics, OpenGL etc

Drawing Directly to Screen Create subclass of UIView Override draw() to include necessary CoreGraphics draw commands Associate custom UIView with desired ViewController Note: Core Graphics is part of the V in MVC Should not affect your models or controllers

UIView draw() draw() is called whenever the View is updated Never call it directly Use setneedsdisplay() to manually request a screen refresh Can use a Timer from the ViewController if you want the screen to update continuously Using OpenGL directly allows for continual display automatically

Drawing in Core Graphics Can define several types of renderers for specific tasks: GraphicsRenderer, ImageRenderer, PDFRenderer Image and PDF Renderers optimize drawing image or PDF content to screen Graphics Renderers handles general draw calls UIBezierPath defines paths and curves for rendering

UIBezierPath Primary tool for customizing geometric paths and drawing properties within Core Graphics Easily reused and manipulated within the code Define as lines, ovals, arcs, rectangles, and arbitrary Bezier paths Also used for clipping and intersection tests

Defining Paths On UIBezierPath init, choose the path this object will represent init(rect: CGRect) init(ovalin: CGRect) init(roundedrect: CGRect, cornerradius: CGFloat) init(arccenter: CGPoint, radius: CGFloat, startangle: CGFloat, endangle: CGFloat, clockwise: Bool) init(cgpath: CGPath)

Drawing Paths Set drawing properties such as linewidth, linecapstyle, linejoinstyle Draw enclosed region with fill() Draw stroke outline with stroke() Can also set blend modes and transparency of outline or body for more advanced graphics

UIBezierPath Demo

Order of Draw Calls Order matters when drawing in Core Graphics! Pixels cannot be changed once they re painted Must draw over existing pixels with new draw commands

Other Uses of Paths addclip() intersects object path s body with the current graphics context s clipping path to update object s path contains(cgpoint) determines if object path s body contains specified point Allows for primitive hit detection

Quiz Question! Which function in UIView do we override to change the view s default appearance? A.draw() B.UIBezierPath() C.stroke() D.fill()

Quiz Answer A) draw() overrides UIView s default draw functionality. The other methods define the appearance of geometry within a UIView

Drawing with Contexts Get the view s current context: let context = UIGraphicsGetCurrentContext() Contexts allow for: Draw paths, rectangles, images, PDFs, gradients, etc Set colors, color space, shadows, transformations, etc

What are Transformations?

Affine Transformations (RichDoc Framework) Foundation of rendering in computer graphics Allows for manipulation of objects within a scene

Matrix Representation Represent a single vertex point p as a vector: Represent a 2-D transformation with matrix M: Multiply p by M to apply the transformation: x y " # $ % & ' M = a b c d " # $ % & ' " p = Mp " x " y # $ % & ' ( = a b c d # $ % & ' ( x y # $ % & ' (

Multiplication How do we multiply? # x "& % $ y "' ( = # a b &# % $ c d' ( x & % $ y ( ' x " = ax + by y " = cx + dy What if we multiply by the identity matrix? " 1 0% $ # 0 ' 1& " x = ax " y = dy

Scaling What happens y y with one of these matrices is applied to the square? " 2 0% $ ' # 0 2& 2 1 1 2 x 2 1 2 y 1 2 x " 1 2 0% $ ' # 0 2& 1 1 2 x

Reflection y x y y # 1 0& % ( $ 0 1' x x # 1 0 & % ( $ 0 1'

Shear " 1 b% $ ' # 0 1& x " = x + by y " = y y y 1 1 " 1 1% $ # 0 ' 1& 1 x 1 x

Rotation y y 1 1 x x $ M = R(θ) = cos(θ) R & % sin(θ) sin(θ) ' ) cos(θ) (

Linear Transformations All of these transformations are linear: Scaling Reflection Shearing Rotation What s missing?

Translation We want objects to move, or translate, through space Linear space (for linear transformations) has no notion of position Therefore affine space takes linear space and adds an origin point " p = Mp = # a b t x & % ( % c d t y ( $ % 0 0 1' ( # x& % ( % y ( $ % 1' (

Series of Transformations We can combine a sequence of transformations into one matrix to transform the geometric instance: But we can also think of this transformation as a series of simpler transformations:

Transformation Order Transformation order matters! Mathematical reason: transformation matrices do not commute under matrix multiplication (how we apply transformations) Intuitive reason: what happens when we rotate then translate versus translate then rotate?

Proper Transformation Order To rotate an object within its current position: Scale -> Rotate -> Translate To rotate around a specific point: Scale -> Translate -> Rotate

The Graphics Context What does this have to do with the graphics context? The graphics context stores the state of the world State of the world includes current affine space stored as a sequence of matrix multiplications Necessary to move between affine spaces to perform animations

Puppet Animation in 2D

Applies to 3D as well!

Transformations in Core Graphics Translate coordinate system origin to (tx, ty) context?.translateby(x: CGFloat, y: CGFloat) Scale coordinate system by sx and sy context?.scaleby(x: CGFloat, y: CGFloat) Rotate coordinate system by angle (radians) context?.rotate(by: CGFloat)

Changing Contexts Necessary to save and restore the context s coordinate system after applying a transform Transformations applied to underlying matrix that represents the coordinate space After object is transformed, remove the transforms from the matrix so they don t affect additional objects context?.savegstate() context?.restoregstate()

Graphics Context Demo

OpenGL ES OpenGL is standard open source graphics library for working on the GPU (graphics processing unit) OpenGL ES is for embedded systems (phones, tablets, consoles, etc) Subset of OpenGL API Optimized for simpler GPU architecture Considerations for power and performance

Shaders Small, efficient programs that run on the GPU Intended for high throughput (many simple operations in parallel) Designed to perform vertex transforms and apply colors to pixels (hence shaders!) Works on models in 3D world space Works on pixels in 2D screen space

Using OpenGL in Swift Under Targets -> BuildPhases -> Link Binary with Libraries add OpenGLES.framework Add import OpenGLES to file using OpenGL Use EAGLContext to manage the OpenGL rendering context Can also use GLKit for additional shader and math library support https://www.raywenderlich.com/5146-glkit-tutorial-for-iosgetting-started-with-opengl-es

Quiz Question! Which of these is the standard order of transformations? A. Rotate -> Scale -> Translate B. Translate -> Rotate -> Scale C. Rotate -> Translate -> Scale D. Scale -> Rotate -> Translate