From b6ab24f75bee60cba243e0d6108746cf7a585254 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Thu, 29 Apr 2021 21:50:21 +0300 Subject: [PATCH] beginning of shadow sample --- CMakeLists.txt | 3 ++ light.cpp | 20 ++++++------- shadowSample.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ texture.frag | 2 +- 4 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 shadowSample.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d0071d..f6374f1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,4 +88,7 @@ if(BUILD_SAMPLES) add_executable(lightSample lightSample.cpp) target_link_libraries(lightSample ${LIB_NAME}) + + add_executable(shadowSample shadowSample.cpp) + target_link_libraries(shadowSample ${LIB_NAME}) endif() diff --git a/light.cpp b/light.cpp index 0ee537b..9b18bfc 100755 --- a/light.cpp +++ b/light.cpp @@ -102,7 +102,9 @@ namespace vb01{ void Light::renderShadow(std::vector descendants, mat4 &proj, mat4 &view){ Root *root = Root::getSingleton(); - Vector3 position = node->getPosition(), direction = node->getGlobalAxis(2); + Vector3 position = node->getPosition(), + upDir = node->getGlobalAxis(1), + direction = node->getGlobalAxis(2); glViewport(0, 0, depthMapSize, depthMapSize); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); @@ -122,22 +124,18 @@ namespace vb01{ mat4 model = translate(mat4(1.f), vec3(pos.x, pos.y, pos.z)); model = rotate(model, rot.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z)); - vec3 lightPos, dir = vec3(direction.x, direction.y, direction.z); + vec3 lightPos = vec3(position.x, position.y, position.z), + dir = vec3(direction.x, direction.y, direction.z), + up = vec3(upDir.x, upDir.y, upDir.z); + view = lookAt(lightPos, lightPos + dir, up); + if(type == DIRECTIONAL){ - lightPos = vec3(pos.x, pos.y, pos.z) - .5f * farPlane * dir; float orthoCorner = 10.f; proj = ortho(-orthoCorner, orthoCorner, -orthoCorner, orthoCorner, nearPlane, farPlane); } - else{ - lightPos = vec3(position.x, position.y, position.z); + else proj = perspective(radians(90.f), 1.f, nearPlane, farPlane); - } - Vector3 upDir = direction.cross(Vector3(0, 1, 0)); - if(upDir == Vector3::VEC_ZERO) - upDir = Vector3::VEC_I; - vec3 up = vec3(upDir.x, upDir.y, upDir.z); - view = lookAt(lightPos, lightPos + dir, up); depthMapShader->use(); depthMapShader->setBool(type == POINT, "point"); diff --git a/shadowSample.cpp b/shadowSample.cpp new file mode 100644 index 0000000..8f81b6d --- /dev/null +++ b/shadowSample.cpp @@ -0,0 +1,74 @@ +#include "root.h" +#include "box.h" +#include "material.h" +#include "node.h" +#include "light.h" +#include "model.h" +#include "animation.h" +#include "animationController.h" +#include "animationChannel.h" + +using namespace std; +using namespace vb01; + +int main(){ + const string PATH = "../", TEX_PATH = PATH + "samples/textures/", MODEL_PATH = PATH + "samples/models/"; + string skyboxTextures[] = { + TEX_PATH + "top.jpg", + TEX_PATH + "bottom.jpg", + TEX_PATH + "left.jpg", + TEX_PATH + "right.jpg", + TEX_PATH + "front.jpg", + TEX_PATH + "back.jpg" + }; + + Root *root = Root::getSingleton(); + root->start(800, 600, PATH, "Shadow sample"); + root->createSkybox(skyboxTextures); + Node *rootNode = root->getRootNode(); + + Camera *cam = root->getCamera(); + cam->setPosition(Vector3(0, 1, 1) * 20); + cam->lookAt(Vector3(0, -1, -1).norm(), Vector3(0, 1, -1).norm()); + + Model *box = new Model(MODEL_PATH + "platform.vb"); + Material *mat = new Material(); + mat->setTexturingEnabled(true); + mat->setLightingEnabled(true); + mat->setNormalMapEnabled(false); + mat->setSpecularMapEnabled(false); + string im0[] = {TEX_PATH + "bricks.jpg"}; + Texture *diffuseTex = new Texture(im0, 1); + mat->addDiffuseMap(diffuseTex); + box->setMaterial(mat); + rootNode->attachChild(box); + + { + Model *box = new Model(MODEL_PATH + "jet00.obj"); + Material *mat = new Material(); + mat->setTexturingEnabled(true); + mat->setLightingEnabled(true); + mat->setNormalMapEnabled(false); + mat->setSpecularMapEnabled(false); + string im0[] = {TEX_PATH + "defaultTexture.jpg"}; + Texture *diffuseTex = new Texture(im0, 1); + mat->addDiffuseMap(diffuseTex); + box->setMaterial(mat); + rootNode->attachChild(box); + box->setCastShadow(true); + box->setPosition(Vector3(0, 3, 0)); + } + + Light *light = new Light(Light::DIRECTIONAL); + light->setColor(Vector3(1, 1, 1)); + Node *lightNode = new Node(Vector3::VEC_ZERO, Quaternion::QUAT_W, Vector3::VEC_IJK, "", new AnimationController()); + lightNode->addLight(light); + rootNode->attachChild(lightNode); + lightNode->setOrientation(Quaternion(1.57, Vector3(1, 0, 0))); + lightNode->setPosition(Vector3(0, 5, -5)); + + while(true) + root->update(); + + return 0; +} diff --git a/texture.frag b/texture.frag index e3a379b..28091e3 100755 --- a/texture.frag +++ b/texture.frag @@ -126,11 +126,11 @@ void main(){ } /* +*/ for(int i = 0; i < numLights; i++){ float shadow = getShadow(i); diffuseCol *= (1 - shadow); } -*/ if(environmentMapEnabled){ vec3 projDir = normalize(fragPos - camPos); projDir = reflect(projDir, norm);