GLDrawArrays中的访问冲突()

时间:2014-11-24 22:52:38

标签: opengl

我在一个简单三角形的glDrawArray中遇到了Access Violation错误。 在此处附加代码以供审核。

void VerifyGL() {
    GLenum err;
    if((err = glGetError()) != GL_NO_ERROR) { 
        char sFinalMessage[1536]; 
        sprintf_s(sFinalMessage, "Error!  %u",  err); 
        MessageBoxA(NULL, sFinalMessage, "Error", MB_ICONERROR); 
        assert(false);
    }
}




void LoadShader(_In_ const std::wstring& FileName, _Out_ GLShader* shader)
{
    //Find type. 
    std::wstring Ext = FileName.substr(FileName.size()-4, 4);
    int Type = Ext == L"vert" ? GL_VERTEX_SHADER : (Ext == L"frag" ? GL_FRAGMENT_SHADER : GL_GEOMETRY_SHADER);


    FILE* fp = nullptr;
    _wfopen_s(&fp, FileName.c_str(), L"rt"); 
    if(!fp){ return;}


    std::vector<string> SourceLines;
    char Line[255];
    while(fgets(Line, 255, fp))
    {
        SourceLines.push_back(Line);
    }
    fclose(fp);


    const char** ShaderSourceArray = new const char*[SourceLines.size()];
    for(int i = 0 ; i < SourceLines.size(); i++)
    {
        ShaderSourceArray[i] = SourceLines[i].c_str();
    }


    GLuint ID = glCreateShader(Type);
    glShaderSource(ID, (GLsizei)SourceLines.size(), ShaderSourceArray, NULL);
    VerifyGL();
    glCompileShader(ID);
    delete[] ShaderSourceArray;
    int result = 0;
    glGetShaderiv(ID, GL_COMPILE_STATUS, &result);
    if(!result){ 
        char sInfoLog[1024];
        char sFinalMessage[1536];
        int iLogLength;
        glGetShaderInfoLog(ID, 1024, &iLogLength, sInfoLog);
        sprintf_s(sFinalMessage, "Error! Shader file %s wasn't compiled! The compiler returned:\n\n%s", FileName.c_str(), sInfoLog);
        MessageBoxA(NULL, sFinalMessage, "Error", MB_ICONERROR);
        return;}


    shader->Initialize(ID, Type);


}
In Main.cpp

SpriteInfo* Info = new SpriteInfo();
    glGenVertexArrays(1, &(Info->VertexArrayObject));
    VerifyGL();
    glBindVertexArray(Info->VertexArrayObject);
    VerifyGL();
    glGenBuffers(1, &(Info->VertexBuffer));
    VerifyGL();
    glBindBuffer(GL_ARRAY_BUFFER, Info->VertexBuffer);
    VerifyGL();
    glBindVertexArray(0);


    loader->LoadShader( L"Shaders\\VertexShader.vert", m_vertexShader.Get()); //assume loader is initialized. 
    loader->LoadShader( L"Shaders\\PixelShader.frag", m_pixelShader.Get());


    ShaderProgram = glCreateProgram();
    VerifyGL();
    glAttachShader(ShaderProgram, m_vertexShader->GetID());
    VerifyGL();
    glAttachShader(ShaderProgram, m_pixelShader->GetID());
    VerifyGL();


    glBindAttribLocation(ShaderProgram, 0, "pos");
    glLinkProgram(ShaderProgram);
    int result = 0; 
    glGetProgramiv(ShaderProgram, GL_LINK_STATUS, &result);
    if(!result)
    {
        char sInfoLog[1024];
        char sFinalMessage[1536];
        int iLogLength;
        glGetProgramInfoLog(ShaderProgram, 1024, &iLogLength, sInfoLog);
        sprintf_s(sFinalMessage, "Error! Link Shaders : %s",  sInfoLog);
        MessageBoxA(NULL, sFinalMessage, "Error", MB_ICONERROR);
        return;
    }

In Main.cpp Draw()

    glUseProgram(ShaderProgram);
    VerifyGL();
    float PositionArray[6];
    PositionArray[0] = 0;
    PositionArray[1] = 0.5;
    PositionArray[2] = -0.5;
    PositionArray[3] = -0.5;
    PositionArray[4] = 0.5;
    PositionArray[5] = -0.5;


    glBindBuffer(GL_ARRAY_BUFFER, SpriteToDraw->VertexBuffer);
    VerifyGL();
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), &PositionArray[0], GL_STATIC_DRAW); // Using static_draw in draw() call for testing purposes. Actual code would be changing the position of the triangle every frame and would be using dynamic_draw. 
    VerifyGL();
    glBindVertexArray(SpriteToDraw->VertexArrayObject);
    VerifyGL();
    glBindBuffer(GL_ARRAY_BUFFER, SpriteToDraw->VertexBuffer);
    VerifyGL();
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0 );
    VerifyGL();
    glEnableVertexAttribArray(0); 
    VerifyGL();



glDrawArrays(GL_TRIANGLES, 0 , 3); // memory access violation error. 
VerifyGL();

每次打勾都会调用Draw()。

0 个答案:

没有答案