The Projection Matrix

Similar documents
The Projection Matrix

The Graphics Pipeline

The Graphics Pipeline

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Critical-Path Algorithm

Specular Reflection. Lecture 19. Robb T. Koether. Hampden-Sydney College. Wed, Oct 4, 2017

Minimal Spanning Trees

Computer Graphics (4731) Lecture 4: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Density Curves Sections

Sampling Distribution Examples Sections 15.4, 15.5

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Ambient and Diffuse Light

The Mesh Class. Lecture 23. Robb T. Koether. Hampden-Sydney College. Wed, Oct 18, 2017

The Decreasing-Time Algorithm

Computer Graphics (CS 4731) & 2D Graphics Systems

Shading Triangles. Lecture 37. Robb T. Koether. Hampden-Sydney College. Mon, Nov 30, 2015

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Scope and Parameter Passing

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Scheduling and Digraphs

The Pairwise-Comparison Method

Rotations and Translations

The Traveling Salesman Problem Brute Force Method

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 4: Three Dimensions

Scope and Parameter Passing

Street-Routing Problems

Computer Graphics (CS 543) Lecture 2b: 2D Graphics Systems (Drawing Polylines, tiling, & Aspect Ratio)

Binary Tree Applications

Solving Recursive Sequences by Iteration

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Recursive Descent Parsers

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Programming Languages

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

Displaying Distributions - Quantitative Variables

The Class Construct Part 1

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

The View Frustum. Lecture 9 Sections 2.6, 5.1, 5.2. Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2011

Magnification and Minification

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Implementing Linked Lists

Lecture 3 Sections 2.2, 4.4. Mon, Aug 31, 2009

PHP Queries and HTML Forms Lecture 23

Building the Abstract Syntax Trees

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Programming with OpenGL Part 3: Shaders. Ed Angel Professor of Emeritus of Computer Science University of New Mexico

Programming with OpenGL Shaders I. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

LR Parsing - Conflicts

Today s Agenda. Shaders fundamentals. Programming with shader-based OpenGL

The Normal Distribution

The Coefficient of Determination

CS 432 Interactive Computer Graphics

Programming Languages

Lab 2-3D Transformations and Vertex Shaders

The Plurality-with-Elimination Method

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

Function Definition Syntax Tree

Programming shaders & GPUs Christian Miller CS Fall 2011

INFOGR Computer Graphics

The Traveling Salesman Problem Cheapest-Link Algorithm

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Shaders. Slide credit to Prof. Zwicker

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Triggers. Lecture 14. Robb T. Koether. Hampden-Sydney College. Wed, Feb 14, 2018

Friends and Unary Operators

The CYK Parsing Algorithm

Basic CSS Lecture 17

The Mesh Class. Lecture 26. Robb T. Koether. Wed, Oct 28, Hampden-Sydney College

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

INFOGR Computer Graphics

Introduction to Compiler Design

Nondeterministic Programming in C++

The Class Construct Part 2

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

Recursive Linked Lists

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Inheritance: The Fundamental Functions

Recognition of Tokens

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

Insertions, Deletions, and Updates

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CSE 167: Introduction to Computer Graphics Lecture #7: GLSL. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

List Iterator Implementation

Lecture 5 Vertex and Fragment Shaders-1. CITS3003 Graphics & Animation

Stacks and their Applications

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

Transcription:

The Projection Matrix Lecture 5 Robb T. Koether Hampden-Sydney College Wed, Aug 30, 2017 Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 1 / 21

Outline 1 The World Coordinate System 2 The Projection Matrix 3 The Vertex Shader 4 Uniform Shader Variables 5 Assignment Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 2 / 21

Outline 1 The World Coordinate System 2 The Projection Matrix 3 The Vertex Shader 4 Uniform Shader Variables 5 Assignment Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 3 / 21

World Coordinates Definition (World Coordinate System) The world coordinate system is the single coordinate system in which all objects are placed when the scene is rendered. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 4 / 21

World Coordinates in 2D The default world coordinate system is a square with 1 x 1 and 1 y 1, regardless of the size or shape of the window. Typically, this is not the best choice. To change the world coordinate system, we need a transformation. The function ortho2d() will produce the appropriate transformation matrix (called the projection matrix), if we specify the coordinates of the window boundaries: left, right, bottom, top. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 5 / 21

