2013-04-25 58 views
0

我正在用C++编写光线追踪器,而且我很难理解为什么我的输出图像不包含所有应该在那里的对象。也就是说,我正在与球体和飞机一起工作,而且我不能在每个实体上绘制多个实例。Raytracer将不会渲染一个对象的多个实例

从ASCII文件读取对象值(例如半径,位置,法线等)。这是我的相交测试代码。

//check primary ray against each object 
    for(int size = 0; size < objList.size(); size++){ 
     //if intersect 
     if(objList[size]->intersect(ray,origin,&t)){ 
     if(t < minDist){ //check depth 
      minDist = t; //update depth 
      bestObj = size; //update closest object 
     } 
     } 
    } 
    vec3 intersection = origin + minDist*ray; 

//figure out what to draw, if anything 
    color_t shadeColor; 
    if(bestObj != -1){ //valid object 
     //get base color 
     //using rgb color 
     if(objList[bestObj]->rgbColor != vec3(-1)){ 
     shadeColor.r = objList[bestObj]->rgbColor.x; 
     shadeColor.g = objList[bestObj]->rgbColor.y; 
     shadeColor.b = objList[bestObj]->rgbColor.z; 
     } 
     //else using rgbf color 
     else if(objList[bestObj]->rgbfColor != vec4(-1)){ 
     shadeColor.r = objList[bestObj]->rgbfColor.x; 
     shadeColor.g = objList[bestObj]->rgbfColor.y; 
     shadeColor.b = objList[bestObj]->rgbfColor.z; 
     //need to do something with alpha value 
     } 
     //else invalid color 
     else{ 
     cout << "Invalid color." << endl; 
     } 

//...the rest is just shadow and reflection tests. There are bugs here as well, but those are for another post 

上述代码位于检查每个像素的循环内。 '射线'是射线的方向,'起源'是射线的起源。 'objList'是一个保存场景中每个对象的stl向量。我已经测试过以确保每个对象实际上都被放入向量中。

我知道我的交集测试正在工作......至少对于呈现的每种类型的一个对象。我已经将程序打印到文件中的所有'bestObj'获得的值,但似乎从未注意到除最后一个之外的任何对象都是'bestObj'。我意识到这是问题,没有其他对象被设置为'bestObj',但我不明白为什么!

任何帮助,将不胜感激:)

+0

你确定你的vector不能保存'size()'次相同的指针吗? – didierc 2013-04-25 03:09:03

回答

0

我想通了这个问题,这要归功于didierc。我不确定他在说什么,但是这让我想起我是如何处理我的指针的。事实上,尽管我的媒介推动了每一个对象,但我并不是每次推回一个对象时都会创建新的对象。这导致stl向量中的每个球体指向相同的一个(也就是从文件读入的最后一个球体)!

相关问题