OpenGL 分段错误

发布于 2024-12-12 06:03:12 字数 5543 浏览 0 评论 0原文

我在“tut2.c”中有这个程序:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <GL/glew.h>
#include <GL/glfw.h>

GLuint vbo[2];
const GLfloat diamond[4][2] = {
    { 0.0, 0.5 }, //top point
    { 0.5, 0.0 }, //right 
    { 0.0, -0.5 }, // bottom
    { -0.5, 0.5 }}; //left

const GLfloat colors[4][3] = {
    {1.0, 0.0, 0.0},
    {0.0, 1.0, 0.0},
    {0.0, 0.0, 1.0},
    {1.0, 1.0, 1.0}};

GLchar *vertexsource, *fragmentsource;
GLuint vertexshader, fragmentshader;
GLuint shaderprogram;

char* filetobuf(char *file) //will reac a file into an allocated char pointer buffer
{   
    FILE *fptr;
    long length;
    char *buf;
    //opening the file
    fptr = fopen(file, "rb");

    if(!fptr) 
    {
        fprintf(stderr, "failed to open %s\n", file);
        return NULL;
    }
    fseek(fptr,0,SEEK_END); //go to the end of the file
    length = ftell(fptr); //count bytes in "fptr" file
    buf = (char*)malloc(length+1); //allocate a buffer for all the file and +1 for the null terminator
    fseek(fptr, 0, SEEK_SET); //SEEK_SET = Begging of file >> go to hte start of the file 
    fclose(fptr); //close the file

    buf[length] = 0; //null terminator
    return buf;
}

void check(char *where)
{
    char *what;
    int err = glGetError();
    if(!err)
    {
        printf("OpenGL error integer: %d", err);
        return;
    }
    if(err == "GL_INVALID_ENUM")
        what = "GL_INVALID_ENUM";
    else if(err == "GL_INVALID_VALUE")
        what = "GL_INVALID_VALUE";
    else if(err == "GL_INVALID_OPERATION")
        what = "GL_INVALID_OPERATION";
    else if(err == "GL_INVALID_FRAMEBUFFER_OPERATION")
        what = "GL_INVALID_FRAMEBUFFER_OPERATION";
        else if(err == "GL_OUT_OF_MEMORY")
        what = "GL_OUT_OF_MEMORY";
    else
        what = "Unkown error";
    fprintf(stderr, "Error (%d) %s at %s\n, err, what, where");
    exit(1);
}

void SetupShaders(void)
{
    char text[1000];
    int length;
    fprintf(stderr, "Set up shaders\n"); //allocate and assign 2 Vertex Buffer Objects to our handle

    //reading files and placing them into buffers   
    vertexsource = filetobuf("tut2.vert");
    fragmentsource = filetobuf("tut2.frag");

    //assign to our shaders a name
    vertexshader = glCreateShader(GL_VERTEX_SHADER); 
    fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);

    //associate the source code buffers with each handle
    glShaderSource(vertexshader, 1, (const GLchar**)&vertexsource, 0);
    glShaderSource(fragmentshader, 1, (const GLchar**)&fragmentsource, 0);

    //compile our fragment and vertex shaders programs
    glCompileShader(fragmentshader);
    glCompileShader(vertexshader);

    shaderprogram = glCreateProgram(); //assign our program handle a "name"

        //attaching shaders to our program
    glAttachShader(shaderprogram, vertexshader);
    glAttachShader(shaderprogram, fragmentshader);
    //link our program

    glLinkProgram(shaderprogram);
    glGetProgramInfoLog(shaderprogram, 1000, &length, text);

    if(length > 0)
        fprintf(stderr, "Validate Shader\n%s\n", text);

    glUseProgram(shaderprogram); // set program as being actively used
}

