0

I'm writing a short OpenGL-program, and need to import the shader source, which I use the following code for (OpenGL wants the shader source as a c-string):

    std::ifstream t("GLshader.vert");
    if (!t.is_open()) {
        std::cerr << "vertex shader open failed" << std::endl;
    }
    std::stringstream buffer;
    buffer << t.rdbuf();


    const char* source = buffer.str().c_str();
    std::string s = buffer.str().c_str();
    std::cerr << "vs string :" << std::endl << s << std::endl;
    std::cerr << "vs source:" << std::endl << source << std::endl;

What has me really stumped is the fact that sometimes everything is fine, and the last two lines of code output the same text. Sometimes however, for some reason, the output is instead this:

vs string:
/* actual shader code*/
#version 330 core

layout(location=0) in vec3 position;

out vec3 pos;
out vec2 texcoord;

void main(){
  pos = position;
  texcoord = (position.xy +1.f) / 2.f;
  gl_Position = vec4(position, 1.0f);
}

vs source:
@ð4

The printouts, as well as the string, is there because I kept getting shader compilation errors (again, sometimes), and thought it might be some weird concurrency issue with reading the file. Obviously that works fine, but for some reason the c-string creation sometimes fails.

The function is called in a .dll, and I'm using visual studio. (I've run the .exe outside of VS, and the problem occurs there as well)

I'm really out of ideas here, anyone have an answer?

E. Molin
  • 1
  • 1

0 Answers0