Vuo  0.5.7
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Classes | Macros | Typedefs | Functions

Description

A graphics shader program, specifying how to render a 3D object.

Classes

struct  _VuoShader
 A graphics shader program, specifying how to render a 3D object. More...
 

Macros

#define VUOSHADER_GLSL_SOURCE(version, source)   "#version " #version "\n" #source
 A macro to facilitate defining a GLSL shader in a C source file.
 
#define VUOSHADER_GLSL_FRAGMENT_COLOR_CONVERSION_SOURCE   " \ vec3 RgbToHsl(vec3 color) \ { \ vec3 hsl; \ \ float fmin = min(min(color.r, color.g), color.b); \ float fmax = max(max(color.r, color.g), color.b); \ float delta = fmax - fmin; \ \ hsl.z = (fmax + fmin) / 2.0; \ \ if (delta == 0.0) \ { \ hsl.x = 0.0; \ hsl.y = 0.0; \ } \ else \ { \ if (hsl.z < 0.5) \ hsl.y = delta / (fmax + fmin); \ else \ hsl.y = delta / (2.0 - fmax - fmin); \ \ float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta; \ float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta; \ float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta; \ \ if (color.r == fmax ) \ hsl.x = deltaB - deltaG; \ else if (color.g == fmax) \ hsl.x = (1.0 / 3.0) + deltaR - deltaB; \ else if (color.b == fmax) \ hsl.x = (2.0 / 3.0) + deltaG - deltaR; \ \ if (hsl.x < 0.0) \ hsl.x += 1.0; \ else if (hsl.x > 1.0) \ hsl.x -= 1.0; \ } \ \ return hsl; \ } \ \ float HueToRGB(float f1, float f2, float hue) \ { \ if (hue < 0.0) \ hue += 1.0; \ else if (hue > 1.0) \ hue -= 1.0; \ float res; \ if ((6.0 * hue) < 1.0) \ res = f1 + (f2 - f1) * 6.0 * hue; \ else if ((2.0 * hue) < 1.0) \ res = f2; \ else if ((3.0 * hue) < 2.0) \ res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; \ else \ res = f1; \ return res; \ } \ \ vec3 HslToRgb(vec3 hsl) \ { \ vec3 rgb; \ \ if (hsl.y == 0.0) \ rgb = vec3(hsl.z); \ else \ { \ float f2; \ \ if (hsl.z < 0.5) \ f2 = hsl.z * (1.0 + hsl.y); \ else \ f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); \ \ float f1 = 2.0 * hsl.z - f2; \ \ rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); \ rgb.g = HueToRGB(f1, f2, hsl.x); \ rgb.b = HueToRGB(f1, f2, hsl.x - (1.0/3.0)); \ } \ \ return rgb; \ } \"
 Provides a GLSL method which converts rgb to hsl.
 
#define VUOSHADER_GLSL_FRAGMENT_SOURCE_WITH_COLOR_CONVERSIONS(source)   "#version 120\n" VUOSHADER_GLSL_FRAGMENT_COLOR_CONVERSION_SOURCE "\n" #source
 Provides the template for a glsl shader with hsl to rgb conversion methods defined (RgbToHsl(vec3 rgb), HslToRgb(vec3 hs))
 

Typedefs

typedef struct _VuoShaderVuoShader
 A graphics shader program, specifying how to render a 3D object.
 

Functions

void VuoShader_free (void *shader)
 Decrements the retain count of the OpenGL Texture Object associated with the specified VuoImage, and frees the texture VuoImage struct.
 
void VuoShader_printProgramInfoLog (CGLContextObj cgl_ctx, GLuint obj)
 Prints GLSL debug information to the console.
 
VuoShader VuoShader_make (const char *summary, const char *vertexShaderSource, const char *fragmentShaderSource)
 Compiles, links, and uploads the specified shader sources.
 
VuoShader VuoShader_valueFromJson (json_object *js)
 Decodes the JSON object js, expected to contain a 64-bit integer (memory address or 0), to create a new VuoShader.
 
json_object * VuoShader_jsonFromValue (const VuoShader value)
 Encodes value as a JSON object.
 
char * VuoShader_summaryFromValue (const VuoShader value)
 Returns a summary of the shader: the text description provided to VuoShader_make(), and the number of textures associated with the shader.
 
void VuoShader_setUniformFloat (VuoShader shader, VuoGlContext glContext, const char *uniformIdentifier, float value)
 Sets a float uniform value on the specified shader.
 
void VuoShader_setUniformPoint2d (VuoShader shader, VuoGlContext glContext, const char *uniformIdentifier, VuoPoint2d value)
 Sets a vec2 uniform value on the specified shader.
 
