GLSL プログラムをコンパイル、リンク、および実行できますが、すべての属性へのハンドルを抽出することはできません。
const std :: string v = shaders .read_file (VERTEX_SHADER);
const std :: string f = shaders .read_file (FRAGMENT_SHADER);
std :: cout
<< "Vertex shader:\n" << v
<< "Fragment shader:\n" << f
<< "End of shaders.\n";
// Create objects
auto f_id = glCreateShader (GL_FRAGMENT_SHADER);
auto v_id = glCreateShader (GL_VERTEX_SHADER);
auto p = glCreateProgram ();
// Success flags
GLint v_ok, f_ok, p_ok;
// Compile vertex shader
const GLchar * source = v .c_str ();
GLint length = v .size ();
glShaderSource (v_id, 1, & source, & length);
glCompileShader (v_id);
glGetShaderiv (v_id, GL_COMPILE_STATUS, &v_ok);
// Compile fragment shader
source = f .c_str ();
length = f .size ();
glShaderSource (f_id, 1, & source, & length);
glCompileShader (f_id);
glGetShaderiv (f_id, GL_COMPILE_STATUS, &f_ok);
// Link
glAttachShader (p, v_id);
glAttachShader (p, f_id);
glLinkProgram (p);
glGetProgramiv (p, GL_LINK_STATUS, &p_ok);
if (f_ok && v_ok && p_ok)
{
std :: cout << "Build OK\n";
for (auto n : {"normal", "position", "xxx", "fail"})
{
auto a = glGetAttribLocation (p, n);
std :: cout << n <<" is " << a << std :: endl;
}
}
else
std :: cout << "Build failed.\n";
assert (GL_NO_ERROR == glGetError ());
「通常」、「位置」、および「xxx」属性が配置されることを期待していますが、「失敗」ではありません。これが出力です。
Vertex shader:
#version 130
attribute vec3 normal;
attribute vec3 position;
attribute vec3 xxx;
void main ()
{
gl_Position = vec4 (position, 1);
}
Fragment shader:
#version 130
out vec4 finalColor;
void main ()
{
finalColor = vec4 (1.0, 1.0, 1.0, 1.0);
}
End of shaders.
Build OK
normal is -1
position is 0
xxx is -1
fail is -1
実際に実行してみると、予想通り、変換されていない白い三角形が得られます。「位置」だけが正しくロードされるのはなぜですか?