OpenGL Shading Language Course. Chapter 5 Appendix. By Jacobo Rodriguez Villar jacobo.rodriguez@typhoonlabs.com



Similar documents
CSE 564: Visualization. GPU Programming (First Steps) GPU Generations. Klaus Mueller. Computer Science Department Stony Brook University

NVPRO-PIPELINE A RESEARCH RENDERING PIPELINE MARKUS TAVENRATH MATAVENRATH@NVIDIA.COM SENIOR DEVELOPER TECHNOLOGY ENGINEER, NVIDIA

OpenGL Insights. Edited by. Patrick Cozzi and Christophe Riccio

Aston University. School of Engineering & Applied Science

CS418 GLSL Shader Programming Tutorial (I) Shader Introduction. Presented by : Wei-Wen Feng 3/12/2008

Methodology for Lecture. Review of Last Demo

Tutorial 9: Skeletal Animation

Vertex and fragment programs

NVFX : A NEW SCENE AND MATERIAL EFFECT FRAMEWORK FOR OPENGL AND DIRECTX. TRISTAN LORACH Senior Devtech Engineer SIGGRAPH 2013

Computer Graphics on Mobile Devices VL SS ECTS

Performance Optimization and Debug Tools for mobile games with PlayCanvas

Programmable Graphics Hardware

OpenGL & Delphi. Max Kleiner. 1/22

How To Use An Amd Graphics Card In Greece And (Amd) With Greege (Greege) With An Amd Greeper 2.2.

OpenGL "Hello, world!"

AR-media TUTORIALS OCCLUDERS. (May, 2011)

Introduction to Computer Graphics with WebGL

Introduction to Computer Graphics

A Pipeline From COLLADA to WebGL for Skeletal Animation

Programming 3D Applications with HTML5 and WebGL

Lab7 : Putting Everything Together

Shader Model 3.0. Ashu Rege. NVIDIA Developer Technology Group

Image Processing and Computer Graphics. Rendering Pipeline. Matthias Teschner. Computer Science Department University of Freiburg

Android and OpenGL. Android Smartphone Programming. Matthias Keil. University of Freiburg

How To Teach Computer Graphics

AMD RenderMonkey IDE Version 1.71

Color correction in 3D environments Nicholas Blackhawk

GearVRf Developer Guide

LLVM for OpenGL and other stuff. Chris Lattner Apple Computer

BRINGING UNREAL ENGINE 4 TO OPENGL Nick Penwarden Epic Games Mathias Schott, Evan Hart NVIDIA

Parallel Web Programming

Web Based 3D Visualization for COMSOL Multiphysics

Introduction to Computer Graphics

DATA VISUALIZATION OF THE GRAPHICS PIPELINE: TRACKING STATE WITH THE STATEVIEWER

Game Development in Android Disgruntled Rats LLC. Sean Godinez Brian Morgan Michael Boldischar

Learning Modern 3D Graphics Programming. Jason L. McKesson

Surface and Volumetric Data Rendering and Visualization

GUI GRAPHICS AND USER INTERFACES. Welcome to GUI! Mechanics. Mihail Gaianu 26/02/2014 1

Graphics Input Primitives. 5. Input Devices Introduction to OpenGL. String Choice/Selection Valuator

IT 386: 3D Modeling and Animation. Review Sheet. Notes from Professor Nersesian s IT 386: 3D Modeling and Animation course

Introduction to Computer Graphics. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

Using the VMRC Plug-In: Startup, Invoking Methods, and Shutdown on page 4

Graphics Pipeline in a Nutshell

Developer Tools. Tim Purcell NVIDIA

SimLab 3D PDF. Settings

Optimizing Unity Games for Mobile Platforms. Angelo Theodorou Software Engineer Unite 2013, 28 th -30 th August

Introduction to GPGPU. Tiziano Diamanti

Implementation of eye tracking functions in the Presentation interface for the EyeLink-II eye tracking system, Version 0.9-Beta

Business Objects Version 5 : Introduction

Multiresolution 3D Rendering on Mobile Devices

Specifications of Paradox for Windows

Programming Mobile Applications with Android

A Virtual Environment for Review and Annotation of Character Animation

Computer Graphics. Anders Hast

OpenGL Performance Tuning

3D Modeling and Simulation using Image Stitching

OpenGL Insights. Edited by. Patrick Cozzi and Christophe Riccio

iz3d Stereo Driver Description (DirectX Realization)

SQLITE C/C++ TUTORIAL

Visualizing Data: Scalable Interactivity

16 Collection Classes

Getting Started with STATISTICA Enterprise Programming

