绘制模型时OpenGL分段错误

时间:2018-05-24 23:41:19

标签: c++ opengl model segmentation-fault

我遇到了一件奇怪的事情。看看这部分代码:

Models::ziemia.drawSolid();
Models::ptaszek.drawSolid();
Models::dach5.drawSolid();

Models::sciany1.drawSolid();
Models::sciany2.drawSolid();
Models::sciany3.drawSolid();
Models::sciany4.drawSolid();
// Models::sciany5.drawSolid();

Models::zewnetrznydach1.drawSolid();
Models::zewnetrznydach2.drawSolid();
// Models::zewnetrznydach3.drawSolid();
// Models::zewnetrznydach4.drawSolid();

Models::wewnetrznydach1.drawSolid();
Models::wewnetrznydach2.drawSolid();
Models::wewnetrznydach3.drawSolid();
Models::wewnetrznydach4.drawSolid();

通常,这些模型是由我的python脚本生成的(因此所有文件都是相同的,只有一些数据发生了变化 - 比如每个模型的顶点)。我可以用这样的一套正常运行我的程序。但是,当我取消注释其中一个评论模型时,会出现分段错误和核心转储。我在Ubuntu 16.04上工作。我尝试更换驱动程序(在其他地方它帮助了类似的问题)并没有改变。这里有一个比较示例(一个工作模型和一个不工作模型):

zewnetrznyDach2模型(工作):

#include "zewnetrznydach2.h"

namespace Models {

    ZewnetrznyDach2 zewnetrznydach2;

    ZewnetrznyDach2::ZewnetrznyDach2() {
        vertices=ZewnetrznyDach2Internal::vertices;
        normals=ZewnetrznyDach2Internal::normals;
        vertexNormals=ZewnetrznyDach2Internal::vertexNormals;
        texCoords=ZewnetrznyDach2Internal::texCoords;
        colors=ZewnetrznyDach2Internal::colors;
        vertexCount=ZewnetrznyDach2Internal::vertexCount;
    }

    ZewnetrznyDach2::~ZewnetrznyDach2() {
    }

    void ZewnetrznyDach2::drawSolid() {
        glEnable(GL_NORMALIZE);

        glEnableClientState(GL_VERTEX_ARRAY);
        //glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glVertexPointer(4,GL_FLOAT,0,vertices);
        //glColorPointer(4,GL_FLOAT,0,colors);
        glNormalPointer(GL_FLOAT,sizeof(float)*4,vertexNormals);
        glTexCoordPointer(2,GL_FLOAT,0,texCoords);

        glDrawArrays(GL_TRIANGLES,0,vertexCount);

        glDisableClientState(GL_VERTEX_ARRAY);
        //glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }

    namespace ZewnetrznyDach2Internal {
            unsigned int vertexCount=36;

            float vertices[]={
                -16.193,-2.25,-9.829,1.0,
                -15.999,-2.099,-9.829,1.0,
                -5.413,-2.25,-9.829,1.0,
                -15.999,-2.099,-9.829,1.0,
                -16.193,-2.25,-9.829,1.0,
                -16.193,6.2,-9.829,1.0,
                -15.999,-2.099,-9.829,1.0,
                -16.193,6.2,-9.829,1.0,
                -15.999,5.820,-9.829,1.0,
                -15.999,5.820,-9.829,1.0,
                -16.193,6.2,-9.829,1.0,
                -5.639,5.820,-9.829,1.0,
                -5.413,6.2,-9.829,1.0,
                -5.639,5.820,-9.829,1.0,
                -16.193,6.2,-9.829,1.0,
                -5.413,6.2,-9.829,1.0,
                -5.639,-2.099,-9.829,1.0,
                -5.639,5.820,-9.829,1.0,
                -5.413,-2.25,-9.829,1.0,
                -5.639,-2.099,-9.829,1.0,
                -5.413,6.2,-9.829,1.0,
                -5.639,-2.099,-9.829,1.0,
                -5.413,-2.25,-9.829,1.0,
                -15.999,-2.099,-9.829,1.0,
                -5.639,-2.099,-9.829,1.0,
                -15.999,-2.099,-9.829,1.0,
                -15.999,-2.099,-9.769,1.0,
                -15.999,-2.099,-9.829,1.0,
                -15.999,5.820,-9.829,1.0,
                -15.999,5.820,-9.769,1.0,
                -15.999,5.820,-9.829,1.0,
                -5.639,5.820,-9.829,1.0,
                -5.639,5.820,-9.769,1.0,
                -5.639,5.820,-9.829,1.0,
                -5.639,-2.099,-9.829,1.0,
                -5.639,-2.099,-9.769,1.0,


            };

            float colors[]={
            };

            float normals[]={
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,1,-0,0,
                0,1,-0,0,
                0,1,-0,0,
                1,0,-0,0,
                1,0,-0,0,
                1,0,-0,0,
                0,-1,-0,0,
                0,-1,-0,0,
                0,-1,-0,0,
                -1,0,-0,0,
                -1,0,-0,0,
                -1,0,-0,0,

            };

            float vertexNormals[]={
            };

            float texCoords[]={
                637.526,-88.582,
                629.904,-82.667,
                213.116,-88.582,
                629.904,-82.667,
                637.526,-88.582,
                637.526,244.094,
                629.904,-82.667,
                637.526,244.094,
                629.904,229.143,
                629.904,229.143,
                637.526,244.094,
                222.03,229.143,
                213.116,244.094,
                222.03,229.143,
                637.526,244.094,
                213.116,244.094,
                222.03,-82.667,
                222.03,229.143,
                213.116,-88.582,
                222.03,-82.667,
                213.116,244.094,
                222.03,-82.667,
                213.116,-88.582,
                629.904,-82.667,
                -222.03,386.989,
                -629.904,386.989,
                -629.904,384.626,
                386.989,-82.667,
                386.989,229.143,
                384.626,229.143,
                629.904,386.989,
                222.03,386.989,
                222.03,384.626,
                -386.989,229.143,
                -386.989,-82.667,
                -384.626,-82.667,

            };
    }
}

zewnetrznydach3模型(评论并且不工作):

#include "zewnetrznydach3.h"

namespace Models {

