5

我是 OpenGL 新手,并尝试按照http://www.openglbook.com教程学习 OpenGL 4。在第二页,我们使用着色器创建了一个简单的三角形。但是在创建顶点着色器之后,程序就崩溃了。通过调试,我可以找到一个 OpenGL 错误GL_INVALID_ENUM(0x0500)。请参阅下面的代码...

不幸的是,我找不到任何解决方案。也许你知道某事?

编辑:

所以,在再次搜索GL_INVALID_ENUMand之后glewInit(),我发现其他网站上已经有很多帖子了,但是:他们中的大多数在他们的着色器中都有一些拼写错误,我绝对没有。在 http://www.opengl.org/wiki/OpenGL_Loading_Library 我发现:

您可能仍然会得到 GL_INVALID_ENUM(取决于您使用的 GLEW 版本),但至少 GLEW 会忽略 glGetString(GL_EXTENSIONS)​并获取所有函数指针。

有些人建议忽略该错误,但我仍然无法运行该程序...顺便说一句,你们中的任何人都可以尝试运行该程序吗?如果您有同样的错误,我们肯定知道这不是我的错误 IDE/项目配置。

眼镜:

Windows 8.1 64bit
Intel Core i7-3517U with
Intel HD4000 GPU (OpenGL 4.0.0 Support)
8 GB RAM

IDE:

Eclipse IDE for C/C++ Developers
Version: Kepler Service Release 1
Build id: 20130919-0819
using MinGW Compiler

代码:

#include <stdlib.h>
#include <stdio.h>
#include <string>
#define GLEW_STATIC
#include <gl/glew.h>
#include <gl/freeglut.h>


#define WINDOW_TITLE_PREFIX "Chapter 2"

// initialization
int CurrentWidth = 800,
    CurrentHeight = 600,
    WindowHandle = 0;

// timer
unsigned FrameCount = 0;

// chapter2 buffer object.........................
GLuint VertexShaderId,
       FragmentShaderId,
       ProgramId,
       VaoId,
       VboId,
       ColorBufferId;

const GLchar *VertexShader = {
        "#version 400\n"\

        "layout(location=0) in vec4 in_Position;\n"\
        "layout(location=1) in vec4 in_Color;\n"\
        "out vec4 ex_Color;\n"\

        "void main(void) {\n"\
        "    gl_Position = in_Position;\n"\
        "    ex_Color = in_Color;\n"\
        "}\n"
};
const GLchar *FragmentShader = {
        "#version 400\n"\

        "in vec4 ex_Color;\n"\
        "out vec4 out_Color;\n"\

        "void main(void) {\n"\
        "    out_Color = ex_Color;\n"\
        "}\n"
};
//................................chapter2 buffer object


// initialization
void Initialize(int,char*[]);
void InitWindow(int,char*[]);
void ResizeFunction(int,int);
void RenderFunction(void);

//timer
void TimerFunction(int);
void IdleFunction(void);

// chapter2 buffer object
void Cleanup(void);
void CreateVBO(void);
void DestroyVBO(void);
void CreateShaders(void);
void DestroyShaders(void);


int main(int argc, char *argv[]) {
    fprintf(stdout,"Starting OpenGL programm...\n");

    Initialize(argc,argv);
    glutMainLoop();

    fprintf(stdout,"Program terminated.\n");
    exit(EXIT_SUCCESS);
}


void Initialize(int argc, char *argv[]) {
    GLenum GlewInitResult; //glew

    InitWindow(argc,argv);

    //glew...........
    GlewInitResult = glewInit();

    if(GLEW_OK != GlewInitResult) {
        fprintf(stderr,
                "ERROR: %s\n",
                glewGetErrorString(GlewInitResult) );
        exit(EXIT_FAILURE);
    }
    //.................glew.

    fprintf(stdout,
            "INFO: OpenGL Version: %s\n",
            glGetString(GL_VERSION) );

    //chapter2 buffer objects
    CreateShaders();
    CreateVBO();



    glClearColor(0.0f,0.0f,0.0f,0.0f);
}

void InitWindow(int argc, char *argv[]) {
    glutInit(&argc,argv); // always the first function to call!!!

    glutInitContextVersion(4,0);
    glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
    glutInitContextProfile(GLUT_CORE_PROFILE);

    glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
                  GLUT_ACTION_GLUTMAINLOOP_RETURNS);

    glutInitWindowSize(CurrentWidth,CurrentHeight);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

    WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);

    if(WindowHandle < 1) {
        fprintf(stderr,
                "ERROR: Could not create a new rendering window.\n");
        exit(EXIT_FAILURE);
    }

    glutReshapeFunc(ResizeFunction);
    glutDisplayFunc(RenderFunction);

    //timer
    glutIdleFunc(IdleFunction);
    glutTimerFunc(0,TimerFunction,0);

    //chapter2 buffer object
    glutCloseFunc(Cleanup);
}

void ResizeFunction(int Width, int Height) {
    CurrentWidth = Width;
    CurrentHeight = Height;
    glViewport(0,0,CurrentWidth,CurrentHeight);
}

