A Really (too) Short Introduction to OpenGL Peter Rautek.

Slides:



Advertisements
Similar presentations
GLSL Basics Discussion Lecture for CS 418 Spring 2015 TA: Zhicheng Yan, Sushma S Kini, Mary Pietrowicz.
Advertisements

 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.
Informationsteknologi Wednesday, December 12, 2007Computer Graphics - Class 171 Today’s class OpenGL Shading Language.
MAT 594CM S10Fundamentals of Spatial ComputingAngus Forbes Week 5 : GLSL Shaders Topics: Shader syntax, passing textures into shaders, per-pixel lighting,
GLSL I May 28, 2007 (Adapted from Ed Angel’s lecture slides)
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Mohan Sridharan Based on slides created by Edward Angel GLSL I 1 CS4395: Computer Graphics.
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.
CS 480/680 Computer Graphics Course Overview Dr. Frederick C Harris, Jr. Fall 2012.
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.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
WebGL: in-browser 3D graphics Nick Whitelegg Maritme and Technology Faculty Southampton Solent University.
1 Angel and Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Introduction to Computer Graphics Ed Angel Professor Emeritus of Computer.
1 Introduction to Computer Graphics SEN Introduction to OpenGL Graphics Applications.
Computer Graphics I, Fall 2008 Introduction to Computer Graphics.
1 Graphics CSCI 343, Fall 2015 Lecture 4 More on WebGL.
CS 480/680 Intro Dr. Frederick C Harris, Jr. Fall 2014.
به نام خدا تنظیم کننده : فرانه حدادی استاد : مهندس زمانیان تابستان 92.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Computer Graphics The Rendering Pipeline - Review CO2409 Computer Graphics Week 15.
1 Graphics CSCI 343, Fall 2015 Lecture 2 Introduction to HTML, JavaScript and WebGL.
Computer Graphics Bing-Yu Chen National Taiwan University.
OpenGL Shader Language Vertex and Fragment Shading Programs.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Shading in OpenGL Ed Angel Professor Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E.
CSE 381 – Advanced Game Programming GLSL. Rendering Revisited.
OpenGL-ES 3.0 And Beyond Boston Photo credit :Johnson Cameraface OpenGL Basics.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
1 Graphics CSCI 343, Fall 2015 Lecture 5 Color in WebGL.
1 Graphics CSCI 343, Fall 2015 Lecture 3 Introduction to WebGL.
Vertex Buffer Objects and Shader Attributes. For Further Reading Angel 7 th Ed: –Most parts of Chapter 2. Beginning WebGL: –Chapter 1: vertex Buffer Objects,
Programming with OpenGL Part 0: 3D API. For Further Reading Angel 7 th Ed: –2.2: JavaScript –2.3: OpenGL & WebGL –2.8: fragment & vertex shaders Beginning.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor.
Week 3 Lecture 4: Part 2: GLSL I Based on Interactive Computer Graphics (Angel) - Chapter 9.
Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Emeritus of Computer Science University of New Mexico.
 Learn some important functions and process in OpenGL ES  Draw some triangles on the screen  Do some transformation on each triangle in each frame.
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
Programming with OpenGL Part 3: Shaders Ed Angel Professor of Emeritus of Computer Science University of New Mexico 1 E. Angel and D. Shreiner: Interactive.
GLSL I.  Fixed vs. Programmable  HW fixed function pipeline ▪ Faster ▪ Limited  New programmable hardware ▪ Many effects become possible. ▪ Global.
An Introduction to the Cg Shading Language Marco Leon Brandeis University Computer Science Department.
1 Introduction to Computer Graphics with WebGL ADOPTED from Ed Angel Chapter 2 Angel and Shreiner: Interactive Computer Graphics 7E © Addison-Wesley 2015.
COMP 175 | COMPUTER GRAPHICS Remco Chang1/XX13 – GLSL Lecture 13: OpenGL Shading Language (GLSL) COMP 175: Computer Graphics April 12, 2016.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Shader.
Introduction to OpenGL
COMP 5441: ADVANCED COMPUTER GRAPHICS FALL 2017
Objectives Simple Shaders Programming shaders with GLSL
GLSL I Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts Director, Arts Technology Center University of New Mexico.
Programming with OpenGL Part 2: Complete Programs
Introduction to Computer Graphics with WebGL
Day 05 Shader Basics.
Transformation, perspective projection, and LookAT in WebGL vs.OpenGL
Chapter VI OpenGL ES and Shader
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Programming with OpenGL Part 3: Shaders
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 OpenGL
CS 480/680 Computer Graphics GLSL Overview.
CS 480/680 Part 1: Model Loading.
Introduction to Computer Graphics with WebGL
OpenGL-Rendering Pipeline
CS 480/680 Fall 2011 Dr. Frederick C Harris, Jr. Computer Graphics
Presentation transcript:

