glm (1125872), страница 4
Текст из файла (страница 4)
Following GLSL conventions is a way to find consensus. Moreover,basically when a developer knows GLSL, he knows GLM.6.2. Does GLM run GLSL program?No, GLM is a C++ implementation of a subset of GLSL.6.3. Does a GLSL compiler build GLM codes?No, this is not what GLM attends to do!6.4. Should I use ‘GTX’ extensions?GTX extensions are qualified to be experimental extensions. In GLM this means thatthese extensions might change from version to version without any restriction. Inpractice, it doesn’t really change except time to time.
GTC extensions are stabled,tested and perfectly reliable in time. Many GTX extensions extend GTC extensionsand provide a way to explore features and implementations and APIs and then arepromoted to GTC extensions. This is fairly the way OpenGL features are developed;through extensions.6.5. Where can I ask my questions?A good place is the OpenGL Toolkits forum on OpenGL.org6.6. Where can I find the documentation of extensions?The Doxygen generated documentation includes a complete list of all extensionsavailable. Explore this API documentation to get a complete view of all GLMcapabilities!6.7.
Should I use ‘using namespace glm;’?NO! Chances are that if ‘using namespace glm;’ is called, especially in a header file,name collisions will happen as GLM is based on GLSL which uses common tokens fortypes and functions. Avoiding ‘using namespace glm;’ will a higher compatibility withthird party library and SDKs.6.8. Is GLM fast?First, GLM is mainly designed to be convenient and that's why it is written againstGLSL specification. Following the 20-80 rules where 20% of the code grad 80% of theperformances, GLM perfectly operates on the 80% of the code that consumes 20% ofthe performances. This said, on performance critical code section, the developerswill probably have to write to specific code based on a specific design to reach peakperformances but GLM can provides some descent performances alternatives basedon approximations or SIMD instructions.6.9.
When I build with Visual C++ with /W4 warning level, I havewarnings...You should not have any warnings even in /W4 mode. However, if you expect suchlevel for you code, then you should ask for the same level to the compiler by at leastdisabling the Visual C++ language extensions (/Za) which generates warnings whenused. If these extensions are enabled, then GLM will take advantage of them and thecompiler will generate warnings.7. Code samplesThis series of samples only shows various GLM features without consideration of anysort.7.1.
Compute a triangle normal#include <glm/glm.hpp> // vec3 normalize crossglm::vec3 computeNormal(glm::vec3 const & a,glm::vec3 const & b,glm::vec3 const & c){return glm::normalize(glm::cross(c - a, b - a));}// A much faster but less accurate alternative:#include <glm/glm.hpp> // vec3 cross#include <glm/gtx/fast_square_root.hpp> // fastNormalizeglm::vec3 computeNormal(glm::vec3 const & a,glm::vec3 const & b,glm::vec3 const & c){return glm::fastNormalize(glm::cross(c - a, b - a));}7.2. Matrix transform// vec3, vec4, ivec4, mat4#include <glm/glm.hpp>// translate, rotate, scale, perspective#include <glm/gtc/matrix_transform.hpp>// value_ptr#include <glm/gtc/type_ptr.hpp>void setUniformMVP(GLuint Location,glm::vec3 const & Translate,glm::vec3 const & Rotate){glm::mat4 Projection =glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.f);glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f),Translate);glm::mat4 ViewRotateX = glm::rotate(ViewTranslate,Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f));glm::mat4 View = glm::rotate(ViewRotateX,Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f));glm::mat4 Model = glm::scale(glm::mat4(1.0f),glm::vec3(0.5f));glm::mat4 MVP = Projection * View * Model;glUniformMatrix4fv(Location, 1, GL_FALSE, glm::value_ptr(MVP));}7.3.
Vector types#include <glm/glm.hpp> //vec2#include <glm/gtc/type_precision.hpp> //hvec2, i8vec2, i32vec2std::size_t const VertexCount = 4;// Float quad geometrystd::size_t const PositionSizeF32 = VertexCount * sizeof(glm::vec2);glm::vec2 const PositionDataF32[VertexCount] ={glm::vec2(-1.0f,-1.0f),glm::vec2( 1.0f,-1.0f),glm::vec2( 1.0f, 1.0f),glm::vec2(-1.0f, 1.0f)};// Half-float quad geometrystd::size_t const PositionSizeF16 = VertexCount * sizeof(glm::hvec2);glm::hvec2 const PositionDataF16[VertexCount] ={glm::hvec2(-1.0f, -1.0f),glm::hvec2( 1.0f, -1.0f),glm::hvec2( 1.0f, 1.0f),glm::hvec2(-1.0f, 1.0f)};// 8 bits signed integer quad geometrystd::size_t const PositionSizeI8 = VertexCount * sizeof(glm::i8vec2);glm::i8vec2 const PositionDataI8[VertexCount] ={glm::i8vec2(-1,-1),glm::i8vec2( 1,-1),glm::i8vec2( 1, 1),glm::i8vec2(-1, 1)};// 32 bits signed integer quad geometrystd::size_t const PositionSizeI32 = VertexCount * sizeof(glm::i32vec2);glm::i32vec2 const PositionDataI32[VertexCount] ={glm::i32vec2(-1,-1),glm::i32vec2( 1,-1),glm::i32vec2( 1, 1),glm::i32vec2(-1, 1)};7.4.
Lighting#include <glm/glm.hpp> // vec3 normalize reflect dot pow#include <glm/gtx/random.hpp> // vecRand3// vecRand3, generate a random and equiprobable normalized vec3glm::vec3 lighting(intersection const & Intersection,material const & Material,light const & Light,glm::vec3 const & View){glm::vec3 Color = glm::vec3(0.0f);glm::vec3 LightVertor = glm::normalize(Light.position() - Intersection.globalPosition() +glm::vecRand3(0.0f, Light.inaccuracy());if(!shadow(Intersection.globalPosition(),Light.position(),LightVertor)){float Diffuse = glm::dot(Intersection.normal(), LightVector);if(Diffuse <= 0.0f)return Color;if(Material.isDiffuse())Color += Light.color() * Material.diffuse() * Diffuse;if(Material.isSpecular()){glm::vec3 Reflect = glm::reflect(-LightVector,Intersection.normal());float Dot = glm::dot(Reflect, View);float Base = Dot > 0.0f ? Dot : 0.0f;float Specular = glm::pow(Base, Material.exponent());Color += Material.specular() * Specular;}}return Color;}8.
References8.1. GLM development- GLM website- GLM HEAD snapshot- GLM bug report and feature request- G-Truc Creation’s page8.2. OpenGL specifications- OpenGL 4.3 core specification- GLSL 4.30 specification- GLU 1.3 specification8.3. External links- The OpenGL Toolkits forum to ask questions about GLM8.4. Projects using GLM- Outerra:3D planetary engine for seamless planet rendering from space down to the surface.Can use arbitrary resolution of elevation data, refining it to centimeter resolutionusing fractal algorithms.- opencloth:A collection of source codes implementing cloth simulation algorithms in OpenGL.- OpenGL 4.0 Shading Language Cookbook:A full set of recipes demonstrating simpleand advanced techniques for producinghigh-quality, real-time 3D graphics usingGLSL 4.0.How to use the OpenGL Shading Languageto implement lighting and shadingtechniques.Use the new features of GLSL 4.0including tessellation and geometryshaders.How to use textures in GLSL as part of awide variety of techniques from basictexture mapping to deferred shading.Simple, easy-to-follow examples withGLSL source code, as well as a basicdescription of the theory behind eachtechnique.- Are you using GLM in a project?8.5.
OpenGL tutorials using GLM- The OpenGL Samples Pack, samples that show how to set up all the different newfeatures- Learning Modern 3D Graphics Programming, a great OpenGL tutorial using GLM byJason L. McKesson- Morten Nobel-Jørgensen’s review and use an OpenGL renderer- Swiftless’ OpenGL tutorial using GLM by Donald Urquhart- Rastergrid, many technical articles with companion programs using GLM by DanielRákos- OpenGL Tutorial, tutorials for OpenGL 3.1 and later- OpenGL Programming on Wikibooks: For beginners who are discovering OpenGL.- 3D Game Engine Programming: Learning the latest 3D Game Engine Programmingtechniques.- Are you using GLM in a tutorial?8.6. Alternatives to GLM- CML: The CML (Configurable Math Library) is a free C++ math library for games andgraphics.- Eigen: A more heavy weight math library for general linear algebra in C++.- glhlib: A much more than glu C library.- Are you using or working an alternative library to GLM?8.7.
AcknowledgementsGLM is developed and maintained by Christophe Riccio but many contributors havemade this project what it is.Special thanks to:- Ashima Arts and Stefan Gustavson for their work on webgl-noise which has beenused for GLM noises implementation.- Arthur Winters for the C++11 and Visual C++ swizzle operators implementation andtests.- Joshua Smith and Christoph Schied for the discussions and the experiments aroundthe swizzle operator implementation issues.- Guillaume Chevallereau for providing and maintaining the nightlight build system.- Ghenadii Ursachi for GLM_GTX_matrix_interpolation implementation.- Mathieu Roumillac for providing some implementation ideas.- Grant James for the implementation of all combination of none-squared matrixproducts.- All the GLM users that have report bugs and hence help GLM to become a greatlibrary!8.8.
Quotes from the web“I am also going to make use of boost for its time framework and the matrix libraryGLM, a GL Shader-like Math library for C++. A little advertise for the latter which hasa nice design and is useful since matrices have been removed from the latestOpenGL versions”Code Xperiments“OpenGL Mathmatics Library (GLM): Used for vector classes, quaternion classes,matrix classes and math operations that are suited for OpenGL applications.”Jeremiah van Oosten“Today I ported my codebase from my own small linear algebra library to GLM, aGLSL-style vector library for C++. The transition went smoothly.”Leonard Ritter“A more clever approach is to use a math library like GLM instead.
I wish someonehad showed me this library a few years ago.”Morten Nobel-Jørgensen.