void RenderFunction() {
    //timer
    ++FrameCount;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //chapter2 buffer object
    glDrawArrays(GL_TRIANGLES,0,3);

    glutSwapBuffers();
    glutPostRedisplay();
}

//timer.................................
void IdleFunction() {
    glutPostRedisplay();
}

void TimerFunction(int Value) {
    if(0 != Value) {
        char *TempString = (char*) malloc(512 + strlen(WINDOW_TITLE_PREFIX));

        sprintf(TempString,
                "%s: %d Frames Per Second @ %d x %d",
                WINDOW_TITLE_PREFIX,
                FrameCount * 4,
                CurrentWidth,
                CurrentHeight );

        glutSetWindowTitle(TempString);
        free(TempString);
    }
    FrameCount = 0;
    glutTimerFunc(250,TimerFunction,1);
}
//...........................................timer

//chapter2 buffer object..................................
void Cleanup() {
    DestroyShaders();
    DestroyVBO();
}

void CreateVBO() {
    GLfloat Vertices[] = {
            -0.8f, -0.8f,  0.0f, 1.0f,
             0.0f,  0.8f,  0.0f, 1.0f,
             0.8f, -0.8f,  0.0f, 1.0f
    };

    GLfloat Colors[] = {
            1.0f, 0.0f, 0.0f, 1.0f,
            0.0f, 1.0f, 0.0f, 1.0f,
            0.0f, 0.0f, 1.0f, 1.0f
    };

    GLenum ErrorCheckValue = glGetError();

    glGenVertexArrays(1,&VaoId);
    glBindVertexArray(VaoId);

    glGenBuffers(1,&VboId);
    glBindBuffer(GL_ARRAY_BUFFER, VboId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(0,4,GL_FLOAT, GL_FALSE, 0,0);
    glEnableVertexAttribArray(0);

    glGenBuffers(1,&ColorBufferId);
    glBindBuffer(GL_ARRAY_BUFFER, ColorBufferId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
    glVertexAttribPointer(1,4,GL_FLOAT, GL_FALSE, 0,0);
    glEnableVertexAttribArray(1);

    ErrorCheckValue = glGetError();
    if(ErrorCheckValue != GL_NO_ERROR) {
        fprintf(stderr,
                "ERROR: Could not create a VBO: %s \n",
                gluErrorString(ErrorCheckValue) );
        exit(-1);
    }
}

void DestroyVBO() {
    GLenum ErrorCheckValue = glGetError();

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER,0);

    glDeleteBuffers(1, &ColorBufferId);
    glDeleteBuffers(1, &VboId);

    glBindVertexArray(0);
    glDeleteVertexArrays(1,&VaoId);

    ErrorCheckValue = glGetError();
    if(ErrorCheckValue != GL_NO_ERROR) {
        fprintf(stderr,
                "ERROR: Could not destroy the VBO: %s \n",
                gluErrorString(ErrorCheckValue) );
        exit(-1);
    }
}

void CreateShaders() {
    GLenum ErrorCheckValue = glGetError();

    VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(VertexShaderId,1,&VertexShader,NULL);
    glCompileShader(VertexShaderId);

    FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(FragmentShaderId,1,&FragmentShader,NULL);
    glCompileShader(FragmentShaderId);

    ProgramId = glCreateProgram();
        glAttachShader(ProgramId, VertexShaderId);
        glAttachShader(ProgramId, FragmentShaderId);
    glLinkProgram(ProgramId);

    if(glIsProgram(ProgramId) != GL_TRUE) {
            fprintf(stderr,
                    "ERROR: This is no GL program!\n");
            exit(-1);
        }
    glUseProgram(ProgramId);

    ErrorCheckValue = glGetError();
    if(ErrorCheckValue != GL_NO_ERROR) {
        fprintf(stderr,
                "ERROR: Could not create the shaders: %s \n",
                gluErrorString(ErrorCheckValue) );
        exit(-1);
    }
}

void DestroyShaders() {
    GLenum ErrorCheckValue = glGetError();

    glUseProgram(0);

    glDetachShader(ProgramId, VertexShaderId);
    glDetachShader(ProgramId, FragmentShaderId);

    glDeleteShader(FragmentShaderId);
    glDeleteShader(VertexShaderId);

    glDeleteProgram(ProgramId);

    ErrorCheckValue = glGetError();
    if(ErrorCheckValue != GL_NO_ERROR) {
        fprintf(stderr,
                "ERROR: Could not destroy the VBO: %s \n",
                gluErrorString(ErrorCheckValue) );
        exit(-1);
    }
}
4

1 回答 1

5

我终于可以解决问题了:首先,我做了严重的小改动,所以我不能告诉你,如果只是一个改动还是所有的总和,是什么解决了它。

  1. 删除glu库。我必须链接这个库才能使用gluErrorString(...). 将此函数替换为glewGetErrorString(...). 现在你不再需要glu图书馆了。
  2. 可能是错误的原因: Delete glutInitContextVersion(4,0);。我不能告诉你真正的原因——可能glew不支持新的 OpenGL 4 函数。

而已。我希望这会有所帮助(我在其他几个网站上发现了同样的问题)!并感谢您的建议:)

于 2013-10-19T13:16:30.210 回答