Skip to content

10.基础的OpenGL错误监测

制造错误

C++
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
glDrawElements(GL_TRIANGLES, 6, GL_INT, nullptr);
/* 这导致我们只看到黑屏 */

错误处理

C++

static void GlClearError()
{
    while (glGetError() != GL_NO_ERROR);
}

static void GLCheckError()
{
    while (GLenum error = glGetError())
    {
        std::cout << "[OpenGL Error](" << error << ")" << std::endl;
    }
}

GlClearError();
glDrawElements(GL_TRIANGLES, 6, GL_INT, nullptr);
GLCheckError();

错误CODE

具体信息

优化

C++
#define ASSERT(x) if (!(x)) __debugbreak(); // __ MSVC特有
#define GLCall(x) GLClearError();\
    x;\
    ASSERT(GLLogCall())

static void GLCheckError()
static boll GLLogCall()
{
    while (GLenum error = glGetError())
    {
        std::cout << "[OpenGL Error](" << error << ")" << std::endl;
        return false;
    }
    return true;
}

GLCheckError();
ASSERT(GLLogCall());

作用

详细错误

C++
#define GLCall(x) GlClearError();\
    x;\
    ASSERT(GLLogCall(#x, __FILE__, __LINE__))

/* 详细错误 */
static bool GLLogCall(const char* function, const char* file, int line)
{
    while (GLenum error = glGetError())
    {
        std::cout << "[OpenGL Error](" << error << ")" << function <<
            " " << file << ":" << line << std::endl;
        return false;
    }
    return true;
}

效果