简体   繁体   中英

OpenGL Render Sphere Without Glut: What is wrong with this implementation?

In my rendering loop I have the following logic. I have other things render to the screen, and they render, (I removed that code to get right to the point). This code does not render a sphere, and I can't figure out why not. Am I missing something in the math? I have stepped through with the debugger and the values seem right. Note mBubbleDiameter is 20 as set in the constructor of this object.

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

Working GL Algorithm:

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

Check your code in these lines

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

You need changes see below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM