Lab 2-3D Transformations and Vertex Shaders

Similar documents
Overview. By end of the week:

The Projection Matrix

CS1950U Setup Spring 2018

Shaders. Slide credit to Prof. Zwicker

Lab 9 - Metal and Glass

Viewing with Computers (OpenGL)

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

CGT520 Transformations

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

CS 130 Final. Fall 2015

Computer Graphics Coursework 1

The Projection Matrix

The Graphics Pipeline

Topics and things to know about them:

Virtual Cameras and The Transformation Pipeline

Sign up for crits! Announcments

Problem Set 4 Part 1 CMSC 427 Distributed: Thursday, November 1, 2007 Due: Tuesday, November 20, 2007

CSC 471 midterm 2 Winter Name:

CS559 Computer Graphics Fall 2015

CS451Real-time Rendering Pipeline

Computergraphics Exercise 15/ Shading & Texturing

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

Lecture 13: OpenGL Shading Language (GLSL)

Computer graphics 2: Graduate seminar in computational aesthetics

COMP371 COMPUTER GRAPHICS

Rendering Objects. Need to transform all geometry then

CS 432 Interactive Computer Graphics

CS 4620 Program 3: Pipeline

Scanline Rendering 2 1/42

Project 1, 467. (Note: This is not a graphics class. It is ok if your rendering has some flaws, like those gaps in the teapot image above ;-)

CSE 167. Discussion 03 ft. Glynn 10/16/2017

CS 432 Interactive Computer Graphics

Transformation. Computer Graphics October. Dr Anton Gerdelan

Computer Graphics 7: Viewing in 3-D

Tutorial 04. Harshavardhan Kode. September 14, 2015

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

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

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

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

For each question, indicate whether the statement is true or false by circling T or F, respectively.

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

CS 184: Assignment 2 Scene Viewer

Programming shaders & GPUs Christian Miller CS Fall 2011

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

CS452/552; EE465/505. Intro to Lighting

TSBK03 Screen-Space Ambient Occlusion

Outline. Introduction Surface of Revolution Hierarchical Modeling Blinn-Phong Shader Custom Shader(s)

Adaptive Point Cloud Rendering

Blis: Better Language for Image Stuff Project Proposal Programming Languages and Translators, Spring 2017

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

Computer Graphics - Treasure Hunter

Ray Tracing Part 1. CSC418/2504 Introduction to Computer Graphics. TA: Muhammed Anwar & Kevin Gibson

DH2323 DGI13 Lab 3 Rasterization

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

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

DH2323 DGI13. Lab 2 Raytracing

Computação Gráfica. Computer Graphics Engenharia Informática (11569) 3º ano, 2º semestre. Chap. 4 Windows and Viewports

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

COMP 175 COMPUTER GRAPHICS. Ray Casting. COMP 175: Computer Graphics April 26, Erik Anderson 09 Ray Casting

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

1 20 pts Graphics Pipeline short answer

Quick Shader 0.1 Beta

Could you make the XNA functions yourself?

CS452/552; EE465/505. Models & Viewing

OPENGL RENDERING PIPELINE

Assignment 6: Ray Tracing

GLM. Mike Bailey. What is GLM?

Due: Thursday, February 6 th, 11:59 pm TA: Mason Remy

Introduction to 2D Graphics

Pipeline Operations. CS 4620 Lecture 14

SHADER PROGRAMMING. Based on Jian Huang s lecture on Shader Programming

A simple OpenGL animation Due: Wednesday, January 27 at 4pm

We assume that you are familiar with the following:

CS 130 Exam I. Fall 2015

Lecture 5 2D Transformation

Programmable GPUs. Real Time Graphics 11/13/2013. Nalu 2004 (NVIDIA Corporation) GeForce 6. Virtua Fighter 1995 (SEGA Corporation) NV1

Computer Graphics Seminar

Orthogonal Projection Matrices. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Lecture 4. Viewing, Projection and Viewport Transformations

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

The OpenGL Mathematics (GLM) Library

Game Graphics & Real-time Rendering

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

CS 4620 Midterm, October 23, 2018 SOLUTION

CMSC427 Final Practice v2 Fall 2017

Overview. By end of the week:

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

So we have been talking about 3D viewing, the transformations pertaining to 3D viewing. Today we will continue on it. (Refer Slide Time: 1:15)

Recall: Indexing into Cube Map

OUTLINE. Learn the basic design of a graphics system Introduce pipeline architecture Examine software components for a graphics system

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

We created a few different effects and animations using this technique as applied to clones.

Solution Guide for Chapter 20

Tutorial 2 Part B: The View Matrix & Frame Rates

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

THE VIEWING TRANSFORMATION

Homework 3: Programmable Shaders

CS 130 Exam I. Fall 2015

I think this assignment should have 8 objectives but we should still mark it out of 10. The Mean TA.

The Graphics Pipeline and OpenGL III: OpenGL Shading Language (GLSL 1.10)!

Transcription:

