C++ 没有Glut的OpenGL渲染球体:这个实现有什么问题?
在我的渲染循环中,我有以下逻辑。我有其他东西渲染到屏幕上,它们也会渲染(我删除了代码,以便直截了当)。这段代码不渲染球体,我不明白为什么不渲染。我在数学上遗漏了什么吗?我已经使用了调试器,这些值似乎是正确的。注:此对象的构造函数中设置的直径为20C++ 没有Glut的OpenGL渲染球体:这个实现有什么问题?,c++,qt,opengl,C++,Qt,Opengl,在我的渲染循环中,我有以下逻辑。我有其他东西渲染到屏幕上,它们也会渲染(我删除了代码,以便直截了当)。这段代码不渲染球体,我不明白为什么不渲染。我在数学上遗漏了什么吗?我已经使用了调试器,这些值似乎是正确的。注:此对象的构造函数中设置的直径为20 static GLfloat staticDegreesToRadians(GLfloat tmpDegrees) { return tmpDegrees * ((std::atan(1.0f)*4)/180.0f); } void LedP
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();
}
static GLfloat staticdegrees弧度(GLfloat tmpdgrees){
返回tmpDegrees*((标准:atan(1.0f)*4)/180.0f);
}
void LedPannelWidget::updateGL(){
glMatrixMode(GL_投影);
glLoadIdentity();
glViewport(0,0,mWidth,mHeight);
格洛托(0,mWidth,0,mHweight,-mbubbletdiameter,mbubbletdiameter);
glMatrixMode(GLU模型视图);
glScissor(0,0,mWidth,mhweight);
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
glClearColor(0.92f,0.92f,0.92f,1.0);
glLoadIdentity();
常数GLfloat tmpRadius=mDiameter/2.0f;
常数GLfloat tmpDelta=5.00f;
常量GLfloat tmpDelta=静态度数弧度(tmpDelta);
对于(int32_t tmpTheta=180;tmpTheta>0;tmpTheta-=tmpDelta){
对于(int32_t tmpPhi=0;tmpPhi<360;tmpPhi+=tmpDelta){
GLfloat tmpThetaRadians=静态度数弧度(tmpTheta);
GLfloat Tmphiradians=静态度数弧度(tmpTheta);
GLfloat tmpX1=tmpRadius*
标准::sin(特姆普塔拉迪亚人)*
标准::cos(tmpPhiRadians);
GLfloat tmpY1=tmpRadius*
标准::sin(特姆普塔拉迪亚人)*
标准::cos(tmpPhiRadians);
GLfloat tmpZ1=tmpRadius*
std::cos(tmpThetaRadians);
GLfloat tmpX2=tmpRadius*
标准::sin(tmpThetaRadians-tmpDeltaRadians)*
标准::cos(tmpPhiRadians);
GLfloat tmpY2=tmpRadius*
标准::sin(tmpThetaRadians-tmpDeltaRadians)*
标准::cos(tmpPhiRadians);
GLfloat tmpZ2=tmpRadius*
标准::cos(tmpThetaRadians-tmpDeltaRadians);
GLfloat tmpX3=tmpRadius*
标准::sin(tmpThetaRadians-tmpDeltaRadians)*
标准::cos(Tmphiradians+Tmpdelataradians);
GLfloat tmpY3=tmpRadius*
标准::sin(tmpThetaRadians-tmpDeltaRadians)*
标准::cos(Tmphiradians+Tmpdelataradians);
GLfloat tmpZ3=tmpRadius*
标准::cos(tmpThetaRadians-tmpDeltaRadians);
GLfloat tmpX4=tmpRadius*
标准::sin(特姆普塔拉迪亚人)*
标准::cos(Tmphiradians+Tmpdelataradians);
GLfloat tmpY4=tmpRadius*
标准::sin(特姆普塔拉迪亚人)*
标准::cos(Tmphiradians+Tmpdelataradians);
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);
格伦德();
如果(tmpGLError!=总帐无错误){
QApplication::退出(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();
}
const GLfloat r=mDiameter/2.0f;
常数GLfloat phid=20.00f;
常数GLfloat thetad=20.00f;
常量GLfloat x=mCenterXCoord;
const GLfloat y=mCenterYCoord;
使用名称空间std;
对于(int32_t phi=180;phi>0;phi-=phid){
int32_tθ=0;
GLRPHI=静态弧度(φ);
GLfloat rtheta=静态度数弧度(θ);
glBegin(GLU四条带);
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));
对于(;θ<360;θ+=θ){
Rheta=静态度数弧度(θ);
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));
}
格伦德();
}
检查这些行中的代码
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 tmpThetaRadians = staticDegreesToRadians(tmpTheta);
GLfloat tmpPhiRadians = staticDegreesToRadians(tmpPhi);
GLfloat tmpX1 = tmpRadius *
std::sin(tmpThetaRadians) *
std::sin(tmpPhiRadians);
GLfloat tmpY1 = tmpRadius *
std::sin(tmpThetaRadians) *
std::cos(tmpPhiRadians);
GLfloat tmpZ1 = tmpRadius *
std::cos(tmpThetaRadians);
您使用的是不推荐使用的方法,并且您使用的函数在OpenGL 3.1及更新版本中不可用,这是一个类似的场景,可以为您提供一个想法。我为其撰写此文章的项目不允许VBO、着色器或任何扩展。我在一个基于opengl 2.0的环境中写这篇文章。不过,非常感谢您的评论。我无法使用glut,也不允许我使用它。你可以使用三角形条带(顶部和底部都有风扇)来改进它。是的,我可以,但我想先让它工作。当我使用代码时,屏幕上不会显示任何内容。但我看不出数学有什么问题。根据我的数学书,公式是:x=r\,\sin\theta\,\cos\varphi\quad y=r\,\sin\theta\,\sin\varphi\quad z=r\,\cos\theta\quad你说的是y=r\,\sin\theta\,\sin\varphi\quad但用的是y=r\,\sin\theta\,\cos\varphi\quad位于代码上方。我还渲染了球体,就像你正在做的一样。你可以在这里看到,它工作得很好。这个程序是在C++和OpenGL(GLUT)中,我们以一种非常简单的方式来处理这个问题,但是即使在你建议的改变之后,它也是不可行的。如果我重新发布我的代码,我可以