Pushin’ Geo to the GPU As well as colors, normals, and other vertex data Made by Jeff Chastine, Modified by Chao Mei Jeff Chastine.

Slides:



Advertisements
Similar presentations
Chapter 11. Faster Geometry Throughput Presented by Garrett Yeh.
Advertisements

Vertex Buffer Objects, Vertex Array Objects, Pixel Buffer Objects.
EECS 700: Computer Modeling, Simulation, and Visualization Dr. Shontz Chapter 2: Shader Fundamentals (continued)
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
TEXTURE MAPPING JEFF CHASTINE 1. TEXTURE MAPPING Applying an image (or a texture ) to geometry 2D images (rectangular) 3D images (volumetric – such as.
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
As well as colors, normals, and other vertex data PUSHIN’ GEO TO THE GPU JEFF CHASTINE 1.
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
Open Graphics Library (OpenGL)
Michael Robertson Yuta Takayama. WebGL is OpenGL on a web browser. OpenGL is a low-level 3D graphics API Basically, WebGL is a 3D graphics API that generates.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
CS 418: Interactive Computer Graphics Introduction to WebGL: HelloTriangle.html Eric Shaffer.
WebGL: in-browser 3D graphics Nick Whitelegg Maritme and Technology Faculty Southampton Solent University.
Speeding Up Rendering After Deciding What to Draw.
CS 450: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS Spring 2012.
OPENGL INTRODUCTION 林宏祥 2014/10/06. 此投影片已被稍微簡化過,跟今日課堂上的內容不太一樣。 如果想要看更詳細說明的人,請參考每頁投影片下面的『備忘稿』
OpenGL Shader Language Vertex and Fragment Shading Programs.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Ed Angel Professor Emeritus of Computer Science.
CS 480/680 Computer Graphics Programming with Open GL Part 5: Putting it all together Dr. Frederick C Harris, Jr. Fall 2011.
Programming with OpenGL Part 5: More GLSL Isaac Gang University of Mary Hardin-Baylor E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
CS 4363/6353 SHADERS/GLSL. ANOTHER LOOK AT THE PIPELINE Vertex Processing Clipping/Assembly Rasterization Fragment Processing.
A study of efficiency INDEX BUFFERS JEFF CHASTINE 1.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Build your own 2D Game Engine and Create Great Web Games using HTML5, JavaScript, and WebGL. Sung, Pavleas, Arnez, and Pace, Chapter 5 Examples 1.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Interactive Computer Graphics CS 418 – Fall 2017 Indexed Meshes Transformations Background Imagery:
Real-Time Rendering Buffers in OpenGL 3.3
and an introduction to matrices
Computer Graphics Index Buffers
Real-Time Rendering Geometry and Buffers
Objectives Simple Shaders Programming shaders with GLSL
OpenGL ARB Superbuffers
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
Programming with OpenGL
Introduction to Computer Graphics with WebGL
Building Models Ed Angel Professor Emeritus of Computer Science
Introduction to Shaders
HW for Computer Graphics
Introduction to Computer Graphics with WebGL
Isaac Gang University of Mary Hardin-Baylor
Vertex Array Objects & Buffer Objects
Computer Graphics Practical Lesson 8
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 3: Shaders
Programming with OpenGL Part 5: More GLSL
Programming with OpenGL Part 2: Complete Programs
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
CIS 441/541: Introduction to Computer Graphics Lecture 15: shaders
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 5: More GLSL
Introduction to OpenGL
CS 480/680 Computer Graphics GLSL Overview.
CS 480/680 (Fall 2018) Part 2: Texture Loading
Computer Graphics Transformations
Mickaël Sereno Graphics Memory Mickaël Sereno 11/07/2019 Mickaël Sereno -
Introduction to Computer Graphics with WebGL
OpenGL Background CS 4722.
Opengl implementation
Computer Graphics Vertex Array Object
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Pushin’ Geo to the GPU As well as colors, normals, and other vertex data Made by Jeff Chastine, Modified by Chao Mei Jeff Chastine

Triangles and Vertices A triangle has 3 vertices. How and when can we describe the triangle to the Graphics Card? Answers: 3 floats per vertex, pass each vertex individually – many calls to graphics card deprecated:: glVertex Immediate mode drawing is deprecated – why? An array (i.e. a vertex array) Lets buffer these in advance – benefits?

A Mesh Class A mesh is a bunch of triangles. It contains information about the vertices (e.g., position, color, etc.). It should be able to Add a triangle to the mesh Render the mesh

Overview: Draw a triangle in OpenGL 1. Define the vertex data for a mesh: put it in a dynamic array in CPU memory 2. Create buffers in graphics memory: dedicated to hold the mesh’s vertices 3. Define the GLSL vertex shader program to use for rendering the mesh 4. Draw each mesh with one function call

This is what we want to make Jeff Chastine

Normalized Device Coordinate System (-1, 1) (0, 1) (1, 1) Normalized Device Coordinate System (-1, 0) (1, 0) (0, 0) (-1, -1) (0, -1) (1, -1) Jeff Chastine

Coordinates of our triangle (0.0f, 0.5f, 0.0f) (-0.5f, -0.5f, 0.0f) (0.5f, -0.5f, 0.0f) Jeff Chastine

QUICK! Color Theory! Represent almost any color by adding red, green and blue Alpha is the transparency Called the primary colors (RGB or RGBA) 0.0 means “all the way off” 1.0 means “all the way on” Examples: Red (1.0, 0.0, 0.0, 1.0) Blue (0.0, 0.0, 1.0, 1.0) Purple (1.0, 0.0, 1.0, 1.0) Yellow (1.0, 1.0, 0.0, 1.0) White (1.0, 1.0, 1.0, 1.0) Black (0.0, 0.0, 0.0, 1.0) Grey (0.5, 0.5, 0.5, 1.0) Brown (0.7, 0.5, 0.1, 1.0) Jeff Chastine

Colors of our triangle* (0.0f, 0.0f, 1.0f, 1.0f) (1.0f, 0.0f, 0.0f, 1.0f) (0.0f, 1.0f, 0.0f, 1.0f) *Note the beautiful interpolation of color! Jeff Chastine

Basic Problem Get the geometry and color to the GPU Typically also need a normal and texture coordinate for each vertex! Ask the OpenGL driver to create a buffer object This is just a chunk of memory (e.g. array) Nothing to be afraid of! Located on the GPU (probably) Jeff Chastine

Working with Buffers To create a buffer ID: // This will be the ID of the buffer GLuint buffer; // Ask OpenGL to generate exactly 1 unique ID glGenBuffers(1, &buffer); To set this buffer as the active one and specify which buffer we’re referring to: glBindBuffer(GL_ARRAY_BUFFER, buffer); Documentation: https://www.opengl.org/sdk/docs/man/ Notes: That buffer is now bound and active! Any “drawing” will come from that buffer Any “loading” goes into that buffer Jeff Chastine

Two Approaches to Loading the Buffer with Data Assume everything is in GLfloat* (called data) One-shot call to load the buffer with data: glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); Other drawing types GL_X_Y: X STREAM for infrequent use and changes STATIC for frequent use and infrequent change DYNAMIC for frequent use and frequent change Y could be DRAW, READ or COPY Jeff Chastine

