Real-Time Rendering Geometry and Buffers

Slides:



Advertisements
Similar presentations
Vertex Buffer Objects, Vertex Array Objects, Pixel Buffer Objects.
Advertisements

Chapter 2: Graphics Programming
CS 450: COMPUTER GRAPHICS FILLING POLYGONS SPRING 2015 DR. MICHAEL J. REALE.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Building Models modified by Ray Wisman Ed Angel Professor of Computer Science,
1 Building Models. 2 Objectives Introduce simple data structures for building polygonal models ­Vertex lists ­Edge lists OpenGL vertex arrays.
Status – Week 257 Victor Moya. Summary GPU interface. GPU interface. GPU state. GPU state. API/Driver State. API/Driver State. Driver/CPU Proxy. Driver/CPU.
CS 4363/6353 BASIC RENDERING. THE GRAPHICS PIPELINE OVERVIEW Vertex Processing Coordinate transformations Compute color for each vertex Clipping and Primitive.
Introduction to OpenGL
 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.
Informationsteknologi Monday, October 29, 2007Computer Graphics - Class 21 Today’s class Graphics programming Color.
Drawing Geometric Objects
1 Lecture 4 Graphical primitives Rasterization: algorithmic approach Rasterization: geometric approach 2D discrete lines, triangles Discrete planes 3D.
Open Graphics Library (OpenGL)
COS 397 Computer Graphics Assoc. Prof. Svetla Boytcheva AUBG 2013 COS 397 Computer Graphics Practical Session №1 Introduction to OpenGL, GLFW and CG.
Basic OpenGL Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Wednesday, September 10, 2003.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
CS 450: COMPUTER GRAPHICS PORTRAIT OF AN OPENGL PROGRAM SPRING 2015 DR. MICHAEL J. REALE.
Image Synthesis Rabie A. Ramadan, PhD 1. 2 About my self Rabie A. Ramadan My website and publications
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
OpenGL Buffer Transfers Patrick Cozzi University of Pennsylvania CIS Spring 2012.
Representation. Objectives Introduce concepts such as dimension and basis Introduce coordinate systems for representing vectors spaces and frames for.
OpenGL Shader Language Vertex and Fragment Shading Programs.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
What are shaders? In the field of computer graphics, a shader is a computer program that runs on the graphics processing unit(GPU) and is used to do shading.
OpenGL Shading Language
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 4: Color and Attributes Isaac Gang University.
A study of efficiency INDEX BUFFERS JEFF CHASTINE 1.
Graphics Graphics Korea University kucg.korea.ac.kr Graphics Programming 고려대학교 컴퓨터 그래픽스 연구실.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL Based on GL (graphics library) by Silicon Graphics Inc. (SGI) Advantages: Runs on everything, including.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
CS 480/680 Computer Graphics Programming with Open GL Part 2: Complete Programs Dr. Frederick C Harris, Jr. Fall 2011.
Unit-4 Geometric Objects and Transformations- I
Pushin’ Geo to the GPU As well as colors, normals, and other vertex data Made by Jeff Chastine, Modified by Chao Mei Jeff Chastine.
Administrivia Back on track: canceling OpenGL lecture 2 Assignment 1
CSC Graphics Programming
Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Real-Time Rendering Buffers in OpenGL 3.3
The Basics: HTML5, Drawing, and Source Code Organization
Computer Graphics Index Buffers
Introduction to OpenGL
Chapter 6 GPU, Shaders, and Shading Languages
Objectives Simple Shaders Programming shaders with GLSL
Building Models Ed Angel
Programming with OpenGL Part 4: Color and Attributes
Day 05 Shader Basics.
Chapter VI OpenGL ES and Shader
CSE 411 Computer Graphics Lecture #3 Graphics Output Primitives
Introduction to Computer Graphics with WebGL
Isaac Gang University of Mary Hardin-Baylor
Primitive Objects Lecture 6 Wed, Sep 5, 2007.
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
Building Models Ed Angel Professor Emeritus of Computer Science
Introduction to Computer Graphics with WebGL
Isaac Gang University of Mary Hardin-Baylor
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 3: Shaders
Programming with OpenGL Part 4: Color and Attributes
Programming with OpenGL Part 2: Complete Programs
Mickaël Sereno Shaders Mickaël Sereno 25/04/2019 Mickaël Sereno -
Introduction to OpenGL
Language Definitions Chap. 3 of Orange Book.
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
Computer Graphics Vertex Array Object
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

