OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main...
Transcript of OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main...
![Page 1: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/1.jpg)
OpenGL
COMP 575/770
Spring 2013
![Page 2: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/2.jpg)
What is OpenGL?
An API for 2D and 3D rendering
Cross-platform (Windows, Mac, consoles, mobile…)
Multi-language (C/C++, Java, Python…)
Presents a unified interface to all kinds of graphics hardware
Scales from low-power phones to high-end desktops
Originally released in 1992
Now at version 4.3
![Page 3: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/3.jpg)
OpenGL Applications
Rage (id Software, 2011)
![Page 4: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/4.jpg)
Similar APIs
OpenGL ES
Variant of OpenGL for use in Embedded Systems
Highly popular due to iOS, Android
Some high-end OpenGL features missing
Direct3D
Microsoft’s rendering API
Driven by Xbox 360
Feature set nearly identical to OpenGL
![Page 5: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/5.jpg)
OpenGL API Family
OpenGL
The main rendering API
GLU
OpenGL Utility functions
Various helper functions for matrices, surfaces, etc.
Packaged with OpenGL
GLUT
OpenGL Utility Toolkit
Manages window creation, keyboard/mouse input, etc.
Modern implementation: FreeGLUT
![Page 6: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/6.jpg)
OpenGL Pipeline
OpenGL implements the standard graphics pipeline
i.e., designed for rasterization
Most GPUs today are programmable
Vertex shaders, fragment shaders, etc.
Many are not, though
For these, OpenGL has fixed-function mode
![Page 7: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/7.jpg)
OpenGL Pipeline
Fixed-Function
Overall pipeline is fixed, with some configurability
Can specify matrices, configure depth buffer tests, etc.
Can perform per-vertex lighting
Programmable
Can specify shaders for different stages of the pipeline
Shaders written in GLSL (OpenGL Shading Language)
Preferred way to write OpenGL code
![Page 8: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/8.jpg)
A Minimal GLUT Program
int main(int argc, char** argv)
{
// Initialize GLUT.
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("OpenGL");
// Set up GLUT callbacks.
glutReshapeFunc(reshape);
glutDisplayFunc(display);
// Start rendering.
glutMainLoop();
return 0;
}
![Page 9: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/9.jpg)
A Minimal GLUT Program
void reshape(int w, int h)
{
glutPostRedisplay();
}
void display()
{
glutSwapBuffers();
}
![Page 10: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/10.jpg)
A Minimal GLUT Program
void reshape(int w, int h)
{
glutPostRedisplay();
}
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
![Page 11: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/11.jpg)
![Page 12: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/12.jpg)
Rasterization
glBegin(GL_TRIANGLES);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(0, 1);
glEnd();
Draws a single triangle
![Page 13: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/13.jpg)
![Page 14: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/14.jpg)
Function Names
glVertex2f(0, 1);
All OpenGL functions start with gl
Vertex is the “name” of the function
Specifies a vertex using 2 numbers
Each of these numbers is a float
![Page 15: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/15.jpg)
Rasterization
glBegin(GL_TRIANGLES);
glColor3f(0, 1, 0);
glVertex2f(0, 0);
glVertex2f(1, 0);
glVertex2f(0, 1);
glEnd();
Draws a single green triangle
![Page 16: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/16.jpg)
![Page 17: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/17.jpg)
Rasterization
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex2f(0, 0);
glColor3f(0, 1, 0);
glVertex2f(1, 0);
glColor3f(0, 0, 1);
glVertex2f(0, 1);
glEnd();
Draws a single triangle with interpolated colors
![Page 18: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/18.jpg)
![Page 19: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/19.jpg)
Primitive Assembly
All OpenGL sees is a sequence of vertices
What to make of them?
Specify using glBegin
Tells OpenGL how to assemble a primitive from a sequence of vertices
Multiple possible values can be passed
![Page 20: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/20.jpg)
GL_POINTS
![Page 21: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/21.jpg)
GL_LINES
![Page 22: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/22.jpg)
GL_LINE_STRIP
![Page 23: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/23.jpg)
GL_TRIANGLES
![Page 24: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/24.jpg)
GL_TRIANGLE_STRIP
![Page 25: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/25.jpg)
Transform Pipeline
Our version:
𝐩 = 𝐌𝑣𝑝𝐌𝑜𝑟𝑡ℎ𝐌𝑝𝑒𝑟𝑠𝑝𝐌𝑐𝑎𝑚𝐌𝑚𝑜𝑑𝑒𝑙𝐩0
OpenGL’s version:
𝐩 = 𝐌𝑣𝑖𝑒𝑤𝑝𝑜𝑟𝑡𝐌𝑝𝑟𝑜𝑗𝑒𝑐𝑡𝑖𝑜𝑛𝐌𝑚𝑜𝑑𝑒𝑙𝑣𝑖𝑒𝑤𝐩0
Viewport matrix not stored explicitly
![Page 26: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/26.jpg)
Transform Pipeline
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0, -1, 0, 1, 0);
glTranslatef(0, 0, -7);
glScalef(2, 2, 2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.1, 0.1, -0.1, 0.1, 0.1, 1000);
glViewport(0, 0, 512, 512);
glutSolidSphere(1, 32, 16);
![Page 27: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/27.jpg)
![Page 28: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/28.jpg)
Transform Pipeline
OpenGL stores matrices in column-major order
Opposite to the usual convention in many languages!
More relevant when using programmable pipeline
Matrices aren’t just calculated, they’re multiplied into the “current” matrix
Multiplication occurs from the right
Near and far depths are positive by convention
![Page 29: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/29.jpg)
Back-Face Culling
To enable back-face culling:
To specify which face is the front face:
In terms of vertex order in window coordinates
To specify which side to cull:
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
![Page 30: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/30.jpg)
Depth Buffering
Depth buffer must be initialized when window is created:
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
Must be cleared before rendering:
glClearDepth(1000);
glClear(GL_DEPTH_BUFFER_BIT);
![Page 31: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/31.jpg)
Depth Buffering
// Clear depth buffer.
// Set up viewport and projection matrices.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -7);
glColor3f(0, 0, 1);
glutSolidSphere(2, 32, 16);
glLoadIdentity();
glTranslatef(2, 0, -10);
glColor3f(1, 0, 0);
glutSolidSphere(2, 32, 16);
![Page 32: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/32.jpg)
![Page 33: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/33.jpg)
Depth Buffering
// Clear depth buffer.
// Set up viewport and projection matrices.
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -7);
glColor3f(0, 0, 1);
glutSolidSphere(2, 32, 16);
glLoadIdentity();
glTranslatef(2, 0, -10);
glColor3f(1, 0, 0);
glutSolidSphere(2, 32, 16);
![Page 34: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/34.jpg)
![Page 35: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/35.jpg)
Shading
Blinn-Phong shading model:
𝐿 = 𝑘𝑎𝐼𝑎 + 𝑘𝑑𝐼 max 0, 𝐧 ⋅ 𝐥 + 𝑘𝑠𝐼 max 0, 𝐧 ⋅ 𝐡 𝑝
Need to specify:
Material properties 𝑘𝑎, 𝑘𝑑 , 𝑘𝑠, 𝑝
Light parameters 𝐼𝑎, 𝐼
Vertex properties:
glVertex3f specifies positions
glNormal3f specifies normals
![Page 36: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/36.jpg)
Material Properties
float ka[] = {0, 1, 0, 0};
float kd[] = {0, 0.5, 0, 0};
float ks[] = {0.5, 0.5, 0.5, 0};
float p = 32;
glMaterialfv(GL_FRONT, GL_AMBIENT, ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, kd);
glMaterialfv(GL_FRONT, GL_SPECULAR, ks);
glMaterialf(GL_FRONT, GL_SHININESS, p);
![Page 37: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/37.jpg)
Light Parameters
float Ia[] = {0.2, 0.2, 0.2, 0};
float l[] = {-4, 4, 4, 1};
float la[] = {0, 0, 0, 0};
float ld[] = {1, 1, 1, 0};
float ls[] = {1, 1, 1, 0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, Ia);
glLightfv(GL_LIGHT0, GL_POSITION, l);
glLightfv(GL_LIGHT0, GL_AMBIENT, la);
glLightfv(GL_LIGHT0, GL_DIFFUSE, ld);
glLightfv(GL_LIGHT0, GL_SPECULAR, ls);
NOTE: Model-View matrix is applied to the light position, so need to compensate
![Page 38: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/38.jpg)
Shading
// Clear depth buffer, enable depth test.
// Set up transform pipeline.
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Configure material properties and light parameters.
glutSolidSphere(2, 32, 16);
![Page 39: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/39.jpg)
![Page 40: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/40.jpg)
Flat Shading
// Clear depth buffer, enable depth test.
// Set up transform pipeline.
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// Configure material properties and light parameters.
glShadeModel(GL_FLAT);
glutSolidSphere(2, 32, 16);
![Page 41: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/41.jpg)
![Page 42: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/42.jpg)
Fixed Function Limitations
No way to do per-pixel shading
Once vertices are specified, OpenGL takes over
No way to do deferred shading
How to redirect output to an offscreen buffer?
There’s still a bottleneck!
We’re specifying vertices over and over again
Data travels from CPU to GPU unnecessarily
How to store vertex data in GPU memory?
![Page 43: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/43.jpg)
Programmable Pipeline
Arbitrary code executed during:
Vertex processing
Fragment processing
Other pipeline stages
Some pipeline stages remain fixed
Perspective divide, clipping, rasterization, depth buffer…
Shader programs written using GLSL
Compiled on CPU, uploaded to GPU
![Page 44: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/44.jpg)
The Simplest Vertex Shader
#version 330
in vec4 position;
void main()
{
gl_Position = position;
}
in defines a vertex attribute
gl_Position is the position in canonical view volume
This shader just passes the vertex through as-is
![Page 45: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/45.jpg)
The Simplest Fragment Shader
#version 330
out vec4 outColor;
void main()
{
outColor = vec4(1, 1, 1, 1);
}
out defines an output value
This shader just outputs the same color for all fragments
![Page 46: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/46.jpg)
Compiling Shaders
// vsSource is a string containing the vertex shader source
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vsSource, NULL);
glCompileShader(vertexShader);
// fsSource is a string containing the fragment shader source
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fsSource, NULL);
glCompileShader(fragmentShader);
![Page 47: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/47.jpg)
Shader Programs
GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
glUseProgram(program);
![Page 48: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/48.jpg)
Transforms
in vec4 position;
uniform mat4 modeling;
uniform mat4 camera;
uniform mat4 projection;
void main()
{
gl_Position = projection * camera * modeling * position;
}
![Page 49: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/49.jpg)
Uniform Inputs
// Assumes modelingMatrix is a row-major 4x4 matrix.
GLint modelingUniform = glGetUniformLocation("modeling");
glUniformMatrix4fv(modelingUniform, 1, GL_TRUE, modelingMatrix);
// Repeat for "camera" and "projection".
uniform variables are inputs to vertex shaders
They have the same value for all vertices
They are not vertex attributes
![Page 50: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/50.jpg)
Per-Vertex Shading (VS)
in vec4 position;
in vec4 normal;
uniform mat4 modeling;
uniform mat4 modeling_inv_tr;
uniform mat4 camera;
uniform mat4 projection;
out vec4 color;
vec4 shade(vec4 wp, vec4 wn) { // Shading code goes here. }
void main()
{
gl_Position = projection * camera * modeling * position;
vec4 wPos = modeling * position;
vec4 wNormal = modeling_inv_tr * normal;
color = shade(wPos, wNormal);
}
![Page 51: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/51.jpg)
Per-Vertex Shading (FS)
in vec4 color;
out vec4 outColor;
void main()
{
outColor = color;
}
![Page 52: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/52.jpg)
Flat Shading
flat in vec4 color;
out vec4 outColor;
void main()
{
outColor = color;
}
![Page 53: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/53.jpg)
Per-Pixel Shading (VS)
in vec4 position;
in vec4 normal;
uniform mat4 modeling;
uniform mat4 modeling_inv_tr;
uniform mat4 camera;
uniform mat4 projection;
out vec4 wPosition;
out vec4 wNormal;
void main()
{
gl_Position = projection * camera * modeling * position;
wPosition = modeling * position;
wNormal = modeling_inv_tr * normal;
}
![Page 54: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/54.jpg)
Per-Pixel Shading (FS)
in vec4 wPosition;
in vec4 wNormal;
out vec4 outColor;
void main()
{
outColor = shade(wPosition, wNormal);
}
![Page 55: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/55.jpg)
GPU Buffers
Can allocate buffers (arrays) in GPU memory Used to store vertices, indices, other data
// "data" is a pointer to an array containing "size" bytes
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
Buffer not allocated until glBufferData called
GL_STATIC_DRAW indicates that the buffer won’t be modified Lets the driver optimize where it allocates the buffer
![Page 56: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/56.jpg)
Binding in OpenGL
glBufferData isn’t passed the buffer itself
GL_ARRAY_BUFFER is a binding point
Like a “global variable” can be set to any buffer
Using glBindBuffer
Buffer bound to GL_ARRAY_BUFFER
Many other binding points and objects that can be bound
![Page 57: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/57.jpg)
Vertex Array Objects
Need to specify how the data in a buffer should be interpreted
Positions? Normals? Positions followed by normals?
Described using vertex array objects
Defines the semantics of buffers
![Page 58: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/58.jpg)
Vertex Array Objects
GLuint vertexArray;
glGenVertexArrays(1, &vertexArray);
glBindVertexArray(vertexArray);
// Positions and normals stored in the buffer
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
// Positions and normals 4 floats each, interleaved
// p0 n0 p1 n1 p2 n2...
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*sizeof(float),
4*sizeof(float));
![Page 59: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/59.jpg)
Element Buffers
Storing each vertex for each triangle is costly
Store each vertex exactly once
Describe the triangle using 3 indices into the vertex array
The indices may also be stored in GPU memory
In an element buffer
Bind a buffer to GL_ELEMENT_ARRAY_BUFFER to use as an element buffer
![Page 60: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/60.jpg)
Drawing From Buffers
Without an index buffer: // Draw two triangles.
glDrawArrays(GL_TRIANGLES, 0, 6);
With an index buffer: // Draw two triangles.
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
The appropriate vertex array object and element buffer must be bound
![Page 61: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/61.jpg)
Other Buffers
Many other uses for GPU memory buffers, including
Textures
Storing images to paint on objects
Uniform shader variables
e.g., an array of light source positions
Frame buffers
Destination for rendering operations
Useful for deferred shading, shadows, etc.
![Page 62: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/62.jpg)
Other Shaders
Vertex and fragment shaders are not the only kinds of shaders
Geometry shaders Runs on each primitive, outputs one or more primitives
Useful for cube map rendering
Tesselation shaders Useful for rendering curved surfaces
Compute shaders Essentially GPGPU code
![Page 63: OpenGL - GAMMAgamma.cs.unc.edu/graphicscourse/12_OpenGL.pdf · OpenGL API Family OpenGL The main rendering API GLU OpenGL Utility functions Various helper functions for matrices,](https://reader030.fdocuments.net/reader030/viewer/2022012313/5c8ba32409d3f22c4e8c3fa8/html5/thumbnails/63.jpg)
Further Information
OpenGL and GLSL Specifications http://www.khronos.org/opengl
Microsoft/Apple documentation http://msdn.microsoft.com/en-us/library/windows/desktop/dd374278(v=vs.85).aspx
https://developer.apple.com/devcenter/mac/resources/opengl/
Tutorials http://www.arcsynthesis.org/gltut/index.html