How We’ll do it Process Create the buffer and pass no data Load the geometry Load the colors (if any) after that Load the normals after that… Note: some like to interlace their vertex data… Jeff Chastine

A Side-By-SIDE Comparison Show the relationship between client code and shader code Assume we’re still trying to draw 1 triangle… numVertices – the number of vertices (will be 3 - duh!) verts – the position information of each vertex (XYZ - array of GLfloats) colors – the color information of each vertex (RGBA - array of GLfloats) Jeff Chastine

Note: buffer “lives” on the graphics card in a nice, two-bedroom loft… GLuint buffer; glGenBuffers(1, &buffer); buffer Note: buffer “lives” on the graphics card in a nice, two-bedroom loft… Jeff Chastine

Hey – I’m active now GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer (GL_ARRAY_BUFFER, buffer); buffer Jeff Chastine

Allocate how much space (in bytes) we need Now I know how big I am! GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer (GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, numVertices*7*sizeof(GLfloat), NULL, GL_STATIC_DRAW); buffer Allocate how much space (in bytes) we need Where’d the 7 come from? Jeff Chastine

Allocate how much space (in bytes) we need Now I know how big I am! GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer (GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, numVertices*7*sizeof(GLfloat), NULL, GL_STATIC_DRAW); buffer Allocate how much space (in bytes) we need Where’d the 7 come from? (x, y, z) + (r, g, b, a) = 7 Jeff Chastine

it’s pretty big though… Now I’m putting verts at the beginning GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer (GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, numVertices*7*sizeof(GLfloat), NULL, GL_STATIC_DRAW); glBufferSubData (GL_ARRAY_BUFFER, 0, numVertices*3*sizeof(GLfloat), verts); glBufferSubData (GL_ARRAY_BUFFER, numVertices*3*sizeof(GLfloat), numVertices*4*sizeof(GLfloat), colors); buffer verts Put verts at 0… it’s pretty big though… Jeff Chastine

