質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
OpenGL

OpenGLは、プラットフォームから独立した、デスクトップやワークステーション、モバイルサービスで使用可能な映像処理用のAPIです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

0回答

1121閲覧

シャドウマップをレンダリングがおかしい原因が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

OpenGL

OpenGLは、プラットフォームから独立した、デスクトップやワークステーション、モバイルサービスで使用可能な映像処理用のAPIです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2021/11/11 06:44

編集2021/12/12 11:27

提示コードですが下記の提示画像のように影が描画されないのですがこれは何が原因なのでしょうか?シェーダーファイルは 参考サイトのコードを写しました。現状はライティングはされて提示画像の下の方に丸い黒い数字のようなものが影だと思うのですがなぜ影の描画がおかしいのでしょうか?

確認したこと

ライトは立方体の真上にあります。
ライト、立方体、を移動できるようにして影がどこに映るかを確認
参考サイトを参考にGLSLコードを記述
描画のメインループを確認してデプスマップに描画されているか確認

参考サイト: https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping

#####現状の画像
イメージ説明
#####地面のテクスチャ
イメージ説明

Ground.cpp
void Ground::Renderer(glm::mat4 view)const { //model->shader = shaderDepth; } void Ground::RendererDepth(glm::mat4 view)const { } void Ground::RendererShadow(GLuint depth,glm::mat4 view)const { model->shader.setEnable(); model->setBindBuffer(); model->shader.setUniformMatrix4fv("lightSpaceMatrix",lightSpaceMatrix); model->shader.setUniform3f("viewPos",viewPos); model->shader.setUniform3f("lightPos",lightPos); model->shader.setUniformMatrix4fv("uViewProjection", view); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, depth); model->setScale(glm::vec3(100,1,100)); model->setRotate(glm::vec3(1,1,1),0); model->Renderer(view); model->shader.setDisable(); model->setUnBindBuffer(); }
cube.cpp
void Cube::RendererDepth(glm::mat4 view)const { model->shader = shader; model->shader.setEnable(); model->setBindBuffer(); // render scene from light's point of view model->setPosition(position); model->setScale(glm::vec3(5,5,5)); model->setRotate(glm::vec3(1,0,0),0); model->shader.setUniformMatrix4fv("lightSpaceMatrix",lightSpaceMatrix); //model->Renderer(view); model->shader.setDisable(); model->setUnBindBuffer(); } void Cube::Renderer(glm::mat4 view)const { model->shader = debugShader; model->shader.setEnable(); model->setBindBuffer(); model->shader.setUniform4f("uFragment",FrameWork::GetGlColor(glm::vec4(0,255,0,255))); model->shader.setUniformMatrix4fv("uViewProjection",view); model->Renderer(view); model->shader.setDisable(); model->setUnBindBuffer(); }
Game.cpp
void Game::Update() { lightCube->Update(); ground->Update(); cube->Update(); control->Update(); glm::mat4 lightProjection, lightView; glm::mat4 lightSpaceMatrix; float near_plane = 0.00001f, far_plane = 1000.5f; //lightProjection = glm::perspective(glm::radians(45.0f), (GLfloat)SHADOW_WIDTH / (GLfloat)SHADOW_HEIGHT, near_plane, far_plane); // note that if you use a perspective projection matrix you'll have to change the light position as the current light position isn't enough to reflect the whole scene lightProjection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, near_plane, far_plane); lightView = glm::lookAt(control->lightCubePosition,glm::vec3(0,-1,0), glm::vec3(0.0, 1.0, 0.0)); lightSpaceMatrix = lightProjection * lightView; cube->lightSpaceMatrix = lightSpaceMatrix; cube->position = control->cubePosition; ground->viewPos = camera->getPosition(); ground->lightPos = control->lightCubePosition; ground->lightSpaceMatrix = lightSpaceMatrix; camera->setPosition(control->cameraPosition); camera->setLook(glm::vec3(cos(control->cameraLookAt.x) * cos(control->cameraLookAt.y), sin(control->cameraLookAt.y), sin(control->cameraLookAt.x) * cos(control->cameraLookAt.y))); }
Camera.cpp
void FrameWork::Camera::Renderer() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); //テクスチャを有効 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //ブレンドタイプ glEnable(GL_BLEND); //ブレンド有効 glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); //半透明 glEnable(GL_DEPTH_TEST); //深度バッファを有効 glDepthFunc(GL_LEQUAL); //深度バッファタイプ glEnable(GL_CULL_FACE); //カリングを有効 glCullFace(GL_BACK); //裏面を無効 glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glClear(GL_DEPTH_BUFFER_BIT); //シャドウオブジェクト for(std::vector<std::shared_ptr<Actor>>::iterator itr = actor.begin(); itr != actor.end(); itr++) { (*itr)->RendererDepth(getViewProjection()); } glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //オブジェクト for(std::vector<std::shared_ptr<Actor>>::iterator itr = actor.begin(); itr != actor.end(); itr++) { (*itr)->Renderer(getViewProjection()); } //シャドウオブジェクト for(std::vector<std::shared_ptr<Actor>>::iterator itr = actor.begin(); itr != actor.end(); itr++) { (*itr)->RendererShadow(depthMap,getViewProjection()); } glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, intermediateFBO); glBlitFramebuffer(0, 0, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y, 0, 0, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y, GL_COLOR_BUFFER_BIT, GL_NEAREST); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); //フレームバッファをレンダリング glDisable(GL_DEPTH_TEST); shaderFrameBuffer->setEnable(); glBindVertexArray(quadVAO); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, screenTexture); glDrawArrays(GL_TRIANGLES, 0, 6); shaderFrameBuffer->setDisable(); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D,0); //glBindFramebuffer(GL_FRAMEBUFFER, 0); actor.resize(0); shadowActor.resize(0); }
.frag
#version 330 core out vec4 FragColor; in VS_OUT { vec3 FragPos; vec3 Normal; vec2 TexCoords; vec4 FragPosLightSpace; } fs_in; uniform sampler2D diffuseTexture; uniform sampler2D shadowMap; uniform vec3 lightPos; uniform vec3 viewPos; float ShadowCalculation(vec4 fragPosLightSpace) { // perform perspective divide vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; // transform to [0,1] range projCoords = projCoords * 0.5 + 0.5; // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) float closestDepth = texture(shadowMap, projCoords.xy).r; // get depth of current fragment from light's perspective float currentDepth = projCoords.z; // calculate bias (based on depth map resolution and slope) vec3 normal = normalize(fs_in.Normal); vec3 lightDir = normalize(lightPos - fs_in.FragPos); float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); // check whether current frag pos is in shadow // float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0; // PCF float shadow = 0.0; vec2 texelSize = 1.0 / textureSize(shadowMap, 0); for(int x = -1; x <= 1; ++x) { for(int y = -1; y <= 1; ++y) { float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0; } } shadow /= 9.0; // keep the shadow at 0.0 when outside the far_plane region of the light's frustum. if(projCoords.z > 1.0) shadow = 0.0; return shadow; } void main() { vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb; vec3 normal = normalize(fs_in.Normal); vec3 lightColor = vec3(0.3); // ambient vec3 ambient = 0.3 * lightColor; // diffuse vec3 lightDir = normalize(lightPos - fs_in.FragPos); float diff = max(dot(lightDir, normal), 0.0); vec3 diffuse = diff * lightColor; // specular vec3 viewDir = normalize(viewPos - fs_in.FragPos); vec3 reflectDir = reflect(-lightDir, normal); float spec = 0.0; vec3 halfwayDir = normalize(lightDir + viewDir); spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0); vec3 specular = spec * lightColor; // calculate shadow float shadow = ShadowCalculation(fs_in.FragPosLightSpace); vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular)) * color; FragColor = vec4(lighting, 1.0); }
.vert
#version 330 core layout (location = 0) in vec3 vertexPosition; layout (location = 1) in vec2 vertexUV; layout (location = 2) in vec3 vertexNormal; out vec2 TexCoords; out VS_OUT { vec3 FragPos; vec3 Normal; vec2 TexCoords; vec4 FragPosLightSpace; } vs_out; uniform mat4 uTranslate; uniform mat4 uRotate; uniform mat4 uScale; uniform mat4 uViewProjection; uniform mat4 lightSpaceMatrix; void main() { vec4 vertex = vec4(vertexPosition,1.0); mat4 model = uTranslate * uRotate * uScale; gl_Position = (uViewProjection * model) * vertex; vs_out.FragPos = vec3(model * vec4(vertexPosition, 1.0)); vs_out.Normal = transpose(inverse(mat3(model))) * vertexNormal; vs_out.TexCoords = vertexUV; vs_out.FragPosLightSpace = lightSpaceMatrix * vec4(vs_out.FragPos, 1.0); }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問