Moved some files around and got mapping working a bit, better texture support too

This commit is contained in:
Ben
2019-09-13 00:47:11 +01:00
parent 3385ff54c4
commit 1fbb093dd4
29 changed files with 7785 additions and 56 deletions

0
resources/lucy.obj → resources/models/lucy.obj Executable file → Normal file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 769 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 928 KiB

1
src/common.cpp Normal file
View File

@@ -0,0 +1 @@
#include "common.hpp"

View File

@@ -38,5 +38,4 @@ enum ToneMapMode {
MODE_TONEMAP_BASIC
};
#endif

View File

@@ -9,14 +9,14 @@ class Texture;
class Material {
public:
Material();
Material(glm::vec3 col, float specularity, float emmitance = 0.0f);
Material(glm::vec3 col, float specularity = 0.0f, float emmitance = 0.0f);
glm::vec3 Bounce(glm::vec3 in, glm::vec3 normal);
glm::vec3 Colour;
Texture* Tex;
Texture* NormalTexture;
Texture* GlossTexture;
Texture* Tex = nullptr;
Texture* NormalTexture = nullptr;
Texture* GlossTexture = nullptr;
float Emittance;
float Specularity; // 1.0f = perfect reflective
// float Index; // refractive index

View File

@@ -0,0 +1,66 @@
#include "texture.hpp"
#define STB_IMAGE_IMPLEMENTATION
#include "../../util/stb_image.hpp"
#include "../../util/stb_image_Write.hpp"
#include "../../common.hpp"
Texture::Texture() {
}
Texture::Texture(std::string texturePath) {
Load(texturePath);
}
void Texture::Load(std::string texturePath) {
Width = 0; Height = 0;
int channels = 0;
uint32_t* imageData = (uint32_t*)stbi_load(texturePath.c_str(), &Width, &Height, &channels, 4);
Data = new glm::vec3[Width * Height];
for (int x = 0; x < Width; x++)
for (int y = 0; y < Height; y++) {
uint32_t p = imageData[y * Width + x];
uint8_t r = (uint8_t)(p); // 0x000000FF
uint8_t g = (uint8_t)(p >> 8); // 0x0000FF00
uint8_t b = (uint8_t)(p >> 16); // 0x00FF0000
Data[y * Width + x] = {(float)r / 255.0f,
(float)g / 255.0f,
(float)b / 255.0f };
}
}
glm::vec3 Texture::Sample(glm::vec2 uv) {
return Sample(uv.x, uv.y);
}
glm::vec3 Texture::Sample(float u, float v) {
u = u * Width;
v = v * Height;
u = floor(u - floor(u / (float)Width) * (float)Width);
v = floor(v - floor(v / (float)Height) * (float)Height);
return Data[(int)(v * Width + u)];
}
glm::vec3 Texture::SampleNormal(glm::vec2 uv) {
return SampleNormal(uv.x, uv.y);
}
glm::vec3 Texture::SampleNormal(float u, float v) {
glm::vec3 c = Sample(u, v);
glm::vec3 s = { c.r * 2.0f - 1.0f, c.g * 2.0f - 1.0f, c.b * 2.0f - 1.0f };
return glm::normalize(s);
}
glm::vec3 Texture::SampleSpecular(glm::vec2 uv) {
return SampleSpecular(uv.x, uv.y);
}
glm::vec3 Texture::SampleSpecular(float u, float v) {
return {};
}

View File

@@ -2,19 +2,26 @@
#define INFERNO_DEFINITIONS_MATERIALS_TEXTURE_H_
#include "../../common.hpp"
#include "../../maths.hpp"
class Texture {
public:
Texture();
Texture(std::string texturePath);
void Load(std::string texturePath);
// if it returns a vector where all components
// equal -1, there is no texture
glm::vec3 Sample(glm::vec2 uv);
glm::vec3 Sample(float u, float v);
glm::vec3 SampleNormal(glm::vec2 uv);
glm::vec3 SampleNormal(float u, float v);
// glm::vec3 SampleSpecular (float u, float v);
private:
glm::vec3 SampleSpecular(glm::vec2 uv);
glm::vec3 SampleSpecular (float u, float v);
glm::vec3* Data;
int Width, Height;
};
#endif

View File

@@ -2,6 +2,8 @@
#define INFERNO_DEFINITIONS_PRIMATIVES_PRIMATIVE_H_
#include "../materials/material.hpp"
#include "../materials/texture.hpp"
#include "../../maths.hpp"
class Ray;
@@ -55,6 +57,15 @@ public:
virtual glm::vec3 SurfaceNormal(glm::vec3 hitPoint) = 0;
virtual glm::vec2 TexCoords(glm::vec3 hitPoint) = 0;
virtual void Translate(glm::vec3 trans) = 0;
inline glm::vec3 Normal(glm::vec3 hitPoint) {
if (material == nullptr) return SurfaceNormal(hitPoint);
if (material->NormalTexture == nullptr) return SurfaceNormal(hitPoint);
return material->NormalTexture->SampleNormal(TexCoords(hitPoint));
// combine with surface normal
}
};
#endif

