问题遇到的现象和发生背景
复现论文:Multiple Viewpoint Rendering
进行三角形切片处理遇到指针指向的内容连续输出两次,第二次输出地址,不是指向的内容。
问题相关代码,请勿粘贴截图
Triangle_slices* slice_triangle(Triangle_slices* triangle_slices, Scene_triangles t) //verts[3] 为场景一个三角形 verts[0]=Va verts[1]=Vb verts[2]=Vs
{
Vertex* sorted_v[3] = {NULL}; //指针数组: 数组元素为指针的数组
//cout << sorted_v[0] << " " << sorted_v[1] << " " << sorted_v[2] << endl;
*sorted_v = sort_by_y(sorted_v, t); //根据y值排好序 返回指针数组的首地址: sort_by_y函数返回一个地址
//cout << sorted_v[0] << " " << sorted_v[1] << " " << sorted_v[2] << endl;
//cout << sorted_v[0]->y << " " << sorted_v[1]->y << " " << sorted_v[2]->y << endl;
//cout << sorted_v[0]->y << " " << sorted_v[0]->y << " " << sorted_v[0]->y << endl;
//cout << sorted_v[0]->y << endl;
//sorted_v[0]->y = 0;
//sorted_v[1]->y = 1;
//sorted_v[2]->y = 2;
cout << sorted_v[0]->y << " " << sorted_v[1]->y << " " << sorted_v[2]->y << endl;
cout << sorted_v[0]->y << " " << sorted_v[1]->y << " " << sorted_v[2]->y << endl;
float dist10 = sorted_v[1]->y - sorted_v[0]->y;
float dist21 = sorted_v[2]->y - sorted_v[1]->y;
float dist20 = sorted_v[2]->y - sorted_v[0]->y;
//cout << dist10 << " " << dist21 << " " << dist20 << endl;
//cout << dist10 << " " << dist21 << " " << dist20 << endl;
//cout << sorted_v[0]->y << " " << sorted_v[1]->y << " " << sorted_v[2]->y << endl;
float ystart = (float)ceil(sorted_v[0]->y); //round up to the nearest scanline floor()函数
//cout << ystart << endl;
float y = ystart;
//cout << ystart - sorted_v[0]->y << endl;
//cout << sorted_v[0]->y << " " << sorted_v[1]->y << " " << sorted_v[2]->y << endl;
float a, b, delta_a, delta_b;
if (dist10 == 0)
{
//cout << "ok" << endl;
a = 1;
delta_a = 0;
}
else
{
a = 1 - ((ystart - sorted_v[0]->y) / dist10);
//cout << "ok" << a << endl;
delta_a = 1.0 / dist10;
//cout << delta_a << endl;
}
if (dist20 == 0)
{
b = 1;
delta_b = 0;
}
else
{
b = 1 - ((ystart - sorted_v[0]->y) / dist20);
delta_b = 1.0 / dist20;
}
//cout << a << endl;
//cout << sorted_v[1]->y << endl;
//first part of triangle: v[0] shares edges with v[1] (param a) and v[2] (param b)
while (y < sorted_v[1]->y) {
//cout << y << " ok " << triangle_slices->f <<endl;
triangle_slices = add_slice(triangle_slices, y, sorted_v[0], a, sorted_v[1], b, sorted_v[2]); //0 1 2
y += 1;
a -= delta_a;
b -= delta_b;
triangle_slices->f++;
}
//second part of triangle: v[2] shares edges with v[1] (param a) and v[0] (param b)
a = ((y - sorted_v[1]->y) / dist21);
delta_a = 1.0 / dist21;
b = 1.0 - b;
while (y < sorted_v[2]->y) {
//cout << y << " " << triangle_slices->f << endl;
triangle_slices = add_slice(triangle_slices, y, sorted_v[2], a, sorted_v[1], b, sorted_v[0]); // 2 1 0
y += 1;
a += delta_a;
b += delta_b;
triangle_slices->f++;
}
return triangle_slices;
}
运行结果及报错内容
93.7809 107.108 149.753
-1.07374e+08 1.4013e-45 2.67131e+35
我的解答思路和尝试过的方法
因为是用指针动态开辟内存,指针再第一次调用过后,指向的地址得到释放,发生错误指向问题。
我想要达到的结果
指针指向内容固定
可以复现这篇论文的,麻烦私信,可以有偿咨询。