Lab 2-3D Transformations and Vertex Shaders Support code: /course/cs1230/src/labs/lab02 Demo: /course/cs1230/bin/cs1230_lab02_demo Intro Last week you learned how to draw 2D shapes in OpenGL using VBOs and VAOs. In this lab, we will use OpenGL to draw a 3D scene with moving shapes! Transformations To move and position objects in 3D space in graphics we use three basic types of transformations: translation, scaling, and rotation. In mathematics and in code, we represent 3D transformations with a 4-by-4 matrix. If we have a square centered at the origin like in the diagram below, we can translate that square to a new location by multiplying each vertex position by a translation matrix T. Note that matrices are multiplied on the left of each vertex position (T * v 1). Representing positions and directions as vectors We represent positions and directions with column vectors. In order to multiply these vectors by 4-by-4 transformation matrices, we represent points not just by an (x, y, z) triplet, but by a vector with four components: (x, y, z, w). What goes in the w component? For vectors representing a position, w always equals 1. For vectors representing a direction, w always equals 0.

In this lab you ll just be dealing with positions. Later on we ll deal with directions and it will become clear why this difference exists. GLM In many of our OpenGL programs we will use a mathematics library called GLM (OpenGL Mathematics). Some GLM types that you will use in this lab are: glm::vec3 - A vector of size three. glm::vec4 - A vector of size four. glm::mat4 - A 4x4 matrix. GLM can also provide transformation matrices, using these functions: glm::translate(glm::vec3 v) - Returns a matrix that translates in X, Y, and Z according to the given vector. glm::scale(glm::vec3 v) - Returns a matrix that scales in X, Y, and Z according to the given vector. glm::rotate(float angle, glm::vec3 axis) - Returns a matrix that rotates by the given angle about the given axis. Here is a brief example of how to use these: glm::vec4 p = glm::vec4(1, 0, 0, 1); // Creates a point at (1,0,0). // Creates a transformation matrix that translates by 1 in the // x-direction and by 3 in the z-direction. glm::mat4 T = glm::translate( glm::vec3(1, 0, 3) ); p = T * p; // Translates p. assert( p == glm::vec4(2, 0, 3, 1) );

Getting Started The support code is located in /course/cs1230/src/labs/lab02. Copy this directory into your course directory and open your copy of the provided.pro file with Qt Creator. Task 0: In the Projects tab on the left of QtCreator, de-select the Shadow Build option, then Clean and Build your code. Support Code Take a look at GLWidget::initializeGL(). Here we have initialized m_sphere and m_square. These are both unique_ptrs to OpenGLShape, the class you implemented in Lab 1. We have included our implementation of OpenGLShape in this lab s support code. Take a look for a refresher! The sphere has a radius of 0.5 and is centered at the origin. The only thing left to do is draw it. Task 1: In GLWidget::paintGL(), draw m_sphere after the call to gluniform3f (which we ll discuss later). Run your program. You should see a sphere! The Vertex Shader Let s say instead of having the sphere in the center of the screen, we want to move it up along the y-axis. One way we could do this would be to redefine all our vertex positions with new y-values and send them to the GPU using OpenGLShape::setVertexData(...). Unfortunately, it is cumbersome and costly to recompute all the vertex positions for a sphere. Also, shuttling data from the CPU to the GPU is expensive. In many programs objects are moving constantly, so we really want to avoid calling setvertexdata(...) on every frame. There has got to be a better way to do it... Vertex shaders to the rescue! Vertex shaders are programs that process each vertex individually before rendering. Here s the OpenGL pipeline diagram again to give an idea of where it fits in the big picture.

Shaders run directly on the GPU, so they are a blazing fast way to do computations for each vertex. Their main job is to output the vertex s final position. Open the file shader.vert, located in the Other files folder in Qt Creator. It is written in a language called GLSL (OpenGL Shading Language) which is designed to resemble C. Let s take a closer look: #version 400 All shaders begin with a line like this that identifies the GLSL version being used. layout(location = 0) in vec3 position; This declares a vertex attribute (called an in variable) with type vec3 that is called position. On each execution of the vertex shader, position gets one of the positions that we stored in the VBO. layout(location = 0) is an optional part of the declaration that allows us to set the location (or index) of this attribute. Remember in Lab 1 we used ShaderAttrib::POSITION as the "index" when calling OpenGLShape::setAttribute()? It was actually equal to 0, because the position attribute's location was set to 0 in the vertex shader. void main() {... } All vertex shaders must contain a main function, which is automatically called for each vertex. gl_position = vec4(position, 1.0); The main function is required to set the value of gl_position, a built-in variable that determines the final position of the vertex. Here we are simply setting it to the value of position. We need to provide a vec4, though, and we set the w-component to 1.0. Okay, so we have a sphere centered at the origin, but we want all of its vertex positions to be translated in the y direction. Let's use the vertex shader to do this! Task 2: In the vertex shader, add 0.5 to gl_position.y. Run the program. The sphere should be moved up on the screen!