Real-Time Rendering Geometry and Buffers CSE 5542 Prof. Roger Crawfis

End Result Slot 0 Slot 4 Slot 2 What is a slot? Mem loc. ??? Fixed or reuseable? Length? Slot 0 Slot 4 Slot 2 Goal today is to configure the data streams coming into a shader program (vertex shader). Shader program here wants 3 streams: Vec3 Vec4 Vec2 Vertex Shader vec3 position; vec4 vertexColor; vec2 texCoords;

Geometric Primitives Primitive types include: Set of points Set of individual line segments A single open polyline A single closed polyline Set of individual triangles Triangle strip Triangle fan

Front/Back Rendering Each triangle has two sides, front and back Can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwise This is basically the right-hand rule Note that this still holds after perspective projection

OpenGL: Triangles We could just have points, lines and triangles, but there are some issues: Performance Memory Visual Artifacts Each triangle has 3 vertices, so N triangles would require 3*N vertices in the array (VBO). v0 v2 v1 v3 v4 v5

OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces redundancy by sharing vertices: Only N+2 vertices! v0 v2 v1 v3 v4 v5 triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)

OpenGL: Triangle Fan The GL_TRIANGLE_FAN primitive is another way to reduce vertex redundancy: v0 v1 v2 v3 v4 v5 v6

Points in OpenGL Old syntax glBegin / glEnd p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_POINTS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Lines in OpenGL (1/3) Line Segments p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_LINES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Lines in OpenGL (2/3) Polylines – Line Strip p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_LINE_STRIP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Lines in OpenGL (3/3) Polylines – Line Loop p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_LINE_LOOP); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons (1/2) Definition Simple Polygon Object that is closed as in a line loop, but that has an interior Simple Polygon No pair of edges of a polygon cross each other Simple Nonsimple

Polygons (2/2) Convexity If all points on the line segment between any two points inside the object, or on its boundary, are inside the object p1 p2 Convex Objects

Polygons in OpenGL (2/6) Quadrilaterals p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_QUADS); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (3/6) Quadstrip p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_QUAD_STRIP); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p0); glVertex2fv(p4); glVertex2fv(p7); glVertex2fv(p5); glVertex2fv(p6); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (4/6) Triangles p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_TRIANGLES); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (5/6) Triangle Strip p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_TRIANGLE_STRIP); glVertex2fv(p0); glVertex2fv(p7); glVertex2fv(p1); glVertex2fv(p6); glVertex2fv(p2); glVertex2fv(p5); glVertex2fv(p3); glVertex2fv(p4); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Polygons in OpenGL (6/6) Triangle Fan p0 p1 p2 p3 p4 p5 p6 p7 glBegin(GL_TRIANGLE_FAN); glVertex2fv(p0); glVertex2fv(p1); glVertex2fv(p2); glVertex2fv(p3); glVertex2fv(p4); glVertex2fv(p5); glVertex2fv(p6); glVertex2fv(p7); glEnd(); p0 p1 p2 p3 p4 p5 p6 p7

Attributes Properties that determines How to render a geometric primitive Color, thickness, pattern of filling, etc. Color Three color theory Red Blue Green M Y C Cyan Yellow Magenta G B R Additive Color Subtractive Color Color Solid

OpenGL API syntax glUniform2f(loc, x, y) Add ‘v’ for vector number of Components/ Dimensions 2 – (x,y) 3 – (x,y,z) 4 – (x,y,z,w) i – int ui – unsigned int f – float d – double Add ‘v’ for vector glUniform2fv(loc,count,array) No method overloading in C or FORTRAN Internally everything is usually a device driver dependent?

Specifying Vertices glBegin / glEnd fed the GPU with a small straw. Data passed one vertex, one color, etc. at a time. All data was stored on the CPU (ignoring display lists). Better to: Send the data as a large memory chunk (aka, an array) Possibly store the data on the GPU

Vertex Buffer Objects (VBO) You will see the term VBO used for specifying the vertices. 3 Steps to create Generate an OpenGL object (GUID) Bind and configure the object. Copy data into the object. Separate steps to then use it Slot assignment and binding.

Data Buffers For most of your geometry you will use one or more GL_ARRAY_BUFFER’s. These are just arrays or void* pointers. Create the pointer using glGenBuffers(). Bind this pointer to have subsequent operations apply to it using glBindBuffer. You bind to the GL_ARRAY_BUFFER proxy.

Proxies in OpenGL Think of GL_ARRAY_BUFFER as a global variable or singleton. When you bind to it, you are setting and using the “object” that is currently bound to this proxy.

