gpt4 book ai didi

c++ - 帮助实现 Diamond Square 算法

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:34:48 24 4
gpt4 key购买 nike

我正在尝试实现 Diamond-square algorithm ,但问题是只有部分位图被填充,我不确定出了什么问题。我递归地做:

    GLuint CreateDsquare()
{
std::vector<GLubyte> pdata(256 * 256 * 4);


vector2i loc;
vector2i sz;
GLubyte val;

sz.x = 256;
sz.y = 256;

val = rand() % 255;

loc = vector2i(0,0);

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;

loc.x = sz.x - 1;
loc.y = 0;

val = rand() % 255;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;

loc.x = sz.x - 1;
loc.y = sz.y - 1;

val = rand() % 255;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;

loc.x = 0;
loc.y = sz.y - 1;

val = rand() % 255;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;

RescursiveDiamond(pdata,sz,vector2i(0,0));

return CreateTexture(pdata,256,256);
}

void RescursiveDiamond(std::vector<GLubyte> &pdata,vector2i psz, vector2i offset)
{
int val;
int newnum;
if(psz.x < 2 && psz.y < 2)
{
return;
}


vector2i loc;
vector2i sz = psz;

std::vector<int> pvertz(4,0);

loc = offset;
pvertz[0] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];

loc.x = offset.x + (psz.x - 1);
loc.y = offset.y;
pvertz[1] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];

loc.x = offset.x + (psz.x - 1);
loc.y = offset.y + (psz.y - 1);
pvertz[2] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];

loc.x = offset.x;
loc.y = offset.y + (psz.y - 1);
pvertz[3] = pdata[loc.y * 4 * sz.x + loc.x * 4 + 0];


val = (pvertz[0] + pvertz[1]) / 2;
val += 255;
loc.x = (offset.x + (sz.x - 1)) / 2;
loc.y = offset.y;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;


val = (pvertz[1] + pvertz[2]) / 2;
val += 255;
loc.x = (offset.x + (sz.x)) - 1;
loc.y = ((offset.y + (sz.y)) / 2) - 1;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;


val = (pvertz[3] + pvertz[2]) / 2;
val += 255;
loc.x = ((offset.x + (sz.x)) / 2) - 1;
loc.y = (offset.y + (sz.y)) - 1 ;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;


val = (pvertz[0] + pvertz[3]) / 2;
val += 255;
loc.x = offset.x;
loc.y = (offset.y + (sz.y)) - 1 ;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;

//center

val = (pdata[(offset.y) * 4 * sz.x + ((offset.x + (sz.x - 1)) / 2) * 4 + 0] +
pdata[(offset.y + (sz.y - 1)) * 4 * sz.x + ((offset.x + (sz.x - 1)) / 2) * 4 + 0]) / 2;

int ad = (rand() % 12) - 6;
if(val + ad < 0)
{
val = 0;
}
else
{
val += ad;
}

val += 255;

loc.x = ((offset.x + (sz.x) ) / 2) - 1;
loc.y = ((offset.y + (sz.y)) / 2) - 1;

pdata[loc.y * 4 * sz.x + loc.x * 4 + 0] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 1] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 2] = val;
pdata[loc.y * 4 * sz.x + loc.x * 4 + 3] = 255;



vector2i newoffset;
vector2i newparentsz;

newoffset = offset;
newparentsz = (psz / 2);

RescursiveDiamond(pdata,newparentsz,newoffset);

newoffset.x = offset.x + (newparentsz.x);
newoffset.y = offset.y;

RescursiveDiamond(pdata,newparentsz,newoffset);


newoffset.x = offset.x;
newoffset.y = offset.y + (newparentsz.y);

RescursiveDiamond(pdata,newparentsz,newoffset);
newoffset.x = offset.x + (newparentsz.x);
newoffset.y = offset.y + (newparentsz.y);

RescursiveDiamond(pdata,newparentsz,newoffset);



}

我怀疑我可能会用错误的偏移量或其他东西记忆函数。

偏移量就像左上角,然后是大小,这些加起来就是正方形。

这里有什么问题吗?

谢谢

最佳答案

好的,首先,让我们开始清理违反D-R-Y的违规行为,您的代码应该阅读更多内容:

int position( _y, _x, _offset ){
return _y * _x * 4 + _x * 4 + _offset;
}

void adjust(vector<GLubyte> &pdata, _x, _y){
GLubyte val = rand() % 255;
for(int j=0; j < 3; ++j){
pdata[ position( _y, _x, j ) ] = val;
}
pdata[ position( _y, _x, 3 ) ] = 255;
}

GLuint CreateDsquare(){
vector2i sz;

sz.x = 256;
sz.y = 256;

adjust( pdata, 0, 0 );
adjust( pdata, sz.x - 1, 0 );
adjust( pdata, sz.x -1, sz.y - 1 );
adjust( pdata, 0, sz.y - 1 );

RescursiveDiamond(pdata,sz,vector2i(0,0));

return CreateTexture(pdata,256,256);
}

您能否将其余部分格式化,使其更具可读性/可理解性?然后我会更新,以便我可以更好地回答你的问题(如果有人没有打败我或者那个女人认为我有足够的计算机时间。)

关于c++ - 帮助实现 Diamond Square 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3692639/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com