Skip to content

Commit 8d8b63e

Browse files
committed
metallic & roughness map support
1 parent baae16c commit 8d8b63e

File tree

7 files changed

+42
-16
lines changed

7 files changed

+42
-16
lines changed

img/metal_rough.png

3.66 MB
Loading

src/image.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ Image::Image(int width, int height) :
1414
Image::Image(const std::string& filename) {
1515
int channels;
1616
// The textures should be in the linear space
17-
stbi_ldr_to_hdr_gamma(1.f);
1817
float* data = stbi_loadf(filename.c_str(), &mWidth, &mHeight, &channels, 3);
1918

2019
if (!data) {

src/main.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,14 @@ void runCuda() {
230230
if (State::camChanged) {
231231
iteration = 0;
232232
Camera& cam = scene->camera;
233-
cameraPosition.x = zoom * sin(phi) * sin(theta);
234-
cameraPosition.y = zoom * cos(theta);
235-
cameraPosition.z = zoom * cos(phi) * sin(theta);
233+
cam.view = glm::vec3(sin(phi) * sin(theta), cos(theta), cos(phi) * sin(theta));
236234

237-
cam.view = -glm::normalize(cameraPosition);
238235
glm::vec3 v = cam.view;
239236
glm::vec3 u = glm::vec3(0, 1, 0);//glm::normalize(cam.up);
240237
glm::vec3 r = glm::cross(v, u);
241238
cam.up = glm::cross(r, v);
242239
cam.right = r;
243240

244-
cameraPosition += cam.lookAt;
245-
cam.position = cameraPosition;
246241
State::camChanged = false;
247242
}
248243

@@ -305,8 +300,8 @@ void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods
305300
}
306301

307302
void mouseScrollCallback(GLFWwindow* window, double offsetX, double offsetY) {
308-
zoom -= offsetY;
309-
zoom = std::fmax(0.1f, zoom);
303+
scene->camera.fov.y -= offsetY;
304+
scene->camera.fov.y = std::min(scene->camera.fov.y, 45.f);
310305
State::camChanged = true;
311306
}
312307

@@ -348,8 +343,8 @@ void mousePositionCallback(GLFWwindow* window, double xpos, double ypos) {
348343
right.y = 0.0f;
349344
right = glm::normalize(right);
350345

351-
cam.lookAt -= (float)(xpos - lastX) * right * 0.01f;
352-
cam.lookAt += (float)(ypos - lastY) * forward * 0.01f;
346+
cam.position -= (float)(xpos - lastX) * right * 0.01f;
347+
cam.position += (float)(ypos - lastY) * forward * 0.01f;
353348
State::camChanged = true;
354349
}
355350
lastX = xpos;

src/material.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,5 +259,7 @@ struct Material {
259259
float ior = 1.5f;
260260

261261
int baseColorMapId = NullTextureId;
262+
int metallicMapId = NullTextureId;
263+
int roughnessMapId = NullTextureId;
262264
int normalMapId = NullTextureId;
263265
};

src/preview.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ void RenderImGui() {
217217
ImGuiWindowFlags_NoFocusOnAppearing |
218218
ImGuiWindowFlags_NoNav); {
219219
ImGui::Text("Traced Depth %d", imguiData->TracedDepth);
220+
ImGui::Text("BVH Size %d", scene->BVHSize);
220221
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
221222

222223
ImGui::End();
@@ -259,6 +260,10 @@ void RenderImGui() {
259260
State::camChanged = true;
260261
}
261262

263+
if (ImGui::DragFloat3("Up", glm::value_ptr(cam.up), .1f)) {
264+
State::camChanged = true;
265+
}
266+
262267
if (ImGui::SliderFloat("FOV", &cam.fov.y, .1f, 45.f, "%.1f deg")) {
263268
State::camChanged = true;
264269
}

src/scene.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "scene.h"
22
#include "common.h"
3+
#include "stb_image.h"
34
#include <iostream>
45
#include <cstring>
56
#include <stack>
@@ -113,6 +114,9 @@ void Resource::clear() {
113114
}
114115

115116
Scene::Scene(const std::string& filename) {
117+
stbi_ldr_to_hdr_gamma(1.f);
118+
stbi_set_flip_vertically_on_load(true);
119+
116120
std::cout << "[Scene loading " << filename << " ...]" << std::endl;
117121
std::cout << " " << std::endl;
118122
char* fname = (char*)filename.c_str();
@@ -335,20 +339,22 @@ void Scene::loadCamera() {
335339
camera.fov = glm::vec2(fovx, fovy);
336340
camera.tanFovY = glm::tan(glm::radians(fovy * 0.5f));
337341

342+
camera.view = glm::normalize(camera.lookAt - camera.position);
338343
camera.right = glm::normalize(glm::cross(camera.view, camera.up));
339344
camera.pixelLength = glm::vec2(2 * xscaled / (float)camera.resolution.x,
340345
2 * yscaled / (float)camera.resolution.y);
341346

342-
camera.view = glm::normalize(camera.lookAt - camera.position);
343-
344347
//set up render camera stuff
345348
int arraylen = camera.resolution.x * camera.resolution.y;
346349
state.image.resize(arraylen);
347350
std::fill(state.image.begin(), state.image.end(), glm::vec3());
348351
}
349352

350353
void Scene::loadEnvMap(const std::string& filename) {
354+
stbi_set_flip_vertically_on_load(false);
351355
envMapTexId = addTexture(filename);
356+
stbi_set_flip_vertically_on_load(true);
357+
352358
auto envMap = textures[envMapTexId];
353359
std::vector<float> pdf(envMap->width() * envMap->height());
354360

@@ -406,10 +412,22 @@ void Scene::loadMaterial(const std::string& matId) {
406412
}
407413
}
408414
else if (tokens[0] == "Metallic") {
409-
material.metallic = std::stof(tokens[1]);
415+
if (std::isdigit(tokens[1][tokens[1].length() - 1])) {
416+
material.metallic = std::stof(tokens[1]);
417+
}
418+
else {
419+
material.metallicMapId = addTexture(tokens[1]);
420+
std::cout << "\t\t[Metallic use texture " << tokens[1] << "]" << std::endl;
421+
}
410422
}
411423
else if (tokens[0] == "Roughness") {
412-
material.roughness = std::stof(tokens[1]);
424+
if (std::isdigit(tokens[1][tokens[1].length() - 1])) {
425+
material.roughness = std::stof(tokens[1]);
426+
}
427+
else {
428+
material.roughnessMapId = addTexture(tokens[1]);
429+
std::cout << "\t\t[Roughness use texture " << tokens[1] << "]" << std::endl;
430+
}
413431
}
414432
else if (tokens[0] == "Ior") {
415433
material.ior = std::stof(tokens[1]);

src/scene.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,18 @@ struct DevScene {
7070
mat.baseColor = textures[mat.baseColorMapId].linearSample(intersec.uv);
7171
}
7272

73+
if (mat.metallicMapId > NullTextureId) {
74+
mat.metallic = textures[mat.metallicMapId].linearSample(intersec.uv).r;
75+
}
76+
77+
if (mat.roughnessMapId > NullTextureId) {
78+
mat.roughness = textures[mat.roughnessMapId].linearSample(intersec.uv).r;
79+
}
80+
7381
if (mat.normalMapId > NullTextureId) {
7482
glm::vec3 mapped = textures[mat.normalMapId].linearSample(intersec.uv);
7583
glm::vec3 localNorm = glm::normalize(glm::vec3(mapped.x, mapped.y, mapped.z) * 1.f - 0.5f);
7684
intersec.norm = Math::localToWorld(intersec.norm, localNorm);
77-
//intersec.norm = getPrimitivePlainNormal
7885
}
7986
return mat;
8087
}

0 commit comments

Comments
 (0)