Skip to content
This repository was archived by the owner on Jul 14, 2022. It is now read-only.

Commit d52c785

Browse files
authored
Merge pull request #9 from Alpine-DAV/world_space
add path for world space zbuffer
2 parents c96a5c0 + ead174e commit d52c785

File tree

7 files changed

+112
-61
lines changed

7 files changed

+112
-61
lines changed

src/apcomp/compositor.cpp

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace apcomp
1616
{
1717

1818
Compositor::Compositor()
19-
: m_composite_mode(Z_BUFFER_SURFACE)
19+
: m_composite_mode(Z_BUFFER_SURFACE_GL)
2020
{
2121

2222
}
@@ -49,24 +49,28 @@ Compositor::AddImage(const unsigned char *color_buffer,
4949
assert(m_composite_mode != VIS_ORDER_BLEND);
5050
assert(depth_buffer != NULL);
5151
Image image;
52+
bool gl_depth = m_composite_mode != Z_BUFFER_SURFACE_WORLD;
5253
if(m_images.size() == 0)
5354
{
5455
m_images.push_back(image);
5556
m_images[0].Init(color_buffer,
5657
depth_buffer,
5758
width,
58-
height);
59+
height,
60+
gl_depth);
5961
//m_images[0].Save("first.png");
6062
}
61-
else if(m_composite_mode == Z_BUFFER_SURFACE)
63+
else if(m_composite_mode == Z_BUFFER_SURFACE_GL ||
64+
m_composite_mode == Z_BUFFER_SURFACE_WORLD)
6265
{
6366
//
6467
// Do local composite and keep a single image
6568
//
6669
image.Init(color_buffer,
6770
depth_buffer,
6871
width,
69-
height);
72+
height,
73+
gl_depth);
7074
apcomp::ImageCompositor compositor;
7175
compositor.ZBufferComposite(m_images[0],image);
7276
}
@@ -91,23 +95,29 @@ Compositor::AddImage(const float *color_buffer,
9195
assert(m_composite_mode != VIS_ORDER_BLEND);
9296
assert(depth_buffer != NULL);
9397
Image image;
98+
99+
bool gl_depth = m_composite_mode != Z_BUFFER_SURFACE_WORLD;
100+
94101
if(m_images.size() == 0)
95102
{
96103
m_images.push_back(image);
97104
m_images[0].Init(color_buffer,
98105
depth_buffer,
99106
width,
100-
height);
107+
height,
108+
gl_depth);
101109
}
102-
else if(m_composite_mode == Z_BUFFER_SURFACE)
110+
else if(m_composite_mode == Z_BUFFER_SURFACE_GL ||
111+
m_composite_mode == Z_BUFFER_SURFACE_WORLD)
103112
{
104113
//
105114
// Do local composite and keep a single image
106115
//
107116
image.Init(color_buffer,
108117
depth_buffer,
109118
width,
110-
height);
119+
height,
120+
gl_depth);
111121

112122
apcomp::ImageCompositor compositor;
113123
compositor.ZBufferComposite(m_images[0],image);
@@ -135,10 +145,12 @@ Compositor::AddImage(const unsigned char *color_buffer,
135145
Image image;
136146
const size_t image_index = m_images.size();
137147
m_images.push_back(image);
148+
bool gl_depth = false;
138149
m_images[image_index].Init(color_buffer,
139150
depth_buffer,
140151
width,
141152
height,
153+
gl_depth,
142154
vis_order);
143155
}
144156

@@ -153,11 +165,12 @@ Compositor::AddImage(const float *color_buffer,
153165
Image image;
154166
const size_t image_index = m_images.size();
155167
m_images.push_back(image);
156-
168+
bool gl_depth = false;
157169
m_images[image_index].Init(color_buffer,
158170
depth_buffer,
159171
width,
160172
height,
173+
gl_depth,
161174
vis_order);
162175
}
163176

