我正在使用OpenGL 4.2。我正在OpenGL片段着色器中的另一个函数内部调用一个函数,其中内部函数(pix2loc)返回一个bool变量作为函数参数,然后外部函数(pix2ang)使用此bool变量做出决定。更确切地说,我有:
void pix2ang (int order_, bool is_nest, int pix, out double theta, out double phi)
{
double z, sth;
bool have_sth;
pix2loc (order_, is_nest, pix, z, phi, sth, have_sth);
if(have_sth)
theta = atan(float(sth),float(z));
else
theta = acos(float(z));
}
其中pix2loc函数返回布尔变量have_sth作为函数参数,如下所示:
void pix2loc (int order_, bool is_nest, int pix, out double z, out double phi, out double sth, out bool have_sth)
{
.....
if <condition true>
have_sth = true;
else
have_sth = false;
}
我注意到pix2ang内部的have_sth不能按预期工作,但是如果我反转if语句并将其更改为
if(!have_sth)
theta = acos(float(z));
else
theta = atan(float(sth),float(z));
然后按预期方式工作。这使代码不可靠,我也不完全了解问题所在。
好的。我决定编写pix2loc的整个功能,以便它可能有助于理解问题。
void pix2loc (int order_, bool is_nest, int pix, out double z, out double phi, out double sth, out bool have_sth)
{
int nside_ = int(1)<<order_;
int npface_ = nside_<<order_;
int ncap_ = (npface_-nside_)<<1;
int npix_ = 12*npface_;
double fact2_ = 4./npix_;
double fact1_ = (nside_<<1)*fact2_;
have_sth=false;
if (!is_nest)
{
if (pix<ncap_) // North Polar cap
{
int iring = (1+int(isqrt(1+2*pix)))>>1; // counted from North pole
int iphi = (pix+1) - 2*iring*(iring-1);
double tmp=(iring*iring)*fact2_;
z = 1.0 - tmp;
if (z>0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
phi = (iphi-0.5) * halfpi/iring;
}
else if (pix<(npix_-ncap_)) // Equatorial region
{
int nl4 = 4*nside_;
int ip = pix - ncap_;
int tmp = (order_>=0) ? ip>>(order_+2) : ip/nl4;
int iring = tmp + nside_;
int iphi = ip-nl4*tmp+1;
// 1 if iring+nside is odd, 1/2 otherwise
double fodd = bool((iring+nside_)&1) ? 1. : 0.5;
z = (2*nside_-iring)*fact1_;
phi = (iphi-fodd) * pi*0.75*fact1_;
}
else // South Polar cap
{
int ip = npix_ - pix;
int iring = (1+int(isqrt(2*ip-1)))>>1; // counted from South pole
int iphi = 4*iring + 1 - (ip - 2*iring*(iring-1));
double tmp=(iring*iring)*fact2_;
z = tmp - 1.0;
if (z<-0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
phi = (iphi-0.5) * halfpi/iring;
}
}
else
{
int face_num, ix, iy;
nest2xyf(order_, pix,ix,iy,face_num);
int jr = (int(jrll[face_num])<<order_) - ix - iy - 1;
int nr;
if (jr<nside_)
{
nr = jr;
double tmp=(nr*nr)*fact2_;
z = 1 - tmp;
if (z>0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
}
else if (jr > 3*nside_)
{
nr = nside_*4-jr;
double tmp=(nr*nr)*fact2_;
z = tmp - 1;
if (z<-0.99) { sth=sqrt(tmp*(2.-tmp)); have_sth=true; }
}
else
{
nr = nside_;
z = (2*nside_-jr)*fact1_;
}
int tmp=int(jpll[face_num])*nr+ix-iy;
if (tmp<0) tmp+=8*nr;
phi = (nr==nside_) ? 0.75*halfpi*tmp*fact1_ : (0.5*halfpi*tmp)/nr;
}
}
其中pi,halfpi,jrll和jpll是在片段着色器开头定义为的常量变量
#version 420 core
const double pi = 3.141592653589793;
const double halfpi = 0.5 * pi;
const int jrll[] = { 2,2,2,2,3,3,3,3,4,4,4,4 };
const int jpll[] = { 1,3,5,7,0,2,4,6,1,3,5,7 };