Outline 1 The World Coordinate System 2 The Projection Matrix 3 The Vertex Shader 4 Uniform Shader Variables 5 Assignment Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 6 / 21

The Projection Matrix The projection matrix produced by ortho2d() is P = 2 r l 0 0 r+l r l 2 0 t b 0 t+b t b 0 0 1 0 0 0 0 1 where l = left, r = right, b = bottom, t = top, Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 7 / 21

The Projection Matrix Matrix multiplication X = PX will perform the transformation. x y 0 1 = 2 r l 0 0 r+l r l 2 0 t b 0 t+b t b 0 0 1 0 0 0 0 1 x y 0 1 Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 8 / 21

The Projection Matrix The Projection Matrix The default projection matrix uses l = 1, r = 1, b = 1, and t = 1, which produces the identity matrix. Then the projection matrix is P = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 = I. Every point X is left unchanged: PX = IX = X. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 9 / 21

The Projection Matrix The Projection Matrix Suppose our scene is drawn in a rectangle with left = 4, right = 4, bottom = 3 and top = 3. Then the projection matrix is P = 1 4 0 0 0 0 1 3 0 0 0 0 1 0 0 0 0 1 Map the corners ( 4, 3), (4, 3), (4, 3), and ( 4, 3). Map the point (2, 1). Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 10 / 21

The Projection Matrix The Projection Matrix Suppose our scene is drawn in a rectangle with left = 0, right = 8, bottom = 0 and top = 4. Then the projection matrix is Map the point (4, 2). Map the point (2, 1). P = 1 4 0 0 1 0 1 2 0 1 0 0 1 0 0 0 0 1 Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 11 / 21

Outline 1 The World Coordinate System 2 The Projection Matrix 3 The Vertex Shader 4 Uniform Shader Variables 5 Assignment Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 12 / 21

The Vertex Shader The multiplication by P takes place in the vertex shader (because the vertices are stored in the GPU buffer). Therefore, we must pass the projection matrix to the vertex shader. The shader will multiply it by the vertex to transform it. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 13 / 21

Outline 1 The World Coordinate System 2 The Projection Matrix 3 The Vertex Shader 4 Uniform Shader Variables 5 Assignment Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 14 / 21

Uniform Shader Variables A uniform shader variable is a shader variable whose value does not change during the processing of the vertices of a primitive, i.e., during a call to gldrawarrays(). Its value is set by the application program and passed to the shader before calling gldrawarrays(). Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 15 / 21

Uniform Shader Variables Passing a Shader Variable GLint glgetuniformlocation(program, var_name); In the application program, we must get a shader location for the uniform variable. The glgetuniformlocation() will return a location. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 16 / 21

Uniform Shader Variables Passing a Shader Variable void gluniform*(location, value); void gluniform*(location, count, values); void gluniformmatrix*(location, count, GL_TRUE, values); The functions gluniform*() and gluniformmatrix*() will pass the value(s) to the shaders. The third parameter of gluniformmatrix*() tells whether the matrix is stored in row-major order (row by row rather than column by column). See p. 48 of the Red Book. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 17 / 21

Passing the Projection Matrix Passing the Projection Matrix mat4 proj = ortho2d(left, right, bottom, top); GLuint proj_loc = glgetuniformlocation(program, "proj"); gluniformmatrix4fv(proj_loc, 1, GL_TRUE, proj); This code with create the projection matrix and pass it to the shaders. "proj" is the name of the uniform variable in the shader. It is a really good idea to keep the same name in order to avoid confusion. Later, we will have many uniform variables. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 18 / 21

Using the Projection Matrix Using the Projection Matrix uniform mat4 proj; layout (location = 0) in vec2 vposition; void main() { gl_position = proj*vec4(vposition, 0.0f, 1.0f); } In the shader program, we simply declare the variable to be uniform. The name must match the name specified in the application program. Then multiply it by the position vector and assign to gl_position. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 19 / 21

Outline 1 The World Coordinate System 2 The Projection Matrix 3 The Vertex Shader 4 Uniform Shader Variables 5 Assignment Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 20 / 21

Assignment Assignment Assignment 5, to be turned in by Monday. Read pp. 203-210, User Transformations. Robb T. Koether (Hampden-Sydney College) The Projection Matrix Wed, Aug 30, 2017 21 / 21