0

レイトレーシングプログラムに問題があります。私が使用した最初のコードは、YouTubeチュートリアルのhttps://www.youtube.com/watch?v=k_aRiYSXcyo(レイトレースのcaleb piercyチュートリアル)でした。どのようにレイトレーシングオブジェクト指向プログラムの変数を宣言するときは、ベクトルとカメラの宣言のほとんどは?

その後、Visual Studioを使用してmain.cppのコードをオブジェクト指向のコードに再作成しました。

重要なのは、OpenMP(Visual Studio)または(Visual Studio)を使用してプログラム全体をマルチスレッド化できるように、関数と計算をカプセル化することでした。

しかし、それを行う前に、変数の特定の宣言に問題があります。それらのほとんどは私のVect.hヘッダーファイルとCamera.hファイルからのベクトルの宣言です。

ここにmain.cppのコードがあります。具体的には、宣言だけです。私はアルゴリズムの関数と計算を投稿することはありません。なぜなら、それは問題なく動作するからです。

iが変数とメソッド関数

//thread class 
class Thread 
{ 
public: 

    void UPrightRend(); 
    void UPleftRend(); 
    void DownrightRend(); 
    void DownleftRend(); 
    void define(); 
    void calcVect(int x, int y); 
    void calcColor(); 
    void saveimg(const char *filename); 
    int winningObjectIndex(vector<double> object_intersections); 
    Color getColorAt(Vect intersection_position, Vect intersecting_ray_direction, vector<Object*> scene_objects, int index_of_winning_object, vector<Source*> light_sources, double accuracy, double ambientlight); 

private: 

    //basic variables 
    int dpi = 72; 
    double sWidth = 1000; 
    double sHeight = 800; 
    int n = sWidth*sHeight; 
    RGBType *pixels = new RGBType[n]; 

    //aadepth sends out n number of pixels/rays that hit one pixels and then reflect to other pixels are return a value that is then averaged 
    static const int aadepth = 5; 
    double aspectratio = sWidth/sHeight; 
    double ambientlight = 0.2; 
    double accuracy = 0.000001; 

    int thisone, aa_index; 
    double xamnt, yamnt; 

    Vect camdir; 
    Vect camright; 
    Vect camdown; 
    Camera scene_cam; 

    vector<Source*> light_sources; 

    vector<Object*> scene_objects; 

    //start with a blank pixel 
    double tempRed[aadepth*aadepth]; 
    double tempGreen[aadepth*aadepth]; 
    double tempBlue[aadepth*aadepth]; 

}; 

および他の変数の減速の一部を宣言したクラス。

void Thread::define() 
{ 

    //Calling Vect.h and establishing the XYZ positions 
    Vect O(0, 0, 0); 
    Vect X(3, 0, 3.5); 
    Vect Y(0, 1, 0); 
    Vect Z(0, 0, 1); 
    Vect P(5.5, 0, 3.5); 
    Vect R(0, -3, 0); 
    Vect M(7, 0, -5); 

    Vect new_sphere_location(2, 0, 0); 

    Vect campos(4, 1, -4); 

    Vect look_at(0, 0, 0); 
    Vect diff_btw(campos.getVectX() - look_at.getVectX(), campos.getVectY() - look_at.getVectY(), campos.getVectZ() - look_at.getVectZ()); 

    //camera direction and position 
    camdir = diff_btw.negative().normalize(); 
    camright = Y.crossProduct(camdir).normalize(); 
    camdown = camright.crossProduct(camdir); 
    Camera scene_cam(campos, camdir, camright, camdown); 

    //color of objects and planes 
    Color white_light(1.0, 1.0, 1.0, 0); 
    Color pretty_green(0.25, 0.25, 0.95, 0.5); 
    Color maroon(0.5, 0.25, 0.25, 0.5); 
    Color tile_floor(1, 1, 1, 2); 
    Color gray(0.5, 0.5, 0.5, 0); 
    Color black(0.0, 0.0, 0.0, 0); 

    //light color and position 
    Vect light_position(-7, 10, -10); 
    Light scene_light(light_position, white_light); 
    light_sources.push_back(dynamic_cast<Source*>(&scene_light)); 

    //scene objects 
    Sphere scene_sphere(O, 0.85, pretty_green); 
    Sphere scene_sphere2(new_sphere_location, 0.5, maroon); 
    Plane scene_plane(Y, -1, tile_floor); 
    Plane scene_plane2(P, -1, gray); 
    Plane scene_plane3(X, 1, gray); 
    Plane scene_plane4(R, -1, tile_floor); 
    Plane scene_plane5(M, -1, black); 

    scene_objects.push_back(dynamic_cast<Object*>(&scene_sphere)); 
    scene_objects.push_back(dynamic_cast<Object*>(&scene_sphere2)); 
    scene_objects.push_back(dynamic_cast<Object*>(&scene_plane)); 
    scene_objects.push_back(dynamic_cast<Object*>(&scene_plane2)); 
    scene_objects.push_back(dynamic_cast<Object*>(&scene_plane3)); 
    scene_objects.push_back(dynamic_cast<Object*>(&scene_plane4)); 
    scene_objects.push_back(dynamic_cast<Object*>(&scene_plane5)); 

} 

