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

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

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

デバッグはプログラムのバグや欠陥を検知し、開発中のバグを取り除く為のプロセスを指します。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

例外

例外(exception)とは、プログラムの処理実行中に発生する、通常の処理の続行を妨げる特殊な事象のことを呼びます。この「例外」が発生した場合に、現在の処理を中断し、変わりに別の処理を実行させる事を「例外処理」と呼びます。

C++

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

Q&A

解決済

1回答

999閲覧

基底クラスのサイズが変わってしまいアクセスエラーが出る原因が知りたい。

退会済みユーザー

退会済みユーザー

総合スコア0

デバッグ

デバッグはプログラムのバグや欠陥を検知し、開発中のバグを取り除く為のプロセスを指します。

継承

継承(インヘリタンス)はオブジェクト指向プログラミングに存在するシステムです。継承はオブジェクトが各自定義する必要をなくし、継承元のオブジェクトで定義されている内容を引き継ぎます。

例外

例外(exception)とは、プログラムの処理実行中に発生する、通常の処理の続行を妨げる特殊な事象のことを呼びます。この「例外」が発生した場合に、現在の処理を中断し、変わりに別の処理を実行させる事を「例外処理」と呼びます。

C++

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

1グッド

0クリップ

投稿2021/11/11 04:23

編集2021/11/11 05:45

提示コードですがActorクラスのサイズが変わっているためアクセスエラーが出る原因がわかりせん。これはなぜなのでしょうか?printfデバッグをした結果void RendererDepth()関数が原因なのでその定義をみましたが問題ありません。これは何が原因なのでしょうか?

GithubコードのCamera.chpp Renderer()関数部 Ground.cpp void RendererDepth()関数部です。
Github: https://github.com/Shigurechan/TopViewGame

端末
[shigurechan@localhost TopViewGame]$ ./program aaaああああ えええ terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 4) >= this->size() (which is 1) 中止 (コアダンプ) [shigurechan@localhost TopViewGame]$

#####Actor.hpp

