Tutorial 04. Harshavardhan Kode. September 14, 2015

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

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

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

CS 548: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE

CS 432 Interactive Computer Graphics

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

OpenGL pipeline Evolution and OpenGL Shading Language (GLSL) Part 2/3 Vertex and Fragment Shaders

CS452/552; EE465/505. Review & Examples

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

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

Computer Graphics (CS 543) Lecture 4a: Linear Algebra for Graphics (Points, Scalars, Vectors)

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

COMP371 COMPUTER GRAPHICS

CS 450: COMPUTER GRAPHICS REVIEW: STATE, ATTRIBUTES, AND OBJECTS SPRING 2015 DR. MICHAEL J. REALE

Comp 410/510 Computer Graphics Spring Programming with OpenGL Part 3: Shaders

Overview. By end of the week:

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

3d Programming I. Dr Anton Gerdelan

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

Tutorial 1: Your First Triangle!

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

Computer Graphics (CS 4731) & 2D Graphics Systems

Shaders. Slide credit to Prof. Zwicker

GLSL Overview: Creating a Program

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

Rendering Objects. Need to transform all geometry then

CS452/552; EE465/505. Image Processing Frame Buffer Objects

CGT520 Transformations

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

Programming with OpenGL Part 5: More GLSL. Ed Angel Professor Emeritus of Computer Science University of New Mexico

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

Computer Graphics Seminar

Programming with OpenGL Complete Programs Objectives Build a complete first program

Starting out with OpenGL ES 3.0. Jon Kirkham, Senior Software Engineer, ARM

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

Computer Graphics (CS 543) Lecture 3b: Shader Setup & GLSL Introduction

One or more objects A viewer with a projection surface Projectors that go from the object(s) to the projection surface

Classical and Computer Viewing. Adapted From: Ed Angel Professor of Emeritus of Computer Science University of New Mexico

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

We assume that you are familiar with the following:

6.S096 Lecture 9 Visualization

Tutorial 3: Texture Mapping

Shaders. Introduction. OpenGL Grows via Extensions. OpenGL Extensions. OpenGL 2.0 Added Shaders. Shaders Enable Many New Effects

Lab 2-3D Transformations and Vertex Shaders

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

gvirtualxray Tutorial 01: Creating a Window and an OpenGL Context Using GLUT

CS 432 Interactive Computer Graphics

An Introduction to OpenGL Programming

Tutorial 12: Real-Time Lighting B

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

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

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

COSC342: Computer Graphics

CPSC 436D Video Game Programming

object (say a cube) will be made up of triangles, each with three vertices, each with known object coordinates.

Geometry Shaders. And how to use them

Building Models. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

8 Three-Dimensional Object Representations. Chapter 8. Three-Dimensional Object Representations. Department of Computer Science and Engineering 8-1

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

Computer Graphics (CS 543) Lecture 1 (Part 2): Introduction to OpenGL/GLUT (Part 1)

The Graphics Pipeline

From system point of view, a graphics application handles the user input, changes the internal state, called the virtual world by modeling or

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

GLM. Mike Bailey. What is GLM?

Introduction to Computer Graphics with WebGL

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

CS559 Computer Graphics Fall 2015

CS452/552; EE465/505. Transformations

Computer graphics 2: Graduate seminar in computational aesthetics

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

Overview. By end of the week:

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

The OpenGL Mathematics (GLM) Library. What is GLM?

CS452/552; EE465/505. Image Formation

CGT520 Lighting. Lighting. T-vertices. Normal vector. Color of an object can be specified 1) Explicitly as a color buffer

Lecture 11 Shaders and WebGL. October 8, 2015

The OpenGL Mathematics (GLM) Library

CS 432 Interactive Computer Graphics

Vertex Buffer Objects

Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

Mobile Application Programing: Android. OpenGL Operation

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

CS452/552; EE465/505. Intro to Lighting

CS 548: COMPUTER GRAPHICS INTRODUCTION TO OPENGL AND GLUT SPRING 2015 DR. MICHAEL J. REALE

Game Architecture. 2/19/16: Rasterization

Virtual Cameras and The Transformation Pipeline

BCA611 Video Oyunları için 3B Grafik

CS452/552; EE465/505. Models & Viewing

Objectives. Programming with OpenGL Part 5: More GLSL. Program Object. Reading a Shader. Shader Reader. Linking Shaders with Application

CMSC427 Fall 2017 OpenGL Notes A: Starting with JOGL and OpenGL Objectives of notes Readings Computer Graphics Programming in OpenGL with Java

Models and Architectures. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

3D Modeling. 3D Modeling 1

2D graphics with WebGL

Viewing with Computers (OpenGL)

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