CS 378: Computer Game Technology

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

2: Introducing image synthesis. Some orientation how did we get here? Graphics system architecture Overview of OpenGL / GLU / GLUT

UniFinger Engine SDK Manual (sample) Version 3.0.0

SketchUp Instructions

Create a New Database in Access 2010

Load balancing and failover For kbmmw v ProPlus and Enterprise Editions

Handout: Creating Forms in Word 2010

Intro to 3D Animation Using Blender

Blender Notes. Introduction to Digital Modelling and Animation in Design Blender Tutorial - week 9 The Game Engine

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

A Hybrid Visualization System for Molecular Models

VoIP Probe 3.4. Functions, Parameters & Metrics for integration with HP Business Availability Center via HP Business Process Monitor

nvfx: A New Shader-Effect Framework for OpenGL, DirectX and Even Compute Tristan Lorach ( tlorach@nvidia.com )

Writing Applications for the GPU Using the RapidMind Development Platform

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

3D VIRTUAL DESKTOP APPLICATION Of UNSW. Introduction. What we propose to do 2/4. What we propose to do 1/4. What we propose to do 4/4

IV3Dm provides global settings which can be set prior to launching the application and are available through the device settings menu.

Embedded SQL programming

GPUs Under the Hood. Prof. Aaron Lanterman School of Electrical and Computer Engineering Georgia Institute of Technology

Installation & User Guide

AMD GPU Architecture. OpenCL Tutorial, PPAM Dominik Behr September 13th, 2009

C Event Display (CED)

Bachelor of Games and Virtual Worlds (Programming) Subject and Course Summaries

The C Programming Language course syllabus associate level

WhatsUp Gold v11 Features Overview

Character Animation Tutorial

SPARROW Gateway. Developer API. Version 2.00

HELIX MEDIA LIBRARY API DEVELOPER S GUIDE

Lesson 8: Simon - Arrays

The Design of the OpenGL Graphics Interface

Dashboard Skin Tutorial. For ETS2 HTML5 Mobile Dashboard v3.0.2

The OpenGL R Graphics System: A Specification (Version 3.3 (Core Profile) - March 11, 2010)

Transcription:

OpenGL Shading Language Course Chapter 5 Appendix By Jacobo Rodriguez Villar jacobo.rodriguez@typhoonlabs.com TyphoonLabs GLSL Course 1/1

APPENDIX INDEX Using GLSL Shaders Within OpenGL Applications 2 Loading and Using Shaders 2 Using Uniforms 4 Using Textures 6 Using Vertex Attributes 7 How To Use the 3DSMax5 Mesh Exporter 9 References 10 TyphoonLabs GLSL Course 2/2

