From 1e913c61e6fa209aa8356b888f97bbfc190029c0 Mon Sep 17 00:00:00 2001 From: Pawel Felcyn Date: Sat, 20 Jan 2024 21:01:14 +0100 Subject: [PATCH] create multiple suns --- grk/project/shaders/shader_9_1.frag | 38 +++++++------------ grk/project/shaders/shader_9_1.vert | 16 ++++---- grk/project/src/Render_Utils.cpp | 26 ++++++------- grk/project/src/Render_Utils.h | 1 - grk/project/src/ex_9_1.hpp | 58 ++++++++++++++--------------- 5 files changed, 62 insertions(+), 77 deletions(-) diff --git a/grk/project/shaders/shader_9_1.frag b/grk/project/shaders/shader_9_1.frag index 595bdc7..e208a8e 100644 --- a/grk/project/shaders/shader_9_1.frag +++ b/grk/project/shaders/shader_9_1.frag @@ -9,11 +9,8 @@ uniform vec3 cameraPos; uniform vec3 color; -//uniform vec3 sunDir; -//uniform vec3 sunColor; - -uniform vec3 lightPos; -uniform vec3 lightColor; +uniform vec3 lightPositions[100]; +uniform vec3 lightColors[100]; uniform vec3 spotlightPos; uniform vec3 spotlightColor; @@ -32,7 +29,7 @@ out vec4 outColor; in vec3 viewDirTS; -in vec3 lightDirTS; +in vec3 lightDirTS[4]; in vec3 spotlightDirTS; in vec3 sunDirTS; @@ -74,13 +71,11 @@ vec3 fresnelSchlick(float cosTheta, vec3 F0){ vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){ float diffuse=max(0,dot(normal,lightDir)); - //vec3 V = normalize(cameraPos-worldPos); vec3 F0 = vec3(0.04); F0 = mix(F0, color, metallic); vec3 H = normalize(V + lightDir); - // cook-torrance brdf float NDF = DistributionGGX(normal, H, roughness); float G = GeometrySmith(normal, V, lightDir, roughness); vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0); @@ -93,7 +88,6 @@ vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){ float denominator = 4.0 * max(dot(normal, V), 0.0) * max(dot(normal, lightDir), 0.0) + 0.0001; vec3 specular = numerator / denominator; - // add to outgoing radiance Lo float NdotL = max(dot(normal, lightDir), 0.0); return (kD * color / PI + specular) * radiance * NdotL; } @@ -101,35 +95,29 @@ vec3 PBRLight(vec3 lightDir, vec3 radiance, vec3 normal, vec3 V){ void main() { - //vec3 normal = vec3(0,0,1); vec3 normal = normalize(vecNormal); - //vec3 viewDir = normalize(viewDirTS); vec3 viewDir = normalize(cameraPos-worldPos); - //vec3 lightDir = normalize(lightDirTS); - vec3 lightDir = normalize(lightPos-worldPos); + vec3 lightDirs[4]; + vec3 ambient = AMBIENT * color; + vec3 ilumination = ambient; + for (int i = 0; i < 100; ++i) { + lightDirs[i] = normalize(lightPositions[i] - worldPos); + vec3 attenuatedlightColor = lightColors[i] / pow(length(lightPositions[i] - worldPos), 2); + ilumination = ilumination+PBRLight(lightDirs[i], attenuatedlightColor * 300, normal, viewDir); + } - vec3 ambient = AMBIENT*color; - vec3 attenuatedlightColor = lightColor/pow(length(lightPos-worldPos),2); - vec3 ilumination; - ilumination = ambient+PBRLight(lightDir,attenuatedlightColor * 300,normal,viewDir); - //flashlight - //vec3 spotlightDir= normalize(spotlightDirTS); + vec3 spotlightDir= normalize(spotlightPos-worldPos); float angle_atenuation = clamp((dot(-normalize(spotlightPos-worldPos),spotlightConeDir)-0.5)*3,0,1); - attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2); + vec3 attenuatedlightColor = angle_atenuation*spotlightColor/pow(length(spotlightPos-worldPos),2); ilumination=ilumination+PBRLight(spotlightDir,attenuatedlightColor,normal,viewDir); - //sun - //ilumination=ilumination+PBRLight(sunDir,sunColor,normal,viewDir); - outColor = vec4(vec3(1.0) - exp(-ilumination*exposition),1); - //outColor = vec4(roughness,metallic,0,1); - //outColor = vec4(test; } diff --git a/grk/project/shaders/shader_9_1.vert b/grk/project/shaders/shader_9_1.vert index b125b24..35f6edf 100644 --- a/grk/project/shaders/shader_9_1.vert +++ b/grk/project/shaders/shader_9_1.vert @@ -12,15 +12,13 @@ uniform mat4 modelMatrix; out vec3 vecNormal; out vec3 worldPos; -uniform vec3 lightPos; +uniform vec3 lightsPositions[100]; uniform vec3 spotlightPos; uniform vec3 cameraPos; -//uniform vec3 sunDir; out vec3 viewDirTS; -out vec3 lightDirTS; +out vec3 lightDirTS[100]; out vec3 spotlightDirTS; -//out vec3 sunDirTS; void main() { @@ -34,10 +32,12 @@ void main() vec3 V = normalize(cameraPos-worldPos); viewDirTS = TBN*V; - vec3 L = normalize(lightPos-worldPos); - lightDirTS = TBN*L; + + for (int i = 0; i < 100; ++i) { + vec3 L = normalize(lightsPositions[i]-worldPos); + lightDirTS[i] = TBN*L; + } + vec3 SL = normalize(spotlightPos-worldPos); spotlightDirTS = TBN*SL; - //sunDirTS = TBN*sunDir; - } diff --git a/grk/project/src/Render_Utils.cpp b/grk/project/src/Render_Utils.cpp index 4956fb7..739d977 100644 --- a/grk/project/src/Render_Utils.cpp +++ b/grk/project/src/Render_Utils.cpp @@ -166,22 +166,20 @@ void Core::drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, gl glUniform3f(glGetUniformLocation(program, "cameraPos"), spaceship->cameraPos.x, spaceship->cameraPos.y, spaceship->cameraPos.z); - - const int NUM_LIGHTS = 23; - - glm::vec3 lightsPositions[NUM_LIGHTS]; - glm::vec3 lightsColors[NUM_LIGHTS]; - glm::vec3 lightsDirections[NUM_LIGHTS]; - - /*glUniform3f(glGetUniformLocation(program, "sunDir"), sun.sunDir.x, sun.sunDir.y, sun.sunDir.z); - glUniform3f(glGetUniformLocation(program, "sunColor"), sun.sunColor.x, sun.sunColor.y, sun.sunColor.z);*/ - std::list* suns = GameUtils::getInstance()->getSuns(); - glm::vec3 firstSunPos = suns->front()->getPosition(); - glm::vec3 firstSunColor = suns->front()->getColor(); - glUniform3f(glGetUniformLocation(program, "lightPos"), firstSunPos.x, firstSunPos.y, firstSunPos.z); - glUniform3f(glGetUniformLocation(program, "lightColor"), firstSunColor.x, firstSunColor.y, firstSunColor.z); + glm::vec3 lightsPositions[100]; + glm::vec3 lightsColors[100]; + + int i = 0; + for (Sun* sun : *suns) { + lightsPositions[i] = sun->getPosition(); + lightsColors[i] = sun->getColor(); + ++i; + } + + glUniform3fv(glGetUniformLocation(program, "lightPositions"), 100, glm::value_ptr(lightsPositions[0])); + glUniform3fv(glGetUniformLocation(program, "lightColors"), 100, glm::value_ptr(lightsColors[0])); glUniform3f(glGetUniformLocation(program, "spotlightConeDir"), spaceship->spotlightConeDir.x, spaceship->spotlightConeDir.y, spaceship->spotlightConeDir.z); glUniform3f(glGetUniformLocation(program, "spotlightPos"), spaceship->spotlightPos.x, spaceship->spotlightPos.y, spaceship->spotlightPos.z); diff --git a/grk/project/src/Render_Utils.h b/grk/project/src/Render_Utils.h index 5490791..c537f37 100644 --- a/grk/project/src/Render_Utils.h +++ b/grk/project/src/Render_Utils.h @@ -73,6 +73,5 @@ namespace Core glm::mat4 createPerspectiveMatrix(); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, glm::vec3 color, float roughness, float metallic, GLuint program); - void drawFireball(const glm::mat4& model, float radius, const glm::mat4& view, const glm::mat4& cameraMatrix); void loadModelToContext(std::string path, Core::RenderContext& context); } \ No newline at end of file diff --git a/grk/project/src/ex_9_1.hpp b/grk/project/src/ex_9_1.hpp index 280f606..0aec084 100644 --- a/grk/project/src/ex_9_1.hpp +++ b/grk/project/src/ex_9_1.hpp @@ -36,6 +36,8 @@ namespace texture { GLuint cubemapTexture; } +void createGalaxy(glm::vec3 galaxyPosition); + GLuint depthMapFBO; GLuint depthMap; @@ -58,7 +60,7 @@ float lastTime = -1.f; float deltaTime = 0.f; Spaceship* spaceship = Spaceship::getInstance(); - +void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef); void updateDeltaTime(float time) { if (lastTime < 0) { lastTime = time; @@ -130,35 +132,32 @@ void framebuffer_size_callback(GLFWwindow* window, int width, int height) void createSuns() { - GameUtils* gu = GameUtils::getInstance(); - sun = new Sun(programSun, models::sphereContext, glm::vec3(0, 2, 0), glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, 1); - Planet* planet = new Planet(sun, 20.f, 0.25f, 1.f, models::sphereContext); - planets.push_back(planet); - Planet* moon = new Planet(planet, 5.f, 1.f, 0.2f, models::sphereContext); - planets.push_back(moon); - gu->getSuns()->push_back(sun); - Planet* planet2 = new Planet(sun, 50.f, 0.5f, 1.5f, models::sphereContext); - planets.push_back(planet2); - /*suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-80, 20, 50), glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(1.0f, 0.8f, 0.2f), 4.0)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(50, 40, -30), glm::vec3(-0.73633f, 0.451106, 0.023226f), glm::vec3(0.9f, 0.5f, 0.1f), 3.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(0, -60, 100), glm::vec3(-0.53633f, 0.551106, 0.043226f), glm::vec3(0.8f, 0.2f, 0.2f), 4.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(20, 80, -70), glm::vec3(0.33633f, 0.651106, 0.063226f), glm::vec3(0.5f, 0.7f, 0.9f), 3.8)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-60, -20, 30), glm::vec3(-0.23633f, 0.751106, 0.083226f), glm::vec3(0.7f, 0.2f, 0.7f), 4.2)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(70, -50, -80), glm::vec3(-0.03633f, 0.851106, 0.103226f), glm::vec3(0.1f, 0.3f, 0.9f), 3.9)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(10, 30, 60), glm::vec3(0.13633f, -0.951106, -0.123226f), glm::vec3(0.6f, 0.9f, 0.4f), 4.3)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-40, -80, -50), glm::vec3(0.33633f, -0.851106, -0.143226f), glm::vec3(0.7f, 0.5f, 0.2f), 3.7)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(90, 70, 20), glm::vec3(0.53633f, -0.751106, -0.163226f), glm::vec3(0.4f, 0.1f, 0.9f), 4.1)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-30, 10, -40), glm::vec3(0.73633f, -0.651106, -0.183226f), glm::vec3(0.8f, 0.4f, 0.1f), 4.4)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(150, -100, 80), glm::vec3(0.33633f, -0.951106, -0.143226f), glm::vec3(0.2f, 0.8f, 0.5f), 3.9)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-120, 50, -60), glm::vec3(0.73633f, 0.551106, 0.103226f), glm::vec3(0.9f, 0.2f, 0.7f), 3.6)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(0, 120, -150), glm::vec3(-0.23633f, -0.751106, 0.083226f), glm::vec3(0.5f, 0.6f, 0.9f), 3.4)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-180, -50, 120), glm::vec3(0.13633f, 0.351106, -0.003226f), glm::vec3(0.7f, 0.9f, 0.2f), 4.2)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(60, -150, 180), glm::vec3(-0.93633f, -0.451106, -0.023226f), glm::vec3(0.3f, 0.7f, 0.8f), 3.8)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(100, 80, -120), glm::vec3(0.53633f, -0.651106, 0.163226f), glm::vec3(0.8f, 0.1f, 0.4f), 4.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-50, -180, 150), glm::vec3(-0.33633f, 0.951106, -0.083226f), glm::vec3(0.4f, 0.8f, 0.6f), 3.5)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(200, 150, -100), glm::vec3(0.03633f, 0.151106, 0.103226f), glm::vec3(0.6f, 0.2f, 0.9f), 3.9)); - suns.push_back(&Sun(programSun, models::sphereContext, glm::vec3(-150, -100, -50), glm::vec3(0.83633f, -0.251106, -0.123226f), glm::vec3(0.7f, 0.5f, 0.8f), 4.1));*/ + createGalaxy(glm::vec3(0.f)); + createGalaxy(glm::vec3(0.f, 50.f, 200.f)); } + +void createGalaxy(glm::vec3 galaxyPosition) { + float planetsSizes[] = { 1.f, 1.5f, 0.8f, 1.2f, 0.2f }; + createSolarSystem(galaxyPosition + glm::vec3(0, 2, 0), 3, planetsSizes, 5, 15.f, 0.2f); + float planetsSizes2[] = { 0.6f, 1.1f, 0.9f }; + createSolarSystem(galaxyPosition + glm::vec3(150, 5, 0), 2, planetsSizes2, 3, 15.f, 0.2f); + float planetsSizes3[] = { 0.7f, 1.5f, 1.2f, 1.f }; + createSolarSystem(galaxyPosition + glm::vec3(-20, -30, 50), 4, planetsSizes3, 4, 20.f, 0.2f); + float planetSizes4[] = { 1.f, 0.5f }; + createSolarSystem(galaxyPosition + glm::vec3(100, 20, -50), 5, planetsSizes3, 2, 25.f, 0.2f); +} + +void createSolarSystem(glm::vec3 sunPos, float sunScale, float* planetSizes, int numberOfPlanets, float planetsDistance, float planetSpeedCoef) { + GameUtils* gu = GameUtils::getInstance(); + sun = new Sun(programSun, models::sphereContext, sunPos, glm::vec3(-0.93633f, 0.351106, 0.003226f), glm::vec3(0.9f, 0.9f, 0.7f) * 5, sunScale); + gu->getSuns()->push_back(sun); + for (int i = 0; i < numberOfPlanets; i++) { + float distanceFromSum = (i + 1) * planetsDistance; + Planet* planet = new Planet(sun, distanceFromSum, i * 0.1f + planetSpeedCoef, planetSizes[i], models::sphereContext); + planets.push_back(planet); + } +} + void init(GLFWwindow* window) { GameUtils* gameUtils = GameUtils::getInstance(); @@ -183,6 +182,7 @@ void init(GLFWwindow* window) "bkg2_top3.png", "bkg2_bottom4.png", "bkg2_front5.png", + "bkg2_back6.png" }; texture::cubemapTexture = Core::LoadCubemap(cubeFaces); -- 2.20.1