glm (1125161), страница 2
Текст из файла (страница 2)
A safer way to do swizzling is to use the extension GLM_GTC_swizzle. In term of features, this extension is at the same level than GLSL expect that GLM support both static and dynamic swizzling where GLSL only support static swizzling.
Static swizzling is an operation which is resolved at build time but dynamic swizzling is revolved at runtime which is more flexible but slower especially when SSE optimisations are used.
#include <glm/glm.hpp>
#include <glm/gtc/swizzle.hpp>
void foo()
{
glm::vec4 ColorRGBA(1.0f, 0.5f, 0.0f, 1.0f);
…
// Dynamic swizzling (at run time, more flexible)
// l-value:
glm::vec4 ColorBGRA1 =
glm::swizzle(ColorRGBA, glm::B, glm::G, glm::R, glm::A);
// r-value:
glm::swizzle(ColorRGBA, glm::B, glm::G, glm::R, glm::A) = ColorRGBA;
// Static swizzling (at build time, faster)
// l-value:
glm::vec4 ColorBGRA2 =
glm::swizzle<glm::B, glm::G, glm::R, glm::A>(ColorRGBA);
// r-value:
glm::swizzle<glm::B, glm::G, glm::R, glm::A>(ColorRGBA) = ColorRGBA;
}
2.2. Notification system
GLM includes a notification system which can display some information at build time:
- Compiler
- Build model: 32bits or 64 bits
- C++ version
- Architecture: x86, SSE, AVX, etc.
- Included extensions
- etc.
This system is disabled by default. To enable this system, define GLM_MESSAGES before any inclusion of <glm/glm.hpp>.
#define GLM_MESSAGES
#include <glm/glm.hpp>
2.3. Force inline
To push further the software performance, a programmer can define GLM_FORCE_INLINE before any inclusion of <glm/glm.hpp> to force the compiler to inline GLM code.
#define GLM_FORCE_INLINE
#include <glm/glm.hpp>
2.4. SIMD support
GLM provides some SIMD optimization based on compiler intrinsics. These optimizations will be automatically utilized based on the build environment. These optimizations are mainly available through extensions, GLM_GTX_simd_vec4 and GLM_GTX_simd_mat4.
A programmer can restrict or force instruction sets used for these optimizations using GLM_FORCE_SSE2 or GLM_FORCE_AVX.
A programmer can discard the use of intrinsics by defining GLM_FORCE_PURE before any inclusion of <glm/glm.hpp>. If GLM_FORCE_PURE is defined, then including a SIMD extension will generate a build error.
#define GLM_FORCE_PURE
#include <glm/glm.hpp>
2.5. Compatibility
Compilers have some language extensions that GLM will automatically take advantage of them when they are enabled. To increase cross platform compatibility and to avoid compiler extensions, a programmer can define GLM_FORCE_CXX98 before any inclusion of <glm/glm.hpp>.
#define GLM_FORCE_CXX98
#include <glm/glm.hpp>
For C++11, an equivalent value is available: GLM_FORCE_CXX11.
#define GLM_FORCE_CXX11
#include <glm/glm.hpp>
If both GLM_FORCE_CXX98 and GLM_FORCE_CXX11 are defined then C++ 11 features will be utilized.
2.6. Default precision
With C++ it isn’t possible to implement GLSL default precision (GLSL 4.10 specification section 4.5.3) the way it is specified in GLSL. Hence, instead of writing:
precision mediump int;
precision highp float;
With GLM we need to add before any include of glm.hpp:
#define GLM_PRECISION_MEDIUMP_INT;
#define GLM_PRECISION_HIGHP_FLOAT;
#include <glm/glm.hpp>
2.7. Force use of radians
OpenGL API legacy functions used to take angle values expressed in degrees, a precedent that GLM is following. However, GLSL functions are typically radians based. GLM allows switching all the GLM functions to use radians by declaring GLM_FORCE_RADIANS.
// Using radians
#define GLM_FORCE_RADIANS
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <glm/gtc/matrix_transform.hpp>
glm::mat4 m(1.0f);
glm::mat4 r(glm::rotate(m, pi * 0.5f, glm::vec3(0, 0, 1)));
// Using degrees
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
glm::mat4 m(1.0f);
glm::mat4 r(glm::rotate(m, 90.f, glm::vec3(0, 0, 1)));
3. Deprecated function replacements
OpenGL 3.0 specification has deprecated some features that have been removed from OpenGL 3.2 core profile specification. GLM provides some replacement functions.
3.1. OpenGL functions (Section 2.11.2 Matrices, OpenGL 2.1 specification)
glRotate{f, d}:
glm::mat4 glm::rotate(
glm::mat4 const & m,
float angle,
glm::vec3 const & axis);
glm::dmat4 glm::rotate(
glm::dmat4 const & m,
double angle,
glm::dvec3 const & axis);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
glScale{f, d}:
glm::mat4 glm::scale(
glm::mat4 const & m,
glm::vec3 const & factors);
glm::dmat4 glm::scale(
glm::dmat4 const & m,
glm::dvec3 const & factors);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
glTranslate{f, d}:
glm::mat4 glm::translate(
glm::mat4 const & m,
glm::vec3 const & translation);
glm::dmat4 glm::translate(
glm::dmat4 const & m,
glm::dvec3 const & translation);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
glLoadIdentity:
glm::mat4(1.0) or glm::mat4();
glm::dmat4(1.0) or glm::dmat4();
From GLM core library: <glm/glm.hpp>
glMultMatrix{f, d}:
glm::mat4() * glm::mat4();
glm::dmat4() * glm::dmat4();
From GLM core library: <glm/glm.hpp>
glLoadTransposeMatrix{f, d}:
glm::transpose(glm::mat4());
glm::transpose(glm::dmat4());
From GLM core library: <glm/glm.hpp>
glMultTransposeMatrix{f, d}:
glm::mat4() * glm::transpose(glm::mat4());
glm::dmat4() * glm::transpose(glm::dmat4());
From GLM core library: <glm/glm.hpp>
glFrustum:
glm::mat4 glm::frustum(
float left, float right,
float bottom, float top,
float zNear, float zFar);
glm::dmat4 glm::frustum(
double left, double right,
double bottom, double top,
double zNear, double zFar);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
glOrtho:
glm::mat4 glm::ortho(
float left, float right,
float bottom, float top,
float zNear, float zFar);
glm::dmat4 glm::ortho(
double left, double right,
double bottom, double top,
double zNear, double zFar);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
3.2. GLU functions (Chapter 4: Matrix Manipulation, GLU 1.3 specification)
gluLookAt:
glm::mat4 glm::lookAt(
glm::vec3 const & eye,
glm::vec3 const & center,
glm::vec3 const & up);
glm::dmat4 glm::lookAt(
glm::dvec3 const & eye,
glm::dvec3 const & center,
glm::dvec3 const & up);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
gluOrtho2D:
glm::mat4 glm::ortho(
float left, float right, float bottom, float top);
glm::dmat4 glm::ortho(
double left, double right, double bottom, double top);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
gluPerspective:
glm::mat4 perspective(
float fovy, float aspect, float zNear, float zFar);
glm::dmat4 perspective(
double fovy, double aspect, double zNear, double zFar);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
gluPickMatrix:
glm::mat4 pickMatrix(
glm::vec2 const & center,
glm::vec2 const & delta,
glm::ivec4 const & viewport);
glm::dmat4 pickMatrix(
glm::dvec2 const & center,
glm::dvec2 const & delta,
glm::ivec4 const & viewport);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
gluProject:
glm::vec3 project(
glm::vec3 const & obj,
glm::mat4 const & model,
glm::mat4 const & proj,
glm::{i, ' '}vec4 const & viewport);
glm::dvec3 project(
glm::dvec3 const & obj,
glm::dmat4 const & model,
glm::dmat4 const & proj,
glm::{i, ' ', d}vec4 const & viewport);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
gluUnProject:
glm::vec3 unProject(
glm::vec3 const & win,
glm::mat4 const & model,
glm::mat4 const & proj,
glm::{i, ' '}vec4 const & viewport);
glm::dvec3 unProject(
glm::dvec3 const & win,
glm::dmat4 const & model,
glm::dmat4 const & proj,
glm::{i, ' ', d}vec4 const & viewport);
From GLM_GTC_matrix_transform extension: <glm/gtc/matrix_transform.hpp>
4. Extensions
4.1. GLM_GTC_constants
This extension provides a list of built-in constants.
<glm/gtc/constants.hpp> need to be included to use these features.
4.2. GLM_GTC_epsilon
Approximated equal and not equal comparisons with selectable epsilon.
<glm/gtc/epsilon.hpp> need to be included to use these features.
4.3. GLM_GTC_half_float
This extension provides half-precision floating-point types support for scalar (half), vectors (hvec*) and matrices (hmat*x*) for every GLM core functions that takes floating-point parameter.
Half float should be essentially used for storage as it isn’t a native format and even if all the scalar operations are supported, there are implemented by converting half to float and then make this conversion back to half.
<glm/gtc/half_float.hpp> need to be included to use these features.
4.4. GLM_GTC_matrix_access
Defines functions to access rows or columns of a matrix easily.
<glm/gtc/matrix_access.hpp> need to be included to use these features.
4.5. GLM_GTC_matrix_integer
Provides integer matrix types. Inverse and determinant functions are not supported for these types.
<glm/gtc/matrix_integer.hpp> need to be included to use these features.
4.6. GLM_GTC_matrix_inverse
Defines additional matrix inverting functions.
<glm/gtc/matrix_inverse.hpp> need to be included to use these features.
4.7. GLM_GTC_matrix_transform
Defines functions that generate common transformation matrices.
The matrices generated by this extension use standard OpenGL fixed-function conventions. For example, the lookAt function generates a transform from world space into the specific eye space that the projective matrix functions (perspective, ortho, etc) are designed to expect. The OpenGL compatibility specifications define the particular layout of this eye space.
<glm/gtc/matrix_transform.hpp> need to be included to use these features.
4.8. GLM_GTC_noise
Defines 2D, 3D and 4D procedural noise functions.
<glm/gtc/noise.hpp> need to be included to use these features.
Figure 4.8.1: glm::simplex(glm::vec2(x / 16.f, y / 16.f));
Figure 4.8.2: glm::simplex(glm::vec3(x / 16.f, y / 16.f, 0.5f));
Figure 4.8.3: glm::simplex(glm::vec4(x / 16.f, y / 16.f, 0.5f, 0.5f));
Figure 4.8.4: glm::perlin(glm::vec2(x / 16.f, y / 16.f));
Figure 4.8.5: glm::perlin(glm::vec3(x / 16.f, y / 16.f, 0.5f));
Figure 4.8.6: glm::perlin(glm::vec4(x / 16.f, y / 16.f, 0.5f, 0.5f)));
Figure 4.8.7: glm::perlin(glm::vec2(x / 16.f, y / 16.f), glm::vec2(2.0f));
Figure 4.8.8: glm::perlin(glm::vec3(x / 16.f, y / 16.f, 0.5f), glm::vec3(2.0f));