void VuoShader_setUniformPoint3d (VuoShader shader, VuoGlContext glContext, const char *uniformIdentifier, VuoPoint3d value)
 Sets a vec3 uniform value on the specified shader.
 
void VuoShader_setUniformPoint4d (VuoShader shader, VuoGlContext glContext, const char *uniformIdentifier, VuoPoint4d value)
 Sets a vec4 uniform value on the specified shader.
 
void VuoShader_setUniformFloatArray (VuoShader shader, VuoGlContext glContext, const char *uniformIdentifier, const float *value, int length)
 Sets a float[] uniform value on the specified shader.
 
const char * VuoShader_getDefaultVertexShader (void)
 Returns the default vertex shader, which projects verties and passes through texture coordinates.
 
VuoPoint2d VuoShader_samplerCoordinatesFromVuoCoordinates (VuoPoint2d vuoCoordinates, VuoImage image)
 Converts the provided vuoCoordinates into GLSL Sampler Coordinates relative to the provided image.
 
VuoReal VuoShader_samplerSizeFromVuoSize (VuoReal vuoSize)
 Converts an x-axis distance in Vuo Coordinates into GLSL Sampler Coordinates.
 
VuoShader VuoShader_makeImageShader (void)
 Returns a shader that renders objects with an image (ignoring lighting), specified by uniform texture.
 
void VuoShader_resetTextures (VuoShader shader)
 Empties the list of textures associated with shader.
 
void VuoShader_addTexture (VuoShader shader, VuoGlContext glContext, const char *uniformIdentifier, VuoImage texture)
 Adds to shader an association between texture and uniformIdentifier.
 
void VuoShader_activateTextures (VuoShader shader, VuoGlContext glContext)
 Assigns each of the shader's textures to a texture unit, and passes the texture unit number along to the shader.
 
void VuoShader_deactivateTextures (VuoShader shader, VuoGlContext glContext)
 Unbinds the texture units used by this shader.
 
VuoShader VuoShader_makeColorShader (VuoColor color)
 Returns a shader that renders a solid color.
 
VuoShader VuoShader_makeLitColorShader (VuoColor diffuseColor, VuoColor highlightColor, VuoReal shininess)
 Returns a shader that renders a color with lighting.
 
VuoShader VuoShader_makeLitImageShader (VuoImage image, VuoReal alpha, VuoColor highlightColor, VuoReal shininess)
 Returns a shader that renders an image with lighting.
 
VuoShader VuoShader_makeLitImageDetailsShader (VuoImage image, VuoReal alpha, VuoImage specularImage, VuoImage normalImage)
 Returns a shader that renders an image with lighting and surface details.
 
VuoShader VuoShader_makeLinearGradientShader (VuoList_VuoColor colors, VuoPoint2d start, VuoPoint2d end)
 Returns a shader that renders a linear gradient using the provided colors and start and end coordinates.
 
VuoShader VuoShader_makeRadialGradientShader (VuoList_VuoColor colors, VuoPoint2d center, VuoReal radius, VuoReal width, VuoReal height)
 Returns a shader that renders a radial gradient using the provided colors, center point, and radius.
 
VuoShader VuoShader_valueFromString (const char *str)
 Automatically generated function.
 
char * VuoShader_stringFromValue (const VuoShader value)
 Automatically generated function.
 

Class Documentation

struct _VuoShader
Class Members
unsigned int glFragmentShaderName
unsigned int glProgramName
VuoList_VuoInteger glTextureUniformLocations
unsigned int glVertexShaderName
dispatch_semaphore_t lock Serializes operations that modify the state of this GL program object.
char * summary Text describing the shader, displayed in port popovers.
VuoList_VuoImage textures

Macro Definition Documentation

