Skip to content

Commit

Permalink
Fix 4 channel png loading
Browse files Browse the repository at this point in the history
  • Loading branch information
iaomw committed Oct 16, 2024
1 parent 4aa45bc commit 0154820
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions zenovis/xinxinoptix/OptiXStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -982,37 +982,35 @@ inline void addTexture(std::string path, bool blockCompression=false, TaskType*
ny = std::max(img->userData().get2<int>("h"), 1);
nc = std::max(img->userData().get2<int>("channels"), 1);

if (nc < 4) {
auto ucdata = std::make_shared<std::vector<unsigned char>>(img->verts.size() * nc);

std::vector<unsigned char> ucdata;
ucdata.resize(img->verts.size() * nc);
if (nc < 4) {

for(size_t i=0; i<img->verts.size(); i+=1 ) {

for (int c=0; c<nc; ++c) {
ucdata[i*nc+c] = (img->verts[i][c] * 255.0);
ucdata->at(i*nc+c) = (img->verts[i][c] * 255.0);
}
}
tex_lut[tex_key] = makeCudaTexture(ucdata.data(), nx, ny, nc, blockCompression);
tex_lut[tex_key] = makeCudaTexture(ucdata->data(), nx, ny, nc, blockCompression);

} else {

assert(nc == 4);
std::vector<uchar4> data(nx * ny);
auto data = (uchar4*)ucdata->data();
auto &alpha = img->verts.attr<float>("alpha");
for (auto i = 0; i < nx * ny; i++) {
data[i].x = (unsigned char)(img->verts[i][0]*255.0);
data[i].y = (unsigned char)(img->verts[i][1]*255.0);
data[i].z = (unsigned char)(img->verts[i][2]*255.0);
data[i].w = (unsigned char)(alpha[i] *255.0);

}
tex_lut[tex_key] = makeCudaTexture((unsigned char *)data.data(), nx, ny, 4, blockCompression);
tex_lut[tex_key] = makeCudaTexture((unsigned char *)data, nx, ny, 4, blockCompression);
}

lookupTexture = [img=img](uint32_t idx) {
auto ptr = (float*)img->verts->data();
return ptr[idx];
lookupTexture = [ucdata=ucdata, img=img](uint32_t idx) {
auto ptr = ucdata->data();
return ptr[idx]/255.0;
};
}
else if (stbi_is_hdr(native_path.c_str())) {
Expand Down

0 comments on commit 0154820

Please sign in to comment.