~ubuntu-branches/ubuntu/saucy/blender/saucy-proposed

« back to all changes in this revision

Viewing changes to source/blender/nodes/composite/nodes/node_composite_displace.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 
36
36
/* **************** Displace  ******************** */
37
37
 
38
 
static bNodeSocketTemplate cmp_node_displace_in[]= {
39
 
        {       SOCK_RGBA, 1, "Image",                  1.0f, 1.0f, 1.0f, 1.0f},
40
 
        {       SOCK_VECTOR, 1, "Vector",                       1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_TRANSLATION},
41
 
        {       SOCK_FLOAT, 1, "X Scale",                               0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f, PROP_FACTOR},
42
 
        {       SOCK_FLOAT, 1, "Y Scale",                               0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f, PROP_FACTOR},
43
 
        {       -1, 0, ""       }
44
 
};
45
 
static bNodeSocketTemplate cmp_node_displace_out[]= {
46
 
        {       SOCK_RGBA, 0, "Image"},
47
 
        {       -1, 0, ""       }
48
 
};
49
 
 
50
 
/* minimum distance (in pixels) a pixel has to be displaced
51
 
 * in order to take effect */
52
 
#define DISPLACE_EPSILON        0.01f
53
 
 
54
 
static void do_displace(bNode *node, CompBuf *stackbuf, CompBuf *cbuf, CompBuf *vecbuf, float *UNUSED(veccol), CompBuf *xbuf,  CompBuf *ybuf, float *xscale, float *yscale)
55
 
{
56
 
        ImBuf *ibuf;
57
 
        int x, y;
58
 
        float p_dx, p_dy;       /* main displacement in pixel space */
59
 
        float d_dx, d_dy;
60
 
        float dxt, dyt;
61
 
        float u, v;
62
 
        float xs, ys;
63
 
        float vec[3], vecdx[3], vecdy[3];
64
 
        float col[3];
65
 
        
66
 
        ibuf= IMB_allocImBuf(cbuf->x, cbuf->y, 32, 0);
67
 
        ibuf->rect_float= cbuf->rect;
68
 
        
69
 
        for (y=0; y < stackbuf->y; y++) {
70
 
                for (x=0; x < stackbuf->x; x++) {
71
 
                        /* calc pixel coordinates */
72
 
                        qd_getPixel(vecbuf, x-vecbuf->xof, y-vecbuf->yof, vec);
73
 
                        
74
 
                        if (xbuf)
75
 
                                qd_getPixel(xbuf, x-xbuf->xof, y-xbuf->yof, &xs);
76
 
                        else
77
 
                                xs = xscale[0];
78
 
                        
79
 
                        if (ybuf)
80
 
                                qd_getPixel(ybuf, x-ybuf->xof, y-ybuf->yof, &ys);
81
 
                        else
82
 
                                ys = yscale[0];
83
 
 
84
 
                        /* clamp x and y displacement to triple image resolution - 
85
 
                         * to prevent hangs from huge values mistakenly plugged in eg. z buffers */
86
 
                        CLAMP(xs, -stackbuf->x*4, stackbuf->x*4);
87
 
                        CLAMP(ys, -stackbuf->y*4, stackbuf->y*4);
88
 
                        
89
 
                        p_dx = vec[0] * xs;
90
 
                        p_dy = vec[1] * ys;
91
 
                        
92
 
                        /* if no displacement, then just copy this pixel */
93
 
                        if (fabsf(p_dx) < DISPLACE_EPSILON && fabsf(p_dy) < DISPLACE_EPSILON) {
94
 
                                qd_getPixel(cbuf, x-cbuf->xof, y-cbuf->yof, col);
95
 
                                qd_setPixel(stackbuf, x, y, col);
96
 
                                continue;
97
 
                        }
98
 
                        
99
 
                        /* displaced pixel in uv coords, for image sampling */
100
 
                        u = (x - cbuf->xof - p_dx + 0.5f) / (float)stackbuf->x;
101
 
                        v = (y - cbuf->yof - p_dy + 0.5f) / (float)stackbuf->y;
102
 
                        
103
 
                        
104
 
                        /* calc derivatives */
105
 
                        qd_getPixel(vecbuf, x-vecbuf->xof+1, y-vecbuf->yof, vecdx);
106
 
                        qd_getPixel(vecbuf, x-vecbuf->xof, y-vecbuf->yof+1, vecdy);
107
 
                        d_dx = vecdx[0] * xs;
108
 
                        d_dy = vecdy[0] * ys;
109
 
 
110
 
                        /* clamp derivatives to minimum displacement distance in UV space */
111
 
                        dxt = p_dx - d_dx;
112
 
                        dyt = p_dy - d_dy;
113
 
 
114
 
                        dxt = signf(dxt)*maxf(fabsf(dxt), DISPLACE_EPSILON)/(float)stackbuf->x;
115
 
                        dyt = signf(dyt)*maxf(fabsf(dyt), DISPLACE_EPSILON)/(float)stackbuf->y;
116
 
                        
117
 
                        ibuf_sample(ibuf, u, v, dxt, dyt, col);
118
 
                        qd_setPixel(stackbuf, x, y, col);
119
 
                        
120
 
                        if (node->exec & NODE_BREAK) break;
121
 
                }
122
 
                
123
 
                if (node->exec & NODE_BREAK) break;
124
 
        }
125
 
        IMB_freeImBuf(ibuf);
126
 
        
127
 
        
128
 
/* simple method for reference, linear interpolation */
129
 
/*      
130
 
        int x, y;
131
 
        float dx, dy;
132
 
        float u, v;
133
 
        float vec[3];
134
 
        float col[3];
135
 
        
136
 
        for (y=0; y < stackbuf->y; y++) {
137
 
                for (x=0; x < stackbuf->x; x++) {
138
 
                        qd_getPixel(vecbuf, x, y, vec);
139
 
                        
140
 
                        dx = vec[0] * (xscale[0]);
141
 
                        dy = vec[1] * (yscale[0]);
142
 
                        
143
 
                        u = (x - dx + 0.5f) / (float)stackbuf->x;
144
 
                        v = (y - dy + 0.5f) / (float)stackbuf->y;
145
 
                        
146
 
                        qd_getPixelLerp(cbuf, u*cbuf->x - 0.5f, v*cbuf->y - 0.5f, col);
147
 
                        qd_setPixel(stackbuf, x, y, col);
148
 
                }
149
 
        }
150
 
*/
151
 
}
152
 
 
153
 
 
154
 