    ZewnetrznyDach3 zewnetrznydach3;

    ZewnetrznyDach3::ZewnetrznyDach3() {
        vertices=ZewnetrznyDach3Internal::vertices;
        normals=ZewnetrznyDach3Internal::normals;
        vertexNormals=ZewnetrznyDach3Internal::vertexNormals;
        texCoords=ZewnetrznyDach3Internal::texCoords;
        colors=ZewnetrznyDach3Internal::colors;
        vertexCount=ZewnetrznyDach3Internal::vertexCount;
    }

    ZewnetrznyDach3::~ZewnetrznyDach3() {
    }

    void ZewnetrznyDach3::drawSolid() {
        glEnable(GL_NORMALIZE);

        glEnableClientState(GL_VERTEX_ARRAY);
        //glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glVertexPointer(4,GL_FLOAT,0,vertices);
        //glColorPointer(4,GL_FLOAT,0,colors);
        glNormalPointer(GL_FLOAT,sizeof(float)*4,vertexNormals);
        glTexCoordPointer(2,GL_FLOAT,0,texCoords);

        glDrawArrays(GL_TRIANGLES,0,vertexCount);

        glDisableClientState(GL_VERTEX_ARRAY);
        //glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }

    namespace ZewnetrznyDach3Internal {
            unsigned int vertexCount=36;

            float vertices[]={
                0.011,-6.68,-10.305,1.0,
                0.142,-6.589,-10.305,1.0,
                4.741,-6.68,-10.305,1.0,
                0.142,-6.589,-10.305,1.0,
                0.011,-6.68,-10.305,1.0,
                0.011,-2,-10.305,1.0,
                0.142,-6.589,-10.305,1.0,
                0.011,-2,-10.305,1.0,
                0.142,-2.229,-10.305,1.0,
                0.142,-2.229,-10.305,1.0,
                0.011,-2,-10.305,1.0,
                4.572,-2.229,-10.305,1.0,
                4.741,-2,-10.305,1.0,
                4.572,-2.229,-10.305,1.0,
                0.011,-2,-10.305,1.0,
                4.741,-2,-10.305,1.0,
                4.572,-6.589,-10.305,1.0,
                4.572,-2.229,-10.305,1.0,
                4.741,-6.68,-10.305,1.0,
                4.572,-6.589,-10.305,1.0,
                4.741,-2,-10.305,1.0,
                4.572,-6.589,-10.305,1.0,
                4.741,-6.68,-10.305,1.0,
                0.142,-6.589,-10.305,1.0,
                4.572,-6.589,-10.305,1.0,
                0.142,-6.589,-10.305,1.0,
                0.142,-6.589,-10.265,1.0,
                0.142,-6.589,-10.305,1.0,
                0.142,-2.229,-10.305,1.0,
                0.142,-2.229,-10.265,1.0,
                0.142,-2.229,-10.305,1.0,
                4.572,-2.229,-10.305,1.0,
                4.572,-2.229,-10.265,1.0,
                4.572,-2.229,-10.305,1.0,
                4.572,-6.589,-10.305,1.0,
                4.572,-6.589,-10.265,1.0,


            };

            float colors[]={
            };

            float normals[]={
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,0,-1,0,
                0,1,-0,0,
                0,1,-0,0,
                0,1,-0,0,
                1,0,-0,0,
                1,0,-0,0,
                1,0,-0,0,
                0,-1,-0,0,
                0,-1,-0,0,
                0,-1,-0,0,
                -1,0,-0,0,
                -1,0,-0,0,
                -1,0,-0,0,

            };

            float vertexNormals[]={
            };

            float texCoords[]={
                -0.440,-262.992,
                -5.607,-259.413,
                -186.661,-262.992,
                -5.607,-259.413,
                -0.440,-262.992,
                -0.440,-78.740,
                -5.607,-259.413,
                -0.440,-78.740,
                -5.607,-87.759,
                -5.607,-87.759,
                -0.440,-78.740,
                -180.017,-87.759,
                -186.661,-78.740,
                -180.017,-87.759,
                -0.440,-78.740,
                -186.661,-78.740,
                -180.017,-259.413,
                -180.017,-87.759,
                -186.661,-262.992,
                -180.017,-259.413,
                -186.661,-78.740,
                -180.017,-259.413,
                -186.661,-262.992,
                -5.607,-259.413,
                180.017,405.722,
                5.607,405.722,
                5.607,404.147,
                405.722,-259.413,
                405.722,-87.759,
                404.147,-87.759,
                -5.607,405.722,
                -180.017,405.722,
                -180.017,404.147,
                -405.722,-87.759,
                -405.722,-259.413,
                -404.147,-259.413,

            };
    }
}

这怎么可能?

1 个答案:

答案 0 :(得分:0)

vertexNormals是空的,但你要求OpenGL从中读取数据。

最好的情况是,OpenGL变得垃圾。最糟糕的情况是,当OpenGL尝试在进程的已分配内存页面之外读取时,会出现段错误。

一些选择:

  • 使用适当数量的法线填充数组
  • 告诉OpenGL访问另一个填充的数组
  • 禁用GL_NORMAL_ARRAY状态,以便OpenGL不会从正常指针读取