void SetupGeometry(void)
{
    fprintf(stderr, "Setup vertices\n");
    glGenBuffers(2,vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glEnableVertexAttribArray(0);
    glBufferData(GL_ARRAY_BUFFER, 8* sizeof(GLfloat), diamond, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)0,2, GL_FLOAT,GL_FALSE, 0,0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glEnableVertexAttribArray(1);
    glBufferData(GL_ARRAY_BUFFER, 12* sizeof(GLfloat), colors, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)1,3, GL_FLOAT,GL_FALSE, 0,0);
    glBindAttribLocation(shaderprogram, 0, "in_Position");
    glBindAttribLocation(shaderprogram, 1, "in_Color");
}

void Render(void)
{
    glClearColor(0.0,0.0,0.0,0.1);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_LINE_LOOP,0,4);

    check("Test Point");
    glFlush();
}

int main(void)
{
    int running = GL_TRUE;
    if(!glfwInit())
    {
        exit(EXIT_FAILURE);
    }
    if( !glfwOpenWindow(600,600,0,0,0,0,0,0, GLFW_WINDOW))
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glewInit();
    SetupShaders();
    SetupGeometry();

    //Main loop
    while(running)
    {
        Render();
        glfwSwapBuffers();
        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
    }

    glfwTerminate();
    exit(EXIT_SUCCESS);
}

这个文件“tut2.vert”和“tut2.frag”

tut2.frag

#version 150

precision highp float;

in vec3 ex_Color;
out vec4 gl_FragColor;

void main(void)
{
    gl_FragColor = vec4(ex_Color, 1.0);
}

tut2.vert

#version 150

in vec2 in_Position;
in vec3 in_Color;
out vec3 ex_Color;

void main(void)
{
    gl_Position = vec4(in_Position.x, in_Position.y, 0.0, 1.0);
    ex_Color = in_Color;
}

我编译所有内容,

gcc tut2.c -o tut2 -lglfw -lGLEW -lGLU -lGL

除了一些警告之外,一切都编译得很好。

但是当我运行该程序时,我收到一个无法修复的奇怪错误:

Set up shaders
Validate Shader
Vertex info
-----------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Fragment info
-------------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Setup vertices
Segmentation fault (core dumped)

请问有什么帮助吗?

I have this program in "tut2.c":

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <GL/glew.h>
#include <GL/glfw.h>

GLuint vbo[2];
const GLfloat diamond[4][2] = {
    { 0.0, 0.5 }, //top point
    { 0.5, 0.0 }, //right 
    { 0.0, -0.5 }, // bottom
    { -0.5, 0.5 }}; //left

const GLfloat colors[4][3] = {
    {1.0, 0.0, 0.0},
    {0.0, 1.0, 0.0},
    {0.0, 0.0, 1.0},
    {1.0, 1.0, 1.0}};

GLchar *vertexsource, *fragmentsource;
GLuint vertexshader, fragmentshader;
GLuint shaderprogram;

char* filetobuf(char *file) //will reac a file into an allocated char pointer buffer
{   
    FILE *fptr;
    long length;
    char *buf;
    //opening the file
    fptr = fopen(file, "rb");

    if(!fptr) 
    {
        fprintf(stderr, "failed to open %s\n", file);
        return NULL;
    }
    fseek(fptr,0,SEEK_END); //go to the end of the file
    length = ftell(fptr); //count bytes in "fptr" file
    buf = (char*)malloc(length+1); //allocate a buffer for all the file and +1 for the null terminator
    fseek(fptr, 0, SEEK_SET); //SEEK_SET = Begging of file >> go to hte start of the file 
    fclose(fptr); //close the file

    buf[length] = 0; //null terminator
    return buf;
}

void check(char *where)
{
    char *what;
    int err = glGetError();
    if(!err)
    {
        printf("OpenGL error integer: %d", err);
        return;
    }
    if(err == "GL_INVALID_ENUM")
        what = "GL_INVALID_ENUM";
    else if(err == "GL_INVALID_VALUE")
        what = "GL_INVALID_VALUE";
    else if(err == "GL_INVALID_OPERATION")
        what = "GL_INVALID_OPERATION";
    else if(err == "GL_INVALID_FRAMEBUFFER_OPERATION")
        what = "GL_INVALID_FRAMEBUFFER_OPERATION";
        else if(err == "GL_OUT_OF_MEMORY")
        what = "GL_OUT_OF_MEMORY";
    else
        what = "Unkown error";
    fprintf(stderr, "Error (%d) %s at %s\n, err, what, where");
    exit(1);
}