The Projection Matrix

OPENGL RENDERING PIPELINE

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

Mali & OpenGL ES 3.0. Dave Shreiner Jon Kirkham ARM. Game Developers Conference 27 March 2013

Transcription:

Tutorial 04 Harshavardhan Kode September 14, 2015 1 About This tutorial an extension of the Tutorial 03. So you might see quite a lot similarities. The following things are new. A Plane is added underneath the cube A Camera is added that can be moved around the cube The view of the camera can be toggled between Orthographic or Perspective 2 Running the Code If you have a driver supporting OpenGL 4.1 then running 02 colorcube will do the trick for you. But if your system supports OpenGL 3.2+. Then in order to run the code the following changes need to be made in the 02 colorcube.cpp. In the line number 136 and 137, glfwwindowhint ( GLFW_ CONTEXT_ VERSION_ MAJOR, 4); glfwwindowhint ( GLFW_ CONTEXT_ VERSION_ MINOR, 1); you need to change to glfwwindowhint ( GLFW_ CONTEXT_ VERSION_ MAJOR, 3); glfwwindowhint ( GLFW_ CONTEXT_ VERSION_ MINOR, 3); Also in the 02 fshader.glsl and 02 vshader.glsl you need to modify the first line to 1

# version 330 Once you make the above changes you can compile and run the 02 colorcube file. 3 Understanding the code The understanding for the code of basics, VAOs, VBOs and Shaders is documented in the previous tutorials. This tutorial would not go into much detail about those again, but explain all the new stuff. To draw multiple objects, it is essential that you have a good way of storing the attributes (position, color etc.) of each object and be able to draw it appropriately. One way to do it is to store the attributes (vposition, vcolor here ) corresponding each object in a different VAO. While drawing, we simply bind the VAO corresponding to the object, load the appropriate transformation matrix and draw it. You will see that in this tutorial we have defined two VAOs and VBOs. GLuint vbo [2], vao [2]; It is followed by the creation of the cube vertex and colour arrays which was discussed in the previous tutorial. Apart from that you will see this extra piece of code. glm :: vec4 v_ positions2 [6] = { glm :: vec4 (1.0, -1.0, 1.0, 1.0), glm :: vec4 (1.0, -1.0, -1.0, 1.0), glm :: vec4 ( -1.0, -1.0, -1.0, 1.0), glm :: vec4 (1.0, -1.0, 1.0, 1.0), glm :: vec4 ( -1.0, -1.0, -1.0, 1.0), glm :: vec4 ( -1.0, -1.0, 1.0, 1.0) }; glm :: vec4 v_ colors2 [6] = { 2

glm :: vec4 (0.5, 0.5, 0.5, 1.0) }; This is basically the positions of vertices and the colours to form a plane below the cube (the plane is made using two triangle primitives, as are all the faces of the cube, and hence 6 vertices) Next, in the function initbuffersgl, we actually initialize the Shader program and the VAOs, VBOs corresponding to the two objects. I will split the function into three major parts to explain it. First part is the loading of the shaders and obtaining the attribute indices. // Load shaders and use the resulting shader program std :: string vertex_shader_file ("04 _vshader. glsl "); std :: string fragment_shader_file ("04 _fshader. glsl "); std :: vector < GLuint > shaderlist ; shaderlist. push_ back ( csx75 :: LoadShaderGL ( GL_ VERTEX_ SHADER, vertex_shader_file )); shaderlist. push_ back ( csx75 :: LoadShaderGL ( GL_ FRAGMENT_ SHADER, fragment_shader_file )); shaderprogram = csx75 :: CreateProgramGL ( shaderlist ); gluseprogram ( shaderprogram ); // getting the attributes from the shader program GLuint vposition = glgetattriblocation ( shaderprogram, " vposition " ); GLuint vcolor = glgetattriblocation ( shaderprogram, " vcolor " ); umodelviewmatrix = glgetuniformlocation ( shaderprogram, " umodelviewmatrix "); This is same as in the previous tutorials. Initially the shader is loaded and the attribute indices are obtained. It is done in the beginning because the plan is to use the same shader to draw all the objects and so we require the index of the attributes in the shader program. The description of each of the steps can be seen in the previous tutorials Next we allocate the buffers // Ask GL for two Vertex Attribute Objects ( vao ), one for the colorcube and one for the plane. glgenvertexarrays (2, vao ); 3

