C ++ openGL向量下标超出范围

时间:2017-11-19 15:27:52

标签: c++ opengl-3

当我在我的openGL 2D平台游戏中工作时,我注意到我的矩形上没有颜色。所以我认为这可能是因为我错过了错误检查。实际上我忘了调用错误检查函数 facepalm 但是这导致了一个错误,说我的std :: vector在创建时没有索引。我甚至无法启动该计划。

该函数名为CheckShaderError,发生错误的地方是:

    if (isProgram) {
        glGetProgramiv(_ShaderProgramID, GL_INFO_LOG_LENGTH, &maxLength);

        std::vector<GLchar> errorlog(maxLength);
        glGetProgramInfoLog(_ShaderProgramID, maxLength, &maxLength, &errorlog[0]);

        printf("Program failed to linkL: %s\n", &errorlog[0]);
        FatalError();
    }

// SHADERPROGRAM.CPP

#include "ShaderProgram.h"
#include <stdio.h>
#include "ErrorHandeler.h"
#include <fstream>
#include <vector>

ShaderProgram::ShaderProgram() : _ShaderProgramID(0){}

//Creates, compiles and links the shaders.
void ShaderProgram::Init()
{
//Create the shader program.
_ShaderProgramID = glCreateProgram();

//Create and compile shaders.
InstantiateShader(_Shaders[0], GL_VERTEX_SHADER, "Mesh/Mesh.vert");
InstantiateShader(_Shaders[1], GL_FRAGMENT_SHADER, "Mesh/Mesh.frag");

//Link the shaders after they are created and compiled.
LinkShaders();
}


//Creates a shader.
void ShaderProgram::InstantiateShader(GLuint shaderID, const GLenum 
shaderType, const std::string dataPath)
{
//Create the shader.
shaderID = glCreateShader(shaderType);

//Set the shadersource.
std::string shaderSource = ReadShaderFromFile("../2D Platformer/Shaders/" + dataPath).c_str();
const GLchar* shaderSourcePtr = shaderSource.c_str();
glShaderSource(shaderID, 1, &shaderSourcePtr, 0);

//Compile the shader.
glCompileShader(shaderID);

//Check if the shader did compile.
CheckShaderError(shaderID, false);
}


//Links the program and the shaders.
void ShaderProgram::LinkShaders()
{
//Attach the shaders. the shaders.
int shaderCount = sizeof(_Shaders) / sizeof(GLuint);
for (int i = 0; i < shaderCount; i++)
{
    glAttachShader(_ShaderProgramID, _Shaders[i]);
}

//Link the program.
glLinkProgram(_ShaderProgramID);


CheckShaderError(_ShaderProgramID, true);
}


//Check for any compile or linking errors.
void ShaderProgram::CheckShaderError(const GLuint shaderID, const bool 
isProgram)
{
GLint compileCheck = 0;
if (isProgram) {
    glGetProgramiv(_ShaderProgramID, GL_LINK_STATUS,(int *)&compileCheck);
}
else {
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compileCheck);
}

if (compileCheck == GL_FALSE)
{
    //The application does not need any garbage collection here because the 
deconstructor takes care of that.
    GLint maxLength = 0;

    if (isProgram) {
        glGetProgramiv(_ShaderProgramID, GL_INFO_LOG_LENGTH, &maxLength);

        std::vector<GLchar> errorlog(maxLength);
        glGetProgramInfoLog(_ShaderProgramID, maxLength, &maxLength, 
 &errorlog[0]);

        printf("Program failed to linkL: %s\n", &errorlog[0]);
        FatalError();
    }
    else {
        glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &maxLength);

        std::vector<GLchar> errorlog(maxLength);
        glGetShaderInfoLog(shaderID, maxLength, &maxLength, &errorlog[0]);

        printf("Shader failed to compile: %s\n", &errorlog[0]);
        FatalError();
    }
}
}


//Read shadercontent out of file.
std::string ShaderProgram::ReadShaderFromFile(std::string dataPath)
{
std::string shaderContent;
std::string currLine;
std::ifstream shaderContentFileStream(dataPath);
if (shaderContentFileStream.is_open())
{
    while (std::getline(shaderContentFileStream, currLine))
    {
        shaderContent.append(currLine + "\n");
    }
    shaderContentFileStream.close();
    return shaderContent;
}
else
{
    printf("Failed to read the shader");
    FatalError();
}

return NULL;
}


//Enables the current shaderprogram.
void ShaderProgram::EnableShaderProgram()
{
glUseProgram(_ShaderProgramID);
}



//Garbage collector.
ShaderProgram::~ShaderProgram()
{
//Detach and delete all shaders.
int shaderCount = sizeof(_Shaders) / sizeof(GLuint);
for (int i = 0; i < shaderCount; i++)
{
    glDetachShader(_ShaderProgramID, _Shaders[i]);
    glDeleteShader(_Shaders[i]);
}

//Delete the shader program.
glDeleteProgram(_ShaderProgramID);
}

有人可以帮我解决问题吗?

0 个答案:

没有答案