A Really (too) Short Introduction to OpenGL Peter Rautek

What Is OpenGL? OpenGL is a computer graphics rendering application programming interface (API for short) High-quality rendering with geometric and image primitives Basis of many interactive applications that include 3D graphics Graphics part of your application can be Operating system independent Window system independent Low level API You program the graphics hardware You need to do the math Many platforms support it

What Is OpenGL?

Goal of the Course - Agenda OpenGL Pipeline and Application Development Shaders and OpenGL Shading Language (GLSL) Examples (code and visuals) all using modern OpenGL Hands On Session Visualize your own pointcloud Modify shaders and program

OpenGL Pipeline

Pipelines OpenGL 4.1 (released July 25 th, 2010) Latest version is introduced compute shaders Primitive Setup and Rasterization Primitive Setup and Rasterization Fragment Shader Blending Vertex Data Pixel Data Vertex Shader Texture Store Geometry Shader Tessellation Control Shader Tessellation Evaluation Shader

A Simplified Pipeline Model Vertex Processing Rasterizer Fragment Processing Vertex Shader Vertex Shader Fragment Shader Fragment Shader GPU Data Flow Application Framebuffer Vertices Fragments Pixels

OpenGL for Embedded Systems and the Web OpenGL ES 2.0 Designed for embedded and hand-held devices such as cell phones Based on OpenGL 3.1 OpenGL ES 3.0 released but support limited to most recent smartphones WebGL 1.0 JavaScript implementation of OpenGL ES 2.0 Runs on most recent browsers WebGL 2.0 under development – will be based on OpenGL ES 3.0 Takeaway message: Lots of versions We define modern OpenGL: OpenGL 3.1 ~ OpenGL ES 2.0 ~ WebGL 1.0

OpenGL Application Development

OpenGL Programming in a Nutshell Modern OpenGL programs essentially do the following steps: Create shader programs: Compile, link and create shader programs Create buffer objects and load data into them Setup buffers for vertex data or other data you need to compute your image Initialize buffers and send them to the GPU “Plumbing” data locations with shader variables Create variables in shaders Initialize them with values from your application Render Tell the GPU how to render using your buffers and shaders

Application Framework Requirements OpenGL applications need a place to render into On-screen window Off-screen buffer Need to communicate with native windowing system Windowing system interfaces are different for different platforms We use WebGL with html5 and the canvas element

Representing Geometric Objects Geometric objects are represented using vertices A vertex is a collection of generic attributes Positional coordinates Colors Texture coordinates Normals Other data Position stored in 4 dimensional homogeneous coordinates Vertex buffer objects (VBOs) store vertex data Array of structs or struct of arrays A junk of memory on your graphics card Vertex array objects (VAOs) are a collection of VBOs

OpenGL’s Geometric Primitives All primitives are specified by vertices GL_TRIANGLE_STRIP GL_TRIANGLE_FAN GL_LINES GL_LINE_LOOP GL_LINE_STRIP GL_TRIANGLES GL_POINTS

A First Program

Our First Program We’ll render a cube with colors at each vertex Our example demonstrates: initializing vertex data organizing data for rendering simple object modeling –building up 3D objects from geometric primitives –building geometric primitives from vertices

Initializing the Cube’s Data We’ll build each cube face from individual triangles How many vertices required (6 faces)*(2 triangles/face)*(3 vertices/triangle) var NumVertices = 36; We use a vec4 class similar to GLSL’s vec4 type Before we can initialize our VBO, we need to stage the data Our cube has two attributes per vertex Position Color We create two arrays to hold the VBO data on the application side var vPositions; var vColors;

