定义顶点属性时可能存在的错误

时间:2014-03-31 23:52:12

标签: opengl glsl vertex-attributes

我在尝试编译顶点着色器时遇到了分段错误。我想我在传递顶点属性时发现了问题。以下几行编译(它们可能不起作用,但它们仍然可以编译):

# version 330
layout(location=0) in vec4 in_Loc;
layout(location=1) in vec4 in_Color;
layout(location=2) in vec4 in_Norm;

另外

# version 330
layout(location=0) in vec4 in_Loc;
layout(location=1) in vec4 in_Color;
layout(location=25) in vec4 in_Norm;

但是

# version 330
layout(location=0) in vec4 in_Loc;
layout(location=1) in vec4 in_Color;
layout(location=2) in vec4 in_Norm;
layout(location=33) in vec4 in_Anything

不会编译。我想我只能定义3个vec4属性。但GL_MAX_VERTEX_ATTRIBS的glGetIntegerv返回16,符合OpenGL标准。这是与我的硬件有关的某种错误吗?我正在使用英特尔显卡

    *-display
         description: VGA compatible controller
         product: 3rd Gen Core processor Graphics Controller
         vendor: Intel Corporation
         physical id: 2
         bus info: pci@0000:00:02.0
         version: 09
         width: 64 bits
         clock: 33MHz
         capabilities: msi pm vga_controller bus_master cap_list rom
         configuration: driver=i915 latency=0
         resources: irq:49 memory:f0000000-f03fffff memory:e0000000-efffffff ioport:3000(size=64)

和mesa 10:

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Ivybridge Mobile 
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.1.0
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile.

操作系统是ubuntu 14.04。

1 个答案:

答案 0 :(得分:0)

由于你的评论,我发现了这个错误。在仅使用c编译的glsl文件创建测试用例之后。事实证明,分段错误发生在“cython”中,但如果我直接从c调用库(尽管c中有“bug”)则不然。通过在glsl文件中添加额外的行来产生错误,这会触发分段错误。我对此感到困惑,并认为错误在于编译glsl。但是,问题出在加载glsl文件的函数中:

void load_file(const char* fname, char** buffer)
{
    FILE* fp;
    long fsize;
    fprintf(stdout, "Loading %s \n", fname);
    fp = fopen(fname, "r");
    fseek(fp, 0, SEEK_END);
    fsize = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    if (fp == NULL)
    {
      fprintf(stderr, "Can't open %s\n", fname);
      exit(1);
    }
    *buffer = (char* ) malloc(fsize + 1);
    fprintf(stdout, "%d\n", (int ) fsize);
    fread(*buffer, fsize, 1, fp);
    buffer[fsize] = 0;
    fclose(fp);
}

通过替换buffer [fsize] = 0; with buffer [0] [fsize] = 0;我解决了这个问题。我不知道它来自何处,也不知道这是否是我的代码中的错误,但它可能与cython如何分配内存有关。