在我的渲染循环中,我有以下逻辑。我有其他东西渲染到屏幕上,它们渲染,(我删除了该代码以直截了当)。这段代码不渲染球体,我不知道为什么不。我在数学中遗漏了什么吗?我已经使用了调试器,并且这些值似乎是正确的。注意 mBubbleDiameter 在此对象的构造函数中设置为 20。
static GLfloat staticDegreesToRadians(GLfloat tmpDegrees) {
return tmpDegrees * ((std::atan(1.0f)*4)/180.0f);
}
void LedPannelWidget::updateGL() {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, mWidth, mHeight);
glOrtho(0, mWidth, 0, mHeight, -mBubbleDiameter, mBubbleDiameter);
glMatrixMode(GL_MODELVIEW);
glScissor(0, 0, mWidth, mHeight);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.92f, 0.92f, 0.92f, 1.0);
glLoadIdentity();
const GLfloat tmpRadius = mDiameter/2.0f;
const GLfloat tmpDelta = 5.00f;
const GLfloat tmpDeltaRadians = staticDegreesToRadians(tmpDelta);
for (int32_t tmpTheta = 180; tmpTheta > 0; tmpTheta -= tmpDelta) {
for (int32_t tmpPhi = 0; tmpPhi < 360; tmpPhi += tmpDelta) {
GLfloat tmpThetaRadians = staticDegreesToRadians(tmpTheta);
GLfloat tmpPhiRadians = staticDegreesToRadians(tmpTheta);
GLfloat tmpX1 = tmpRadius *
std::sin(tmpThetaRadians) *
std::cos(tmpPhiRadians);
GLfloat tmpY1 = tmpRadius *
std::sin(tmpThetaRadians) *
std::cos(tmpPhiRadians);
GLfloat tmpZ1 = tmpRadius *
std::cos(tmpThetaRadians);
GLfloat tmpX2 = tmpRadius *
std::sin(tmpThetaRadians - tmpDeltaRadians) *
std::cos(tmpPhiRadians);
GLfloat tmpY2 = tmpRadius *
std::sin(tmpThetaRadians - tmpDeltaRadians) *
std::cos(tmpPhiRadians);
GLfloat tmpZ2 = tmpRadius *
std::cos(tmpThetaRadians - tmpDeltaRadians);
GLfloat tmpX3 = tmpRadius *
std::sin(tmpThetaRadians - tmpDeltaRadians) *
std::cos(tmpPhiRadians + tmpDeltaRadians);
GLfloat tmpY3 = tmpRadius *
std::sin(tmpThetaRadians - tmpDeltaRadians) *
std::cos(tmpPhiRadians + tmpDeltaRadians);
GLfloat tmpZ3 = tmpRadius *
std::cos(tmpThetaRadians - tmpDeltaRadians);
GLfloat tmpX4 = tmpRadius *
std::sin(tmpThetaRadians) *
std::cos(tmpPhiRadians + tmpDeltaRadians);
GLfloat tmpY4 = tmpRadius *
std::sin(tmpThetaRadians) *
std::cos(tmpPhiRadians + tmpDeltaRadians);
GLfloat tmpZ4 = tmpRadius *
std::cos(tmpThetaRadians);
glBegin(GL_QUADS);
glVertex3f(tmpX1, tmpY1, tmpZ1);
glVertex3f(tmpX2, tmpY2, tmpZ2);
glVertex3f(tmpX3, tmpY3, tmpZ3);
glVertex3f(tmpX4, tmpY4, tmpZ4);
glEnd();
if (tmpGLError != GL_NO_ERROR) {
QApplication::exit(0);
}
}
}
swapBuffers();
}
工作 GL 算法:
const GLfloat r = mDiameter/2.0f;
const GLfloat phid = 20.00f;
const GLfloat thetad = 20.00f;
const GLfloat x = mCenterXCoord;
const GLfloat y = mCenterYCoord;
using namespace std;
for (int32_t phi = 180; phi > 0; phi -= phid) {
int32_t theta = 0;
GLfloat rphi = staticDegreesToRadians(phi);
GLfloat rtheta = staticDegreesToRadians(theta);
glBegin(GL_QUAD_STRIP);
glColor3f(mCurrentColor.red()/255.0, mCurrentColor.green()/255.0,
mCurrentColor.blue()/255.0);
glVertex3f(
(x + (r * sin(rphi) * cos(rtheta))),
(y + (r * cos(rphi))),
(0 + (r * sin(rphi) * cos(rtheta))));
glVertex3f(
(x + (r * sin(rphi + phid) * cos(rtheta))),
(y + (r * cos(rphi + phid))),
(0 + (r * sin(rphi + phid) * cos(rtheta))));
for (; theta < 360; theta += thetad) {
rtheta = staticDegreesToRadians(theta);
glVertex3f(
(x + (r * sin(rphi + phid) * cos(rtheta + thetad))),
(y + (r * cos(rphi + phid))),
(0 + (r * sin(rphi + phid) * cos(rtheta + thetad))));
glVertex3f(
(x + (r * sin(rphi) * cos(rtheta + thetad))),
(y + (r * cos(rphi))),
(0 + (r * sin(rphi) * cos(rtheta + thetad))));
}
glEnd();
}