// Ask GL for two Vertex Buffer Object ( vbo ) glgenbuffers (2, vbo ); Two VBOs and VAOs are allocated, one each for the cube and the plane. Next we fill up the buffer for the cube and enable the appropriate attributes and assign them appropriate locations in the VBOs. // Set 0 as the current array to be used by binding it glbindvertexarray ( vao [ 0]) ; // Set 0 as the current buffer to be used by binding it glbindbuffer ( GL_ ARRAY_ BUFFER, vbo [ 0]) ; colorcube (); // Copy the points into the current buffer glbufferdata ( GL_ ARRAY_ BUFFER, sizeof ( v_ positions ) + sizeof ( v_colors ), NULL, GL_STATIC_DRAW ); glbuffersubdata ( GL_ ARRAY_ BUFFER, 0, sizeof ( v_ positions ), v_ positions ); glbuffersubdata ( GL_ ARRAY_ BUFFER, sizeof ( v_ positions ), sizeof ( v_colors ), v_colors ); // set up vertex array glenablevertexattribarray ( vposition ); glvertexattribpointer ( vposition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_ OFFSET (0) ); glenablevertexattribarray ( vcolor ); glvertexattribpointer ( vcolor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET ( sizeof ( v_positions )) ); See that all the attribute locations are set when vao[0] is bound. so all the changes happen in that VAO. This is same as in the previous tutorial. Next we do the same for the plane, but use the other VAO and VBO. // Set 1 as the current array to be used by binding it glbindvertexarray ( vao [ 1]) ; // Set 1 as the current buffer to be used by binding it glbindbuffer ( GL_ ARRAY_ BUFFER, vbo [ 1]) ; // Copy the points into the current buffer glbufferdata ( GL_ ARRAY_ BUFFER, sizeof ( v_ positions2 ) + sizeof ( v_colors2 ), NULL, GL_STATIC_DRAW ); 4

glbuffersubdata ( GL_ ARRAY_ BUFFER, 0, sizeof ( v_ positions2 ), v_ positions2 ); glbuffersubdata ( GL_ ARRAY_ BUFFER, sizeof ( v_ positions2 ), sizeof ( v_colors2 ), v_colors2 ); // set up vertex array glenablevertexattribarray ( vposition ); glvertexattribpointer ( vposition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_ OFFSET (0) ); glenablevertexattribarray ( vcolor ); glvertexattribpointer ( vcolor, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET ( sizeof ( v_positions2 )) ); With this, our buffers for each of the object are ready! To draw, we simply enable the corresponding VAO and call gldrawarrays() with the appropriate number of vertices. 4 Camera/Eye In this tutorial, as mentioned at the beginning, apart from rotating the cube, we also add functionality to move the camera and toggle the view between ortho or perspective. I would be using the word camera in this tutorial for the eye. In the header file, 04 camera viewing.hpp, you will see the following lines have been added // Camera position and rotation Parameters GLfloat c_ xpos = 0.0, c_ ypos = 0.0, c_ zpos = 2. 0; GLfloat c_up_x = 0.0, c_up_y = 1.0, c_up_z = 0.0; GLfloat c_xrot =0.0, c_yrot =0.0, c_zrot =0.0; These represent the parameters of the camera location and the up vector. c pos represent the initial position of the camera. c up represent the upvector. c rot represent the rotation of the camera s initial position vector about the world origin. Now, coming back to the cpp file, the function rendergl() is actually where the drawing happens. 5

rotation_matrix = glm :: rotate ( glm :: mat4 (1.0 f), glm :: radians ( xrot ), glm :: vec3 (1.0f,0.0f,0.0 f)); rotation_ matrix = glm :: rotate ( rotation_ matrix, glm :: radians ( yrot ), glm :: vec3 (0.0f,1.0f,0.0 f)); rotation_ matrix = glm :: rotate ( rotation_ matrix, glm :: radians ( zrot ), glm :: vec3 (0.0f,0.0f,1.0 f)); model_ matrix = rotation_ matrix ; These are the modelling transforms applied to the cube and are same as in the last tutorial, except you will notice the use of glm::radians() function. The glm library from version 0.9.7 treats all parameter angles as radians. So if you want to use degrees, you have to convert it to radians using that function Next is creating the lookat matrix the transforms the vertices from the WCS to the VCS c_rotation_matrix = glm :: rotate ( glm :: mat4 (1.0 f), glm :: radians ( c_xrot ), glm :: vec3 (1.0f,0.0f,0.0 f)); c_ rotation_ matrix = glm :: rotate ( c_ rotation_ matrix, glm :: radians ( c_yrot ), glm :: vec3 (0.0f,1.0f,0.0 f)); c_ rotation_ matrix = glm :: rotate ( c_ rotation_ matrix, glm :: radians ( c_zrot ), glm :: vec3 (0.0f,0.0f,1.0 f)); glm :: vec4 c_pos = glm :: vec4 ( c_xpos, c_ypos, c_zpos, 1.0) * c_ rotation_ matrix ; glm :: vec4 c_up = glm :: vec4 ( c_up_x, c_up_y, c_up_z, 1.0) * c_ rotation_ matrix ; // Creating the lookat matrix lookat_matrix = glm :: lookat ( glm :: vec3 ( c_pos ),glm :: vec3 (0.0),glm :: vec3 ( c_up )); In this piece of code we multiply the rotations about x,y and z axes to the camera rotation matrix and then multiply this matrix to the initial position of the camera to get its final location. This matrix is also multiplied to the initial up vector to rotate it appropriately. Note that the camera rotation matrix is just used to calculate the position of the camera, and not a part of the modelling-viewing matrix. Finally, the lookat matrix is created by using the function glm::lookat, which takes as parameters the position of the camera, the look-at vector and the up vector and outputs the appropriate transformation from the WCS to the VCS. Next, we create the projection matrices based on the current mode 6