@@ -166,7 +179,8 @@ Compositor::Composite()
166179
{
167180
assert(m_images.size() != 0);
168181

169-
if(m_composite_mode == Z_BUFFER_SURFACE)
182+
if(m_composite_mode == Z_BUFFER_SURFACE_GL ||
183+
m_composite_mode == Z_BUFFER_SURFACE_WORLD )
170184
{
171185
CompositeZBufferSurface();
172186
}

src/apcomp/compositor.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ class APCOMP_API Compositor
1212
{
1313
public:
1414
enum CompositeMode {
15-
Z_BUFFER_SURFACE, // zbuffer composite no transparency
16-
Z_BUFFER_BLEND, // zbuffer composite with transparency
17-
VIS_ORDER_BLEND // blend images in a specific order
15+
Z_BUFFER_SURFACE_WORLD,// expect world depth no transparency
16+
Z_BUFFER_SURFACE_GL, // zbuffer composite no transparency
17+
Z_BUFFER_BLEND, // zbuffer composite with transparency
18+
VIS_ORDER_BLEND // blend images in a specific order
1819
};
1920
Compositor();
2021

src/apcomp/image.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ namespace apcomp
1111
Image::Image()
1212
: m_orig_rank(-1),
1313
m_has_transparency(false),
14-
m_composite_order(-1)
14+
m_composite_order(-1),
15+
m_gl_depth(true)
1516
{
16-
1717
}
1818

1919
Image::Image(const Bounds &bounds)
2020
: m_orig_bounds(bounds),
2121
m_bounds(bounds),
2222
m_orig_rank(-1),
2323
m_has_transparency(false),
24-
m_composite_order(-1)
24+
m_composite_order(-1),
25+
m_gl_depth(true)
2526

2627
{
2728
const int dx = bounds.m_max_x - bounds.m_min_x + 1;
@@ -44,13 +45,15 @@ Image::InitOriginal(const Image &other)
4445
m_orig_rank = -1;
4546
m_has_transparency = false;
4647
m_composite_order = -1;
48+
m_gl_depth = other.m_gl_depth;
4749
}
4850

4951
void
5052
Image::Init(const float *color_buffer,
5153
const float *depth_buffer,
5254
int width,
5355
int height,
56+
bool gl_depth,
5457
int composite_order)
5558
{
5659
m_composite_order = composite_order;
@@ -75,7 +78,10 @@ Image::Init(const float *color_buffer,
7578
m_pixels[offset + 3] = static_cast<unsigned char>(color_buffer[offset + 3] * 255.f);
7679
float depth = depth_buffer[i];
7780
//make sure we can do a single comparison on depth
78-
depth = depth < 0 ? 2.f : depth;
81+
if(gl_depth)
82+
{
83+
depth = depth < 0 ? 2.f : depth;
84+
}
7985
m_depths[i] = depth;
8086
}
8187
}
@@ -85,6 +91,7 @@ Image::Init(const unsigned char *color_buffer,
8591
const float *depth_buffer,
8692
int width,
8793
int height,
94+
bool gl_depth,
8895
int composite_order)
8996
{
9097
m_composite_order = composite_order;
@@ -109,8 +116,11 @@ Image::Init(const unsigned char *color_buffer,
109116
{
110117
float depth = depth_buffer[i];
111118
//make sure we can do a single comparison on depth
112-
depth = depth < 0 ? 2.f : depth;
113-
m_depths[i] = depth;
119+
if(gl_depth)
120+
{
121+
depth = depth < 0 ? 2.f : depth;
122+
}
123+
m_depths[i] = depth;
114124
} // for
115125
}
116126

src/apcomp/image.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct APCOMP_API Image
2323
int m_orig_rank;
2424
bool m_has_transparency;
2525
int m_composite_order;
26+
bool m_gl_depth; // expect depth values in (0,1)
2627

2728
Image();
2829

@@ -42,12 +43,14 @@ struct APCOMP_API Image
4243
const float *depth_buffer,
4344
int width,
4445
int height,
46+
bool gl_depth = true,
4547
int composite_order = -1);
4648

4749
void Init(const unsigned char *color_buffer,
4850
const float *depth_buffer,
4951
int width,
5052
int height,
53+
bool gl_depth = true,
5154
int composite_order = -1);
5255

5356
void CompositeBackground(const float color[4]);

src/apcomp/internal/ImageCompositor.hpp

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,40 @@ struct CompositeOrderSort
2121
class APCOMP_API ImageCompositor
2222
{
2323
public:
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)
5858
void 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

src/tests/t_apcomp_zbuffer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ TEST(apcomp_zbuffer, apcomp_zbuffer)
6868
remove_test_file(output_file);
6969

7070
apcomp::Compositor compositor;
71-
auto mode = apcomp::Compositor::CompositeMode::Z_BUFFER_SURFACE;
71+
auto mode = apcomp::Compositor::CompositeMode::Z_BUFFER_SURFACE_GL;
7272
compositor.SetCompositeMode(mode);
7373

7474
const int width = 1024;

src/tests/t_apcomp_zbuffer_mpi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ TEST(apcomp_zbuffer_mpi, apcomp_zbuffer_mpi)
7676
apcomp::mpi_comm(MPI_Comm_c2f(comm));
7777

7878
apcomp::Compositor compositor;
79-
auto mode = apcomp::Compositor::CompositeMode::Z_BUFFER_SURFACE;
79+
auto mode = apcomp::Compositor::CompositeMode::Z_BUFFER_SURFACE_GL;
8080
compositor.SetCompositeMode(mode);
8181

8282
const int width = 1024;

0 commit comments

Comments
 (0)