标识符“”未定义

时间:2017-03-04 14:16:41

标签: c++ function opengl

我正在使用openGL编写3D测试应用程序,我正在遵循thebennybox的教程并使用visual studio和c ++。这是包含错误的类:

 #include "Shader.h"
#include <fstream>
#include <iostream>

static std::string LoadShader(const std::string& fileName);
static GLuint CreateShader(const std::string& Text, GLenum shaderType);

Shader::Shader(const std::string& fileName)
{
    program = glCreateProgram();
    shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
    shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);

    for (unsigned int i=0; i < NUM_SHADERS; i++)
        glAttachShader(program, shaders[i]);

    glBindAttribLocation(program, 0, "position");

    glLinkProgram(program);
    CheckShaderError(program, GL_LINK_STATUS, true, "Program Linking Failed: ");

    glValidateProgram(program);
    CheckShaderError(program, GL_VALIDATE_STATUS, true, "Invalid Program: ");
}


Shader::~Shader()
{
    for (unsigned int i = 0; i < NUM_SHADERS; i++)
    {
        glDetachShader(program, shaders[i]);
        glDeleteShader(shaders[i]);
    }

    glDeleteProgram(program);
}

void Shader::Bind()
{
    glUseProgram(program);
}

void Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string& errorMessage)
{
    GLint success = 0;
    GLchar error[1024] = { 0 };

    if (isProgram)
        glGetProgramiv(shader, flag, &success);
    else
        glGetShaderiv(shader, flag, &success);

    if (success == GL_FALSE)
    {
        if (isProgram)
            glGetProgramInfoLog(shader, sizeof(error), NULL, error);
        else
            glGetShaderInfoLog(shader, sizeof(error), NULL, error);

        std::cerr << errorMessage << ": '" << error << "'" << std::endl;
    }
}


static GLuint CreateShader(const std::string& Text, GLenum shaderType)
{
    GLuint shader = glCreateShader(shaderType);

    if (shader == 0)
        std::cerr << "error compiling shader type: " << shaderType << std::endl;

    const GLchar *shaderSourceStrings[1];
    GLint shaderSourceStringLengths[1];

    shaderSourceStrings[0] = Text.c_str();
    shaderSourceStringLengths[0] = Text.length();

    glShaderSource(shader, 1, shaderSourceStrings, shaderSourceStringLengths);
    glCompileShader(shader);

    CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");

    return shader;
}

std::string LoadShader(const std::string& fileName)
{
    std::ifstream file;
    file.open((fileName).c_str());

    std::string output;
    std::string line;

    if (file.is_open())
    {
        while (file.good())
        {
            getline(file, line);
            output.append(line + "\n");
        }
    }
    else
    {
        std::cerr << "Unable to load shader: " << fileName << std::endl;
    }

    return output;
}

我在尝试检查CreateShader函数中的错误时收到错误: Identifier "CheckShaderError" is undefined ,我对此感到困惑,因为 CheckShaderError 适用于同一 .cpp 中的其他功能。任何有关此错误的帮助将不胜感激,谢谢!

1 个答案:

答案 0 :(得分:0)

您已包含Shader.h而不是shader.h请注意大写的“S”