So。 オブジェクトでmain()関数のdefine()関数を呼び出すと、画像出力として黒い画面が表示されます。計算機能でdefine()関数を呼び出すと、プログラムは開始時にすぐにクラッシュします。

define()関数の内容をクラスprivate:に移動すると、宣言のエラーが発生します。

define()の変数はすべて表示する必要がありますが、その方法はわかりません。

これを修正するにはどうすればよいですか?それは動作しません、私は結果として黒い画面イメージを取得するか、ちょうどクラッシュします。

ご迷惑をおかけして申し訳ありません。(これは私の最初の質問stackoverflowです)。

ありがとうございました!

+0

あなたの質問は何ですか? – immibis

+0

どうすればこの問題を解決できますか?私はイメージを持っていたい、それは動作しません – mnestorov

答えて

0

私は正確には、黒い画面を引き起こす問題はどこにあるのですか?私はクラッシュに驚いていません。

define()のように、メソッド内で宣言された変数(および相対値)は、メソッドの実行終了時に失われることを考慮する必要があります。

だからあなたは7つのローカル変数(scene_spherescene_sphere2、など)を宣言し、そのポインタを追加隣に(私はObjectSpherePlaneの基底クラスがあると仮定)次のコード

Sphere scene_sphere(O, 0.85, pretty_green); 
Sphere scene_sphere2(new_sphere_location, 0.5, maroon); 
Plane scene_plane(Y, -1, tile_floor); 
Plane scene_plane2(P, -1, gray); 
Plane scene_plane3(X, 1, gray); 
Plane scene_plane4(R, -1, tile_floor); 
Plane scene_plane5(M, -1, black); 

scene_objects.push_back(dynamic_cast<Object*>(&scene_sphere)); 
scene_objects.push_back(dynamic_cast<Object*>(&scene_sphere2)); 
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane)); 
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane2)); 
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane3)); 
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane4)); 
scene_objects.push_back(dynamic_cast<Object*>(&scene_plane5)); 

を見ますクラスのメンバー(scene_objects)。

define()が終了すると、7つの変数(scene_sphere,scene_sphere2など)の寿命が終了し、メモリが解放されます。したがって、scene_objectsの7つのポインタは、解放されたスタックメモリを指しています。解放されたスタックメモリは、他のさまざまな変数に対してリサイクルできます。

7つのポインタのうちの1つを使用しようとすると、プログラムがクラッシュする可能性が非常に高いです。

Light scene_light(light_position, white_light); 
light_sources.push_back(dynamic_cast<Source*>(&scene_light)); 

同じ問題私はあなたがそう尖った物体がdefine()の終わりを生き残ることができ、このように

light_sources.push_back(new Light(light_position, white_light)); 

// ... 

scene_objects.push_back(new Sphere(O, 0.85, pretty_green)); 
scene_objects.push_back(new Sphere(new_sphere_location, 0.5, maroon)); 
scene_objects.push_back(new Plane(Y, -1, tile_floor)); 
scene_objects.push_back(new Plane(P, -1, gray)); 
scene_objects.push_back(new Plane(X, 1, gray)); 
scene_objects.push_back(new Plane(R, -1, tile_floor)); 
scene_objects.push_back(new Plane(M, -1, black)); 

を動的割り当て(new)を使用する必要がありますと仮定します。

ただし、リークメモリが必要ない場合は、適切な場合はdeleteを覚えておいてください。

また、単純なポインタの代わりにスマートポインタ(例ではstd::unique_ptr)を使用することもできます。

p .:申し訳ありませんが、私の悪い英語

関連する問題