Setting Data You can set the memory of the buffer using glBufferData and optionally glBufferSubData. Pass in a null pointer (0) to glBufferData to have it initialized to zero. glBufferSubData can be used to fill a subset of the data.

What is your data for? When you set the buffer data with glBufferData you also give OpenGL a hint as to what it will be used for. GL_STATIC_DRAW indicates that it will be set once and used to draw each frame, so perhaps move this data to the GPU. GL_DYNAMIC_DRAW indicates that it may be changed every frame, so why take up GPU space. GL_STREAM_DRAW indicates that is similar to GL_STATIC_DRAW but may not be used very often, so don’t waste GPU memory.

Using Buffers You set the buffer with a chunk of memory. OpenGL has no knowledge of what it is or what to do with it. For this we associate buffers with semantic streams. The glVertexAttribPointer can be used to indicate the type (e.g., GL_FLOAT) and dimension of the data (e.g., vec2). More importantly, it indicates which slot or stream the data should be bound to. The last argument in this routine indicates an offset (not a pointer). The pointer is actually to the currently bound buffer.

Serialized Buffers You can use the offset to place one attribute immediately after the other. This would be one big buffer with say the vertices, followed by the normals, followed by the texture coordinates. Not terribly useful. Why not just use three separate buffers! Vertex positions Vertex normals offset Other per vertex data

Interleaved Data A more useful strategy is to interleave the data such that the first vertex position is followed by the first normal and the first texture coordinate. This can be used to stream an array of structs. Vertex data: x0,y0,nx0, … x1,y1,nx1, … …

Multiple Buffers A clean way is to allocated a separate buffer for each attribute. How do we associate each of these buffers (or offsets; or offsets and strides)?

Multiple Streams A Shader Program can have many streams of data coming into it. These are indicated by two mappings A mapping from buffers to slots. A mapping from slots to vertex shader variables (aka the semantics). The end result is we have a mapping from buffers to vertex shader variables. Each slot or stream also needs to be enabled using glEnableVertexAttribArray.

Vertex Variables You connect streams to the “in” variables in your vertex shader. Remember, uniforms are constants. “in” variables in the other shaders come from the previous stage. Each “in” variable is a different stream or slot. Two approaches to wire these slots: In the source code (C, C++, etc.) In the shader (OpenGL 3.3 or greater and not in OpenGL ES (as of 2009)).

Slot Assignments Slot 0 Slot 4 Slot 2 Vertex Shader vec3 position; vec4 vertexColor; vec2 texCoords;

glBindAttribLocation You configure the association between slots and variable names with the glBindAttribLocation, which takes in a shader program, a slot number and a string for the variable name. The association between vertex variables and slots needs to be done before the shader is linked (or you need to link again). The data or buffers do not need to be defined or even known at this time. A common name system works well for this: inVertex : slot 0 inNormal : slot 1 TexCoord0 : slot 2 …

glGetAttribLocation You can also have OpenGL assign the slots automatically during linking. Calling glGetAttribLocation can then be called after linking to determine the slot assigned. This seems to be more common.

Using layout in GLSL A third approach allows you to explicitly specify the location in the shader. Example: layout (location = 0) in vec4 Position; You can use this with the glGetAttribLocation to keep things consistent or just assume positions are at slot 0.

Vertex Array Objects A Vertex Array Object (VAO) organizes all of the state required to draw an object. Saves the many BindBuffer / VertexAttribPointer calls needed to draw. In other words it stores the association between buffers and slots, including which and how many slots are enabled. Think of it as a Dictionary (C#) or map (C++).

Using VAO’s When everything is all set up you can draw models: glUseProgram(myShader) // Bind the Shader program glBindVertexArray(vaoMyGeometry); // Bind Vertex Array Object  glDrawArrays(GL_TRIANGLES, 0, numTris); // Draw our geom.   glBindVertexArray(0); // Unbind  Vertex Array Object  You should have a different VAO for each chunk of geometry (model, etc.)

Best Practices Note: Tight coupling between shader and geometry ! Standardize the naming conventions on the inputs to the vertex shader. Uber shaders

Problems Issues Vertex inputs < geometry specification? Shadows Wireframe Debug Vertex inputs > geometry specification? Uber shader

Render Modes Typically have a composite (or component) model where geometry contains a material. Move decision as to what shader to use away from the geometry object. RenderManager controls the bindings and can override the object’s shader. Works with some things (shadows, debug, NPR), but not others (geometry images)