#ifndef ___ACTOR_HPP___ #define ___ACTOR_HPP___ class Actor { public: Actor(); ~Actor(); //virtual void Update(); virtual void Update() = 0; virtual void Renderer(glm::mat4 view)const = 0; virtual void RendererDepth(GLuint depth,glm::mat4 view)const = 0; //virtual void Renderer(glm::mat4 view)const; std::shared_ptr<FrameWork::D3::Object> model; protected: FrameWork::ObjFile file; }; #endif
Ground.cpp
Ground::Ground() : Actor() { FrameWork::D3::LoadObj("Game/model/ground.obj",file); model = std::make_shared<FrameWork::D3::Object>(&file,GL_STATIC_DRAW); shader.Load("FrameWork/shader/3D//ShadowMap/ShadowMap.vert","FrameWork/shader/3D/ShadowMap/ShadowMap.frag"); shaderDepth.Load("FrameWork/shader/3D//ShadowMap/ShadowTexture.vert","FrameWork/shader/3D/ShadowMap/ShadowTexture.frag"); } void Ground::Renderer(glm::mat4 view)const { model->shader = shaderDepth; model->setBindBuffer(); model->shader.setEnable(); model->shader.setUniformMatrix4fv("lightSpaceMatrix",lightSpaceMatrix); model->shader.setDisable(); model->setUnBindBuffer(); } void Ground::RendererDepth(GLuint depth,glm::mat4 view)const { model->shader = shader; model->setBindBuffer(); model->shader.setEnable(); /*文字数の関係で割愛*/ model->shader.setDisable(); model->setUnBindBuffer(); } Ground::~Ground() { } Ground::Ground() : Actor() { FrameWork::D3::LoadObj("Game/model/ground.obj",file); model = std::make_shared<FrameWork::D3::Object>(&file,GL_STATIC_DRAW); shader.Load("FrameWork/shader/3D//ShadowMap/ShadowMap.vert","FrameWork/shader/3D/ShadowMap/ShadowMap.frag"); shaderDepth.Load("FrameWork/shader/3D//ShadowMap/ShadowTexture.vert","FrameWork/shader/3D/ShadowMap/ShadowTexture.frag"); texture = FrameWork::LoadTexture("Game/Assets/debug.png"); model->setTexture(texture); model->setPosition(glm::vec3(0,0,-5)); model->setScale(glm::vec3(100,1,100)); model->setRotate(glm::vec3(1,0,0),0); } void Ground::setLightPosition(glm::vec3 pos) { model->shader.setEnable(); model->shader.setUniform4f("uFragment",FrameWork::GetGlColor(glm::vec4(200,255,255,255))); model->shader.setDisable(); } void Ground::Update() { model->shader.setEnable(); /* glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, depthMap); */ model->shader.setDisable(); } void Ground::Renderer(glm::mat4 view)const { model->shader = shaderDepth; model->setBindBuffer(); model->shader.setEnable(); glm::mat4 lightProjection, lightView; glm::mat4 lightSpaceMatrix; float near_plane = 1.0f, far_plane = 7.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(glm::vec3(0,0,-5), glm::vec3(0.0f), glm::vec3(0.0, 1.0, 0.0)); lightSpaceMatrix = lightProjection * lightView; // render scene from light's point of view model->shader.setUniformMatrix4fv("lightSpaceMatrix",lightSpaceMatrix); model->shader.setDisable(); model->setUnBindBuffer(); } void Ground::RendererDepth(GLuint depth,glm::mat4 view)const { model->shader = shader; model->setBindBuffer(); model->shader.setEnable(); glm::mat4 lightProjection, lightView; glm::mat4 lightSpaceMatrix; float near_plane = 1.0f, far_plane = 7.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(glm::vec3(0,0,-5), glm::vec3(0.0f), glm::vec3(0.0, 1.0, 0.0)); lightSpaceMatrix = lightProjection * lightView; // render scene from light's point of view model->shader.setUniformMatrix4fv("lightSpaceMatrix",lightSpaceMatrix); model->shader.setUniform3f("viewPos", glm::vec3(0,0,-5)); model->shader.setUniform3f("lightPos", glm::vec3(0,100,-5)); model->setEnableTexture(texture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, depth); model->Renderer(view); model->shader.setDisable(); model->setUnBindBuffer(); } Ground::~Ground() { }
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_BLEND); //ブレンド有効 glEnable(GL_TEXTURE_2D); //テクスチャを有効 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //ブレンドタイプ glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE); //半透明 glEnable(GL_DEPTH_TEST); //深度バッファを有効 glDepthFunc(GL_LEQUAL); //深度バッファタイプ glEnable(GL_CULL_FACE); //カリングを有効 glCullFace(GL_BACK); //裏面を無効 //フレームバッファをバインド glBindFramebuffer(GL_FRAMEBUFFER, framebuffer); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glClear(GL_DEPTH_BUFFER_BIT); printf("えええ\n"); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////// for(std::vector<std::shared_ptr<Actor>>::iterator itr = actor.begin(); itr != actor.end(); itr++) { (*itr)->RendererDepth(depthMapFBO,getViewProjection()); } printf("ううう\n"); for(std::vector<std::shared_ptr<Actor>>::iterator itr = actor.begin(); itr != actor.end(); itr++) { (*itr)->Renderer(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); glViewport(0, 0, FrameWork::windowContext->getSize().x, FrameWork::windowContext->getSize().y); 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); glBindFramebuffer(GL_FRAMEBUFFER, 0); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); //////////////////////////////////////////////////////////////////////////////////////////////////// for(std::vector<std::shared_ptr<Actor>>::iterator itr = actor.begin(); itr != actor.end(); itr++) { (*itr)->RendererDepth(depthMapFBO,getViewProjection()); } /////////////////////////////////////////////////////////////////////////////////////////////////// //フレームバッファをレンダリング 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); }
退会済みユーザー👍を押しています

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

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

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

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

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

int32_t

2021/11/11 05:52

基底クラスのサイズが変わっているのではありません。std::vectorの要素数です。
guest

回答1

0

ベストアンサー

vectorで範囲外アクセスをしているという意味なので、おそらく getViewProjection()RendererDepth()Renderer() の中で actor のサイズが変わっているのでしょう。

投稿2021/11/11 04:38

int32_t

総合スコア20884

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

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

退会済みユーザー

退会済みユーザー

2021/11/11 04:49

なるほど。サイズが変る原因としてどんなものが考えられるのでしょうか?
int32_t

2021/11/11 05:01

そりゃあ、vectorの要素を足すか消すしかないでしょう。それ以上のことはコードが開示されていないのでわかりません。 Camera::Renderer() 内のループ内の最初と最後で actor.size() を表示してサイズが変わっていることを確認したら、getViewProjection() RendererDepth() Renderer() のコードをよく読んで actor を変更しているかどうかチェックしてください。
退会済みユーザー

退会済みユーザー

2021/11/11 05:03

std::shared<Model> model 変数のshader 変数に値を代入しているのですがこれは大丈夫なのでしょうか?
int32_t

2021/11/11 05:13

なぜその代入が不安なのでしょうか? プログラミングは当てずっぽうじゃないですよ。
退会済みユーザー

退会済みユーザー

2021/11/11 05:17

アドレスがわってるだけで変数自体が大きくなっているわけじゃないので自分も関係無いと思ったのですが一様聞きました。
int32_t

2021/11/11 05:50

vector内で例外が出ているので、vectorを使っていないコードを疑う必要は(まずは)ありません。 本当にactorのサイズが変更されているかどうかを確認するのがまず必要なことです。「えええ」と「ううう」の間に他にも vector が使われていれば、それも怪しいです。
退会済みユーザー

退会済みユーザー

2021/11/11 06:01

結局model->setEnalbeTexture():の引数にはベクターの要素番号を指定するという部分が間違ていました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問