1

我正在尝试构建“OpenGL Redbook”的第一个示例。好吧,只是我,还是他们的第 3 方 LoadShaders 坏了?

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

不知道该怎么办...

4

3 回答 3

1

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

于 2013-07-05T03:41:43.617 回答
1

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

于 2020-03-12T01:00:24.177 回答
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);
};

共产党:

#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;
}
于 2013-12-29T01:15:22.307 回答