jjzjj

c++ - 没有过剩的 OpenGL 渲染球体 : What is wrong with this implementation?

coder 2024-02-24 原文

在我的渲染循环中,我有以下逻辑。我还有其他东西渲染到屏幕上,它们也渲染了(我删除了该代码以切中要点)。这段代码不渲染球体,我不明白为什么不。我在数学上遗漏了什么吗?我已经逐步调试了调试器,值似乎是正确的。注意 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();
}

最佳答案

检查这些行中的代码

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

关于c++ - 没有过剩的 OpenGL 渲染球体 : What is wrong with this implementation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11964361/

有关c++ - 没有过剩的 OpenGL 渲染球体 : What is wrong with this implementation?的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  5. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  6. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  7. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  10. ruby - 没有类方法获取 Ruby 类名 - 2

    如何在Ruby中获取BasicObject实例的类名?例如,假设我有这个:classMyObjectSystem我怎样才能使这段代码成功?编辑:我发现Object的实例方法class被定义为returnrb_class_real(CLASS_OF(obj));。有什么方法可以从Ruby中使用它? 最佳答案 我花了一些时间研究irb并想出了这个:classBasicObjectdefclassklass=class这将为任何从BasicObject继承的对象提供一个#class您可以调用的方法。编辑评论中要求的进一步解释:假设你有对象

随机推荐