Cube Data Vertices of a unit cube centered at origin, sides aligned with axes var vPositions = [ // Front face -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, // Back face -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, // Top face -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, // Bottom face -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, // Right face 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, // Left face -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5 ]; var vColors = [ … ]

Cube Index Buffer var vIndices = [ 0, 1, 2, 0, 2, 3, // front 4, 5, 6, 4, 6, 7, // back 8, 9, 10, 8, 10, 11, // top 12, 13, 14, 12, 14, 15, // bottom 16, 17, 18, 16, 18, 19, // right 20, 21, 22, 20, 22, 23 // left ]; 18

Vertex Attributes Vertex data must be stored in a buffer (the VBO) Generate VBO by calling // generate buffer for position data var vertexPositionBuffer = gl.createBuffer(); // generate buffer for index buffer var vertexIndexBuffer = gl.createBuffer(); Bind specific VBO for initialization and load data into it // bind vertex position buffer gl.bindBuffer(gl.ARRAY_BUFFER, vertexPositionBuffer); // load data into it gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vPositions), gl.STATIC_DRAW); // bind index buffer gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(vIndices), gl.STATIC_DRAW);

Connecting Vertex Shaders with Geometric Data Application vertex data enters the OpenGL pipeline through the vertex shader Need to connect vertex data to shader variables We do this right after the setup of shaders Requires knowing the attribute location Attribute location queried by calling // specify the program to be used gl.useProgram(shaderProgram); // get the attribute location vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition"); // enable the attribute gl.enableVertexAttribArray(vertexPositionAttribute);

Drawing Geometric Primitives Usually invoked in display callback Invokes shader pipeline // draw the cube gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, vertexIndexBuffer); gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);

Shaders and GLSL

GLSL Data Types Scalar types: float, int, bool Vector types: vec2, vec3, vec4 ivec2, ivec3, ivec4 bvec2, bvec3, bvec4 Matrix types: mat2, mat3, mat4 Texture sampling: sampler1D, sampler2D, sampler3D, samplerCube Constructors: vec3 a = vec3(1.0, 2.0, 3.0);

Components, Swizzling, Operators Access vector components using either: [ ] (c-style array indexing) xyzw, rgba or strq (named components) For example: vec3 v; v[1], v.y, v.g, v.t // all refer to the same element Component swizzling: vec3 a, b; Operators: a.xy = b.yx; mat4 m; vec4 a, b, c; b = a*m; c = m*a;

Qualifiers in, out Copy vertex attributes and other variable into and out of shaders in vec2 texCoord; out vec4 color; uniform shader-constant variable from application uniform float time; uniform vec4 rotation; Varying variables interpolated: per vertex value -> per fragment value varying float s;

Functions Built in Arithmetic: sqrt, power, abs Trigonometric: sin, asin Graphical: length, reflect User defined

Built-in Variables gl_Position (required) output position from vertex shader gl_FragCoord input fragment position gl_FragDepth input depth value in fragment shader

Simple Vertex Shader for Cube Example attribute vec3 aVertexPosition; attribute vec4 aVertexColor; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; varying lowp vec4 vColor; void main(void) { vec4 pos = vec4(aVertexPosition, 1.0); gl_Position = uPMatrix * uMVMatrix * pos; vColor = aVertexColor; }

The Simplest Fragment Shader varying lowp vec4 vColor; void main(void) { gl_FragColor = vColor; }

Getting Your Shaders into OpenGL Shaders need to be compiled and linked to form an executable shader program OpenGL provides the compiler and linker A program must contain vertex and fragment shaders other shaders are optional Create Shader Load Shader Source Compile Shader Create Program Attach Shader to Program Link Program gl.createProgram() gl.shaderSource() gl.compileShader() gl.createShader() gl.attachShader() gl.linkProgram() Use Program gl.useProgram() These steps need to be repeated for each type of shader in the shader program

Initializing Uniform Variable Values Shader code: uniform mat4 uPMatrix; Application code: var perspectiveMatrix = mat4.perspective(45, 640.0/480.0, 0.1, 100.0); var pUniform = gl.getUniformLocation(shaderProgram, "uPMatrix"); gl.uniformMatrix4fv(pUniform, false, new Float32Array(perspectiveMatrix));

Conclusion Wasn’t that easy? No! …but drawing one cube is not so much different from Drawing thousands of cubes Drawing other geometric objects What we haven’t covered Transformations: How to move stuff around Interaction: How mouse, keys, gestures can affect your scene Animation: How geometry can deform Textures: How to add all the nice details Lighting: How to add fancy illumination to your scene Shaders: Do more useful computation in your shaders and tons more 32

Q & A & Examples

Resources

Books Modern discussion The OpenGL Programming Guide, 8 th Edition Interactive Computer Graphics: A Top-down Approach using OpenGL, 6 th Edition The OpenGL Superbible, 5 th Edition Older resources The OpenGL Shading Language Guide, 3 rd Edition OpenGL and the X Window System OpenGL Programming for Mac OS X OpenGL ES 2.0 Programming Guide Not quite yet … WebGL Programming Guide: Interactive 3D Graphics Programming with WebGL

Online Resources SIGGRAPH University - An introduction to OpenGL programming: Youtube: Material and code: The OpenGL Website: API specifications Reference pages and developer resources Downloadable OpenGL (and other APIs) reference cards Discussion forums The Khronos Website: Overview of all Khronos APIs Numerous presentations

Acknowledgements Slides and resources: Dave Shreiner and Edward Angel (SIGGRAPH 2013 course) Code and examples: Three.js, K3D.js, glMatrix.js WebGL Playground Google Inc.

Hands On Session - WebGL

Setup – Node.js and Http-Server Install node.js Run command line as administrator From the command line locate the nodejs directory npm install http-server –g http-server From the browser: 39

How-to get your point cloud into the right format Download Meshlab Import Mesh: your pointcloud Export Mesh: Select ply format Check: –Colors –Normal Un-check: –Binary Encoding 40