35eedf435f6ed00b5f7f01e6e1da71e5a34415ca
[blender.git] / source / blender / compositor / nodes / COM_ZCombineNode.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include "COM_ZCombineNode.h"
24
25 #include "COM_ZCombineOperation.h"
26
27 #include "COM_ExecutionSystem.h"
28 #include "COM_SetValueOperation.h"
29 #include "COM_MathBaseOperation.h"
30 #include "COM_AntiAliasOperation.h"
31 #include "COM_MixBlendOperation.h"
32
33 #include "DNA_material_types.h" // the ramp types
34
35 void ZCombineNode::convertToOperations(ExecutionSystem *system, CompositorContext *context)
36 {
37         if (context->getRenderData()->scemode & R_FULL_SAMPLE) {
38                 if (this->getOutputSocket(0)->isConnected()) {
39                         ZCombineOperation *operation = NULL;
40                         if (this->getbNode()->custom1) {
41                                 operation = new ZCombineAlphaOperation();
42                         }
43                         else {
44                                 operation = new ZCombineOperation();
45                         }
46
47                         this->getInputSocket(0)->relinkConnections(operation->getInputSocket(0), 0, system);
48                         this->getInputSocket(1)->relinkConnections(operation->getInputSocket(1), 1, system);
49                         this->getInputSocket(2)->relinkConnections(operation->getInputSocket(2), 2, system);
50                         this->getInputSocket(3)->relinkConnections(operation->getInputSocket(3), 3, system);
51                         this->getOutputSocket(0)->relinkConnections(operation->getOutputSocket());
52                         system->addOperation(operation);
53                         if (this->getOutputSocket(1)->isConnected()) {
54                                 MathMinimumOperation *zoperation = new MathMinimumOperation();
55                                 addLink(system, operation->getInputSocket(1)->getConnection()->getFromSocket(), zoperation->getInputSocket(0));
56                                 addLink(system, operation->getInputSocket(3)->getConnection()->getFromSocket(), zoperation->getInputSocket(1));
57                                 this->getOutputSocket(1)->relinkConnections(zoperation->getOutputSocket());
58                                 system->addOperation(zoperation);
59                         }
60                 }
61                 else {
62                         if (this->getOutputSocket(1)->isConnected()) {
63                                 MathMinimumOperation *zoperation = new MathMinimumOperation();
64                                 this->getInputSocket(1)->relinkConnections(zoperation->getInputSocket(0), 1, system);
65                                 this->getInputSocket(3)->relinkConnections(zoperation->getInputSocket(1), 3, system);
66                                 this->getOutputSocket(1)->relinkConnections(zoperation->getOutputSocket());
67                                 system->addOperation(zoperation);
68                         }
69                 }
70         } else {
71                 // not full anti alias, use masking for Z combine. be aware it uses anti aliasing.
72                 // step 1 create mask
73                 MathGreaterThanOperation *maskoperation = new MathGreaterThanOperation();
74                 this->getInputSocket(1)->relinkConnections(maskoperation->getInputSocket(0), 1, system);
75                 this->getInputSocket(3)->relinkConnections(maskoperation->getInputSocket(1), 3, system);
76
77                 // step 2 anti alias mask bit of an expensive operation, but does the trick
78                 AntiAliasOperation *antialiasoperation = new AntiAliasOperation();
79                 addLink(system, maskoperation->getOutputSocket(), antialiasoperation->getInputSocket(0));
80
81                 // use mask to blend between the input colors.
82                 ZCombineMaskOperation *zcombineoperation = this->getbNode()->custom1?new ZCombineMaskAlphaOperation():new ZCombineMaskOperation();
83                 addLink(system, antialiasoperation->getOutputSocket(), zcombineoperation->getInputSocket(0));
84                 this->getInputSocket(0)->relinkConnections(zcombineoperation->getInputSocket(1), 0, system);
85                 this->getInputSocket(2)->relinkConnections(zcombineoperation->getInputSocket(2), 2, system);
86                 this->getOutputSocket(0)->relinkConnections(zcombineoperation->getOutputSocket());
87
88                 system->addOperation(maskoperation);
89                 system->addOperation(antialiasoperation);
90                 system->addOperation(zcombineoperation);
91
92                 if (this->getOutputSocket(1)->isConnected()) {
93                         MathMinimumOperation *zoperation = new MathMinimumOperation();
94                         addLink(system, maskoperation->getInputSocket(0)->getConnection()->getFromSocket(), zoperation->getInputSocket(0));
95                         addLink(system, maskoperation->getInputSocket(1)->getConnection()->getFromSocket(), zoperation->getInputSocket(1));
96                         this->getOutputSocket(1)->relinkConnections(zoperation->getOutputSocket());
97                         system->addOperation(zoperation);
98                 }
99         }
100 }