pbr connected

This commit is contained in:
dompia5 2024-02-07 22:51:50 +01:00
parent ad3be0a00b
commit 50f790fbe7
1 changed files with 69 additions and 19 deletions

View File

@ -86,7 +86,21 @@ void loadMTLAndGetTextureID(const std::string& filePath, Material& material) {
}
void loadModelToContext(std::string path, Core::RenderContext& context)
{
Assimp::Importer import;
const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
return;
}
context.initFromAssimpMesh(scene->mMeshes[0]);
}
void loadModelToContext2(std::string path, Core::RenderContext& context)
{
@ -115,7 +129,7 @@ namespace Plant
this->tempSD = tempSD;
this->name = name;
this->fileName = fileName;
loadModelToContext2(fileName, modelContext);
loadModelToContext(fileName, modelContext);
this->pos = glm::vec3(1,0,0);
//this->pos = glm::vec3((rand() % 100) / 100, (rand() % 100) / 100, (rand() % 100) / 100);
@ -258,6 +272,56 @@ glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f);
GLuint VAO,VBO;
glm::mat4 planetMatrix = glm::mat4();
void DrawContextInstanced(Core::RenderContext& context, std::vector<glm::mat4> transformations, std::vector<glm::mat4> Modelmatrices, int numberOfInstances,Material material, GLuint program)
{
GLuint VAO = context.vertexArray;
glBindVertexArray(VAO);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, transformations.size() * sizeof(glm::mat4), transformations.data(), GL_STATIC_DRAW);
std::size_t mat4Size = sizeof(glm::mat4);
for (int i = 0; i < 4; i++) {
glEnableVertexAttribArray(6 + i);
glVertexAttribPointer(6 + i, 4, GL_FLOAT, GL_FALSE, mat4Size, (void*)(sizeof(glm::vec4) * i));
glVertexAttribDivisor(6 + i, 1);
}
GLuint matricesBuffer;
glGenBuffers(1, &matricesBuffer);
glBindBuffer(GL_ARRAY_BUFFER, matricesBuffer);
glBufferData(GL_ARRAY_BUFFER, Modelmatrices.size() * sizeof(glm::mat4), Modelmatrices.data(), GL_STATIC_DRAW);
for (int i = 0; i < 4; i++) {
glEnableVertexAttribArray(10 + i);
glVertexAttribPointer(10 + i, 4, GL_FLOAT, GL_FALSE, mat4Size, (void*)(sizeof(glm::vec4) * i));
glVertexAttribDivisor(10 + i, 1);
}
glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0);
//Material
glUniform1f(glGetUniformLocation(program, "shininess"), material.Ns);
glUniform3f(glGetUniformLocation(program, "ambientColor"), material.Ka.r, material.Ka.g, material.Ka.b);
glUniform3f(glGetUniformLocation(program, "specularColor"), material.Ks.r, material.Ks.g, material.Ks.b);
glUniform3f(glGetUniformLocation(program, "emissiveColor"), material.Ke.r, material.Ke.g, material.Ke.b);
glUniform1f(glGetUniformLocation(program, "opticalDensity"), material.Ni);
//glUniform1f(glGetUniformLocation(program, "dissolve"), material.d);
glUniform1i(glGetUniformLocation(program, "illuminationModel"), material.illum);
glUniform1f(glGetUniformLocation(program, "metallic"), 0.05);
glUniform1f(glGetUniformLocation(program, "roughness"), 0.2);
glDrawElementsInstanced(
GL_TRIANGLES, // mode
context.size, // count
GL_UNSIGNED_INT, // type
(void*)0, numberOfInstances // element array buffer offset
);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
float aspectRatio = 1.f;
bool DoTheImportThing(const std::string& pFile) {
@ -392,7 +456,7 @@ void drawObjectTexture_plant(Core::RenderContext& context, glm::mat4 modelMatrix
glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, (float*)&transformation);
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, (float*)&modelMatrix);
glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 0, 0);
glUniform3f(glGetUniformLocation(program, "lightPos"), 0, 20, 0);
//Material
glUniform1f(glGetUniformLocation(program, "shininess"), material.Ns);
@ -429,7 +493,7 @@ void drawObjectTexture_plantInstanced(Core::RenderContext& context, std::vector<
//glUniform1f(glGetUniformLocation(program, "metallic"), 0.05);
//glUniform1f(glGetUniformLocation(program, "roughness"), 0.2);
Core::DrawContextInstanced(context, transformations,modelMatrices,count,program);
DrawContextInstanced(context, transformations,modelMatrices,count,material,program);
glUseProgram(0);
}
void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMatrix,float scale,std::vector<glm::vec3>placePoints, PlanetParams planetParams,int count) {
@ -458,7 +522,7 @@ void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMat
drawObjectTexture_plantInstanced(objectContext, matrices, plant3Material, program_pbr_instanced,count);
drawObjectTexture_plantInstanced(objectContext, matrices, plant2_1Material, program_pbr_instanced,count);
@ -611,21 +675,7 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height)
//TODO :REMOVE THIS
void loadModelToContext(std::string path, Core::RenderContext& context)
{
Assimp::Importer import;
const aiScene* scene = import.ReadFile(path, aiProcess_Triangulate | aiProcess_CalcTangentSpace);
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
{
std::cout << "ERROR::ASSIMP::" << import.GetErrorString() << std::endl;
return;
}
context.initFromAssimpMesh(scene->mMeshes[0]);
}
@ -666,7 +716,7 @@ void init(GLFWwindow* window)
loadMTLAndGetTextureID("./models/plant_1_1.mtl", plant3Material);
loadMTLAndGetTextureID("./models/plant_2_1_small.mtl", plant2_1Material);
plant_specimens.push_back(Plant::Plant(1, 0.99, 1, 1, "plant1_s", "./models/plant_1_1.obj"));
plant_specimens.push_back(Plant::Plant(1, 0.99, 1, 1, "plant1_s", "./models/plant_1_1_zmn.obj"));
plant_specimens.push_back(Plant::Plant(1, 0.95, 1, 1, "plant1_m", "./models/plant_1_1.obj"));
plant_specimens.push_back(Plant::Plant(1, 0.93, 1, 1, "plant1_l", "./models/plant_1_1.obj"));
//plant_specimens.push_back(Plant::Plant(1, 0.90, 1, 1, "testPlantCCCCCCCCCCC", "./models/plant_4.ply"));