fixed plant placement for planets not on the (0,0,0)

This commit is contained in:
dompia5 2024-03-04 16:33:16 +01:00
parent 2cc420e957
commit 7652ce3e9f

View File

@ -336,21 +336,17 @@ glm::vec3 cameraDir = glm::vec3(1.f, 0.f, 0.f);
glm::vec3 sunPosition = glm::vec3(0.0f, 10.0f, 10.0f); // Позиция солнца
glm::vec3 sunColor = glm::vec3(1.0f, 1.0f, 1.0f); // Цвет солнца
glm::vec3 skyPos = glm::vec3(0.f, 0.f, 0.f);
bool refresh = true;
float skySize = 4.f;
GLuint VAO, VBO;
float currentTime = 0.0;
GLuint VAO, VBO, matricesBuffer;
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;
void DrawContextInstancedSetup(Core::RenderContext& context, std::vector<glm::mat4> transformations, std::vector<glm::mat4> Modelmatrices, int numberOfInstances, Material material, GLuint program, bool refresh)
{ VAO = context.vertexArray;
glBindVertexArray(VAO);
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, 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++) {
@ -358,7 +354,6 @@ void DrawContextInstanced(Core::RenderContext& context, std::vector<glm::mat4> t
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);
@ -367,6 +362,16 @@ void DrawContextInstanced(Core::RenderContext& context, std::vector<glm::mat4> t
glVertexAttribPointer(10 + i, 4, GL_FLOAT, GL_FALSE, mat4Size, (void*)(sizeof(glm::vec4) * i));
glVertexAttribDivisor(10 + i, 1);
}
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void DrawContextInstanced(Core::RenderContext& context, std::vector<glm::mat4> transformations, std::vector<glm::mat4> Modelmatrices, int numberOfInstances, Material material, GLuint program,bool refresh)
{
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ARRAY_BUFFER, matricesBuffer);
glUniform3f(glGetUniformLocation(program, "lightPos"), sunPosition.x, sunPosition.y, sunPosition.z);
glUniform1f(glGetUniformLocation(program, "shininess"), material.Ns);
@ -377,7 +382,6 @@ void DrawContextInstanced(Core::RenderContext& context, std::vector<glm::mat4> t
glUniform1i(glGetUniformLocation(program, "illuminationModel"), material.illum);
glUniform1f(glGetUniformLocation(program, "metallic"), 0.05);
glUniform1f(glGetUniformLocation(program, "roughness"), 0.9);
glDrawElementsInstanced(
GL_TRIANGLES, // mode
context.size, // count
@ -386,7 +390,6 @@ void DrawContextInstanced(Core::RenderContext& context, std::vector<glm::mat4> t
);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
@ -450,6 +453,7 @@ void renderImGui() {
planets.push_back(newPlanetParams);
newPlanetParams.Plant_info.clear();
newPlanetParams.Plant_type_count.clear();
refresh = true;
}
ImGui::End();
@ -678,7 +682,7 @@ void drawObjectTexture_plant(Core::RenderContext& context, glm::mat4 modelMatrix
Core::DrawContext(context);
glUseProgram(0);
}
void drawObjectTexture_plantInstanced(Core::RenderContext& context, std::vector<glm::mat4> modelMatrices, Material& material, GLuint program, int count) {
void drawObjectTexture_plantInstanced(Core::RenderContext& context, std::vector<glm::mat4> modelMatrices, Material& material, GLuint program, int count,bool refresh) {
glUseProgram(program);
Core::SetActiveTexture(material.textureID, "colorTexture", program, 0);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
@ -688,10 +692,11 @@ void drawObjectTexture_plantInstanced(Core::RenderContext& context, std::vector<
glm::mat4 transformation = viewProjectionMatrix * matrix;
transformations.push_back(transformation);
}
DrawContextInstanced(context, transformations, modelMatrices, count, material, program);
DrawContextInstancedSetup(context, transformations, modelMatrices, count, material, program, refresh);
DrawContextInstanced(context, transformations, modelMatrices, count, material, program,refresh);
glUseProgram(0);
}
void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMatrix, float scale, std::vector<glm::vec3>placePoints, PlanetParams planetParams, int count, Material material) {
void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMatrix, float scale, std::vector<glm::vec3>placePoints, PlanetParams planetParams, int count, Material material,bool refresh) {
float planetScale = planetParams.size;
glm::mat4 savedobjectMatrix;
@ -710,14 +715,13 @@ void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMat
axis = glm::cross(normalized_placePoint, base);
angle = dot(base, normalized_placePoint);
angle = acos(angle);
objectMatrix = objectMatrix * glm::rotate(objectMatrix, angle, axis) * glm::translate(base * diameter);
objectMatrix = objectMatrix * glm::translate(planetParams.position * (1 / scale));
objectMatrix = objectMatrix * glm::translate(planetParams.position * (1 / scale)) * glm::rotate(objectMatrix, angle, axis) * glm::translate(base * diameter);
//objectMatrix = objectMatrix * glm::translate(planetParams.position * (1 / scale));
matrices.push_back(objectMatrix);
}
drawObjectTexture_plantInstanced(objectContext, matrices, material, program_pbr_instanced, count);
drawObjectTexture_plantInstanced(objectContext, matrices, material, program_pbr_instanced, count,refresh);
@ -807,21 +811,22 @@ void renderScene(GLFWwindow* window)
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 transformation;
float time = glfwGetTime();
/*float currentTime = glfwGetTime();
static double lastFrameTime = 0.0;
float deltaTime = static_cast<float>(time - lastFrameTime);
lastFrameTime = time;
float deltaTime = static_cast<float>(currentTime - lastFrameTime);
lastFrameTime = currentTime;
float firstTreeDuration = 20.0f;
float secondTreeDuration = 8.0f;
float thirdTreeDuration = 5.0f;
float thirdTreeDuration = 5.0f;*/
glm::vec3 plantPosition = glm::vec3(1.f, 0.f, 3.5f);
glm::vec3 plantPosition1 = glm::vec3(1.f, 0.f, 1.5f);
glm::vec3 plantPosition2 = glm::vec3(1.f, 0.f, 0.5f);
glm::vec3 plantPosition3 = glm::vec3(1.f, 0.f, -0.5f);
float scaleFactor = 0.025f;
glm::mat4 plantModelMatrix = glm::translate(plantPosition) * glm::scale(glm::vec3(scaleFactor));
renderSun();
//float scaleFactor = 0.025f;
//glm::mat4 plantModelMatrix = glm::translate(plantPosition) * glm::scale(glm::vec3(scaleFactor));
for (const auto& planet : planets) {
glm::mat4 modelMatrix = glm::translate(planet.position) * glm::scale(glm::vec3(planet.size));
drawObjectTexture(sphereContext, modelMatrix, planet.texture, programTex);
@ -841,7 +846,7 @@ void renderScene(GLFWwindow* window)
current_type_plant_positions.push_back(std::get<1>(plants[iterator]));
iterator += 1;
}
placeObjectOnPlanet(plant_specimens[i].modelContext, glm::mat4(), 0.2, current_type_plant_positions, planet, plant_type_count, plant_specimens[0].material);
placeObjectOnPlanet(plant_specimens[i].modelContext, glm::mat4(), 0.2, current_type_plant_positions, planet, plant_type_count, plant_specimens[0].material,refresh);
}
current_type_plant_positions.clear();
@ -863,6 +868,7 @@ void renderScene(GLFWwindow* window)
renderImGui();
glfwSwapBuffers(window);
refresh = false;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{