adding texture to plants

This commit is contained in:
s473621 2024-01-31 16:09:43 +01:00
parent 00471a1b93
commit d28c6eecb9
11 changed files with 172286 additions and 7 deletions

View File

@ -66,6 +66,8 @@
<None Include="shaders\shader_5_sun.vert" />
<None Include="shaders\shader_biomes.frag" />
<None Include="shaders\shader_biomes.vert" />
<None Include="shaders\shader_pbr.frag" />
<None Include="shaders\shader_pbr.vert" />
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{3952C396-B1C6-44CD-96DD-C1AC15D32978}</ProjectGuid>

View File

@ -51,6 +51,27 @@
<ClCompile Include="src\SOIL\image_helper.c">
<Filter>Source Files\SOIL</Filter>
</ClCompile>
<ClCompile Include="src\imgui.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\imgui_demo.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\imgui_draw.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\imgui_impl_glfw.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\imgui_impl_opengl3.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\imgui_tables.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\imgui_widgets.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\objload.h">
@ -89,6 +110,33 @@
<ClInclude Include="src\ex_6_1.hpp">
<Filter>Source Files</Filter>
</ClInclude>
<ClInclude Include="src\imconfig.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imgui.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imgui_impl_glfw.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imgui_impl_opengl3.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imgui_impl_opengl3_loader.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imgui_internal.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imstb_rectpack.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imstb_textedit.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\imstb_truetype.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="shaders\shader_5_sun.frag">
@ -115,5 +163,12 @@
<None Include="shaders\shader_biomes.vert">
<Filter>Shader Files</Filter>
</None>
<None Include="packages.config" />
<None Include="shaders\shader_pbr.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_pbr.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

View File

@ -0,0 +1,12 @@
# Blender 4.0.2 MTL File: 'None'
# www.blender.org
newmtl Material.001
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd ./models/image_plant_1_1.jpg

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 123 KiB

View File

@ -0,0 +1,12 @@
# Blender 4.0.2 MTL File: 'None'
# www.blender.org
newmtl Material.001
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd ./models/red.jpg

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,51 @@
#version 430 core
float AMBIENT = 0.1;
uniform vec3 color;
uniform vec3 lightPos;
uniform sampler2D colorTexture;
in vec3 fragNormal;
in vec3 fragPosition;
in vec2 texCoords;
out vec4 outColor;
uniform float shininess;
uniform vec3 ambientColor;
uniform vec3 specularColor;
uniform vec3 emissiveColor;
uniform float opticalDensity;
uniform float dissolve;
uniform int illuminationModel;
void main()
{
vec3 lightDir = normalize(lightPos - fragPosition);
vec3 normal = normalize(fragNormal);
float diffuse = max(0.0, dot(normal, lightDir));
vec3 lambertian = texture(colorTexture, texCoords).rgb * diffuse;
vec3 viewDir = normalize(-fragPosition);
vec3 halfwayDir = normalize(lightDir + viewDir);
float specular = pow(max(0.0, dot(normal, halfwayDir)), shininess);
vec3 blinnPhong = specularColor * specular;
vec3 emissive = emissiveColor;
vec3 ambient = ambientColor * AMBIENT;
vec3 finalColor = lambertian + blinnPhong + emissive + ambient;
finalColor *= (1.0 - dissolve);
outColor = vec4(finalColor, 1.0);
}

View File

@ -0,0 +1,21 @@
#version 430 core
layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inTexCoord;
out vec2 texCoords;
out vec3 fragNormal;
out vec3 fragPosition;
uniform mat4 transformation;
uniform mat4 modelMatrix;
uniform sampler2D colorTexture;
void main()
{
gl_Position = transformation * vec4(inPosition, 1.0);
fragPosition = (modelMatrix* vec4(inPosition,1)).xyz;
texCoords = inTexCoord;
fragNormal = (modelMatrix* vec4(inNormal,0)).xyz;
}

View File