#define VUOSHADER_GLSL_FRAGMENT_COLOR_CONVERSION_SOURCE   " \ vec3 RgbToHsl(vec3 color) \ { \ vec3 hsl; \ \ float fmin = min(min(color.r, color.g), color.b); \ float fmax = max(max(color.r, color.g), color.b); \ float delta = fmax - fmin; \ \ hsl.z = (fmax + fmin) / 2.0; \ \ if (delta == 0.0) \ { \ hsl.x = 0.0; \ hsl.y = 0.0; \ } \ else \ { \ if (hsl.z < 0.5) \ hsl.y = delta / (fmax + fmin); \ else \ hsl.y = delta / (2.0 - fmax - fmin); \ \ float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta; \ float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta; \ float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta; \ \ if (color.r == fmax ) \ hsl.x = deltaB - deltaG; \ else if (color.g == fmax) \ hsl.x = (1.0 / 3.0) + deltaR - deltaB; \ else if (color.b == fmax) \ hsl.x = (2.0 / 3.0) + deltaG - deltaR; \ \ if (hsl.x < 0.0) \ hsl.x += 1.0; \ else if (hsl.x > 1.0) \ hsl.x -= 1.0; \ } \ \ return hsl; \ } \ \ float HueToRGB(float f1, float f2, float hue) \ { \ if (hue < 0.0) \ hue += 1.0; \ else if (hue > 1.0) \ hue -= 1.0; \ float res; \ if ((6.0 * hue) < 1.0) \ res = f1 + (f2 - f1) * 6.0 * hue; \ else if ((2.0 * hue) < 1.0) \ res = f2; \ else if ((3.0 * hue) < 2.0) \ res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0; \ else \ res = f1; \ return res; \ } \ \ vec3 HslToRgb(vec3 hsl) \ { \ vec3 rgb; \ \ if (hsl.y == 0.0) \ rgb = vec3(hsl.z); \ else \ { \ float f2; \ \ if (hsl.z < 0.5) \ f2 = hsl.z * (1.0 + hsl.y); \ else \ f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z); \ \ float f1 = 2.0 * hsl.z - f2; \ \ rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0)); \ rgb.g = HueToRGB(f1, f2, hsl.x); \ rgb.b = HueToRGB(f1, f2, hsl.x - (1.0/3.0)); \ } \ \ return rgb; \ } \"

Provides a GLSL method which converts rgb to hsl.

Accepts a vec3 and returns a vec3 (hsl).

#define VUOSHADER_GLSL_FRAGMENT_SOURCE_WITH_COLOR_CONVERSIONS (   source)    "#version 120\n" VUOSHADER_GLSL_FRAGMENT_COLOR_CONVERSION_SOURCE "\n" #source

Provides the template for a glsl shader with hsl to rgb conversion methods defined (RgbToHsl(vec3 rgb), HslToRgb(vec3 hs))

#define VUOSHADER_GLSL_SOURCE (   version,
  source 
)    "#version " #version "\n" #source

A macro to facilitate defining a GLSL shader in a C source file.

Typedef Documentation

typedef struct _VuoShader * VuoShader

A graphics shader program, specifying how to render a 3D object.

The struct is typedef'd to a pointer so that VuoShaders are reference-counted, enabling us to automatically delete the GL Program Objects when the last reference is released.

Function Documentation

void VuoShader_activateTextures ( VuoShader  shader,
VuoGlContext  glContext 
)

Assigns each of the shader's textures to a texture unit, and passes the texture unit number along to the shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_addTexture ( VuoShader  shader,
VuoGlContext  glContext,
const char *  uniformIdentifier,
VuoImage  texture 
)

Adds to shader an association between texture and uniformIdentifier.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

Todo:
instead of querying this every time an image is added, query once when the shader is compiled, and store it in a dictionary in VuoShader.
void VuoShader_deactivateTextures ( VuoShader  shader,
VuoGlContext  glContext 
)

Unbinds the texture units used by this shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_free ( void *  shader)

Decrements the retain count of the OpenGL Texture Object associated with the specified VuoImage, and frees the texture VuoImage struct.

This function may be called from any thread.

const char* VuoShader_getDefaultVertexShader ( void  )

Returns the default vertex shader, which projects verties and passes through texture coordinates.

This function may be called from any thread.

struct json_object * VuoShader_jsonFromValue ( const VuoShader  value)
read

Encodes value as a JSON object.

Serializes the pointer to the VuoShader object, since we need to preserve its reference count.

This function may be called from any thread.

VuoShader VuoShader_make ( const char *  summary,
const char *  vertexShaderSource,
const char *  fragmentShaderSource 
)

Compiles, links, and uploads the specified shader sources.

The vertex shader must define:

uniform mat4 projectionMatrix;
uniform mat4 modelviewMatrix;
attribute vec4 position;
attribute vec4 normal;
attribute vec4 tangent;
attribute vec4 bitangent;
attribute vec4 textureCoordinate;

This function may be called from any thread.

VuoShader VuoShader_makeColorShader ( VuoColor  color)

Returns a shader that renders a solid color.

VuoShader VuoShader_makeImageShader ( void  )

Returns a shader that renders objects with an image (ignoring lighting), specified by uniform texture.

This function may be called from any thread.

VuoShader VuoShader_makeLinearGradientShader ( VuoList_VuoColor  colors,
VuoPoint2d  start,
VuoPoint2d  end 
)

Returns a shader that renders a linear gradient using the provided colors and start and end coordinates.

Coordinates should be passed in Vuo scene coordinates (-1,-1) to (1,1).

VuoShader VuoShader_makeLitColorShader ( VuoColor  diffuseColor,
VuoColor  highlightColor,
VuoReal  shininess 
)

Returns a shader that renders a color with lighting.

