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

Similar documents
CPSC 436D Video Game Programming

COMP371 COMPUTER GRAPHICS

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

Tutorial 04. Harshavardhan Kode. September 14, 2015

Overview. By end of the week:

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

Computer Graphics Seminar

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

Geometry Shaders. And how to use them

CS 432 Interactive Computer Graphics

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

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

CS475/CS675 - Computer Graphics. OpenGL Drawing

CS 432 Interactive Computer Graphics

COSC342: Computer Graphics

Rendering Objects. Need to transform all geometry then

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

Introductory Seminar

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

CENG 477 Introduction to Computer Graphics. Graphics Hardware and OpenGL

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

OPENGL RENDERING PIPELINE

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 3: Shaders

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

3d Programming I. Dr Anton Gerdelan

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

Shaders. Slide credit to Prof. Zwicker

Best practices for effective OpenGL programming. Dan Omachi OpenGL Development Engineer

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

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

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

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

CS452/552; EE465/505. Review & Examples

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

Lecture 11 Shaders and WebGL. October 8, 2015

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

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

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

Lab 2-3D Transformations and Vertex Shaders

Tutorial 1: Your First Triangle!

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

Computer graphics Labs: OpenGL (2/2) Vertex Shaders and Fragment Shader

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

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

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

Introduction to OpenGL/GLSL and WebGL GLSL

2/3/16. Interaction. Triangles (Clarification) Choice of Programming Language. Buffer Objects. The CPU-GPU bus. CSCI 420 Computer Graphics Lecture 3

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

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

This Lecture. Introduction to OpenGL. Outline. Why OpenGL? Programmer s View. Foundations of Computer Graphics

CS452/552; EE465/505. Transformations

Interaction. CSCI 420 Computer Graphics Lecture 3

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

To Do. Demo: Surreal (now 15+ years ago) This Lecture. Outline. Computer Graphics. CSE 167 [Win 19], Lecture 6: OpenGL 1 Ravi Ramamoorthi

OpenGL Performances and Flexibility

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

Discussion 3. PPM loading Texture rendering in OpenGL

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

GLSL Overview: Creating a Program

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

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

Particle Systems with Compute & Geometry Shader. Institute of Computer Graphics and Algorithms Vienna University of Technology

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

Mobile Application Programming: Android. OpenGL Operation

Computer Graphics Coursework 1

EECS 487: Interactive Computer Graphics

Sign up for crits! Announcments

CSE 4431/ M Advanced Topics in 3D Computer Graphics. TA: Margarita Vinnikov

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

CGT520 Transformations

OpenGL Performances and Flexibility. Visual Computing Laboratory ISTI CNR, Italy

Computer Graphics (CS 4731) & 2D Graphics Systems

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

CS770/870 Spring 2017 Open GL Shader Language GLSL

CS770/870 Spring 2017 Open GL Shader Language GLSL

INTRODUCTION TO OPENGL PIPELINE

Advanced Graphics. OpenGL and Shaders I. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

Vertex Buffer Objects

Vertex Buffer Objects. Vertex Buffer Objects: The Big Idea

CS380: Computer Graphics Screen Space & World Space. Sung-Eui Yoon ( 윤성의 ) Course URL:

An Overview GLUT GLSL GLEW

Setting up the Assimp library Creating the Mesh class and Model class How to load models in the game world using the Model class

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

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

CS 4620 Program 3: Pipeline

Modern OpenGL Guide. Alexander Overvoorde. May Introduction 3 Credits... 3 Prerequisites... 4

Mobile Application Programing: Android. OpenGL Operation

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

Advanced Graphics. OpenGL and Shaders I. Alex Benton, University of Cambridge Supported in part by Google UK, Ltd

CS 130 Final. Fall 2015

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

Table of Contents

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

The Projection Matrix

CS559 Computer Graphics Fall 2015

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

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

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

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

Transcription:

CSE 167 Discussion 03 ft Glynn 10/16/2017

Announcements - Midterm next Tuesday(10/31) - Sample midterms are up - Project 1 late grading until this Friday - You will receive 75% of the points you ve earned

Contents - Trackball mouse control - Demo - Math - Implementation details - Modern OpenGL - Shaders - VAO/VBO/EBO - Some stuff about parsing faces

Trackball mouse control: demo -