View File

@@ -28,43 +28,31 @@ FrameBuffer::FrameBuffer(int xres, int yres) {
}
void FrameBuffer::SetPixelSafeNormal(int x, int y, glm::vec3 p) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
RenderNormalsTarget[y * this->XRes + x] = p;
}
RenderNormalsTarget[y * this->XRes + x] = p;
}
void FrameBuffer::SetPixelSafeAlbedo(int x, int y, glm::vec3 p) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
RenderAlbedoTarget[y * this->XRes + x] = p;
}
RenderAlbedoTarget[y * this->XRes + x] = p;
}
void FrameBuffer::SetPixelSafe(int x, int y, glm::vec3 p, int mode) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
if (m_lastActiveFrameBufferMode != mode) { ClearFramebuffer(); }
m_lastActiveFrameBufferMode = mode;
RenderTarget[y * this->XRes + x] = p;
}
if (m_lastActiveFrameBufferMode != mode) { ClearFramebuffer(); }
m_lastActiveFrameBufferMode = mode;
RenderTarget[y * this->XRes + x] = p;
}
void FrameBuffer::AddPixelSafe(int x, int y, glm::vec3 p, int mode) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
if (m_lastActiveFrameBufferMode != mode) { ClearFramebuffer(); }
m_lastActiveFrameBufferMode = mode;
RenderTarget[y * this->XRes + x] += p;
}
if (m_lastActiveFrameBufferMode != mode) { ClearFramebuffer(); }
m_lastActiveFrameBufferMode = mode;
RenderTarget[y * this->XRes + x] += p;
}
void FrameBuffer::RenderPostProcessSafe(int x, int y, glm::vec3 p) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
RenderPostProcess[y * this->XRes + x] = p;
}
RenderPostProcess[y * this->XRes + x] = p;
}
void FrameBuffer::RenderSetPixelSafe(int x, int y, uint32_t p) {
if (x >= 0 && x < this->XRes && y >= 0 && this->YRes) {
RenderData[y * this->XRes + x] = p;
}
RenderData[y * this->XRes + x] = p;
}
void FrameBuffer::PostProcess(int& spp, ToneMapMode mode, RenderMode rendermode) {

View File

@@ -126,9 +126,7 @@ void ProgressiveRenderer::Render() {
m_threadPool->RunJobsAgain();
frameEndTime = std::chrono::high_resolution_clock::now();
m_calculateTimes(frameStartTime, frameEndTime);
frameStartTime = std::chrono::high_resolution_clock::now();
}
Input();
@@ -140,8 +138,9 @@ void ProgressiveRenderer::Render() {
}
void ProgressiveRenderer::m_calculateTimes(std::chrono::high_resolution_clock::time_point frameStartTime,
std::chrono::high_resolution_clock::time_point frameEndTime) {
void ProgressiveRenderer::m_calculateTimes(std::chrono::high_resolution_clock::time_point& frameStartTime,
std::chrono::high_resolution_clock::time_point& frameEndTime) {
frameEndTime = std::chrono::high_resolution_clock::now();
m_framesRendererd++;
float frameTime = std::chrono::duration_cast<std::chrono::milliseconds>(frameEndTime - frameStartTime).count();
@@ -152,4 +151,5 @@ void ProgressiveRenderer::m_calculateTimes(std::chrono::high_resolution_clock::t
if (FrameTimes.size() > 11) FrameTimes.erase(FrameTimes.begin());
AverageFrameTime = std::accumulate(FrameTimes.begin(), FrameTimes.end(), 0.0) / FrameTimes.size();
frameStartTime = std::chrono::high_resolution_clock::now();
}

View File

@@ -41,8 +41,8 @@ public:
private:
std::mutex m_mutex;
void m_calculateTimes(std::chrono::high_resolution_clock::time_point frameStartTime,
std::chrono::high_resolution_clock::time_point frameEndTime);
void m_calculateTimes(std::chrono::high_resolution_clock::time_point& frameStartTime,
std::chrono::high_resolution_clock::time_point& frameEndTime);
int m_renderModeSelected = 0;
int m_toneMapModeSelected = 0;

View File

@@ -4,6 +4,7 @@
#include "../util/threadpool.hpp"
#include "../definitions/materials/material.hpp"
#include "../definitions/materials/texture.hpp"
#include "../definitions/materials/random.hpp"
#include "../definitions/primatives/primative.hpp"
@@ -43,7 +44,7 @@ void workerThread(RenderThreadPool* threadpool, ProgressiveRenderer* renderer, i
int depth = 0;
glm::vec3 col = renderer->m_engine->GetColour(ray, depth);
if (renderer->m_engine->Mode == MODE_RENDER_NORMALS || renderer->m_engine->Mode == MODE_RENDER_PATH_LENGTH) {
threadpool->ThreadFrameBuffer->SetPixelSafe(x, y, col, (int)renderer->m_engine->Mode);
} else if (renderer->m_engine->Mode == MODE_RENDER_PATH_BOUNCES) {
@@ -79,7 +80,7 @@ glm::vec3 RenderEngine::GetColour(Ray ray, int& depth) {
if (!didhit) return m_scene->SampleSky(ray);
glm::vec3 hitPoint = ray.origin + ray.direction * t;
glm::vec3 normal = hit->SurfaceNormal(hitPoint);
glm::vec3 normal = hit->Normal(hitPoint);
if (Mode == MODE_RENDER_NORMALS) { return GetNormalColour(normal); }
if (Mode == MODE_RENDER_PATH_LENGTH) { if (t > 255.0f) t = 255.0f; return { (float)t, (float)t, (float)t }; }

View File

@@ -12,11 +12,15 @@
#include "definitions/scene.hpp"
#include "definitions/camera.hpp"
#include "definitions/primatives/mesh.hpp"
#include "definitions/primatives/sphere.hpp"
#include "definitions/primatives/plane.hpp"
#include "definitions/primatives/triangle.hpp"
#include "definitions/materials/material.hpp"
#include "definitions/materials/texture.hpp"
class Display;
class Renderer;

7666
src/util/stb_image.hpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -19,35 +19,21 @@ int main(int argc, char** argv) {
Scene* scene = new Scene(width, height);
scene->camera = new Camera(width, height);
Sphere sphere({ 0.0f, 0.0f, -20.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }, 0.5f));
Sphere sphere({ 0.0f, 0.0f, -5.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }));
scene->objects.push_back(&sphere);
Sphere sphere1({ 0.0f, 0.0f, -20.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }, 0.5f));
scene->objects.push_back(&sphere1);
Sphere sphere2({ -3.0f, 1.0f, -10.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }, 0.5f));
scene->objects.push_back(&sphere2);
Sphere sphere3({ 6.0f, 3.0f, -13.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }, 0.5f));
scene->objects.push_back(&sphere3);
Sphere sphere4({ -1.0f, 2.0f, -10.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }, 0.5f));
scene->objects.push_back(&sphere4);
Sphere sphere5({ 2.0f, 5.0f, -15.0f }, 1.0f, new Material({ 0.817f, 0.374, 0.574 }, 0.5f));
scene->objects.push_back(&sphere5);
Sphere light({ 35.0f, 56.0f, 25.0f }, 35.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 5.0f));
scene->objects.push_back(&light);
Plane plane({ 0.0f,-1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.9f, 0.9f, 0.9f }, 0.0f));
Plane plane({ 0.0f,-1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.9f, 0.9f, 0.9f }, 0.2f));
plane.material->NormalTexture = new Texture("E:/Projects/Inferno/resources/textures/normals.png");
scene->objects.push_back(&plane);
//scene->objects.push_back(new Sphere({ 35.0f, 26.0f, 25.0f }, 15.0f, new Material({ 1.0f, 1.0f, 1.0f }, 0.0f, 5.0f)));
//scene->objects.push_back(new Sphere({-0.457001f, 0.19f, -3.53899f}, 0.02f, new Material({ 1.0f, 0.9f, 0.8f }, 0.0f, 500.0f)));
//scene->objects.push_back(new Plane( { 0.0f, -1.0f, 0.0f }, { 0.0f, -1.0f, 0.0f }, new Material({ 0.847f, 0.792f, 0.658f }, 0.2f)));
// std::vector<Triangle*> tris = LoadTrianglesBasic("E://Projects//Inferno//resources//dragon-normals.obj", "E://Projects//Inferno//resources");
//std::vector<Triangle*> tris = LoadTrianglesBasic("E://Projects//Inferno//resources//dragon-normals.obj", "E://Projects//Inferno//resources");
//std::vector<Triangle*> tris = LoadTrianglesBasic("//home//ben//programming//inferno//resources//dragon-normals.obj", "//home//ben//programming//inferno//resources//resources");
//Mesh* mesh1 = new Mesh(tris);