加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – OpenGL Render Sphere Without Glut:这个实现有什么问题?

发布时间:2020-12-16 07:31:51 所属栏目:百科 来源:网络整理
导读:在我的渲染循环中,我有以下逻辑.我还有其他东西渲染到屏幕上,然后渲染,(我删除了那些代码以便直接指向).这段代码不会渲染一个球体,我无法弄清楚为什么不这样做.我在数学中遗漏了什么吗?我已经完成了调试器,值似乎正确.注意mBubbleDiameter在此对象的构造函
在我的渲染循环中,我有以下逻辑.我还有其他东西渲染到屏幕上,然后渲染,(我删除了那些代码以便直接指向).这段代码不会渲染一个球体,我无法弄清楚为什么不这样做.我在数学中遗漏了什么吗?我已经完成了调试器,值似乎正确.注意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,mWidth,mHeight);
        glOrtho(0,mHeight,-mBubbleDiameter,mBubbleDiameter);

    glMatrixMode(GL_MODELVIEW);
        glScissor(0,mHeight);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glClearColor(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))),(0 + (r * sin(rphi + phid) * cos(rtheta + thetad))));
        glVertex3f(
            (x + (r * sin(rphi) * cos(rtheta + thetad))),(0 + (r * sin(rphi) * cos(rtheta + thetad))));
    }
    glEnd();
}

解决方法

在这些行中检查您的代码

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);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读