Parameters
diffuseColorThe primary material color.
highlightColorThe color of shiny specular highlights. Alpha controls the intensity of the highlights.
shininessA number representing how shiny the material is. 0 = dull; 1 = shiny; numbers in between represent varying amounts of shininess.
VuoShader VuoShader_makeLitImageDetailsShader ( VuoImage  image,
VuoReal  alpha,
VuoImage  specularImage,
VuoImage  normalImage 
)

Returns a shader that renders an image with lighting and surface details.

Parameters
imageThe image which provides the diffuse / primary material color.
alphaThe opacity of the image (0 to 1).
specularImageAn image that specifies the specular color (RGB) and shininess (A).
normalImageAn image that specifies the surface details. The red and green channels respectively define the normal direction along the tangent and bitangent axes (0 = negative; 0.5 = straight; 1 = positive). The blue channel defines the height along the normal axis (0 = low; 1 = high).
VuoShader VuoShader_makeLitImageShader ( VuoImage  image,
VuoReal  alpha,
VuoColor  highlightColor,
VuoReal  shininess 
)

Returns a shader that renders an image with lighting.

Parameters
imageThe image which provides the diffuse / primary material color.
alphaThe opacity of the image (0 to 1).
highlightColorThe color of shiny specular highlights. Alpha controls the intensity of the highlights.
shininessA number representing how shiny the material is. 0 = dull; 1 = shiny; numbers in between represent varying amounts of shininess.
VuoShader VuoShader_makeRadialGradientShader ( VuoList_VuoColor  colors,
VuoPoint2d  center,
VuoReal  radius,
VuoReal  width,
VuoReal  height 
)

Returns a shader that renders a radial gradient using the provided colors, center point, and radius.

Center and radius are expected in Vuo scene coordinates. Width and Height may be either pixels or scene coordinates, as they are only used to calculate the aspect ratio.

void VuoShader_printProgramInfoLog ( CGLContextObj  cgl_ctx,
GLuint  obj 
)

Prints GLSL debug information to the console.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_resetTextures ( VuoShader  shader)

Empties the list of textures associated with shader.

This function may be called from any thread.

VuoPoint2d VuoShader_samplerCoordinatesFromVuoCoordinates ( VuoPoint2d  vuoCoordinates,
VuoImage  image 
)

Converts the provided vuoCoordinates into GLSL Sampler Coordinates relative to the provided image.

Vuo Coordinates range from (-1,-1/aspectRatio) in the bottom left to (1,1/aspectRatio) in the top right.

GLSL Sampler Coordinates range from (0,0) to (1,1).

If image is rendered in a scene, centered at (0,0,0), width 2, at its correct aspect ratio, this function will transform 2D coordinates along the XY plane (at Z=0) into correct sampler coordinates for that image.

This function may be called from any thread.

VuoReal VuoShader_samplerSizeFromVuoSize ( VuoReal  vuoSize)

Converts an x-axis distance in Vuo Coordinates into GLSL Sampler Coordinates.

(Divides by 2.)

This function may be called from any thread.

void VuoShader_setUniformFloat ( VuoShader  shader,
VuoGlContext  glContext,
const char *  uniformIdentifier,
float  value 
)

Sets a float uniform value on the specified shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_setUniformFloatArray ( VuoShader  shader,
VuoGlContext  glContext,
const char *  uniformIdentifier,
const float *  value,
int  length 
)

Sets a float[] uniform value on the specified shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_setUniformPoint2d ( VuoShader  shader,
VuoGlContext  glContext,
const char *  uniformIdentifier,
VuoPoint2d  value 
)

Sets a vec2 uniform value on the specified shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_setUniformPoint3d ( VuoShader  shader,
VuoGlContext  glContext,
const char *  uniformIdentifier,
VuoPoint3d  value 
)

Sets a vec3 uniform value on the specified shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

void VuoShader_setUniformPoint4d ( VuoShader  shader,
VuoGlContext  glContext,
const char *  uniformIdentifier,
VuoPoint4d  value 
)

Sets a vec4 uniform value on the specified shader.

This function may be called from any thread. (However, the caller is responsible for ensuring that the GL context is not used simultaneously on multiple threads.)

char* VuoShader_stringFromValue ( const VuoShader  value)

Automatically generated function.

char * VuoShader_summaryFromValue ( const VuoShader  value)

Returns a summary of the shader: the text description provided to VuoShader_make(), and the number of textures associated with the shader.

This function may be called from any thread.

VuoShader VuoShader_valueFromJson ( json_object *  js)

Decodes the JSON object js, expected to contain a 64-bit integer (memory address or 0), to create a new VuoShader.

This function may be called from any thread.

VuoShader VuoShader_valueFromString ( const char *  str)

Automatically generated function.