Trackball mouse control: math I - How to map 2D cursor position (x, y) back to 3D space? Chalkboard time! Vec3f trackballmapping(cpoint point) { Vec3f v; float d; vx = (20*pointx - windowsizex) / windowsizex; vy = (windowsizey - 20*pointy) / windowsizey; vz = 00; d = vlength(); d = (d<10)? d : 10; vz = sqrtf(1001 - d*d); vnormalize(); return v; }

Trackball mouse control: math II - How to define rotation axis, angle and speed? Chalkboard time!

Trackball mouse control: implementation details mouse_callback() Detect rotation Get cursor position(x, y) Save (x, y) as prev_pos cursor_callback() Calculate rotation Apply rotation Set prev_pos = curr_pos cursor_callback() Trackballmap(prev_pos) Get cursor position(x, y) Save (x, y) as curr_pos Trackballmap(curr_pos)

Trackball mouse control: implementation details - http://wwwglfworg/docs/latest/input_guidehtml#input_mouse - Get mouse input - maincpp: glfwsetmousebuttoncallback(window, mouse_button_callback); - windowcpp: define void mouse_button_callback(glfwwindow* window, int button, int action, int mods) - Get cursor position - maincpp: glfwsetcursorposcallback(window, cursor_pos_callback); - windowcpp: define static void cursor_position_callback(glfwwindow* window, double xpos, double ypos)

Modern OpenGL: shaders - Modern OpenGL pipeline

Modern OpenGL: shaders - Vertex shader: handles processing of individual vertices #version 330 core // NOTE: Do NOT use any version older than 330! Bad things will happen! // This is an example vertex shader GLSL is very similar to C // You can define extra functions if needed, and the main() function is // called when the vertex shader gets run // The vertex shader gets called once per vertex layout (location = 0) in vec3 position; // Uniform variables can be updated by fetching their location and passing values to that location uniform mat4 projection; uniform mat4 modelview; // Outputs of the vertex shader are the inputs of the same name of the fragment shader // The default output, gl_position, should be assigned something You can define as many // extra outputs as you need out float sampleextraoutput; void main() { // OpenGL maintains the D matrix so you only need to multiply by P, V (aka C inverse), and M gl_position = projection * modelview * vec4(positionx, positiony, positionz, 10); sampleextraoutput = 10f; }

