我的光线追踪程序遇到了一些问题。我使用的初始代码来自youtube教程 - https://www.youtube.com/watch?v=k_aRiYSXcyo(关于光线追踪的caleb piercy教程)。
然后,我使用Visual Studio将main.cpp中的代码重新制作为面向对象的代码。
重点是封装函数和计算,以便使用OpenMP(在Visual Studio中)或(在Visual Studio中),以便我可以对整个程序进行多线程处理。
但在我这样做之前,我对变量的特定声明有问题。它们中的大多数是来自我的 Vect.h 头文件和 Camera.h 文件的矢量声明。
这是来自主代码的代码.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:
中,则 I 得到声明的错误。
define()
中的所有变量都必须看到,但我不知道该怎么做。
我该如何解决这个问题?它不起作用,结果我只是得到黑屏图像,或者它只是崩溃。
抱歉,如果有什么不清楚的地方。(这是我关于StackOverflow的第一个问题)。
谢谢!
确切地说,导致黑屏的问题在哪里,但我对崩溃并不感到惊讶。
您应该考虑在方法中声明的变量(和相对值),如在 define()
中,在方法结束时会丢失。
所以看看下面的代码
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_sphere
、scene_sphere2
等),然后你在类(scene_objects
)的成员中添加它们的指针(我假设Object
是Sphere
和Plane
的基类)。
当define()
结束时,它就是执行,七个变量(scene_sphere
、scene_sphere2
等)结束了它们的生命,它们的记忆被释放了。因此,scene_objects
中的七个指针指向释放的堆栈内存,这些内存可以回收用于其他不同的变量。
当您尝试(如果您尝试)使用七个指针之一时,程序很可能会崩溃。
同样的问题
Light scene_light(light_position, white_light);
light_sources.push_back(dynamic_cast<Source*>(&scene_light));
我想你应该以这种方式使用动态分配(new
)
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));
所以尖头的物体可以在define()
结束时幸存下来.
但请记住,如果您不想泄漏内存,请在适当的时候delete
它们。
或者,更好的是,您可以使用智能指针(std::unique_ptr
示例)而不是简单的指针。
PS:对不起,我的英语不好