void SetupShaders(void)
{
    char text[1000];
    int length;
    fprintf(stderr, "Set up shaders\n"); //allocate and assign 2 Vertex Buffer Objects to our handle

    //reading files and placing them into buffers   
    vertexsource = filetobuf("tut2.vert");
    fragmentsource = filetobuf("tut2.frag");

    //assign to our shaders a name
    vertexshader = glCreateShader(GL_VERTEX_SHADER); 
    fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);

    //associate the source code buffers with each handle
    glShaderSource(vertexshader, 1, (const GLchar**)&vertexsource, 0);
    glShaderSource(fragmentshader, 1, (const GLchar**)&fragmentsource, 0);

    //compile our fragment and vertex shaders programs
    glCompileShader(fragmentshader);
    glCompileShader(vertexshader);

    shaderprogram = glCreateProgram(); //assign our program handle a "name"

        //attaching shaders to our program
    glAttachShader(shaderprogram, vertexshader);
    glAttachShader(shaderprogram, fragmentshader);
    //link our program

    glLinkProgram(shaderprogram);
    glGetProgramInfoLog(shaderprogram, 1000, &length, text);

    if(length > 0)
        fprintf(stderr, "Validate Shader\n%s\n", text);

    glUseProgram(shaderprogram); // set program as being actively used
}

