Vuo  2.0.0
VuoImageBlend.c
Go to the documentation of this file.
1 
10 #include "node.h"
11 #include "VuoImageBlend.h"
12 #include "VuoImageRenderer.h"
13 
15 #ifdef VUO_COMPILER
17  "title" : "VuoImageBlend",
18  "dependencies" : [
19  "VuoImageRenderer"
20  ]
21  });
22 #endif
23 
28 typedef struct
29 {
30  VuoShader shader;
32 
36 void VuoImageBlend_free(void *blend)
37 {
39  VuoRelease(bi->shader);
40 }
41 
46 {
47  const char *shaderSource = VUOSHADER_GLSL_SOURCE(120,
48  varying vec2 fragmentTextureCoordinate;
49  uniform sampler2D textureA;
50  uniform sampler2D textureB;
51  uniform float factor;
52 
53  void main(void)
54  {
55  vec4 colorA = texture2D(textureA, fragmentTextureCoordinate);
56  vec4 colorB = texture2D(textureB, fragmentTextureCoordinate);
57  vec4 mixed = mix(colorA, colorB, factor);
58  gl_FragColor = mixed;
59  }
60  );
61 
64 
65  bi->shader = VuoShader_make("Blend Image Shader");
66  VuoRetain(bi->shader);
67  VuoShader_addSource(bi->shader, VuoMesh_IndividualTriangles, NULL, NULL, shaderSource);
68 
69  return (VuoImageBlend)bi;
70 }
71 
80 {
81  unsigned long imageCount = VuoListGetCount_VuoImage(images);
82  if (imageCount == 0)
83  return NULL;
84 
85  VuoImage blendedImage = VuoListGetValue_VuoImage(images, 1);
86  if (imageCount == 1)
87  {
88  VuoRetain(blendedImage);
89  return blendedImage;
90  }
91 
92  VuoImageColorDepth cd = VuoImage_getColorDepth(blendedImage);
93 
95 
97  VuoRetain(blendedImage);
98  double factor = 1.;
99  for (unsigned long i = 2; i <= imageCount; ++i)
100  {
101  VuoImage image = VuoListGetValue_VuoImage(images, i);
102 
103  VuoShader_setUniform_VuoImage(bi->shader, "textureA", blendedImage);
104  VuoShader_setUniform_VuoImage(bi->shader, "textureB", image);
105 
106  factor *= ((double)i - 1.) / i;
107  VuoShader_setUniform_VuoReal (bi->shader, "factor", factor);
108 
109  VuoImage b = VuoImageRenderer_render(bi->shader, blendedImage->pixelsWide, blendedImage->pixelsHigh, cd);
110  VuoRetain(b);
111  VuoRelease(blendedImage);
112  blendedImage = b;
113  }
114 
115  return blendedImage;
116 }