@ -22,6 +22,70 @@
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
struct Material {
float Ns; // shininess
glm::vec3 Ka; // ambient color
glm::vec3 Ks; // specular color
glm::vec3 Ke; // emissive color
float Ni; // optical density
float d; // dissolve
int illum; // illumination model
GLuint textureID; // texture ID
Material() : Ns(0.0f), Ka(0.0f), Ks(0.0f), Ke(0.0f), Ni(0.0f), d(1.0f), illum(0), textureID(0) {}
};
void loadMTLAndGetTextureID(const std::string& filePath, Material& material) {
std::ifstream file(filePath);
if (!file.is_open()) {
std::cerr << "Failed to open MTL file: " << filePath << std::endl;
return;
}
std::string line;
while (std::getline(file, line)) {
std::istringstream iss(line);
std::string token;
iss >> token;
if (token == "newmtl") {
iss >> token;
}
else if (token == "map_Kd") {
iss >> token;
material.textureID = Core::LoadTexture(token.c_str());
file.close();
return;
}
else if (token == "Ns") {
iss >> material.Ns;
}
else if (token == "Ka") {
iss >> material.Ka.r >> material.Ka.g >> material.Ka.b;
}
else if (token == "Ks") {
iss >> material.Ks.r >> material.Ks.g >> material.Ks.b;
}
else if (token == "Ke") {
iss >> material.Ke.r >> material.Ke.g >> material.Ke.b;
}
else if (token == "Ni") {
iss >> material.Ni;
}
else if (token == "d") {
iss >> material.d;
}
else if (token == "illum") {
iss >> material.illum;
}
}
file.close();
return ;
}
// Глобальные переменные для хранения параметров новой планеты
struct PlanetParams {
glm::vec3 position = glm::vec3(0.0f);
@ -112,7 +176,7 @@ namespace texture {
GLuint program;
GLuint programSun;
GLuint programTex;
GLuint program_pbr;
GLuint plantProgram;
Core::Shader_Loader shaderLoader;
@ -173,7 +237,7 @@ void renderImGui() {
ImGui::Begin("Dodawanie nowej planety");
static PlanetParams newPlanetParams;
ImGui::InputFloat3("Pozicja", &newPlanetParams.position[0]);
ImGui::InputFloat3("Pozycja", &newPlanetParams.position[0]);
ImGui::SliderFloat("Rozmiar", &newPlanetParams.size, 0.1f, 10.0f);
ImGui::SliderFloat("Wilgotnosc", &newPlanetParams.humidity, 0.0f, 10.0f); // Слайдер для влажности
ImGui::SliderFloat("Temperatura", &newPlanetParams.temperature, 0.0f, 10.0f); // Слайдер для осадков
@ -258,6 +322,29 @@ void drawObjectTexture(Core::RenderContext& context, glm::mat4 modelMatrix, GLui
glUseProgram(0);
}
void drawObjectTexture_plant(Core::RenderContext& context, glm::mat4 modelMatrix, Material& material, GLuint program) {
glUseProgram(program);
Core::SetActiveTexture(material.textureID, "colorTexture", program, 0);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
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);
//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);
Core::DrawContext(context);
glUseProgram(0);
}
void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMatrix,glm::vec3 placePoint, Core::RenderContext& planetContext, glm::mat4 planetMatrix) {
//placePoint is described by normalized vector that points to a point on a sphere from inside of itself
placePoint = glm::normalize(placePoint);
@ -293,6 +380,7 @@ void placeObjectOnPlanet(Core::RenderContext& objectContext, glm::mat4 objectMat
}
Material plant3Material;
void renderScene(GLFWwindow* window)
{
glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
@ -328,8 +416,12 @@ void renderScene(GLFWwindow* window)
float scaleFactor = 0.1f;
glm::mat4 plantModelMatrix = glm::translate(plantPosition) * glm::scale(glm::vec3(scaleFactor));
drawObjectColor(plant2Context, plantModelMatrix, glm::vec3(1,1,1), program);
drawObjectColor(plant3Context,glm::translate(glm::vec3(1.0f, 0.5f, 3.0f)) *glm::scale(glm::vec3(0.03f)) *glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)),glm::vec3(1, 1, 1), program);
//drawObjectColor(plant2Context, plantModelMatrix, glm::vec3(1,1,1), program);
// drawObjectColor(plant3Context,glm::translate(glm::vec3(1.0f, 0.5f, 3.0f)) *glm::scale(glm::vec3(0.03f)) *glm::rotate(glm::mat4(1.0f), glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)),glm::vec3(1, 1, 1), program);
loadMTLAndGetTextureID("./models/plant_1_1.mtl", plant3Material);
drawObjectTexture_plant(plant2Context, plantModelMatrix, plant3Material, program_pbr);
//drawObjectColor(plant2Context, plantModelMatrix, glm::vec3(1, 1, 1), program);
//glfwSwapBuffers(window);
}
@ -358,6 +450,9 @@ void loadModelToContext(std::string path, Core::RenderContext& context)
}
void init(GLFWwindow* window)
{
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
@ -367,12 +462,12 @@ void init(GLFWwindow* window)
programTex = shaderLoader.CreateProgram("shaders/shader_5_1_tex.vert", "shaders/shader_5_1_tex.frag");
programSun = shaderLoader.CreateProgram("shaders/shader_5_sun.vert", "shaders/shader_5_sun.frag");
programBiomes = shaderLoader.CreateProgram("shaders/shader_biomes.vert", "shaders/shader_biomes.frag");
program_pbr = shaderLoader.CreateProgram("shaders/shader_pbr.vert", "shaders/shader_pbr.frag");
loadModelToContext2("./models/plants/polygon.obj", plantContext);
loadModelToContext("./models/sphere.obj", sphereContext);
loadModelToContext2("models/plant_2.ply", plant2Context);
loadModelToContext2("models/plant_4.ply", plant3Context);
loadModelToContext("./models/plant_1_1.obj", plant2Context);
loadModelToContext2("./models/plant_4.ply", plant3Context);
texture::earth=Core::LoadTexture("textures/earth2.png");
texture::clouds = Core::LoadTexture("textures/clouds.jpg");