Skip to content

Commit a43e6fd

Browse files
committed
texture support
1 parent d4ef887 commit a43e6fd

File tree

4 files changed

+82
-23
lines changed

4 files changed

+82
-23
lines changed

src/material.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#define MATERIAL_DIELETRIC_USE_SCHLICK_APPROX false
1010

11+
#define NullTextureId -1
12+
#define ProceduralTexId -2
1113
#define InvalidPdf -1.f
1214

1315
enum BSDFSampleType {
@@ -250,13 +252,11 @@ struct Material {
250252
}
251253
}
252254

253-
int type;
254-
glm::vec3 baseColor;
255-
float metallic;
256-
float roughness;
257-
float ior;
258-
float emittance;
259-
260-
int baseColorMapId;
261-
int normalMapId;
255+
int type = Type::Lambertian;
256+
glm::vec3 baseColor = glm::vec3(.9f);
257+
int baseColorMapId = NullTextureId;
258+
float metallic = 0.f;
259+
float roughness = 1.f;
260+
float ior = 1.5f;
261+
float emittance = 0.f;
262262
};

src/pathtrace.cu

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ __global__ void pathIntegSampleSurface(
245245
thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, 4 + depth * SamplesConsumedOneIter);
246246

247247
Material material = scene->devMaterials[intersec.matId];
248+
if (material.baseColorMapId > NullTextureId) {
249+
material.baseColor = scene->devTextureObjs->linearSample(intersec.uv);
250+
}
251+
248252
glm::vec3 accRadiance(0.f);
249253