static void node_composit_exec_displace(void *UNUSED(data), bNode *node, bNodeStack **in, bNodeStack **out)
155
 
{
156
 
        if (out[0]->hasoutput==0)
157
 
                return;
158
 
        
159
 
        if (in[0]->data && in[1]->data) {
160
 
                CompBuf *cbuf= in[0]->data;
161
 
                CompBuf *vecbuf= in[1]->data;
162
 
                CompBuf *xbuf= in[2]->data;
163
 
                CompBuf *ybuf= in[3]->data;
164
 
                CompBuf *stackbuf;
165
 
                
166
 
                cbuf= typecheck_compbuf(cbuf, CB_RGBA);
167
 
                vecbuf= typecheck_compbuf(vecbuf, CB_VEC3);
168
 
                xbuf= typecheck_compbuf(xbuf, CB_VAL);
169
 
                ybuf= typecheck_compbuf(ybuf, CB_VAL);
170
 
                
171
 
                stackbuf= alloc_compbuf(cbuf->x, cbuf->y, CB_RGBA, 1); /* allocs */
172
 
 
173
 
                do_displace(node, stackbuf, cbuf, vecbuf, in[1]->vec, xbuf, ybuf, in[2]->vec, in[3]->vec);
174
 
                
175
 
                out[0]->data= stackbuf;
176
 
                
177
 
                
178
 
                if (cbuf!=in[0]->data)
179
 
                        free_compbuf(cbuf);
180
 
                if (vecbuf!=in[1]->data)
181
 
                        free_compbuf(vecbuf);
182
 
        }
183
 
}
 
38
static bNodeSocketTemplate cmp_node_displace_in[] = {
 
39
        {       SOCK_RGBA, 1, N_("Image"),                      1.0f, 1.0f, 1.0f, 1.0f},
 
40
        {       SOCK_VECTOR, 1, N_("Vector"),                   1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, PROP_TRANSLATION},
 
41
        {       SOCK_FLOAT, 1, N_("X Scale"),                           0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f, PROP_FACTOR},
 
42
        {       SOCK_FLOAT, 1, N_("Y Scale"),                           0.0f, 0.0f, 0.0f, 0.0f, -1000.0f, 1000.0f, PROP_FACTOR},
 
43
        {       -1, 0, ""       }
 
44
};
 
45
static bNodeSocketTemplate cmp_node_displace_out[] = {
 
46
        {       SOCK_RGBA, 0, N_("Image")},
 
47
        {       -1, 0, ""       }
 
48
};
184
49
 
185
50
void register_node_type_cmp_displace(bNodeTreeType *ttype)
186
51
{
189
54
        node_type_base(ttype, &ntype, CMP_NODE_DISPLACE, "Displace", NODE_CLASS_DISTORT, NODE_OPTIONS);
190
55
        node_type_socket_templates(&ntype, cmp_node_displace_in, cmp_node_displace_out);
191
56
        node_type_size(&ntype, 140, 100, 320);
192
 
        node_type_exec(&ntype, node_composit_exec_displace);
193
57
 
194
58
        nodeRegisterType(ttype, &ntype);
195
59
}