Put colors starting right after that! I’m putting colors next GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer (GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, numVertices*7*sizeof(GLfloat), NULL, GL_STATIC_DRAW); glBufferSubData (GL_ARRAY_BUFFER, 0, numVertices*3*sizeof(GLfloat), verts); glBufferSubData (GL_ARRAY_BUFFER, numVertices*3*sizeof(GLfloat), numVertices*4*sizeof(GLfloat), colors); buffer verts colors Put colors starting right after that! It’s pretty big too… Jeff Chastine

What we Have so far… We have a buffer with an ID That buffer lives on the graphics card That buffer is full of vertex position/color data How do we get that info to our shader? Jeff Chastine

How To link to the Shader Query the shader program for its variables The code below goes into the shader program and gets the “vPosition” ID GLuint vpos; vpos = glGetAttribLocation (programID, “vPosition”); In OpenGL, we have to enable things (attributes, in this case): glEnableVertexAttribArray(vpos); // turn on vPosition Finally, we set the location and tell it the format of the data in the buffer glVertexAttribPointer(vpos, 3, GL_FLOAT, GL_FALSE, 0, 0); void glVertexAttribPointer(GLuint index, GLint size, Glenum type, GLboolean normalized, GLsizei stride, const GLvoid* offset); Jeff Chastine

Find the variable “vPosition” inside the shader GLuint loc = glGetAttribLocation(shaderProgramID, "vPosition"); #version 130 in vec4 vPosition; in vec4 vColor; out vec4 color; void main () { color = vColor; gl_Position = vPosition; } Find the variable “vPosition” inside the shader Jeff Chastine

Guys! I’m still active, remember? GLuint loc = glGetAttribLocation(shaderProgramID, "vPosition"); glEnableVertexAttribArray(loc); glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0); #version 130 in vec4 vPosition; in vec4 vColor; out vec4 color; void main () { color = vColor; gl_Position = vPosition; } Guys! I’m still active, remember? buffer verts colors Jeff Chastine

Tell vColor where to find its data in me… GLuint loc2 = glGetAttribLocation(shaderProgramID, "vColor"); glEnableVertexAttribArray(loc2); glVertexAttribPointer(loc2, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertices*3*sizeof(GLfloat))); #version 130 in vec4 vPosition; in vec4 vColor; out vec4 color; void main () { color = vColor; gl_Position = vPosition; } buffer verts colors Jeff Chastine

GLuint loc = glGetAttribLocation(shaderProgramID, "vPosition"); glEnableVertexAttribArray(loc); glVertexAttribPointer(loc, 3, GL_FLOAT, GL_FALSE, 0, 0); GLuint loc2 = glGetAttribLocation(shaderProgramID, "vColor"); glEnableVertexAttribArray(loc2); glVertexAttribPointer(loc2, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertices*3*sizeof(GLfloat))); #version 130 in vec4 vPosition; in vec4 vColor; out vec4 color; void main () { color = vColor; gl_Position = vPosition; } Jeff Chastine

One Last thing Vertex Array Objects (VAOs) “Pure State” - it remembers almost everything about buffers Set it up once, then just call it before drawing! glVertexAttribPointer… Doesn’t bind the VBO though… Creating a vertex array object: // This will be the ID of the VAO GLuint vao; // Ask the driver for exactly 1 unique ID glGenVertexArrays(1, &vao); // Everything after this will be part of the VAO glBindVertexArray(vao); Jeff Chastine

// Create the "remember all" glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Create a buffer and bind it as active glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); // Create space and load the buffer glBufferData(GL_ARRAY_BUFFER, 7*3*sizeof(GLfloat), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, 3*3*sizeof(GLfloat), vertices); glBufferSubData(GL_ARRAY_BUFFER, 3*3*sizeof(GLfloat),3*4*sizeof(GLfloat), colors); // Find the positions of the variables in the shader positionID = glGetAttribLocation(shaderProgramID, "vPosition"); colorID = glGetAttribLocation(shaderProgramID, "vColor"); // Tell the shader variables where to find their data glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(3*3*sizeof(GLfloat))); glUseProgram(shaderProgramID); glEnableVertexAttribArray(positionID); glEnableVertexAttribArray(colorID); Jeff Chastine

END WITH A UTAH TEAPOT! Jeff Chastine