250254
if (material.type == Material::Type::Light) {
@@ -349,6 +353,10 @@ __global__ void singleKernelPT(
349353
goto WriteRadiance;
350354
}
351355

356+
if (material.baseColorMapId > NullTextureId) {
357+
material.baseColor = scene->devTextureObjs->linearSample(intersec.uv);
358+
}
359+
352360
glm::vec3 throughput(1.f);
353361
intersec.wo = -ray.direction;
354362

src/scene.cpp

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MeshData* Resource::loadOBJMesh(const std::string& filename) {
3232

3333
std::cout << "\t\t[Model loading " << filename << " ...]" << std::endl;
3434
if (!tinyobj::LoadObj(&attrib, &shapes, nullptr, &warn, &err, filename.c_str())) {
35-
std::cout << "\t\t\t[Fail Error msg [" << err << "]" << std::endl;
35+
std::cout << "\t\t\t[Fail Error msg " << err << "]" << std::endl;
3636
return nullptr;
3737
}
3838
bool hasTexcoord = !attrib.texcoords.empty();
@@ -184,6 +184,11 @@ void Scene::buildDevData() {
184184
}
185185
}
186186
#endif
187+
if (primId == 0) {
188+
std::cout << "[No mesh data loaded, quit]" << std::endl;
189+
exit(-1);
190+
}
191+
187192
createLightSampler();
188193
BVHSize = BVHBuilder::build(meshData.vertices, boundingBoxes, BVHNodes);
189194
checkCUDAError("BVH Build");
@@ -219,16 +224,30 @@ void Scene::loadModel(const std::string& objId) {
219224
std::cout << "\t\t[File " << filename << "]" << std::endl;
220225
instance.meshData = Resource::loadModelMeshData(filename);
221226

227+
if (!instance.meshData) {
228+
std::cout << "\t\t[Fail to load, skipped]" << std::endl;
229+
while (!line.empty() && fpIn.good()) {
230+
utilityCore::safeGetline(fpIn, line);
231+
}
232+
return;
233+
}
234+
222235
//link material
223236
utilityCore::safeGetline(fpIn, line);
224237
if (!line.empty() && fpIn.good()) {
225238
std::vector<std::string> tokens = utilityCore::tokenizeString(line);
226-
if (materialMap.find(tokens[1]) == materialMap.end()) {
227-
std::cout << "\t\t[Material " << tokens[1] << " doesn't exist]" << std::endl;
228-
throw;
239+
if (tokens[1] == "Null") {
240+
// Null material, create new one
241+
instance.materialId = addMaterial(Material());
242+
}
243+
else {
244+
if (materialMap.find(tokens[1]) == materialMap.end()) {
245+
std::cout << "\t\t[Material " << tokens[1] << " doesn't exist]" << std::endl;
246+
throw;
247+
}
248+
instance.materialId = materialMap[tokens[1]];
249+
std::cout << "\t\t[Link to Material " << tokens[1] << "{" << instance.materialId << "} ...]" << std::endl;
229250
}
230-
instance.materialId = materialMap[tokens[1]];
231-
std::cout << "\t\t[Link to Material " << tokens[1] << "{" << instance.materialId << "} ...]" << std::endl;
232251
}
233252

234253
//load transformations
@@ -239,7 +258,6 @@ void Scene::loadModel(const std::string& objId) {
239258
//load tranformations
240259
if (tokens[0] == "Translate") {
241260
instance.translation = glm::vec3(std::stof(tokens[1]), std::stof(tokens[2]), std::stof(tokens[3]));
242-
std::cout << vec3ToString(instance.translation) << "\n";
243261
}
244262
else if (tokens[0] == "Rotate") {
245263
instance.rotation = glm::vec3(std::stof(tokens[1]), std::stof(tokens[2]), std::stof(tokens[3]));
@@ -322,6 +340,25 @@ void Scene::loadCamera() {
322340
std::fill(state.image.begin(), state.image.end(), glm::vec3());
323341
}
324342

343+
int Scene::addMaterial(const Material& material) {
344+
materials.push_back(material);
345+
return materials.size() - 1;
346+
}
347+
348+
int Scene::addTexture(const std::string& filename) {
349+
auto texture = Resource::loadTexture(filename);
350+
auto find = textureMap.find(texture);
351+
if (find != textureMap.end()) {
352+
return find->second;
353+
}
354+
else {
355+
int size = textureMap.size();
356+
textureMap[texture] = size;
357+
textures.push_back(texture);
358+
return size;
359+
}
360+
}
361+
325362
void Scene::loadMaterial(const std::string& matId) {
326363
std::cout << "\t[Material " << matId << "]" << std::endl;
327364
Material material;
@@ -336,8 +373,14 @@ void Scene::loadMaterial(const std::string& matId) {
336373
std::cout << "\t\t[Type " << tokens[1] << "]" << std::endl;
337374
}
338375
else if (tokens[0] == "BaseColor") {
339-
glm::vec3 baseColor(std::stof(tokens[1]), std::stof(tokens[2]), std::stof(tokens[3]));
340-
material.baseColor = baseColor;
376+
if (tokens.size() > 2) {
377+
glm::vec3 baseColor(std::stof(tokens[1]), std::stof(tokens[2]), std::stof(tokens[3]));
378+
material.baseColor = baseColor;
379+
}
380+
else {
381+
material.baseColorMapId = addTexture(tokens[1]);
382+
std::cout << "\t\t[BaseColor use texture " << tokens[1] << "]" << std::endl;
383+
}
341384
}
342385
else if (tokens[0] == "Metallic") {
343386
material.metallic = std::stof(tokens[1]);
@@ -366,16 +409,20 @@ void DevScene::create(const Scene& scene) {
366409
textureTotalSize += tex->byteSize();
367410
}
368411
cudaMalloc(&devTextureData, textureTotalSize);
412+
checkCUDAError("DevScene::texture");
369413

370-
size_t textureOffset = 0;
414+
int textureOffset = 0;
371415
for (auto tex : scene.textures) {
372-
cudaMemcpy(devTextureData + textureOffset, tex->data(), tex->byteSize(), cudaMemcpyKind::cudaMemcpyHostToDevice);
416+
cudaMemcpyHostToDev(devTextureData + textureOffset, tex->data(), tex->byteSize());
417+
std::cout << tex->byteSize() << "\n";
418+
checkCUDAError("DevScene::texture::copy");
373419
textureObjs.push_back({ tex, devTextureData + textureOffset });
374-
textureOffset += tex->byteSize();
420+
textureOffset += tex->byteSize() / sizeof(glm::vec3);
375421
}
376422
cudaMalloc(&devTextureObjs, textureObjs.size() * sizeof(DevTextureObj));
423+
checkCUDAError("DevScene::textureObjs::malloc");
377424
cudaMemcpyHostToDev(devTextureObjs, textureObjs.data(), textureObjs.size() * sizeof(DevTextureObj));
378-
checkCUDAError("DevScene::texture");
425+
checkCUDAError("DevScene::textureObjs::copy");
379426

380427
cudaMalloc(&devMaterials, byteSizeOf(scene.materials));
381428
cudaMemcpyHostToDev(devMaterials, scene.materials.data(), byteSizeOf(scene.materials));

src/scene.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Resource {
5353

5454
static void clear();
5555

56-
private:
56+
public:
5757
static std::map<std::string, MeshData*> meshDataPool;
5858
static std::map<std::string, Image*> texturePool;
5959
};
@@ -389,10 +389,14 @@ class Scene {
389389
void loadMaterial(const std::string& materialId);
390390
void loadCamera();
391391

392+
int addMaterial(const Material& material);
393+
int addTexture(const std::string& filename);
394+
392395
public:
393396
RenderState state;
394397
std::vector<ModelInstance> modelInstances;
395398
std::vector<Image*> textures;
399+
std::map<Image*, int> textureMap;
396400
std::vector<Material> materials;
397401
std::map<std::string, int> materialMap;
398402
std::vector<int> materialIds;

0 commit comments

Comments
 (0)