Using GLSL Shaders Within OpenGL Applications Though we have covered creating GLSL shaders, they have not yet been used in any OpenGL-based application outside of Shader Designer. We now need to look at loading, compiling, linking, and using the shaders from within the OpenGL API (more information can be found at http://developer.3dlabs.com/opengl2/slapi/index.htm). Loading and Using Shaders For a GLSL shader to work, it is important that the following four extensions are supported by the graphics card: GL_ARB_vertex_shader GL_ARB_fragment_shader GL_ARB_shader_objects GL_ARB_shading_language This information can be found in one of two ways: Checking for these tokens using the extensions string (glgetstring(gl_extensions);. Using an extension enumeration library like GLEW (http://glew.sourceforge.net), which is much easier and the method we'll use for our example(s). If the card does support GLSL, you can attempt to load/use the shader in the following way: Create a placeholder for your shader using the glcreateshaderarb call: GLhandleARB vertexshader = glcreateshaderobjectarb(glenum shadertype); shadertype must be either GL_VERTEX_SHADER_ARB for vertex shaders or GL_FRAGMENT_SHADER_ARB for fragment shaders. Load the source string into your shader using the glshadersourcearb call: void glshadersourcearb(glhandlearb shader, GLsizei nstrings, const GLcharARB **strings, const GLint *lengths); The shader will be initialized with a glcreateshaderarb call. nstrings holds the number of the strings contained within the shader (usually one); strings is a pointer to an array of strings; and lengths is an array which holds the length of the source strings. The shader is finally compiled with the function: TyphoonLabs GLSL Course 3/3

void glcompileshaderarb(glhandlearb shader) For example, to use a vertex shader called generic.vert (the file extension is not important), you would initialize it using the following code: size_t ProgramSize(char *filename) { int shader; size_t size = -1; } { } shader = _open(filename,_o_rdonly); if(shader == ENOENT) size = -1; size = _lseek(shader, 0, SEEK_END); _close(shader); return size; FILE *source = fopen("generic.vert","rb"); size_t size = ProgramSize("generic.vert"); GLcharARB *programsource = (GLcharARB *)malloc(size*sizeof(glchararb)); fread(m_programsource,m_size,1,source); fclose(source); GLhandleARB vertexshader = glcreateshaderobjectarb(glenum shadertype); glshadersourcearb(vertexshader, 1,(const GLcharARB **)&programsource,null); free(programsource); glcompileshaderarb(vertexshader); The shader is now compiled and ready to be attached to a program object (the executable unit that is a placeholder for compiled shaders). However, you may want to know the compilation result, as well as warning/error messages (if any). int compiled = 0, length = 0, laux = 0; glgetobjectparameterivarb(shader, GL_OBJECT_COMPILE_STATUS_ARB, &compiled); glgetobjectparameterivarb(shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length); GLcharARB *logstring = (GLcharARB *)malloc(length * sizeof(glchararb)); glgetinfologarb(shader, length, &laux, logstring); If the compiled variable returns false the shader will not compile due to errors (ill-formed data, resource issues, etc.). You may gain more information by retrieving the compilation log, but you must first find its length in order to allocate enough space for it. We can now define an executable unit by creating and compiling the program object. This process involves linking global variables, varyings, etc. TyphoonLabs GLSL Course 4/4

A program object (able to contain shaders of both types), which is both linkable and executable, can be created using the following function: GLhandleARB glcreateprogramobjectarb(void) Once created and compiled, the vertex and fragment shaders must be inserted' into the program object (they can be inserted into multiple program objects). This can be achieved using the glattachobjectarb call: void glattachobjectarb(glhandlearb program, GLhandleARB shader) GLhandleARB vertex = MyFuncThatLoadShaders( vertex.vert ); GLhandleARB fragment = MyFuncThatLoadShaders( fragm.frag ); GLhandleARB program = glcreateprogramobjectarb(); glattachobjectarb(program,vertex); glattachobjectarb(program,fragment); When the shaders are attached, you can link your program and leave it ready to be used: void gllinkprogramarb(glhandlearb program) You can also query the link status and information log: gllinkprogramarb(program); glgetobjectparameterivarb(m_program,gl_object_link_status_arb,&resul); glgetobjectparameterivarb(m_program,gl_object_info_log_length_arb,&length); glgetinfologarb(program,length,&laux,infolog); We now need to activate the program by making it part of the current state, which can be done using: gluseprogramobjectarb(program); // Render some stuff, with glvertex, gldrawelements... gluseprogramobjectarb(0); //back to fixed function pipeline The shaders themselves and programs can be deleted using: void gldeleteobjectarb(glhandlearb object) If an object attached to another object is deleted, it will only be flagged for deletion and not actually deleted until it's no longer attached. This means we must detach all objects before any real deletion can occur: void gldetachobjectarb(glhandlearb container, GLhandleARB attached) Using Uniforms Passing variables from an OpenGL application to a GLSL object is done using gluniform calls, but it is important that the program object is linked first. If it is not linked, the calls will have no effect, as linking operations always initialize all uniforms at 0. TyphoonLabs GLSL Course 5/5

In addition to this, gluniform calls do not use strings for variable names, but instead use integers, which can be obtained using: GLint glgetuniformlocationarb(glhandlearb program, const GLcharARB *name) int location = glgetuniformlocationarb(program, bumpsize ); gluniform1farb(location, mybumpsizevariable); There are many gluniform calls: gluniform{1 2 3 4}{f i}arb is used to pass single values. gluniform{1 2 3 4}{f i}varb is used to pass an array of values. gluniformmatrix{2 3 4}fvARB is used to pass mat2, mat3, mat4, or arrays of them. A complete description of these calls can be found at: http://developer.3dlabs.com/opengl2/slapi/uniformarb.htm TyphoonLabs GLSL Course 6/6

Using Textures Using textures involves passing both the texture unit (not the texture object) number to the shader and the correct binding of the texture to that unit. We can pass the texture unit number by using the gluniform1i(location,texture_unit); call, but if you want use this as a sampler within the fragment (or vertex) shader, you must use the [1i] variant of the gluniform or else an error will occur. You can find out the maximum number of textures a program object can support by querying the GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS_ARB define with glgetinteger (which, once known, can simply activate each texture unit and bind the texture to it) (glenable is not required here): for(int I= 0;I< my number of textures;i++) { glactivetexturearb(gl_texture0_arb + I); glbindtexturearb(gl_texture_2d,my_texture_array[i]); // glenable(gl_texture_2d); this is not needed, don t do it int location = glgetuniformlocationarb(myprogramobject, mysamplernamearray[i]); gluniform1iarb(location, I ); } glactivetexturearb(gl_texture0_arb); // back to texture unit 0 TyphoonLabs GLSL Course 7/7

Using Vertex Attributes Every time the glnormal, gltexcoord, and glcolor functions are called, you send pre-defined vertex attributes to the vertex shader. Vertex arrays (like gltexcoordpointer and glnormalpointer) can be used as well. We will now look at some generic attributes which work in much the same way, but are more flexible, as you can assign descriptive names or complex types (like mat4 instead of normal vec types). Vertex attributes must be bound to a free slot, which can be obtained via the OpenGL API, but this the program must be linked or else it will not work (as it is the link operation that decides which slots are free/not free): GLint glgetattriblocationarb(glhandlearb program, const GLcharARB *name) For example: int location = glgetattriblocationarb(myprogram, tangent ); This method will always retrieve a free slot, unless they are all occupied (we can force slots with the glbindattriblocationarb function, but it's better to ask OpenGL for a free one). Once a slot has been established, we can then send the attributes to OpenGL just like standard attributes and vertices. There are two ways to do this: immediate mode and vertex arrays. Immediate mode: Using glvertexattrib*arb between a glbegin/glend pairing, like glnormal or gltexcoord : int location = glgetattriblocationarb(myprogram, myattrib ); glbegin(gl_triangles); glvertexattrib3farb(location,0,1,0); glvertex3f(0,0,0); glvertexattrib3farb(location,1,0,0); glvertex3f(0,0,0); glvertexattrib3farb(location,0,1,1); glvertex3f(0,0,0); glend(); TyphoonLabs GLSL Course 8/8

Vertex arrays: To use a vertex array, you must first enable it for both the server and client states: glactivetexturearb(gl_texture5_arb); glclientactivetexturearb(gl_texture5_arb); glenableclientstate(gl_vertex_array); glenableclientstate(gl_normal_array); glvertexpointer(3,gl_float,0,geometry); glnormalpointer(gl_float,0,normals); //draw stuff gldisableclientstate(gl_vertex_array); gldisableclientstate(gl_normal_array); glactivetexturearb(gl_texture0_arb); glclientactivetexturearb(gl_texture0_arb); To use generic vertex attribute arrays, a location is needed, which allows you to setup your arrays easily: glactivetexturearb(gl_texture5_arb); glclientactivetexturearb(gl_texture5_arb); int location = glgetattriblocationarb(myprogram, myattrib ); glenablevertexattribarrayarb(location); glvertexattribpointerarb(location,size,type,false,0,myarray); //draw some stuff gldisablevertexattribarrayarb(location); glactivetexturearb(gl_texture0_arb); glclientactivetexturearb(gl_texture0_arb); Though this example may not be very good (as it assumes vertex attributes only, without vertices, so no drawing is actually performed), the objective is to show how to correctly setup a generic vertex attribute array. The most complex function in this example is glvertexattribpointerarb, which works the same as glnormalpointer but is generic. A complete description of this function can be found at: http://developer.3dlabs.com/opengl2/slapi/vertexattribpointerarb.htm TyphoonLabs GLSL Course 9/9

How to Use the 3DSMax Mesh Exporter Though Shader Designer has a series of default meshes, you may wish to import your own geometry. This can be done using the Flexporter (http://www.codercorner.com/flexporter.htm) a plug-in manager for 3DSMax5, with the Shader Designer s export plugin. This export plugin allows you to export an entire scene (without animations) to a GSD (Shader Designer mesh format) file. However, there are a couple of items to take into consideration before exporting any data: The default view frustum in Shader Designer is znear=1, zfar=500, fov=45, while the camera position is at (0,0,3) pointing at (0,0,0). If your mesh is too large and not centered correctly at (0,0,0), you may not see anything. If this is the case, reduce the mesh until it fits within a bounding box of 3 units, then place it (the scene) at the origin. Remember to perform a resetxform on every submesh/object of your scene before exporting it. TyphoonLabs GLSL Course 10/10

References: Shader Designer homepage: http://www.typhoonlabs.com OpenGL Shading Language API man pages, GLSL examples and resources: http://developer.3dlabs.com GameInstitute homepage: http://www.clockworkcoders.com/oglsl/tutorials.html GLSL Shaders: http://3dshaders.com GLSL Tutorials: http://www.clockworkcoders.com TyphoonLabs GLSL Course 11/11