从文件加载后,顶点着色器和片段着色器均未编译
Neither vertex shader nor fragment shader are compiling after loading from a file
我是 OpenGL 的新手,我正在尝试编写一个着色器 class 从文件加载着色器并编译它们。问题是,它们都没有编译。两者都显示如下错误消息:
0:1(1): error: syntax error, unexpected $end
我在这里搜索了很多相同问题的问题,但其中 none 有效。代码如下:
Shader.h
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader {
public:
Shader(const GLchar* path);
GLint Compile(GLenum type);
GLuint GetID() const;
const char* GetShaderCode() const;
void InfoLog() const;
private:
GLuint shaderId;
const char* shaderCode;
int shaderCodeLength;
char log[512];
};
#endif
Shader.cpp(问题所在的函数)
#include "Shader.h"
Shader::Shader(const GLchar* path) {
std::string shaderString;
std::ifstream shaderFile;
std::stringstream shaderStream;
shaderFile.exceptions(std::ifstream::badbit);
try {
shaderFile.open(path);
shaderStream << shaderFile.rdbuf();
shaderFile.close();
shaderString = shaderStream.str();
} catch (std::ifstream::failure e) {
std::cout << "Error while reading the file. (Does the file exist?)" << std::endl;
}
shaderCode = const_cast<const GLchar*>(shaderString.c_str());
shaderCodeLength = shaderString.length();
}
GLint Shader::Compile(GLenum type) {
//Compilates the shader and returns GL_TRUE if succesful or GL_FALSE otherwise.
GLint status;
shaderId = glCreateShader(type);
glShaderSource(shaderId, 1, &shaderCode, &shaderCodeLength);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
glGetShaderInfoLog(shaderId, 512, NULL, log);
return status;
}
shader.vert
#version 130
in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
uniform mat4 trans;
void main() {
Color = color;
Texcoord = texcoord;
gl_Position = trans * vec4(position, 0.0, 1.0);
}
shader.frag
#version 130
in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texKitten;
uniform sampler2D texPuppy;
void main() {
outColor = mix(texture(texKitten, Texcoord), texture(texPuppy, Texcoord), 0.5);
};
很多人说问题是着色器源代码最终有点像这样:#version 130in vec3 Color;...
(但是当我在主代码上打印它时,它打印出来就像在文件上一样)许多人说这是从文件加载着色器的正确方法。那么,这样做有什么问题呢?有没有更好的方法?
shaderCode
在构造函数 returns 后立即失效,因为您正在保存 shaderString.c_str()
.
的结果
使用它是未定义的。
使用 std::string
成员并且只在编译着色器时进行转换,或者使用动态分配。
第一个选项更好。
我是 OpenGL 的新手,我正在尝试编写一个着色器 class 从文件加载着色器并编译它们。问题是,它们都没有编译。两者都显示如下错误消息:
0:1(1): error: syntax error, unexpected $end
我在这里搜索了很多相同问题的问题,但其中 none 有效。代码如下:
Shader.h
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
class Shader {
public:
Shader(const GLchar* path);
GLint Compile(GLenum type);
GLuint GetID() const;
const char* GetShaderCode() const;
void InfoLog() const;
private:
GLuint shaderId;
const char* shaderCode;
int shaderCodeLength;
char log[512];
};
#endif
Shader.cpp(问题所在的函数)
#include "Shader.h"
Shader::Shader(const GLchar* path) {
std::string shaderString;
std::ifstream shaderFile;
std::stringstream shaderStream;
shaderFile.exceptions(std::ifstream::badbit);
try {
shaderFile.open(path);
shaderStream << shaderFile.rdbuf();
shaderFile.close();
shaderString = shaderStream.str();
} catch (std::ifstream::failure e) {
std::cout << "Error while reading the file. (Does the file exist?)" << std::endl;
}
shaderCode = const_cast<const GLchar*>(shaderString.c_str());
shaderCodeLength = shaderString.length();
}
GLint Shader::Compile(GLenum type) {
//Compilates the shader and returns GL_TRUE if succesful or GL_FALSE otherwise.
GLint status;
shaderId = glCreateShader(type);
glShaderSource(shaderId, 1, &shaderCode, &shaderCodeLength);
glCompileShader(shaderId);
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &status);
glGetShaderInfoLog(shaderId, 512, NULL, log);
return status;
}
shader.vert
#version 130
in vec2 position;
in vec3 color;
in vec2 texcoord;
out vec3 Color;
out vec2 Texcoord;
uniform mat4 trans;
void main() {
Color = color;
Texcoord = texcoord;
gl_Position = trans * vec4(position, 0.0, 1.0);
}
shader.frag
#version 130
in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texKitten;
uniform sampler2D texPuppy;
void main() {
outColor = mix(texture(texKitten, Texcoord), texture(texPuppy, Texcoord), 0.5);
};
很多人说问题是着色器源代码最终有点像这样:#version 130in vec3 Color;...
(但是当我在主代码上打印它时,它打印出来就像在文件上一样)许多人说这是从文件加载着色器的正确方法。那么,这样做有什么问题呢?有没有更好的方法?
shaderCode
在构造函数 returns 后立即失效,因为您正在保存 shaderString.c_str()
.
的结果
使用它是未定义的。
使用 std::string
成员并且只在编译着色器时进行转换,或者使用动态分配。
第一个选项更好。