[英]pointers messing with my values
我正在这个学校的项目上工作,该项目画了两个兔子,您应该可以看透其中的一个。 请注意,整个代码只是教师制作的模板,我们应该使用自己的代码填写。 因此,我有一个遍历所有像素的循环,而不是立即绘制,我们应该立即将它们存储在Head和Node Buffer中。 HeadBuffer仅存储索引,以后在访问NodeBuffer时将使用这些索引。
参见此处: http : //developer.amd.com/wordpress/media/2013/06/2041_final.pdf
我的问题是,在填充缓冲区时,在存储完值后无法正确撤消该值,因此我无法正确访问它以对其进行排序,混合颜色并写出。
用C写
声明:
//in student.c
local:
int Q;
global:
S_Frag fragmentIn;
S_Frag *fragmentOut;
int *ext;
int index;
//up to here
。
typedef struct S_Vector
{
int size;
int reserved;
int elemSize;
char * data;
} S_Vector;
typedef struct S_Frag
{
S_RGBA color;
double depth;
int next;
} S_Frag;
//returns pointer to the i-th element
IZG_INLINE void * vecGetPtr(S_Vector *pVec, int i)
{
IZG_ASSERT(pVec && i < pVec->size);
return pVec->data + i * pVec->elemSize;
}
IZG_INLINE S_Frag makeFrag(S_RGBA color, double depth, int next)
{
S_Frag frag;
frag.color = color;
frag.depth = depth;
frag.next = next;
return frag;
}
//appends pElem to the end of S_Vector, returns index of new pElem
int vecPushBack(S_Vector *pVec, void *pElem);
//sets the i-th elem. of pVec to pElem
IZG_INLINE void vecSet(S_Vector *pVec, int i, void * pElem)
{
IZG_ASSERT(pVec && i < pVec->size && pElem);
MEMCOPY(pVec->data + i * pVec->elemSize, pElem, pVec->elemSize);
}
。
//student.c
Q = x + y*globWidth; //make 2 dimensional array 1 dimensional
ext = (int *) vecGetPtr(HeadBuffer, Q); //get index that was in headBuffer
fragmentIn = makeFrag(color, z, *ext); //put it in a new fragment
index = vecPushBack(NodeBuffer, &fragmentIn);//put new frag. in nodebuffer
vecSet(HeadBuffer, Q, &index);//update index in headbuffer
//here comes the messed up part
fragmentOut = (S_Frag *) vecGetPtr(NodeBuffer, index);
printf("::%d\n", fragmentOut->next);
// expected: -1 or 435 or 46 or 1120
// gotten: -8503629141
当我让循环运行并像这样打印它时,获得的值也会改变,但仍然有些巨大
头缓冲区被初始化为保留sizeof(int)并保留width * heigth(窗口的高度),每个元素的开头为-1
只是将Nodebuffer设置为容纳sizeof(S_Frag)的元素,而vecPushBack()为我管理内存
当我尝试printf()* ext,index,Q,fragmentIn.next时,一切看起来都很好
如果您能分辨出错误,我将非常感谢,谢谢
哦,顺便说一句,除了我的student.c文件,我无法更改其他任何内容
编辑:我还尝试使用片段来获取深度
printf("::%f\n", fragmentOut->depth);
在输出中有一些0,-0.00000以及数字,例如-1523 ... 25 zeros ... 0.00000
好的,我已经弄清楚了,当然是我犯了错误而不是仔细检查了。还有另一个函数可以清除缓冲区,我认为它不经常清除它们,但是实际上它可以非常频繁地清除它们,并且造成所有混乱:D
无论如何,谢谢您的时间,解决了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.