OpenGL红皮书 - LoadShaders坏了吗?

时间:2013-06-28 14:40:03

标签: c++ opengl

我试图构建" OpenGL红皮书"的第一个例子。 好吧,这只是我,还是他们的第三方LoadShaders坏了?

错误LNK2001:未解析的外部符号_LoadShaders

不知道该怎么做......

3 个答案:

答案 0 :(得分:1)

将位于oglpg-8th-edition / lib中的LoadShaders.cpp添加到项目/编译中。不过,祝你好运过去。一旦最终完成编译和链接,我还没有让这个例子正常工作。您认为他们会包含一些说明,而不仅仅是“这是代码”。

答案 1 :(得分:1)

抱歉,我的LNK2001错误解决方法是使文件本身成为VS解决方案的一部分

答案 2 :(得分:0)

我也遇到了你描述的同样问题......

您可以将其用作加载着色器的解决方法!

参数与书不同但它可以工作,只需将顶点和片段着色器代码放入main.cpp所在的文本文件中,然后实例化一个Shader对象并调用.LoadShader(vertexShaderFileName,fragmentShaderFileName);

部首:

#pragma once
#include <vgl.h>
#include <string>
using namespace std;

class Shader
{
public:
    Shader(void);
    ~Shader(void);

    GLuint LoadShader(const char* vertPath, const char* fragPath);
private:
    string ReadFile(const char* path);
};

CPP:

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

Shader::Shader(void) {
}


Shader::~Shader(void) {
}

// read a file and return as a string
string Shader::ReadFile(const char* path) { 
    string content;
    ifstream fileStream(path, ios::in);

    if (!fileStream.is_open()) { 
        cerr << "File could not be opened" << endl;
        return "";
    }

    string line = "";
    while (!fileStream.eof()) { 
        getline(fileStream, line);
        content.append(line + "\n");
    }

    fileStream.close();
    return content;
}

GLuint Shader::LoadShader(const char* vertPath, const char* fragPath) { 
    //generate shader names
    GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
    GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

    //get shader src
    string vertShaderStr = ReadFile(vertPath);
    string fragShaderStr = ReadFile(fragPath);

    const char* vertShaderSrc = vertShaderStr.c_str();
    const char* fragShaderSrc = fragShaderStr.c_str();

    GLint result = GL_FALSE;
    int logLength;

    //compile vertex shader
    glShaderSource(vertShader, 1, &vertShaderSrc, NULL);
    glCompileShader(vertShader);
    glGetShaderiv(vertShader, GL_COMPILE_STATUS, &result);
    glGetShaderiv(vertShader, GL_INFO_LOG_LENGTH, &logLength);
    vector<char> vertShaderError(logLength);
    glGetShaderInfoLog(vertShader, logLength, NULL, &vertShaderError[0]);
    cout << &vertShaderError[0] << endl;

    //compile fragment shader
    glShaderSource(fragShader, 1, &fragShaderSrc, NULL);
    glCompileShader(fragShader);
    glGetShaderiv(fragShader, GL_COMPILE_STATUS, &result);
    glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
    vector<char> fragShaderError(logLength);
    glGetShaderInfoLog(fragShader, logLength, NULL, &fragShaderError[0]);
    cout << &fragShaderError[0] << endl; 

    //link the program
    GLuint program = glCreateProgram();
    glAttachShader(program, vertShader);
    glAttachShader(program, fragShader);
    glLinkProgram(program);

    glGetProgramiv(program, GL_LINK_STATUS, &result);
    glGetProgramiv(program, GL_INFO_LOG_LENGTH, &logLength);
    vector<char> programError(logLength > 1 ? logLength : 1);
    glGetProgramInfoLog(program, logLength, NULL, &programError[0]);
    cout << &programError[0] << endl;

    glDeleteShader(vertShader);
    glDeleteShader(fragShader);

    return program;
}