很簡單,我只是試圖將文件加載到我的OpenGL着色器的字符串中。我曾嘗試以下兩種:將文件加載到字符串ifstream加載其他文件的不完整副本?
std::string result = "";
std::ifstream stream(path, std::ios::in);
std::string line = "";
while (getline(stream, line))
result += line + "\n";
stream.close();
return result.c_str();
以及
std::ifstream stream(path, std::ios::in);
std::string result(std::istreambuf_iterator<char>(stream), (std::istreambuf_iterator<char>()));
stream.close();
return result.c_str();
我在着色器類在這裏使用這些方法:
std::string basePath = std::string("shaders/");
std::string vertPath = basePath + name + ".vsh";
std::string fragPath = basePath + name + ".fsh";
const char* vertSrc = loadFile(vertPath.c_str());
const char* fragSrc = loadFile(fragPath.c_str());
我創建的着色器,其中name
是「標準「,並加載罰款。然後我再次調用構造函數,但是name
是「fxaa」。在精細片段着色器加載,但加載這樣fxaa.vsh
:
#version 330 core
uniform sampler2D sampler;
uniform vec2 fbo_size;
in vec4 frag_color;
in vec2 tex_coord;
layout (location = 0) out vec4 final_color;
void main()
{
float FXAA_SPAN_MAX = 8.0;
float FXAA_REDUCE_MUL = 1.0/8.0;
float FXAA_REDUCE_MIN = 1.0/128.0;
vec3 rgbNW = texture(sampler, tex_coord + (vec2(-1.0, -1.0)/fbo_size)).rgb;
vec3 rgbNE = texture(sampler, tex_coord + (vec2(1.0, -1.0)/fbo_size)).rgb;
vec3 rgbSW = texture(sampler, tex_coord + (vec2(-1.0, 1.0)/fb
當它應該裝載這樣的:
#version 330 core
uniform mat4 projection_matrix;
uniform mat4 view_matrix;
uniform mat4 model_matrix;
layout (location = 10) in vec3 vertex_in;
layout (location = 11) in vec4 color_in;
layout (location = 12) in vec2 tex_coord_in;
out vec4 frag_color;
out vec2 tex_coord;
void main()
{
gl_Position = projection_matrix * view_matrix *
model_matrix * vec4(vertex_in, 1.0);
frag_color = color_in;
tex_coord = tex_coord_in;
}
和片段着色器的頂部看起來像什麼的頂點着色器加載: (線路1 - 21)
#version 330 core
uniform sampler2D sampler;
uniform vec2 fbo_size;
in vec4 frag_color;
in vec2 tex_coord;
layout (location = 0) out vec4 final_color;
void main()
{
float FXAA_SPAN_MAX = 8.0;
float FXAA_REDUCE_MUL = 1.0/8.0;
float FXAA_REDUCE_MIN = 1.0/128.0;
vec3 rgbNW = texture(sampler, tex_coord + (vec2(-1.0, -1.0)/fbo_size)).rgb;
vec3 rgbNE = texture(sampler, tex_coord + (vec2(1.0, -1.0)/fbo_size)).rgb;
vec3 rgbSW = texture(sampler, tex_coord + (vec2(-1.0, 1.0)/fbo_size)).rgb;
vec3 rgbSE = texture(sampler, tex_coord + (vec2(1.0, 1.0)/fbo_size)).rgb;
vec3 rgbM = texture(sampler, tex_coord).rgb;
我在心臟Java程序員,所以我不熟悉C++的IO系統。我相信這是我的工作,我的ifstream
甚至我的文件或整個文件加載器,但我不知道。
看來還是有問題。在我添加的第三個代碼塊中,'vertSrc'和'fragSrc'都具有相同的值,即使它們是單獨的文件。 –
實際上,現在它加載第一個着色器,然後加載第二個着色器的頂點着色器的隨機字符,然後在片段着色器上崩潰。 –
對不起,我通過首先將源文件加載到'std :: string'中,然後複製'c_str()'來修復它。 (與這個問題相同的問題。) –