Uniform Variables Instead of hardcoding the transformation like this in the shader, we would like the shader to work for arbitrary transformations. To do this, we d like to send a transformation matrix from the C++ code to the vertex shader. We ve already seen one way to pass information to the shader: store it in a VBO and have it show up in an in variable. This is good for data that varies across vertices (like positions). But we want the transformation matrix to be the same for all vertices, so instead we ll use something called a uniform! To set the value of a uniform, you must call gluniform before drawing. On each execution of the vertex shader, it will use the last value that was set using gluniform. Here s a chart to summarize the differences between a vertex attributes and uniforms: Vertex attribute Different values for different vertices Defined as an in in the vertex shader Uniform Same value for all vertices Defined as a uniform in the shader Values are set using a VBO and a VAO Value is set by calling gluniform The first argument of gluniform is the location of the uniform variable, which you can obtain from glgetuniformlocation. Here s an example of setting a uniform variable with type vec3: // This sets the active shaders. It must be called before setting // any uniforms. gluseprogram(m_program); GLint uniformloc = glgetuniformlocation(m_program, myuniform ); // Sets the uniform to (1, 0, 0). gluniform3f(uniformloc, 1, 0, 0); // Draws the shape with myuniform set to (1, 0, 0). m_shape->draw(); // Alternatively you can use gluniform*v and pass a pointer to a GLM

// type. glm::vec3 v = glm::vec3(0, 1, 0); gluniform3fv(uniformloc, 1, glm::value_ptr(v)); // Draws the shape with the uniform set to (0, 1, 0). m_shape->draw(); We already defined a uniform in the vertex shader called model. The model matrix is a matrix that transforms an object (discussed more in the next section). Note: gluniform is a family of functions that have different names depending on what you re passing. gluniform3fv passes a vec3, whereas gluniformmatrix3fv passes a mat3. Task 3: In paintgl(), pass a translation matrix to the model uniform. Check the gluniform documentation to decide which version of the function to use. Then in the shader, instead of adding 0.5 to gl_position.y, transform gl_position using the matrix. Run your program. The sphere should be moved according to the translation matrix! Tip: If you re having trouble creating a translation matrix, review the example we gave you at the bottom of the second page of this lab! You may have noticed that the support code sets another uniform called color in paintgl(). This is used by the fragment shader (which we ll cover in Lab 3) to assign the object s color. Feel free to change that value to change the sphere s color! Model, View, and Projection Matrices We used a model matrix to transform an object from object coordinates (where the origin is at the center of the object) to world coordinates (where the origin is at another set location). In a typical 3D scene, there are two additional matrices we use to produce the final vertex positions.

To start, the camera might be located at some arbitrary location in world coordinates. The view matrix takes all objects from world coordinates to camera coordinates, where the camera is at the origin and pointed along the negative z-axis. You could make your own view matrix by combining a translation and a rotation matrix, but GLM has a handy-dandy function to do this for you. glm::lookat takes in the camera s position, the center point where the camera is looking, and the camera s up direction (usually (0,1,0)); and it returns the corresponding view matrix. The final matrix we care about is the projection matrix. This is a more complicated matrix that takes care of making a perspective projection, which you ll learn more about in a lecture later in the semester. For now, you should know that it transforms from eye coordinates to clip coordinates, where closer objects appear bigger. To obtain a projection matrix you can call glm::perspective, which takes in some camera parameters as well as the aspect ratio of the screen. In order to render a scene with a perspective projection, we need gl_position to be in clip coordinates. Since the sphere s vertex positions are defined in object coordinates, we need to multiply each vertex position by all three of these matrices. Task 4:

In the vertex shader, use the uniforms for the view and projection (perspective) matrices. Multiply gl_position by all three matrices. ***Pay attention to the multiplication order!*** Matrices are multiplied right-to-left, so the transformation matrix on the left will be applied last. Now, in paintgl(), use glm::lookat, glm::perspective, and the variables we defined for you in paintgl() to create the view and projection matrices. Send the matrices to the respective uniform variables. Your program should now show a smaller sphere, because the camera is farther away at (0, 1, 6). Drawing Multiple Shapes Now let s draw m_square! We ll assign it a different color so we can tell the difference between the sphere and the square. Task 5: After drawing m_sphere, set the color uniform to a different RGB color. Then draw m_square. You should now see a colored square. But where s the sphere? Based on the vertices initial positions, we would expect to see the front half of the sphere in front of the square... By default, OpenGL will draw each shape on top of what it already drew. But we only want to draw on top if we re drawing a triangle that s closer to the camera! Fortunately, it s easy to make OpenGL do a depth test so farther triangles aren t drawn on top of closer ones. Task 6: In initializegl(), call glenable(gl_depth_test). You should now see half of a sphere poking out front of your square. If you still don t see the sphere, make sure you set the color uniform to a new color between m_sphere->draw() and m_square->draw(). Now let s use our knowledge of uniforms and transformations to make the scene more interesting! Task 7: Make your scene act like the demo! Here s what the demo does: The square is scaled by 2 and rotated to lie on the XZ plane. For rotation note that we have told GLM that angles are in radians. The ball is translated to look like it s bouncing. For this, you can use the equation y = 0.5 + fabs( sin(3 * time) ); Camera position (eye) goes in a circle of radius 6 where y always equals 1.

Once you ve got a scene with a moving camera and transformed objects, have a TA check you off!