我的光線跟蹤程序存在一些問題。我使用的初始代碼來自YouTube教程 - https://www.youtube.com/watch?v=k_aRiYSXcyo(關於光線追蹤的caleb穿透教程)。如何在光線跟蹤面向對象程序中聲明變量時,其中大多數矢量和相機聲明?
我然後重新制成在main.cpp中的代碼轉換成使用Visual Studio一個面向對象的代碼。
點是封裝函數和計算,以便然後爲使用的OpenMP(在Visual Studio)或(在Visual Studio),這樣我可以多線程整個程序。
但在此之前我這樣做,我有變量的具體聲明的一個問題。他們大多數是從我的Vect.h頭文件和Camera.h文件的矢量聲明。
下面是從的main.cpp和更具體的代碼 - 只是聲明。我不會發布算法函數和計算,因爲他們工作,我沒有與他們的問題。
類中,我聲明一些變量和方法的功能
//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));
}
所以, 當我用對象調用main()
函數中的define()
函數時,我得到一個黑屏作爲圖像輸出。當我在計算函數中調用define()
函數時,程序在啓動時立即崩潰。
如果我將define()
函數的內容移動到private:
類中,我會得到聲明的錯誤。
define()
中的所有變量都必須看到,但我不知道該怎麼做。
我該如何解決這個問題?它不起作用,我只是得到一個黑屏圖像,或者它只是崩潰。
對不起,如果有什麼不清楚(這是我的第一個問題在stackoverflow)。
謝謝!
你的問題是什麼? – immibis
我該如何解決這個問題。我想要一張圖片,但它不起作用 – mnestorov