How to do it...

To create a 2D noise texture with GLM, perform the following steps:

  1. Include the GLM header that includes the noise functions:
#include <glm/gtc/noise.hpp> 
  1. Generate the noise data using the previous equation:
GLubyte *data = new GLubyte[ width * height * 4 ]; float xFactor = 1.0f / (width - 1); float yFactor = 1.0f / (height - 1); for( int row = 0; row < height; row++ ) { for( int col = 0 ; col < width; col++ ) { float x = xFactor * col; float y = yFactor * row; float sum = 0.0f; float freq = a; float scale = b; // Compute the sum for each octave for( int oct = 0; oct < 4; oct++ ) { glm::vec2 p(x * freq, y * freq); float val = glm::perlin(p) / scale; sum += val; float result = (sum + 1.0f)/ 2.0f; // Store in texture buffer ...

Get OpenGL 4 Shading Language Cookbook - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.