diff --git a/light.cpp b/light.cpp index 9b18bfc..561aadc 100755 --- a/light.cpp +++ b/light.cpp @@ -51,7 +51,7 @@ namespace vb01{ } void Light::initDepthMap(){ - string basePath = "../../vb01/depthMap."; + string basePath = Root::getSingleton()->getLibPath() + "depthMap."; glGenFramebuffers(1, &depthmapFBO); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); @@ -69,7 +69,7 @@ namespace vb01{ glReadBuffer(GL_NONE); if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) cout << "Not complete\n"; - glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindFramebuffer(GL_FRAMEBUFFER, *(Root::getSingleton()->getFBO())); } void Light::update(){ @@ -106,9 +106,24 @@ namespace vb01{ upDir = node->getGlobalAxis(1), direction = node->getGlobalAxis(2); + 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){ + float orthoCorner = 10.f; + proj = ortho(-orthoCorner, orthoCorner, -orthoCorner, orthoCorner, nearPlane, farPlane); + } + else + proj = perspective(radians(90.f), 1.f, nearPlane, farPlane); + + depthMapShader->use(); + depthMapShader->setBool(type == POINT, "point"); + glViewport(0, 0, depthMapSize, depthMapSize); glBindFramebuffer(GL_FRAMEBUFFER, depthmapFBO); - glClear(GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); for(Node *n : descendants){ vector meshes = n->getMeshes(); @@ -124,24 +139,8 @@ 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 = 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){ - float orthoCorner = 10.f; - proj = ortho(-orthoCorner, orthoCorner, -orthoCorner, orthoCorner, nearPlane, farPlane); - } - else - proj = perspective(radians(90.f), 1.f, nearPlane, farPlane); - - - depthMapShader->use(); - depthMapShader->setBool(type == POINT, "point"); depthMapShader->setMat4(model, "model"); - depthMapShader->setFloat(farPlane, "farPlane"); - depthMapShader->setVec3(position, "lightPos"); + if(type == POINT){ vec3 dirs[] = {vec3(1, 0, 0), vec3(-1, 0, 0), vec3(0, 1, 0), vec3(0, -1, 0), vec3(0, 0, 1), vec3(0, 0, -1)}; for(int i = 0; i < 6; i++){ @@ -152,6 +151,9 @@ namespace vb01{ upVec = vec3(0, -1, 0); depthMapShader->setMat4(proj * lookAt(lightPos, lightPos + dirs[i], upVec), "shadowMat[" + to_string(i) + "]"); } + + depthMapShader->setFloat(farPlane, "farPlane"); + depthMapShader->setVec3(position, "lightPos"); } else depthMapShader->setMat4(proj * view, "lightMat"); @@ -160,6 +162,7 @@ namespace vb01{ } } } + glBindFramebuffer(GL_FRAMEBUFFER, *(root->getFBO())); glViewport(0, 0, root->getWidth(), root->getHeight()); } @@ -173,9 +176,8 @@ namespace vb01{ shader->setVec3(color, "light[" + to_string(thisId) + "].color"); shader->setFloat(nearPlane, "light[" + to_string(thisId) + "].near"); shader->setFloat(farPlane, "light[" + to_string(thisId) + "].far"); - shader->setInt(12, "light[" + to_string(thisId) + "].depthMapCube"); - shader->setInt(13, "light[" + to_string(thisId) + "].depthMap"); - depthMap->select(type == POINT ? 12 : 13); + shader->setInt(12, "light[" + to_string(thisId) + "].depthMap" + (type == POINT ? "Cube" : "")); + depthMap->select(12); switch(type){ case POINT: diff --git a/root.cpp b/root.cpp index ba64849..2cf3f4a 100755 --- a/root.cpp +++ b/root.cpp @@ -149,9 +149,6 @@ namespace vb01{ } void Root::updateBloomFramebuffer(){ - Material *material = guiPlane->getMaterial(); - Shader *shader = material->getShader(); - bool horizontal = false; blurShader->use(); blurShader->setVec2(Vector2(width, height), "screen"); @@ -159,7 +156,7 @@ namespace vb01{ glBindFramebuffer(GL_FRAMEBUFFER, pingpongBuffers[horizontal]); blurShader->setBool(horizontal, "horizontal"); if(i == 0) - glBindTexture(GL_TEXTURE_2D, *(material->getDiffuseMap(1)->getTexture())); + glBindTexture(GL_TEXTURE_2D, *(guiPlane->getMaterial()->getDiffuseMap(1)->getTexture())); else pingPongTextures[!horizontal]->select(); guiPlane->render(); @@ -167,6 +164,9 @@ namespace vb01{ } glBindFramebuffer(GL_FRAMEBUFFER, 0); + Material *material = guiPlane->getMaterial(); + Shader *shader = material->getShader(); + shader->use(); shader->setVec2(Vector2(width, height), "screen"); shader->setBool(bloom, "bloom"); diff --git a/shadowSample.cpp b/shadowSample.cpp index 8f81b6d..0a29d46 100644 --- a/shadowSample.cpp +++ b/shadowSample.cpp @@ -31,18 +31,34 @@ int main(){ 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"); + Box *box = new Box(Vector3(40, .2, 40)); + Node *boxNode = new Node(); 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); + boxNode->attachMesh(box); + rootNode->attachChild(boxNode); + { + Box *box = new Box(Vector3(4, .2, 4)); + Node *boxNode = new Node(); + Material *mat = new Material(); + mat->setTexturingEnabled(true); + mat->setLightingEnabled(true); + string im0[] = {TEX_PATH + "defaultTexture.jpg"}; + Texture *diffuseTex = new Texture(im0, 1); + mat->addDiffuseMap(diffuseTex); + box->setMaterial(mat); + boxNode->attachMesh(box); + rootNode->attachChild(boxNode); + box->setCastShadow(true); + boxNode->setPosition(Vector3(0, 2, 0)); + } + /* { Model *box = new Model(MODEL_PATH + "jet00.obj"); Material *mat = new Material(); @@ -58,6 +74,7 @@ int main(){ box->setCastShadow(true); box->setPosition(Vector3(0, 3, 0)); } + */ Light *light = new Light(Light::DIRECTIONAL); light->setColor(Vector3(1, 1, 1)); @@ -65,7 +82,7 @@ int main(){ lightNode->addLight(light); rootNode->attachChild(lightNode); lightNode->setOrientation(Quaternion(1.57, Vector3(1, 0, 0))); - lightNode->setPosition(Vector3(0, 5, -5)); + lightNode->setPosition(Vector3(0, 5, 0)); while(true) root->update(); diff --git a/texture.frag b/texture.frag index 28091e3..dd7a9be 100755 --- a/texture.frag +++ b/texture.frag @@ -46,17 +46,18 @@ float getShadow(int id){ int type = light[id].type; float shadow = 0, closestDepth = 0, currentDepth = 0, bias = .005, val = .7; +/* if(type == 0){ vec3 projDir = fragPos - light[id].pos; closestDepth = texture(light[id].depthMapCube, projDir).r * light[id].far; currentDepth = length(projDir); shadow = (currentDepth - bias > closestDepth) ? val : 0; } -/* -*/ else{ - vec3 projCoords = (light[id].lightMat * vec4(fragPos, 1)).xyz / (light[id].lightMat * vec4(fragPos, 1)).w; - projCoords = projCoords * .5 + .5; + } +*/ + vec4 lightSpaceFragPos = light[id].lightMat * vec4(fragPos, 1); + vec3 projCoords = (lightSpaceFragPos.xyz / lightSpaceFragPos.w) * .5 + .5; closestDepth = texture(light[id].depthMap, projCoords.xy).r; currentDepth = projCoords.z; if(type == 2){ @@ -66,7 +67,6 @@ float getShadow(int id){ shadow = (currentDepth - bias > closestDepth) ? val : 0; if(projCoords.z > 1) shadow = 0; - } return shadow; }