From e7fd0f5fa248df34a8f28ff20901c1db87d39f63 Mon Sep 17 00:00:00 2001 From: xkamikoo <58092037+xkamikoo@users.noreply.github.com> Date: Mon, 25 Jan 2021 23:50:53 +0100 Subject: [PATCH] prototype of engine lights --- grk-project.vcxproj | 2 +- shaders/shader_4_tex.frag | 9 ++++---- src/main.cpp | 47 ++++++++++++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/grk-project.vcxproj b/grk-project.vcxproj index 4fdbc9a..09917c8 100644 --- a/grk-project.vcxproj +++ b/grk-project.vcxproj @@ -57,7 +57,7 @@ Application true Unicode - v142 + v141 Application diff --git a/shaders/shader_4_tex.frag b/shaders/shader_4_tex.frag index 62ea4c9..096d4ce 100644 --- a/shaders/shader_4_tex.frag +++ b/shaders/shader_4_tex.frag @@ -3,9 +3,10 @@ struct PointLight { vec3 position; vec3 color; + float intensity; }; -#define NR_POINT_LIGHTS 2 +#define NR_POINT_LIGHTS 3 uniform vec3 objectColor; uniform vec3 lightPos; @@ -32,14 +33,14 @@ void main() vec3 V = normalize(cameraPos-fragPos); vec3 R = reflect(-lightDir,normal); - float dist = 1 + distance(fragPos, pointLights[i].position) / 50; + float dist = distance(fragPos, pointLights[i].position) / 5; float distance = (1/dist) * (1/dist); float spec = pow(max(0,dot(R,V)),2); float diff = max(0,dot(normal,normalize(lightDir))); - vec3 diffuse = pointLights[i].color * diff * distance; - vec3 specular = spec * pointLights[i].color * (1/dist); + vec3 diffuse = pointLights[i].color * diff * distance * pointLights[i].intensity; + vec3 specular = spec * pointLights[i].color * (pointLights[i].intensity/dist); vec3 texture = vec3(textureColor.x, textureColor.y, textureColor.z) * pointLights[i].color; fragColor += mix(texture,texture*diffuse+vec3(1)*specular,0.9); diff --git a/src/main.cpp b/src/main.cpp index 652f4ce..b97423c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -56,12 +56,19 @@ glm::vec3 cameraDir; glm::mat4 cameraMatrix, perspectiveMatrix; +glm::vec3 sunPos = glm::vec3(10.0f, 0.0f, -5.0f); +glm::vec3 sunPos2 = glm::vec3(25.0f, -1.0f, 10.0f); +glm::vec3 engineLight = cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0); + struct Light { glm::vec3 position; glm::vec3 color; + float intensity; }; +int engineLightTimer; + //wczytywanie skyboxa (musi byc jpg!) std::vector faces { @@ -84,7 +91,13 @@ void keyboard(unsigned char key, int x, int y) { case 'z': cameraAngle -= angleSpeed; break; case 'x': cameraAngle += angleSpeed; break; - case 'w': cameraPos += cameraDir * moveSpeed; break; + case 'w': + { + cameraPos += cameraDir * moveSpeed; + lights[2].intensity = 0.001; + engineLightTimer = 0; + break; + } case 's': cameraPos -= cameraDir * moveSpeed; break; case 'd': cameraPos += glm::cross(cameraDir, glm::vec3(0, 1, 0)) * moveSpeed; break; case 'a': cameraPos -= glm::cross(cameraDir, glm::vec3(0, 1, 0)) * moveSpeed; break; @@ -237,8 +250,7 @@ void renderScene() glUniform3f(glGetUniformLocation(programSun, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); //ustalanie pozycji słońc (lightPos) - glm::vec3 sunPos = glm::vec3(10.0f, 0.0f, -5.0f); - glm::vec3 sunPos2 = glm::vec3(25.0f, -1.0f, 10.0f); + //rysowanie słońc glm::mat4 sunModelMatrix = glm::mat4(1.0f); @@ -250,27 +262,36 @@ void renderScene() sunModelMatrix2 = glm::translate(sunModelMatrix2, sunPos2); drawObjectTexture(programSun, sphereContext, sunModelMatrix2, glm::vec3(0.9f, 0.9f, 2.0f), sunTexture); - - glUseProgram(programTex); - + + lights[0].position = sunPos; lights[1].position = sunPos2; + + lights[2].position = cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0); + lights[2].color = glm::vec3(1.0f, 0.0f, 0.0f); + for (int i = 0; i < lights.size(); i++) { std::string col = "pointLights[" + std::to_string(i) + "].color"; std::string pos = "pointLights[" + std::to_string(i) + "].position"; + std::string ins = "pointLights[" + std::to_string(i) + "].intensity"; glUniform3f(glGetUniformLocation(programTex, col.c_str()), lights[i].color.x, lights[i].color.y, lights[i].color.z); glUniform3f(glGetUniformLocation(programTex, pos.c_str()), lights[i].position.x, lights[i].position.y, lights[i].position.z); + glUniform1f(glGetUniformLocation(programTex, ins.c_str()), lights[i].intensity); } glUniform3f(glGetUniformLocation(programTex, "cameraPos"), cameraPos.x, cameraPos.y, cameraPos.z); + //rysowanie statku glm::mat4 shipModelMatrix = glm::translate(cameraPos + cameraDir * 0.6f + glm::vec3(0, -0.25f, 0)) * glm::rotate(-cameraAngle + glm::radians(90.0f), glm::vec3(0, 1, 0)) * glm::scale(glm::vec3(0.0001f)); drawFromAssimpModel(programTex, corvette, shipModelMatrix, glm::vec3(1)); + + + //rysowanie Ziemi z księżycem glm::mat4 earth = drawPlanet(time / 5.0f, sunPos*glm::vec3(1.5f,1,1), glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(-10.5f, 0.0f, -10.5f), glm::vec3(0.5f, 0.5f, 0.5f)); glm::mat4 moon = drawMoon(earth, time/2.0f, glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0, 1, 1), glm::vec3(1.5f, 1.0f, 1.0f), glm::vec3(0.3f, 0.3f, 0.3f)); @@ -278,6 +299,8 @@ void renderScene() drawObjectTexture(programTex, sphereContext, earth, glm::vec3(0.8f, 0.8f, 0.8f), earthTexture); drawObjectTexture(programTex, sphereContext, moon, glm::vec3(0.9f, 1.0f, 0.9f), moonTexture); + if (engineLightTimer < 50) engineLightTimer++; + else lights[2].intensity = 0.0001; glUseProgram(0); glutSwapBuffers(); @@ -309,15 +332,23 @@ void init() skyboxTexture = loadCubemap(faces); Light l1; - l1.position = glm::vec3(0.0f, 0.0f, 0.0f); + l1.position = sunPos; l1.color = glm::vec3(0.8f, 0.8f, 0.7f); + l1.intensity = 3; lights.push_back(l1); Light l2; - l2.position = glm::vec3(5.0f, -1.0f, 10.0f); + l2.position = sunPos2; l2.color = glm::vec3(0.5f, 0.5f, 0.5f); + l2.intensity = 3; lights.push_back(l2); + Light l3; + l3.position = engineLight; + l3.color = glm::vec3(0.5f, 0.5f, 0.5f); + l3.intensity = 0.0001; + lights.push_back(l3); + } void shutdown()