@@ -21,40 +21,40 @@ struct CompositeOrderSort
2121class APCOMP_API ImageCompositor
2222{
2323public:
24- void Blend (apcomp::Image &front, apcomp::Image &back)
25- {
24+ void Blend (apcomp::Image &front, apcomp::Image &back)
25+ {
2626
27- assert (front.m_bounds .m_min_x == back.m_bounds .m_min_x );
28- assert (front.m_bounds .m_min_y == back.m_bounds .m_min_y );
29- assert (front.m_bounds .m_max_x == back.m_bounds .m_max_x );
30- assert (front.m_bounds .m_max_y == back.m_bounds .m_max_y );
31- const int size = static_cast <int >(front.m_pixels .size () / 4 );
27+ assert (front.m_bounds .m_min_x == back.m_bounds .m_min_x );
28+ assert (front.m_bounds .m_min_y == back.m_bounds .m_min_y );
29+ assert (front.m_bounds .m_max_x == back.m_bounds .m_max_x );
30+ assert (front.m_bounds .m_max_y == back.m_bounds .m_max_y );
31+ const int size = static_cast <int >(front.m_pixels .size () / 4 );
3232
3333#ifdef APCOMP_USE_OPENMP
34- #pragma omp parallel for
34+ #pragma omp parallel for
3535#endif
36- for (int i = 0 ; i < size; ++i)
37- {
38- const int offset = i * 4 ;
39- unsigned int alpha = front.m_pixels [offset + 3 ];
40- const unsigned int opacity = 255 - alpha;
41-
42- front.m_pixels [offset + 0 ] +=
43- static_cast <unsigned char >(opacity * back.m_pixels [offset + 0 ] / 255 );
44- front.m_pixels [offset + 1 ] +=
45- static_cast <unsigned char >(opacity * back.m_pixels [offset + 1 ] / 255 );
46- front.m_pixels [offset + 2 ] +=
47- static_cast <unsigned char >(opacity * back.m_pixels [offset + 2 ] / 255 );
48- front.m_pixels [offset + 3 ] +=
49- static_cast <unsigned char >(opacity * back.m_pixels [offset + 3 ] / 255 );
50-
51- float d1 = std::min (front.m_depths [i], 1 .001f );
52- float d2 = std::min (back.m_depths [i], 1 .001f );
53- float depth = std::min (d1,d2);
54- front.m_depths [i] = depth;
55- }
36+ for (int i = 0 ; i < size; ++i)
37+ {
38+ const int offset = i * 4 ;
39+ unsigned int alpha = front.m_pixels [offset + 3 ];
40+ const unsigned int opacity = 255 - alpha;
41+
42+ front.m_pixels [offset + 0 ] +=
43+ static_cast <unsigned char >(opacity * back.m_pixels [offset + 0 ] / 255 );
44+ front.m_pixels [offset + 1 ] +=
45+ static_cast <unsigned char >(opacity * back.m_pixels [offset + 1 ] / 255 );
46+ front.m_pixels [offset + 2 ] +=
47+ static_cast <unsigned char >(opacity * back.m_pixels [offset + 2 ] / 255 );
48+ front.m_pixels [offset + 3 ] +=
49+ static_cast <unsigned char >(opacity * back.m_pixels [offset + 3 ] / 255 );
50+
51+ float d1 = std::min (front.m_depths [i], 1 .001f );
52+ float d2 = std::min (back.m_depths [i], 1 .001f );
53+ float depth = std::min (d1,d2);
54+ front.m_depths [i] = depth;
5655 }
57-
56+ }
57+ // Only composite values the GL depths range (0,1)
5858void ZBufferComposite (apcomp::Image &front, const apcomp::Image &image)
5959{
6060 assert (front.m_depths .size () == front.m_pixels .size () / 4 );
@@ -64,23 +64,46 @@ void ZBufferComposite(apcomp::Image &front, const apcomp::Image &image)
6464 assert (front.m_bounds .m_max_y == image.m_bounds .m_max_y );
6565
6666 const int size = static_cast <int >(front.m_depths .size ());
67-
67+ bool gl_depth = front.m_gl_depth ;
68+ if (gl_depth)
69+ {
6870#ifdef apcomp_USE_OPENMP
69- #pragma omp parallel for
71+ #pragma omp parallel for
7072#endif
71- for (int i = 0 ; i < size; ++i)
73+ for (int i = 0 ; i < size; ++i)
74+ {
75+ const float depth = image.m_depths [i];
76+ if (depth > 1 .f || front.m_depths [i] < depth)
77+ {
78+ continue ;
79+ }
80+ const int offset = i * 4 ;
81+ front.m_depths [i] = depth;
82+ front.m_pixels [offset + 0 ] = image.m_pixels [offset + 0 ];
83+ front.m_pixels [offset + 1 ] = image.m_pixels [offset + 1 ];
84+ front.m_pixels [offset + 2 ] = image.m_pixels [offset + 2 ];
85+ front.m_pixels [offset + 3 ] = image.m_pixels [offset + 3 ];
86+ }
87+ }
88+ else
7289 {
73- const float depth = image.m_depths [i];
74- if (depth > 1 .f || front.m_depths [i] < depth)
90+ #ifdef apcomp_USE_OPENMP
91+ #pragma omp parallel for
92+ #endif
93+ for (int i = 0 ; i < size; ++i)
7594 {
76- continue ;
95+ const float depth = image.m_depths [i];
96+ if (front.m_depths [i] < depth)
97+ {
98+ continue ;
99+ }
100+ const int offset = i * 4 ;
101+ front.m_depths [i] = depth;
102+ front.m_pixels [offset + 0 ] = image.m_pixels [offset + 0 ];
103+ front.m_pixels [offset + 1 ] = image.m_pixels [offset + 1 ];
104+ front.m_pixels [offset + 2 ] = image.m_pixels [offset + 2 ];
105+ front.m_pixels [offset + 3 ] = image.m_pixels [offset + 3 ];
77106 }
78- const int offset = i * 4 ;
79- front.m_depths [i] = depth;
80- front.m_pixels [offset + 0 ] = image.m_pixels [offset + 0 ];
81- front.m_pixels [offset + 1 ] = image.m_pixels [offset + 1 ];
82- front.m_pixels [offset + 2 ] = image.m_pixels [offset + 2 ];
83- front.m_pixels [offset + 3 ] = image.m_pixels [offset + 3 ];
84107 }
85108}
86109
0 commit comments