FIrst step in Physically based rendering

This commit is contained in:
2023-05-20 21:20:02 +02:00
parent 4cba6ad37e
commit 7627df0fa0
6 changed files with 313 additions and 6 deletions

View File

@ -14,11 +14,29 @@ static enum class RenderPass {
NONE = 0,
SKYBOX,
DEFAULT,
PBR
};
const int num_passes = static_cast<int>(RenderPass::DEFAULT) ;
Texture* colourAttachment;
glm::vec3 lightPositions[] = {
glm::vec3(-10.0f, 10.0f, 10.0f),
glm::vec3(10.0f, 10.0f, 10.0f),
glm::vec3(-10.0f, -10.0f, 10.0f),
glm::vec3(10.0f, -10.0f, 10.0f)
};
glm::vec3 lightColors[] = {
glm::vec3(300.0f, 300.0f, 300.0f),
glm::vec3(300.0f, 300.0f, 300.0f),
glm::vec3(300.0f, 300.0f, 300.0f),
glm::vec3(300.0f, 300.0f, 300.0f)
};
int nrRows = 7;
int nrColumns = 7;
float spacing = 2.5;
void Renderer::Setup()
{
@ -49,6 +67,8 @@ void Renderer::Setup()
shaders[static_cast<int>(RenderPass::SKYBOX)].Load("../Shaders/skybox.vs", "../Shaders/Cubemap.fs");
shaders[static_cast<int>(RenderPass::DEFAULT)] = Shader();
shaders[static_cast<int>(RenderPass::DEFAULT)].Load("../Shaders/shader.vs", "../Shaders/shader.fs");
shaders[static_cast<int>(RenderPass::PBR)] = Shader();
shaders[static_cast<int>(RenderPass::PBR)].Load("../Shaders/pbr.vs", "../Shaders/pbr.fs");
}
@ -94,10 +114,107 @@ void Renderer::resize(int width, int height ) {
}
unsigned int sphereVAO = 0;
unsigned int indexCount;
void renderSphere() {
if (sphereVAO == 0) {
glGenVertexArrays(1, &sphereVAO);
unsigned int vbo, ebo;
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
std::vector<glm::vec3> positions;
std::vector<glm::vec2> uv;
std::vector<glm::vec3> normals;
std::vector<unsigned int> indices;
const unsigned int X_SEGMENTS = 64;
const unsigned int Y_SEGMENTS = 64;
const float PI = 3.14159265359f;
for (unsigned int x = 0; x <= X_SEGMENTS; ++x) {
for (unsigned int y = 0; y <= Y_SEGMENTS; ++y) {
float xSegment = (float)x / (float)X_SEGMENTS;
float ySegment = (float)y / (float)Y_SEGMENTS;
float xPos = std::cos(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
float yPos = std::cos(ySegment * PI);
float zPos = std::sin(xSegment * 2.0f * PI) * std::sin(ySegment * PI);
positions.push_back(glm::vec3(xPos, yPos, zPos));
uv.push_back(glm::vec2(xSegment, ySegment));
normals.push_back(glm::vec3(xPos, yPos, zPos));
}
}
bool oddRow = false;
for (unsigned int y = 0; y < Y_SEGMENTS; ++y)
{
if (!oddRow) // even rows: y == 0, y == 2; and so on
{
for (unsigned int x = 0; x <= X_SEGMENTS; ++x)
{
indices.push_back(y * (X_SEGMENTS + 1) + x);
indices.push_back((y + 1) * (X_SEGMENTS + 1) + x);
}
}
else
{
for (int x = X_SEGMENTS; x >= 0; --x)
{
indices.push_back((y + 1) * (X_SEGMENTS + 1) + x);
indices.push_back(y * (X_SEGMENTS + 1) + x);
}
}
oddRow = !oddRow;
}
indexCount = static_cast<unsigned int>(indices.size());
std::vector<float> data;
for (unsigned int i = 0; i < positions.size(); ++i)
{
data.push_back(positions[i].x);
data.push_back(positions[i].y);
data.push_back(positions[i].z);
if (normals.size() > 0)
{
data.push_back(normals[i].x);
data.push_back(normals[i].y);
data.push_back(normals[i].z);
}
if (uv.size() > 0)
{
data.push_back(uv[i].x);
data.push_back(uv[i].y);
}
}
glBindVertexArray(sphereVAO);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(float), &data[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
unsigned int stride = (3 + 2 + 3) * sizeof(float);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)(6 * sizeof(float)));
}
glBindVertexArray(sphereVAO);
glDrawElements(GL_TRIANGLE_STRIP, indexCount, GL_UNSIGNED_INT, 0);
}
void Renderer::Render(Scene& scene)
{
static RenderPass currentPass = RenderPass::SKYBOX;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
@ -105,7 +222,7 @@ void Renderer::Render(Scene& scene)
auto view = scene.MainCamera.GetViewMatrix();
auto model = glm::mat4(1.0f);
std::cout << "SKYBOX PASS!" << std::endl;
// Skybox
glDepthMask(GL_FALSE);
Shader shader = shaders.at(static_cast<int>(RenderPass::SKYBOX));
@ -124,7 +241,7 @@ void Renderer::Render(Scene& scene)
glDepthMask(GL_TRUE);
std::cout << "DEFAULT PASS!" << std::endl;
// Phong lighting
shader = shaders.at(static_cast<int>(RenderPass::DEFAULT));
shader.use();
@ -135,7 +252,7 @@ void Renderer::Render(Scene& scene)
scene.skybox.Bind();
glActiveTexture(GL_TEXTURE0);
model = glm::translate(model, glm::vec3(20.0f, 0.0f, 0.0f));
shader.setMat4("model", model);
shader.setMat4("view", view);
shader.setMat4("projection", projection);
@ -143,6 +260,59 @@ void Renderer::Render(Scene& scene)
entity.Draw(shader);
}
scene.skybox.Unbind();
// PBR
shader = shaders.at(static_cast<int>(RenderPass::PBR));
shader.use();
shader.setVec3("albedo", glm::vec3(0.5f, 0.0f, 0.0f));
shader.setFloat("ao", 1.0f);
shader.setMat4("projection", projection);
view = scene.MainCamera.GetViewMatrix();
shader.setMat4("view", view);
shader.setVec3("camPos", scene.MainCamera.Position);
model = glm::mat4(1.0f);
for (int row = 0; row < nrRows; ++row) {
shader.setFloat("metallic", (float)row / (float)nrRows);
for (int col = 0; col < nrColumns; ++col) {
shader.setFloat("roughness", glm::clamp((float)col / (float)nrColumns, 0.05f, 1.0f));
model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(
(col - (nrColumns / 2)) * spacing,
(row - (nrRows / 2)) * spacing,
0.0f
));
shader.setMat4("model", model);
shader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
renderSphere();
}
}
// Render light source
for (unsigned int i = 0; i < sizeof(lightPositions) / sizeof(lightPositions[0]); ++i) {
glm::vec3 newPos = lightPositions[i] + glm::vec3(sin(glfwGetTime() * 5.0) * 5.0, 0.0, 0.0);
newPos = lightPositions[i];
shader.setVec3("lightPositions[" + std::to_string(i)+"]", newPos);
shader.setVec3("lightColors[" + std::to_string(i) + "]", lightColors[i]);
model = glm::mat4(1.0f);
model = glm::translate(model, newPos);
model = glm::scale(model, glm::vec3(0.5f));
shader.setMat4("model", model);
shader.setMat3("normalMatrix", glm::transpose(glm::inverse(glm::mat3(model))));
renderSphere();
}
/*
Shader OutlineShader;