void SetupGeometry(void)
{
    fprintf(stderr, "Setup vertices\n");
    glGenBuffers(2,vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
    glEnableVertexAttribArray(0);
    glBufferData(GL_ARRAY_BUFFER, 8* sizeof(GLfloat), diamond, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)0,2, GL_FLOAT,GL_FALSE, 0,0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
    glEnableVertexAttribArray(1);
    glBufferData(GL_ARRAY_BUFFER, 12* sizeof(GLfloat), colors, GL_STATIC_DRAW);
    glVertexAttribPointer((GLuint)1,3, GL_FLOAT,GL_FALSE, 0,0);
    glBindAttribLocation(shaderprogram, 0, "in_Position");
    glBindAttribLocation(shaderprogram, 1, "in_Color");
}

void Render(void)
{
    glClearColor(0.0,0.0,0.0,0.1);
    glClear(GL_COLOR_BUFFER_BIT);
    glDrawArrays(GL_LINE_LOOP,0,4);

    check("Test Point");
    glFlush();
}

int main(void)
{
    int running = GL_TRUE;
    if(!glfwInit())
    {
        exit(EXIT_FAILURE);
    }
    if( !glfwOpenWindow(600,600,0,0,0,0,0,0, GLFW_WINDOW))
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glewInit();
    SetupShaders();
    SetupGeometry();

    //Main loop
    while(running)
    {
        Render();
        glfwSwapBuffers();
        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
    }

    glfwTerminate();
    exit(EXIT_SUCCESS);
}

and this files "tut2.vert" and "tut2.frag"

tut2.frag

#version 150

precision highp float;

in vec3 ex_Color;
out vec4 gl_FragColor;

void main(void)
{
    gl_FragColor = vec4(ex_Color, 1.0);
}

tut2.vert

#version 150

in vec2 in_Position;
in vec3 in_Color;
out vec3 ex_Color;

void main(void)
{
    gl_Position = vec4(in_Position.x, in_Position.y, 0.0, 1.0);
    ex_Color = in_Color;
}

i compile everything with

gcc tut2.c -o tut2 -lglfw -lGLEW -lGLU -lGL

everythings compiles fine except some warnings.

but when i run the program I get this strange error that I can't fix:

Set up shaders
Validate Shader
Vertex info
-----------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Fragment info
-------------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Setup vertices
Segmentation fault (core dumped)

any help please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-12-19 06:03:12

您在一个程序中遇到了两个问题,并且它们不相关。首先你的着色器不编译:

设置着色器
验证着色器
顶点信息
------------
0(1) : 错误 C0000: 语法错误,标记“”处出现意外的 $undefined

片段信息
-------------
0(1) : 错误 C0000: 语法错误,标记“”处出现意外的 $undefined

所以你需要修复你的着色器。

然后,您在 SetupGeometry 函数中的某处访问了无效地址(您之前也可能在某处溢出了缓冲区,现在会在这里触发故障)。

设置顶点
分段错误(核心转储)

由于评论而编辑

当说您需要修复着色器时,我的意思是您应该检查着色器加载代码。基本上GLSL编译器一开始就报告了一些无效数据。

着色器加载代码缺少一些关键内容: 读取步骤:

char* filetobuf(char *file) //will reac a file into an allocated char pointer buffer
{   
    FILE *fptr;
    long length;
    char *buf;
    //opening the file
    fptr = fopen(file, "rb");

    if(!fptr) 
    {
        fprintf(stderr, "failed to open %s\n", file);
        return NULL;
    }
    fseek(fptr,0,SEEK_END);
    length = ftell(fptr);

您可以使用 fstat(fileno(fptr), statbuf) 来检索文件大小,而不是 fseek、ftell 方法。请参阅 stat 系统调用手册。

    buf = (char*)malloc(length+1);

在 C 中,您不会在赋值中强制转换 void*。只需分配裸 void* 指针即可。这与 C++ 不同

    fseek(fptr, 0, SEEK_SET);

,在这里您应该读取一些数据,例如 fread(fptr, 1 length, buf);

    fclose(fptr); //close the file

    buf[length] = 0; //null terminator
    return buf;
}

另外,您还需要在之后free(...) 您的着色器源缓冲区上传到OpenGL。

You got two issues in one program, and they are not related. First your shaders don't compile:

Set up shaders
Validate Shader
Vertex info
-----------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

Fragment info
-------------
0(1) : error C0000: syntax error, unexpected $undefined at token "<undefined>"

So you need to fix your shaders.

Then you're accessing invalid address somewhere in the SetupGeometry function (you may also have overrun a buffer somewhere before, which now triggers a fault here).

Setup vertices
Segmentation fault (core dumped)

Edit due to comment

When saying you need to fix your shaders, I meant you should check the shader loading code. Basically the GLSL compiler reported some invalid data at the very beginning.

The shader loading code is missing something crucial: The reading step:

char* filetobuf(char *file) //will reac a file into an allocated char pointer buffer
{   
    FILE *fptr;
    long length;
    char *buf;
    //opening the file
    fptr = fopen(file, "rb");

    if(!fptr) 
    {
        fprintf(stderr, "failed to open %s\n", file);
        return NULL;
    }
    fseek(fptr,0,SEEK_END);
    length = ftell(fptr);

Instead of the fseek, ftell method, you can use fstat(fileno(fptr), statbuf) to retrieve the file size. See the manual of the stat syscall.

    buf = (char*)malloc(length+1);

In C you don't cast void* in an assignment. Just assign the naked void* pointer. This is different from C++

    fseek(fptr, 0, SEEK_SET);

Here you should read some data, like fread(fptr, 1 length, buf);

    fclose(fptr); //close the file

    buf[length] = 0; //null terminator
    return buf;
}

Also you need to free(…) your shader source buffer after uploading to OpenGL.

一曲爱恨情仇 2024-12-19 06:03:12
#version 150
// It was expressed that some drivers required this next line to function properly precision highp float;

in  vec3 ex_Color;
out vec4 ex_FragColor;

void main(void) {
    // Pass through our original color with full opacity.
    ex_FragColor = vec4(ex_Color,1.0);
}

感谢克里斯蒂安·劳

#version 150
// It was expressed that some drivers required this next line to function properly precision highp float;

in  vec3 ex_Color;
out vec4 ex_FragColor;

void main(void) {
    // Pass through our original color with full opacity.
    ex_FragColor = vec4(ex_Color,1.0);
}

Thanks to Christian Rau

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文