glUseProgram
のリファレンス には、以下のように書かれています。
If program is zero, then the current rendering state refers to an invalid program object and the results of shader execution are undefined. However, this is not an error.
…まぁ、ちょっと何言ってるかわかりませんが。
つまり、「invalid program」を指定している、言い換えれば「有効なプログラムを使っていない事 」を指定している、と考えていいと思います。
特に、質問にあるプログラム例では、「glColor4f
」やら「glPushMatrix
」やら、固定機能レンダリングの機能を使っているので、「これ以降はプラグラマブルシェーダは使わないよ」と明示的に設定しているのでしょう。
例えば、ざっとこんなプログラムを書いてみました。
cpp
1 # include <GL/glew.h>
2 # include <GLFW/glfw3.h>
3 # include <cstdio>
4
5 int main ( )
6 {
7 if ( ! glfwInit ( ) ) {
8 std :: fprintf ( stderr , "%s(%d): failed to initialize GLFW.\n" , __FILE__ , __LINE__ ) ;
9 return 1 ;
10 }
11 GLFWwindow * window = glfwCreateWindow ( 640 , 480 , "Test0902a" , NULL , NULL ) ;
12 glfwMakeContextCurrent ( window ) ;
13
14 if ( glewInit ( ) != GLEW_OK ) {
15 std :: fprintf ( stderr , "%s(%d): failed to initialize GLEW.\n" , __FILE__ , __LINE__ ) ;
16 return 1 ;
17 }
18
19 GLuint program ;
20 {
21 GLuint vs = glCreateShader ( GL_VERTEX_SHADER ) ;
22 static const char * vs_source =
23 "attribute vec2 vertex;\n"
24 "void main()\n"
25 "{\n"
26 " gl_Position = vec4(vertex, 0.0f, 1.0f);\n"
27 "}\n"
28 ;
29 glShaderSource ( vs , 1 , & vs_source , NULL ) ;
30 glCompileShader ( vs ) ;
31
32 GLuint fs = glCreateShader ( GL_FRAGMENT_SHADER ) ;
33 static const char * fs_source =
34 "void main()\n"
35 "{\n"
36 " gl_FragColor = vec4(0.0f, 0.0f, 0.5f, 1.0f);\n"
37 "}\n"
38 ;
39 glShaderSource ( fs , 1 , & fs_source , NULL ) ;
40 glCompileShader ( fs ) ;
41
42 program = glCreateProgram ( ) ;
43 glAttachShader ( program , vs ) ;
44 glAttachShader ( program , fs ) ;
45 glLinkProgram ( program ) ;
46
47 glDeleteShader ( vs ) ;
48 glDeleteShader ( fs ) ;
49 }
50
51 while ( ! glfwWindowShouldClose ( window ) ) {
52 glClearColor ( 0.2f , 0.2f , 0.2f , 1.0f ) ;
53 glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
54
55 glUseProgram ( program ) ;
56 GLint loc = glGetAttribLocation ( program , "vertex" ) ;
57 glEnableVertexAttribArray ( loc ) ;
58 const GLfloat vertices [ ] = {
59 0.0f , 0.0f ,
60 - 1.0f , - 0.9f ,
61 1.0f , - 0.9f ,
62 } ;
63 glVertexAttribPointer ( loc , 2 , GL_FLOAT , GL_FALSE , 0 , vertices ) ;
64
65 glDrawArrays ( GL_TRIANGLES , 0 , 3 ) ;
66
67 glUseProgram ( 0 ) ;
68
69 glColor4f ( 0.0f , 0.5f , 0.0f , 1.0f ) ;
70 glBegin ( GL_TRIANGLES ) ;
71 glVertex2f ( 0.0f , 0.0f ) ;
72 glVertex2f ( - 1.0f , 0.9f ) ;
73 glVertex2f ( 1.0f , 0.9f ) ;
74 glEnd ( ) ;
75
76 glfwSwapBuffers ( window ) ;
77 glfwPollEvents ( ) ;
78 }
79 glfwTerminate ( ) ;
80 return 0 ;
81 }
これを実行すると、
プログララブルシェーダを使用した箇所は緑、固定機能レンダリングを使用した箇所は青になっています。
これを、「glUseProgram(0)
」の箇所をコメントアウトすると、
というような感じになりました。
固定機能レンダリングの方でもシェーダプログラムが動いているように思われます。
プログラマブルシェーダを使用する可能性があるのであれば、「glUseProgram(0)
」を実行して、シェーダプログラムを無効にしておいたほうがいいでしょう。
「glUseProgramObjectARB
」に関しては知りませんが、ググった限りでは、OpenGLにプラグラマブルシェーダが実装される前の、拡張機能として使うシェーダプログラムだと思うので、ほぼglUseProgram
と同様と考えていいんじゃないでしょうか。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。