520 likes | 702 Views
Echtzeitgraphik Repetitorium. Outline. Introduction to GLSL Framebuffer objects Engine and Demo Design Popular Effects Info. Introduction to GLSL. Outline. Introduction Compiling shaders Enabling shaders Parameter types Shader datatypes Writing shaders Error handling Examples.
E N D
Outline • Introduction to GLSL • Framebuffer objects • Engine and Demo Design • Popular Effects Info RTR Team 2008
Introduction to GLSL RTR Team 2008
Outline • Introduction • Compiling shaders • Enabling shaders • Parameter types • Shader datatypes • Writing shaders • Error handling • Examples RTR Team 2008
Introduction 1/2 • Shaders replace fixed function pipeline • Small programs used for transformation and lighting purpose • Shader for • Per vertex operations • Per fragment operations • New: operations to generate geometry RTR Team 2008
Introduction 2/2 • GLSL • Official OpenGL Shading Language • Extension in OpenGL 1.4, integrated in 2.0 • 3D Labs [1] RTR Team 2008
Compiling shaders 1/2 • Shader types • GL_VERTEX_SHADER • GL_FRAGMENT_SHADER • char * myShaderSource; //contains shadersource • int mShaderHandle = glCreateShader(GL_SHADER_TYPE); • glShaderSource(mShaderHandle, 1, myShaderSource, NULL); • glCompileShader(mShaderHandle); RTR Team 2008
Compiling shaders 2/2 • Create program • Attaching shaders • Attach vertex and fragment shader • Link program • int mProgramHandle = glCreateProgram(); • glAttachShader(mProgramHandle, mShaderHandle); • glLinkProgram(mProgramHandle); RTR Team 2008
Enabling shaders • Enable GLSL program • glUseProgram(mProgramHandle); RTR Team 2008
Parameter types 1/2 • Uniforms • Unique per batch • Read-only in shaders • Attributes • Per vertex • Read-only in vertex shader • int mParameterHandle = glGetUniformLocation(mProgramHandle, „parameterName“); • glUniform{1|2|3|4|Matrix}{sifd}(mParameterHandle, value); • Int mParameterHandle = glGetAttribLocation(…); • glVertexAttrib{1234}{sifd}(mParameterHandle, value); RTR Team 2008
Parameter types 2/2 • Varying • Transfer from vertex to fragment shader • Only available in shader code • varying keyword • OpenGL matrices • GLSL • Usable via gl_ModelViewMatrix etc RTR Team 2008
Shader datatypes • float,vec2,vec3, vec4 • mat2, mat3, mat4 • sampler1D, sampler2D, sampler3D • … RTR Team 2008
Writing shaders 1/2 • Fixed set of input and output variables [5] • gl_Position • gl_MultiTexCoord{i} //vertex shader in • gl_TexCoord[] //vs out, fs in • gl_FragColor • ... • uniform sampler2D tex; • void main() { • do something and write into output variables • } RTR Team 2008
Error handling 1/2 • Programs • int return = 1; • glGetProgram(mProgram,GL_LINK_STATUS,&return); • if (!return) { //something went wrong while linking • int length = 0; • glGetProgram(mProgram, GL_INFO_LOG_LENGTH,&length); • std::string info(length, ‘’); • int written = 0; • glGetProgramInfoLog(mProgram, length, &written, &info[0]); • std::cout << "Error linking program\n\n" << info << std::endl; • } RTR Team 2008
Error handling 2/2 • Shaders • int return = 1; • glGetShader(mShader,GL_COMPILE_STATUS,&return); • if (!return) { //something went wrong while compiling • int length = 0; • glGetShader(mShader, GL_INFO_LOG_LENGTH,&length); • std::string info(length, ‘’); • int written = 0; • glGetShaderInfoLog(mShader, length, &written, &info[0]); • std::cout << "Error linking program\n\n" << info << std::endl; • } RTR Team 2008
Examples 1/3 • C++ • Load shader and initialize parameter handles • glActiveTexture(GL_TEXTURE0); • Enable and bind texture • glUseProgram (mProgramHandle); • glUniform1i(mShaderTexture, 0); • Set other parameters • draw geometry • glUseProgram(0); RTR Team 2008
Examples 2/3 • Vertex shader • uniform mat3 NormalMatrix; // Normal matrix • uniform vec3 LightDir; // Light direction in world space • varying float diffuse; • void main() • { • gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; //transform vertex like ffp • vec3 worldnormal = NormalMatrix * gl_Normal.xyz; • diffuse = max(dot(worldnormal, LightDir), 0.0); • gl_TexCoord[0] = gl_MultiTexCoord0; • } RTR Team 2008
Examples 3/3 • Fragment shader • uniform sampler2D Texture; // Object texture • uniform vec4 LightColor; // Light color • varying float diffuse; • void main() • { • vec4 colorlookup = texture2D(Texture, gl_TexCoord[0].xy); • gl_FragColor = colorlookup * LightColor * diffuse; • } RTR Team 2008
Framebuffer objects RTR Team 2008
Outline • Benefit • Description • Setup • Renderbuffer • Textures • Status checking • Render to texture • Use texture • Clean up RTR Team 2008
Benefit • Instead of rendering to the screen • Save as a texture (with color and depth attachments) • Use the texture in further passes (shadow map, mini-map, showing different views of the scene RTR Team 2008
Description • FBO is an encapsulation of attachments • Attachments can be color or renderbuffers • Renderbuffers are objects that support off-screen rendering without an assigned texture • Depth buffer and stencil buffer • There can be up to 8 color attachments • Number depends on your HW • More than one is advanced stuff RTR Team 2008
Setup • Some code you have to use for setup • Create and validate its handle • Bind it • GLuint fbo; • glGenFramebuffersEXT(1, &fbo); • glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); RTR Team 2008
Renderbuffer (depth buffer) 1/2 • Attaching renderable objects (like depth buffer - DB) • Create its handle • Bind it • GLuint depthbuffer; • glGenRenderbuffersEXT(1, &depthbuffer); • glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depthbuffer); RTR Team 2008
Renderbuffer (depth buffer) 2/2 • We define its size… • Last creator’s job: attaching DB to FBO • glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height); • glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, depthbuffer); RTR Team 2008
Textures 1/2 • Assumption: a handle to a texture • NOTE: width and height are the same as those of FBO! • GLuint img; • glGenTextures(1, &img); • glBindTexture(GL_TEXTURE_2D, img); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); RTR Team 2008
Textures 2/2 • We set the target of the FBO • glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, img, 0); RTR Team 2008
Status checking • If everything worked out correctly • GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); RTR Team 2008
Rendering to texture • Bind + render + unbind • Note: keep the same height and width of texture for the viewport size • glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); glPushAttrib(GL_VIEWPORT_BIT); • glViewport(0,0,width, height); • render scene • glPopAttrib(); • glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); RTR Team 2008
Use texture • Just bind it like a regular texture • Note: don’t forget about texture filtering stuff! • glBindTexture(GL_TEXTURE_2D, img); RTR Team 2008
Clean up • Clean up, unbinding, deleting is done by • Cleaning of render buffers • Delete also all textures associated with FBO • glDeleteFramebuffersEXT(1, &fbo); • glDeleteRenderbuffersEXT(1, &depthbuffer); RTR Team 2008
Additional info • With an FBO, you can render into more than 1 texture simultaneously • Check the tutorials about more color attachments and DrawBuffers extension at [3] RTR Team 2008
Engine and Demo Design RTR Team 2008
Outline • Meshes • Animation • Camera-Path • Scene • Engine Design Tips RTR Team 2008
Meshes • Maya, 3ds Max, Blender, … • Mesh-format needs to support all your requirements • OBJ: simple - best choice for geometry only • FBX, X-File: use loader and library functions • Importer and Exporter have to work • Deep Exploration - good conversion and repair tool RTR Team 2008
Animation • Use own Rotors or Translations • Blendshapes: Vertex interpolation of two or more poses of a mesh • Advanced animations depend on mesh format • Keyframes • Bone Hierarchy with Skinning RTR Team 2008
Camera-Path • Take Position, LookAt and Up-Vector • LookAt can also be set to objects • Assign timestamps • Use Catmull-Rom spline interpolation • Hardcoded or with text-script • #Timestamp; Position; LookAt; UpVec; • 0; 10 0 10; 0 5 5; 0 0 1; • 30; 40 0 10; 0 5 5; 0 0 1; • ... • 266; 50 10 10; "Car"; 0 0 1; • ... RTR Team 2008
Scene • Scene content: • Meshes • Lights • Camera-Path • Triggers/Events • Can use hardcoded scene management or visibility RTR Team 2008
Engine Design Tips 1/3 • Please don’t use fixed function pipeline! • Aside from advanced shaders (for effects) you typically only need 2 “basic” shaders • Transform, render solid colored • Transform, render colored and textured • (If you are too lazy to reset transform matrices for fullscreen quads, then 2 additional shaders without transform ) RTR Team 2008
Engine Design Tips 2/3 • No need for complex state management • The only useful states today are: • Alphatesting • Blending • Culling • Depthtest • Polygonmode RTR Team 2008
Engine Design Tips 3/3 • Use a separate world, view, projection matrix • World space is suited best for most effects and lighting • Implement interface for transform matrices yourself (no need to stick to GL_*Matrix) RTR Team 2008
Popular Effects Info RTR Team 2008
Outline • Normal mapping • Shadow mapping • Shadow Volumes (Z-Fail) • Bloom • Motion Blur • Depth Of Field RTR Team 2008
Normal mapping • Needs: Tangent Space Calculator, Shader • Algorithm: • Calculate per-vertex tangent • Per fragment: transform light vector into tangent space • Use normal from normal map for lighting • Work: 1 RTR Team 2008
Shadow mapping • Needs: Rendertarget, Shaders • Algorithm: • From Light’s view: render per-fragment depth into a render target • From Camera’s view: compare distance of fragment to light source to shadowmap depth • Possible extensions: VSM, Cascaded SM, Perspective SM, LispSM • Work: 2 RTR Team 2008
Shadow Volumes (Z-Fail) • Needs: Geometry Extrusion Info, Shader • Algorithm: • Render geometry to depth buffer • Calculate extruded silhouette • Extrude objects away from light, render into stencil buffer (2-sided, wrapping) • Each fragment with stencil value != 0 is shadowed • Work: 2 RTR Team 2008
Bloom • Needs: Rendertargets, Shaders • Algorithm: • Render bloom colors into a render target • Blur this horizontally (using a nice kernel, e.g. Gauss) into another render target • Blur this vertically into another render target. • Overlay over scene as additive blended quad. • Work: 1 RTR Team 2008
HDR • Needs: Rendertargets (lots), Shaders (lots) • Algorithm: • Render scene into rendertarget • Downsample the scene until it is 1x1 to calculate average luminance • Calculate target key for this luminance • Adaption: shift current key towards target • Use the new key for a tonemapper to output the scene into RGB values • If you want to, add bloom • Work: 4 RTR Team 2008
Motion Blur • Needs: Rendertargets, Shaders • Algorithm: • Render scene into rendertarget • Render per-fragment velocity vectors into another rendertarget. Use camera transformation of last frame to calculate the velocity • Use velocity to blur per-fragment along movement vectors • Work: 2 RTR Team 2008
Depth Of Field • Needs: Rendertargets, Shaders • Algorithm: • Render scene into rendertarget • Render depth into rendertarget • Either calculate focus depth from downsampling depth values in the center of the screen, or use scripted focus depth • For each fragment, blur kernel size depends on distance of fragment depth to focus depth • Work: 2 RTR Team 2008