Modern OpenGL: shaders void Cube::draw(GLuint shaderprogram) { // Calculate the combination of the model and view (camera inverse) matrices glm::mat4 modelview = Window::V * toworld; // We need to calculate this because modern OpenGL does not keep track of any matrix other than the viewport (D) // Consequently, we need to forward the projection, view, and model matrices to the shader programs // Get the location of the uniform variables "projection" and "modelview" uprojection = glgetuniformlocation(shaderprogram, "projection"); umodelview = glgetuniformlocation(shaderprogram, "modelview"); // Now send these values to the shader program gluniformmatrix4fv(uprojection, 1, GL_FALSE, &Window::P[0][0]); gluniformmatrix4fv(umodelview, 1, GL_FALSE, &modelview[0][0]); // Now draw the cube We simply need to bind the VAO associated with it glbindvertexarray(vao); // Tell OpenGL to draw with triangles, using 36 indices, the type of the indices, and the offset to start from gldrawelements(gl_triangles, 36, GL_UNSIGNED_INT, 0); // Unbind the VAO when we're done so we don't accidentally draw extra stuff or tamper with its bound buffers glbindvertexarray(0); }

Modern OpenGL: shaders - Fragment shader: handles processing of fragments created by rasterization #version 330 core // This is a sample fragment shader // Inputs to the fragment shader are the outputs of the same name from the vertex shader // Note that you do not have access to the vertex shader's default output, gl_position in float sampleextraoutput; // You can output many things The first vec4 type output determines the color of the fragment out vec4 color; void main() { // Color everything a hot pink color An alpha of 10f means it is not transparent color = vec4(10f, 041f, 07f, sampleextraoutput); }

Modern OpenGL: VAO, VBO, EBO Single VBO, glm::vec3 VAO Attrib ptr 0 Attrib ptr 1 Element array buffer VBO Cube::Cube() { toworld = glm::mat4(10f); glgenvertexarrays(1, &VAO); glgenbuffers(1, &VBO); glgenbuffers(1, &EBO); glbindvertexarray(vao); glbindbuffer(gl_array_buffer, VBO); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, GL_STATIC_DRAW); glenablevertexattribarray(0); glvertexattribpointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader In this case, it's 0 Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS 3, // This second line tells us how any components there are per vertex In this case, it's 3 (we have an x, y, and z component) GL_FLOAT, // What type these components are GL_FALSE, // GL_TRUE means the values should be normalized GL_FALSE means they shouldn't 3 * sizeof(glfloat), // Offset between consecutive indices Since each of our vertices have 3 floats, they should have the size of 3 floats in between (GLvoid*)0); // Offset of the first vertex's component In our case it's 0 since we don't pad the vertices array with anything }

Modern OpenGL: VAO, VBO, EBO Single VBO, float VAO Attrib ptr 0 Attrib ptr 1 Element array buffer VBO pos[0] nor[0] pos[1] nor[1] pos[2] nor[2] Cube::Cube() { toworld = glm::mat4(10f); glgenvertexarrays(1, &VAO); glgenbuffers(1, &VBO); glgenbuffers(1, &EBO); glbindvertexarray(vao); glbindbuffer(gl_array_buffer, VBO); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, GL_STATIC_DRAW); glenablevertexattribarray(0); glvertexattribpointer(0,// This first parameter x should be the same as the number passed into the line "layout (location = x)" in the vertex shader In this case, it's 0 Valid values are 0 to GL_MAX_UNIFORM_LOCATIONS 3, // This second line tells us how any components there are per vertex In this case, it's 3 (we have an x, y, and z component) GL_FLOAT, // What type these components are GL_FALSE, // GL_TRUE means the values should be normalized GL_FALSE means they shouldn't?, // Offset between consecutive indices Since each of our vertices have 3 floats, they should have the size of 3 floats in between (GLvoid*)0); // Offset of the first vertex's component In our case it's 0 since we don't pad the vertices array with anything }

Modern OpenGL: VAO, VBO, EBO Multiple VBOs, glm::vec3 VAO VBO VBO PREFERRED Attrib ptr 0 Attrib ptr 1 Element array buffer THE WAY

Modern OpenGL: VAO, VBO, EBO Multiple VBOs, glm::vec3 VAO VBO VBO Attrib ptr 0 Attrib ptr 1 Element array buffer

Modern OpenGL: VAO, VBO, EBO Multiple VBOs, glm::vec3 Cube::Cube() { toworld = glm::mat4(10f); } glgenvertexarrays(1, &VAO); glgenbuffers(1, &VBO); // (1) Generate VBO for normals, eg VBO2 glgenbuffers(1, &EBO); glbindvertexarray(vao); glbindbuffer(gl_array_buffer, VBO); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, GL_STATIC_DRAW); glenablevertexattribarray(0); glvertexattribpointer(0, ); // (2) Bind VBO2 // (3) Send buffer data of VBO2 // (4) Enable vertex attribute array with position 1 // (5) Define vertex attribute pointer for position 1 #version 330 core // NOTE: Do NOT use any version older than 330! Bad things will happen! layout (location = 0) in vec3 position; // Create layout for location = 1 // Uniform variables can be updated by fetching their location and passing values to that location uniform mat4 projection; uniform mat4 modelview; // Outputs of the vertex shader are the inputs of the same name of the fragment shader // The default output, gl_position, should be assigned something You can define as many // extra outputs as you need out float sampleextraoutput; void main() { // OpenGL maintains the D matrix so you only need to multiply by P, V (aka C inverse), and M gl_position = projection * modelview * vec4(positionx, positiony, positionz, 10); sampleextraoutput = 10f; }

Modern OpenGL: VAO, VBO, EBO EBO VAO Attrib ptr 0 EBO f0_ind1 Cube::Cube() { toworld = glm::mat4(10f); glgenvertexarrays(1, &VAO); glgenbuffers(1, &VBO); glgenbuffers(1, &EBO); glbindvertexarray(vao); Attrib ptr 1 f0_ind2 glbindbuffer(gl_array_buffer, VBO); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, GL_STATIC_DRAW); f0_ind3 f1_ind1 glenablevertexattribarray(0); glvertexattribpointer() glbindbuffer(gl_element_array_buffer, EBO); glbufferdata(gl_element_array_buffer, sizeof(indices), indices, GL_STATIC_DRAW); Element array buffer } glbindbuffer(gl_array_buffer, 0); glbindvertexarray(0);

Some stuff about parsing faces - Indices start from 1 not 0!!! - Indices are stored as unsigned int not glm::vec3!