@@ -79,21 +79,26 @@ namespace glsample {
7979 const unsigned char white[] = {255 , 255 , 255 , 255 };
8080 const unsigned char black[] = {0 , 0 , 0 , 255 };
8181
82- this ->default_textures [TextureType ::Diffuse] = glsample::CommonUtil::createColorTexture (
82+ this ->default_textures [TextureTypeBinding ::Diffuse] = glsample::CommonUtil::createColorTexture (
8383 1 , 1 , fragcore::Color (white[0 ] / 255 .0f , white[1 ] / 255 .0f , white[2 ] / 255 .0f , white[3 ] / 255 .0f ));
84- this ->default_textures [TextureType::AlphaMask] = this ->default_textures [TextureType::Diffuse];
85- this ->default_textures [TextureType::Emission] = this ->default_textures [TextureType::Diffuse];
86- this ->default_textures [TextureType::Irradiance] = this ->default_textures [TextureType::Diffuse];
87- this ->default_textures [TextureType::AmbientOcclusion] = this ->default_textures [TextureType::Diffuse];
88- this ->default_textures [TextureType::DepthBuffer] = this ->default_textures [TextureType::Diffuse];
89- this ->default_textures [TextureType::Specular_Roughness] = this ->default_textures [TextureType::Diffuse];
90- this ->default_textures [TextureType::AmbientOcclusion] = this ->default_textures [TextureType::Diffuse];
91-
92- this ->default_textures [TextureType::Displacement] = glsample::CommonUtil::createColorTexture (
84+ this ->default_textures [TextureTypeBinding::AlphaMask] = this ->default_textures [TextureTypeBinding::Diffuse];
85+ this ->default_textures [TextureTypeBinding::Emission] = this ->default_textures [TextureTypeBinding::Diffuse];
86+ this ->default_textures [TextureTypeBinding::Irradiance] =
87+ this ->default_textures [TextureTypeBinding::Diffuse];
88+ this ->default_textures [TextureTypeBinding::AmbientOcclusion] =
89+ this ->default_textures [TextureTypeBinding::Diffuse];
90+ this ->default_textures [TextureTypeBinding::DepthBuffer] =
91+ this ->default_textures [TextureTypeBinding::Diffuse];
92+ this ->default_textures [TextureTypeBinding::Specular_Roughness] =
93+ this ->default_textures [TextureTypeBinding::Diffuse];
94+ this ->default_textures [TextureTypeBinding::AmbientOcclusion] =
95+ this ->default_textures [TextureTypeBinding::Diffuse];
96+
97+ this ->default_textures [TextureTypeBinding::Displacement] = glsample::CommonUtil::createColorTexture (
9398 1 , 1 , fragcore::Color (black[0 ] / 255 .0f , black[1 ] / 255 .0f , black[2 ] / 255 .0f , black[3 ] / 255 .0f ));
94- this ->default_textures [TextureType ::Metal] = this ->default_textures [TextureType ::Diffuse];
99+ this ->default_textures [TextureTypeBinding ::Metal] = this ->default_textures [TextureTypeBinding ::Diffuse];
95100
96- this ->default_textures [TextureType ::Normal] = glsample::CommonUtil::createColorTexture (
101+ this ->default_textures [TextureTypeBinding ::Normal] = glsample::CommonUtil::createColorTexture (
97102 1 , 1 ,
98103 fragcore::Color (normalForward[0 ] / 255 .0f , normalForward[1 ] / 255 .0f , normalForward[2 ] / 255 .0f ,
99104 normalForward[3 ] / 255 .0f ));
@@ -302,9 +307,9 @@ namespace glsample {
302307 stageLightBase->pointCount = 0 ;
303308 size_t light_count = 0 ;
304309 for (light_count = 0 ; light_count < getLights ().size (); light_count++) {
305- const Light *light = getLights ()[light_count];
310+ Light *light = getLights ()[light_count];
306311
307- switch (light->lightType ) {
312+ switch (light->getLightType () ) {
308313
309314 case Light::LightType::Directional: {
310315
@@ -317,23 +322,23 @@ namespace glsample {
317322 lightData->lightDirection = glm::vec4 (light_direction, 1 );
318323
319324 /* Shadow Setup. */
320- lightData->lightShadow .shadow [0 ] = light->shadow ;
325+ lightData->lightShadow .shadow [0 ] = light->getShadowStrength () ;
321326 lightData->lightShadow .shadow [1 ] = light->bias ;
322327
323- if (light->shadow > 0 ) {
324- // TODO: frustum.
325- const float near_plane = -(dirLight->shadowDistance / 2 .0f ) * 2 ;
326- const float far_plane = (dirLight->shadowDistance / 2 .0f ) * 2 ;
327- const glm::mat4 lightProjection =
328- glm::ortho (-dirLight->shadowDistance , dirLight->shadowDistance , -dirLight->shadowDistance ,
329- dirLight->shadowDistance , near_plane, far_plane);
328+ if (light->getShadowStrength () > 0 ) {
329+
330+ const float near_plane = -(dirLight->getShadowDistance ());
331+ const float far_plane = (dirLight->getShadowDistance ());
332+
333+ const glm::mat4 lightProjection = glm::ortho (
334+ -dirLight->getShadowDistance (), dirLight->getShadowDistance (), -dirLight->getShadowDistance (),
335+ dirLight->getShadowDistance (), near_plane, far_plane);
330336
331337 const glm::mat4 lightView = glm::lookAt (
332338 dirLight->getPosition (), dirLight->getPosition () + light_direction * 100 .0f , dirLight->up ());
333339 const glm::mat4 lightSpaceMatrix = lightProjection * lightView;
334340
335- const glm::mat4 biasMatrix (0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.0 , 0.0 , 0.0 , 0.0 , 0.5 , 0.0 , 0.5 , 0.5 ,
336- 0.5 , 1.0 );
341+ light->shadowData .lightSpaceMatrix = lightSpaceMatrix;
337342 lightData->lightShadow .lightSpaceMatrix = lightSpaceMatrix;
338343 }
339344
@@ -350,6 +355,7 @@ namespace glsample {
350355
351356 stageLightBase->pointCount ++;
352357 } break ;
358+ default :
353359 case Light::LightType::Spot:
354360 break ;
355361 }
@@ -475,12 +481,19 @@ namespace glsample {
475481 if (light) {
476482 GlobalSceneState *globalScene = this ->stageCommonRobin .buffers [getRoundRobinIndex ()];
477483
484+ /* */
485+ globalScene->camera .far = light->getShadowDistance ();
486+ globalScene->camera .near = 0 ;
487+ globalScene->camera .position = glm::vec4 (light->getPosition (), 1 .0f );
478488 globalScene->camera .proj = light->getProjectionMatrix ();
479489 globalScene->camera .view = light->getViewMatrix ();
480- globalScene->camera .viewProj = (globalScene->camera .proj * globalScene->camera .view );
490+ globalScene->camera .viewProj =
491+ light->shadowData .lightSpaceMatrix ; // (globalScene->camera.proj * globalScene->camera.view);
481492
482493 /* */
483494 globalScene->proj [0 ] = light->getProjectionMatrix ();
495+
496+ this ->updateBuffers (); // TODO: update only camera
484497 }
485498
486499 /* */
@@ -561,10 +574,10 @@ namespace glsample {
561574 glDepthFunc (GL_LEQUAL);
562575 glCullFace (GL_BACK);
563576
564- this ->frameIndex ++;
577+ this ->renderPassFrameIndex ++;
565578 }
566579
567- void Scene::bindTexture (const MaterialObject &material, const TextureType texture_type) {
580+ void Scene::bindTexture (const MaterialObject &material, const TextureTypeBinding texture_type) {
568581
569582 /* */
570583 const unsigned int textureMapIndex = texture_type;
@@ -614,17 +627,17 @@ namespace glsample {
614627 /* Only bind if different material the current material binded. */
615628 if (this ->currentBindedMaterial != material) {
616629
617- this ->bindTexture (*material, TextureType ::Diffuse);
618- this ->bindTexture (*material, TextureType ::Normal);
619- this ->bindTexture (*material, TextureType ::AlphaMask);
620- this ->bindTexture (*material, TextureType ::Emission);
621- this ->bindTexture (*material, TextureType ::AmbientOcclusion);
622- this ->bindTexture (*material, TextureType ::Displacement);
623- this ->bindTexture (*material, TextureType ::Specular_Roughness);
624- this ->bindTexture (*material, TextureType ::Metal);
630+ this ->bindTexture (*material, TextureTypeBinding ::Diffuse);
631+ this ->bindTexture (*material, TextureTypeBinding ::Normal);
632+ this ->bindTexture (*material, TextureTypeBinding ::AlphaMask);
633+ this ->bindTexture (*material, TextureTypeBinding ::Emission);
634+ this ->bindTexture (*material, TextureTypeBinding ::AmbientOcclusion);
635+ this ->bindTexture (*material, TextureTypeBinding ::Displacement);
636+ this ->bindTexture (*material, TextureTypeBinding ::Specular_Roughness);
637+ this ->bindTexture (*material, TextureTypeBinding ::Metal);
625638 // this->bindTexture(material, TextureType::Irradiance); //TODO: enable once material has been binded
626639 // with irradiance texture
627- this ->bindTexture (*material, TextureType ::DepthBuffer);
640+ this ->bindTexture (*material, TextureTypeBinding ::DepthBuffer);
628641
629642 /* */
630643 const RenderQueue domain = getQueueDomain (*material);
@@ -787,7 +800,7 @@ namespace glsample {
787800 RenderQueue Scene::getQueueDomain (const MaterialObject &material) const noexcept {
788801 const bool useGeometryAlpha = material.clipping < 1 ;
789802 const bool useBlending = material.transparent [3 ] < 1 .0f ||
790- (material.texture_index [TextureType ::AlphaMask] >= 0 && !useGeometryAlpha);
803+ (material.texture_index [TextureTypeBinding ::AlphaMask] >= 0 && !useGeometryAlpha);
791804 const bool useWireframe = material.wireframe_mode ;
792805
793806 if (useWireframe) {
@@ -932,16 +945,16 @@ namespace glsample {
932945 }
933946
934947 glm::ivec3 size = light->getSize ();
935- if (ImGui::DragInt2 (" Size" , &size[0 ])) {
948+ if (ImGui::DragInt2 (" Size" , &size[0 ], 1 . 0f , 0 , 0 , " %d " )) {
936949 size = glm::max (size, glm::ivec3 (128 ));
937950 light->setSize (size);
938951 }
939952
940953 FrameBuffer *framebuffer = light->getFrameBuffer ();
941- if (framebuffer) {
942- ImGui::Image (static_cast <ImTextureID>(framebuffer->attachments [framebuffer->depthIndex ]),
943- ImVec2 (512 , 512 ), ImVec2 (1 , 1 ), ImVec2 (0 , 0 ));
944- }
954+ if (framebuffer) {
955+ ImGui::Image (static_cast <ImTextureID>(framebuffer->attachments [framebuffer->depthIndex ]),
956+ ImVec2 (512 , 512 ), ImVec2 (1 , 1 ), ImVec2 (0 , 0 ));
957+ }
945958
946959 ImGui::DragFloat (" Shadow Strength" , &light->shadow , 1 , 0 .0f , 1 .0f );
947960 ImGui::DragFloat (" Shadow Bias" , &light->bias , 1 , 0 .0f , 1 .0f , " %.5f" );
@@ -1004,7 +1017,8 @@ namespace glsample {
10041017 ImGui::PushID (mat_tex_index);
10051018
10061019 /* */
1007- const std::string texType = std::string (magic_enum::enum_name ((TextureType)mat_tex_index));
1020+ const std::string texType =
1021+ std::string (magic_enum::enum_name ((TextureTypeBinding)mat_tex_index));
10081022 ImGui::Image (tex, ImVec2 (96 , 96 ), ImVec2 (1 , 1 ), ImVec2 (0 , 0 ));
10091023 ImGui::SameLine ();
10101024 ImGui::Text (" %s (%ld)" , texType.c_str (), mat_tex_index);
0 commit comments