C++ 在半个球体中绘制金字塔,指向某个方向
我有一个随机生成的点,有一些随机方向(随机lat和随机lon),我通过以下数学方程计算:C++ 在半个球体中绘制金字塔,指向某个方向,c++,opengl,math,random,C++,Opengl,Math,Random,我有一个随机生成的点,有一些随机方向(随机lat和随机lon),我通过以下数学方程计算: dirX = cos(lat * DEG_TO_RAD) * sin(lon * DEG_TO_RAD); dirY = sin(lat * DEG_TO_RAD); dirZ = cos(lat * DEG_TO_RAD) * cos(lon * DEG_TO_RAD); 我想做的是抓住这一点,画一个金字塔。这个金字塔的正方形底座在球体的一半上,生成的点应该是这个正方形的中心 然后根据点的方向,我想生成
dirX = cos(lat * DEG_TO_RAD) * sin(lon * DEG_TO_RAD);
dirY = sin(lat * DEG_TO_RAD);
dirZ = cos(lat * DEG_TO_RAD) * cos(lon * DEG_TO_RAD);
我想做的是抓住这一点,画一个金字塔。这个金字塔的正方形底座在球体的一半上,生成的点应该是这个正方形的中心
然后根据点的方向,我想生成这个金字塔的坐标,它应该位于一个半径为r的球体的球形外围,这个球体的中心是一个随机点
金字塔顶部指向随机点的方向,其他点应分别绘制。这是一张帮助你想象我想做什么的图片
这就是我正在做的:
//A
v[0][0][0]= radius * (cos(lat) * sin(lon));
v[0][0][1]= radius * sin(lat);
v[0][0][2]= radius * (cos(lat) * cos(lon));
//B
v[0][2][0]= radius * (cos(lat+(90*DEG_TO_RAD)) * sin(lon));
v[0][3][1]= radius * sin(lat+(90*DEG_TO_RAD));
v[0][4][2]= radius * (cos(lat+(90*DEG_TO_RAD)) * cos(lon));
//D
v[0][2][0]= radius * (cos(lat) * sin(lon+(90*DEG_TO_RAD)));
v[0][2][1]= radius * sin(lat);
v[0][2][2]= radius * (cos(lat) * cos(lon+(90*DEG_TO_RAD)));
// Side ABD
glColor3f(0.0, 0.0, 0.0);
glBegin(GL_POLYGON); // Start drawing a quad primitive
glVertex3f(v[0][0][0], v[0][0][1], v[0][0][2]); //A
glVertex3f(v[0][5][0], v[0][6][1], v[0][7][2]); //B
glVertex3f(v[0][2][0], v[0][2][1], v[0][2][2]); //D
//A
v[1][0][0]= radius * (cos(lat) * sin(lon));
v[1][0][1]= radius * sin(lat);
v[1][0][2]= radius * (cos(lat) * cos(lon));
//D
v[1][8][0]= radius * cos(lat) * sin(lon+(90*DEG_TO_RAD));
v[1][9][1]= radius * sin(lat);
v[1][10][2]= radius * (cos(lat) * cos(lon+(90*DEG_TO_RAD)));
//C
v[1][2][0]= radius * (cos(lat-(90*DEG_TO_RAD)) * sin(lon));
v[1][2][1]= radius * sin(lat-(90*DEG_TO_RAD));
v[1][2][2]= radius * (cos(lat-(90*DEG_TO_RAD)) * cos(lon));
// Side ADC
glColor3f(1, 1, 1);
glVertex3f(v[1][0][0], v[1][0][1], v[1][0][2]); //A
glVertex3f(v[1][11][0], v[1][12][1], v[1][13][2]); //D
glVertex3f(v[1][2][0], v[1][2][1], v[1][2][2]); //C
//A
v[2][0][0]= radius * (cos(lat) * sin(lon));
v[2][0][1]= radius * sin(lat);
v[2][0][2]= radius * (cos(lat) * cos(lon));
//C
v[2][14][0]= radius * (cos(lat-(90*DEG_TO_RAD)) * sin(lon));
v[2][15][1]= radius * sin(lat-(90*DEG_TO_RAD));
v[2][16][2]= radius * (cos(lat-(90*DEG_TO_RAD)) * cos(lon));
//E
v[2][2][0]= radius * (cos(lat) * sin(lon-(90*DEG_TO_RAD)));
v[2][2][1]= radius * sin(lat);
v[2][2][2]= radius * (cos(lat) * cos(lon-(90*DEG_TO_RAD)));
// Side ACE
glColor3f(0.5, 0.3, 0.1);
glVertex3f(v[2][0][0], v[2][0][1], v[2][0][2]); //A
glVertex3f(v[2][17][0], v[2][18][1], v[2][19][2]); //C
glVertex3f(v[2][2][0], v[2][2][1], v[2][2][2]); //E
//A
v[3][0][0]= radius * (cos(lat) * sin(lon));
v[3][0][1]= radius * sin(lat);
v[3][0][2]= radius * (cos(lat) * cos(lon));
//E
v[3][20][0]= radius * (cos(lat) * sin(lon-(90*DEG_TO_RAD)));
v[3][21][1]= radius * sin(lat);
v[3][22][2]= radius * (cos(lat) * cos(lon-(90*DEG_TO_RAD)));
//B
v[3][2][0]= radius * (cos(lat+(90*DEG_TO_RAD)) * sin(lon));
v[3][2][1]= radius * sin(lat+(90*DEG_TO_RAD));
v[3][2][2]= radius * (cos(lat+(90*DEG_TO_RAD)) * cos(lon));
// Side AEB
glColor3f(1.0, 0.8, 0.7);
glVertex3f(v[3][0][0], v[3][0][1], v[3][0][2]); //A
glVertex3f(v[3][23][0], v[3][24][1], v[3][25][2]); //E
glVertex3f(v[3][2][0], v[3][2][1], v[3][2][2]); //B
glEnd();
/*
//Square BECD
glColor3f(0.1, 0.3, 0.1);
glVertex3f((radius * cos(lat+(90*DEG_TO_RAD)) * sin(lon)), (radius * sin(lat+(90*DEG_TO_RAD)) * sin(lon)), (radius * cos(lon))); //headB
glVertex3f((radius * cos(lat) * sin(lon-(90*DEG_TO_RAD))), (radius * sin(lat) * sin(lon-(90*DEG_TO_RAD))), (radius * cos(lon-(90*DEG_TO_RAD)))); //headE
glVertex3f((radius * cos(lat-(90*DEG_TO_RAD)) * sin(lon)), (radius * sin(lat-(90*DEG_TO_RAD)) * sin(lon)), (radius * cos(lon))); //headC
glVertex3f((radius * cos(lat) * sin(lon+(90*DEG_TO_RAD))), (radius * sin(lat) * sin(lon+(90*DEG_TO_RAD))), (radius * cos(lon+(90*DEG_TO_RAD)))); //headD
glEnd();
*/
我们不要考虑画金字塔的方根,这个问题从以前就已经存在了!由于某种原因,它只是把事情搞得一团糟!我没有得到想要的结果。
GL\u POLYGON
绘制一个带有所有顶点的凸多边形
因为你只画三角形,所以你应该切换到
GL\u三角形
嗯,有趣的问题,如果我正确理解你的问题,我会这样做。将单位球体上生成的点设为(a,b,c)
,且a^2+b^2+c^2=1
,其中球体以原点为中心,然后我们通过(a,b,c)
及其对立面(-a,-b,-c)
绘制一条直线。这条线的方程式很容易确定。接下来我们在3d中找到一条垂直于这条线的线,有无限多的选择。通过这两条线,我们可以找到由这两条线确定的两个平面,它们与球体的交点将给出金字塔的坐标。同样,根据您选择的第二条线,有无限多的选择(将此视为将一条线固定,另一条线垂直于其中点并自由旋转)