// creating the projection matrix if( enable_ perspective ) projection_ matrix = glm :: frustum ( -1.0, 1.0, -1.0, 1.0, 1.0, 5.0) ; // projection_matrix = glm :: perspective ( glm :: radians (90.0),1.0,0.1,5.0) ; else projection_ matrix = glm :: ortho ( -2.0, 2.0, -2.0, 2.0, -5.0, 5.0) ; view_ matrix = projection_ matrix * lookat_ matrix ; glm::ortho returns the orthographic projection matrix and was discussed in the last tutorial. glm::frustum converts the frustum specified by the function to a unit cube as discussed in class, and can be directly used to obtain the perspective projection. The arguments to the function are the left, right, top and bottom at the near plane, the distance of the near plane and the far plane respectively. Alternatively, glm::perspective function can also be used. It does the same thing as glm::frustum but has different type of arguments viz. the field of view in the y direction, the y:x aspect ratio, the near plane and the far plane respectively. To enable or disable the perspective view, a boolean is modified based on a key press event, written in the key callback function and the appropriate matrix is chosen. With this we get the projection matrix, we multiply it the the lookat matrix to get the whole viewing transformation. Finally, we draw the objects with the appropriate transfromation matrices // Draw cube modelview_ matrix = view_ matrix * model_ matrix ; gluniformmatrix4fv ( umodelviewmatrix, 1, GL_ FALSE, glm :: value_ptr ( modelview_matrix )); glbindvertexarray ( vao [ 0]) ; gldrawarrays ( GL_ TRIANGLES, 0, num_ vertices ); // Draw plane modelview_ matrix = view_ matrix ; gluniformmatrix4fv ( umodelviewmatrix, 1, GL_ FALSE, glm :: value_ptr ( modelview_matrix )); glbindvertexarray ( vao [ 1]) ; gldrawarrays ( GL_ TRIANGLES, 0, 6); For drawing each of the objects, the approptiate VAO is being bound and 7

gldrawarrays is called with the appropriate number of vertices in the object. For the cube, the entire modelling transformations are to be applied, while the plane has to remain static. Hence, before calling gldrawarrays for the plane, the uniform value that is passed to the shader is changed to just the viewing matrix. 5 Shaders 5.1 Vertex Shader attribute vec4 vposition ; attribute vec4 vcolor ; varying vec4 color ; In the main() function here, instead of directly assigning the vposition to gl position, we multiply it with our Modelview Matrix, hence applying the required transformations. We are also providing the color using the attributes vposition and vcolor, which we defined in the initbuffersgl() call in our main code. 5.2 Fragment Shader The fragment shader is quite similar to the code previous tutorial and you can easily see that we are assigning the color of the fragment, from our input from vertex shader. 5.3 Keyboard Input In the key callback() function in gl framework.cpp we deal with various keyboard inputs. Various additional inputs have been added to handle the movement of the camera and to toggle between the perspective and orthographic views. else if ( key == GLFW_ KEY_ P && action == GLFW_ PRESS ) enable_ perspective =! enable_ perspective ; else if ( key == GLFW_ KEY_ A && action == GLFW_ PRESS ) c_ yrot -= 1. 0; else if ( key == GLFW_ KEY_ D && action == GLFW_ PRESS ) c_ yrot += 1. 0; else if ( key == GLFW_ KEY_ W && action == GLFW_ PRESS ) 8

c_ xrot -= 1. 0; else if ( key == GLFW_ KEY_ S && action == GLFW_ PRESS ) c_ xrot += 1. 0; else if ( key == GLFW_ KEY_ Q && action == GLFW_ PRESS ) c_ zrot -= 1. 0; else if ( key == GLFW_ KEY_ E && action == GLFW_ PRESS ) c_ zrot += 1. 0; 9