PDA

View Full Version : WPF/OpenTK. I cannot load shaders from files.



8Observer8
02-28-2017, 02:12 AM
Hello,

This version of my program works perfect. It gets shaders from strings:



string vShaderSource =
"#version 330\n" +
"in vec4 a_Position;\n" +
"void main() {\n" +
"gl_Position = a_Position;\n" +
"}\n";
string fShaderSource =
"#version 330\n" +
"out vec4 outColor;\n" +
"void main() {\n" +
"outColor = vec4(1.0, 0.0, 0.0, 1.0);\n" +
"}\n";


Screenshot (https://dl.dropboxusercontent.com/s/ri5rqu10slhhu7n/Triangle_SetBackgroundColor.png)
Download the Project: Triangle_StringShaders.zip (https://dl.dropboxusercontent.com/s/u5zc4jpejge9j0t/Triangle_StringShaders.zip)

But when I want to load shaders from files I receive the error, see Screenshot (https://dl.dropboxusercontent.com/s/ab6dcnhx3nnawzc/Triangle_FileShaderError.png)

Download the Project: Triangle_FileShaderError.zip (https://dl.dropboxusercontent.com/s/07tcnpsaockm7te/Triangle_FileShaderError.zip)



string vShaderSource = null;
string fShaderSource = null;

// ...

private void renderCanvas_Load(object sender, EventArgs e)
{
// ...

// Load shaders from files
LoadShader("./Shaders/vShader.glsl", out vShaderSource);
LoadShader("./Shaders/fShader.glsl", out fShaderSource);
if (vShaderSource == null || fShaderSource == null)
{
File.AppendAllText(infoFileName, "Failed to load shaders from files");
return;
}

// ...
}

private void LoadShader(string fileName, out string shaderSource)
{
shaderSource = null;

using (StreamReader sr = new StreamReader(fileName))
{
shaderSource = sr.ReadToEnd();
}
}

8Observer8
02-28-2017, 04:47 AM
The solution. You need to set for files "vShader.glsl" and "fShader.glsl" in settings: "Copy to Output Directory" to "Copy always".