LearnOpenGL

Translation in progress of learnopengl.com.
git clone https://git.mtkn.jp/LearnOpenGL
Log | Files | Refs

Shaders.html (56749B)


      1     <h1 id="content-title">Shaders</h1>
      2     <h1 id="content-title">シェーダー</h1>
      3 <h1 id="content-url" style='display:none;'>Getting-started/Shaders</h1>
      4 <p>
      5   As mentioned in the <a href="https://learnopengl.com/Getting-started/Hello-Triangle" target="_blank">Hello Triangle</a> chapter, shaders are little programs that rest on the GPU. These programs are run for each specific section of the graphics pipeline. In a basic sense, shaders are nothing more than programs transforming inputs to outputs. Shaders are also very isolated programs in that they're not allowed to communicate with each other; the only communication they have is via their inputs and outputs.
      6 <a href="https://learnopengl.com/Getting-started/Hello-Triangle" target="_blank">はじめての三角形</a>の章で説明したように、シェーダーはGPUで実行する小さなプログラムです。このプログラムはグラフィックスパイプラインの特定の場所で実行されます。基本的にシェーダーというのは入力をなんらかの形で変換して出力するものです。シェーダーは入出力以外の手段で互いに通信できないので、非常に独立性の高いプログラムだといえます。
      7 </p>
      8 
      9 <p>
     10   In the previous chapter we briefly touched the surface of shaders and how to properly use them. We will now explain shaders, and specifically the OpenGL Shading Language, in a more general fashion.
     11 前章においてシェーダーの表面的な部分と適切な使い方について少し触れました。ここではシェーダーについて、特にOpenGLのシェーディング言語について、もう少し全般的に見ていきましょう。
     12 </p>
     13 
     14 <h1>GLSL</h1>
     15 <p>
     16   Shaders are written in the C-like language GLSL. GLSL is tailored for use with graphics and contains useful features specifically targeted at vector and matrix manipulation.
     17 シェーダーはC言語に似たGLSLで記述されます。GLSLはグラフィックを扱うために作られ、便利な機能、特にベクトルと行列に関するものが含まれます。
     18 </p>
     19 
     20 <p>
     21   Shaders always begin with a version declaration, followed by a list of input and output variables, uniforms and its <fun>main</fun> function. Each shader's entry point is at its <fun>main</fun> function where we process any input variables and output the results in its output variables. Don't worry if you don't know what uniforms are, we'll get to those shortly.
     22 シェーダーはバージョンの宣言から始まり、入力変数と出力変数およびユニフォームの列挙、そして<fun>main</fum>関数が続きます。シェーダーは<fun>main</fun>関数から実行が開始され、その関数内で入力変数を処理しその結果を出力変数に格納して返します。ユニフォームについてはすぐ後で説明しますので、今は気にしないで下さい。
     23 </p>
     24 
     25 <p>
     26   A shader typically has the following structure:
     27 シェーダーは典型的には以下のような構造をしています:
     28 </p>
     29 
     30 <pre><code>
     31 #version version_number
     32 in type in_variable_name;
     33 in type in_variable_name;
     34 
     35 out type out_variable_name;
     36   
     37 uniform type uniform_name;
     38   
     39 void main()
     40 {
     41   // process input(s) and do some weird graphics stuff
     42   // 入力の処理とグラフィックに関するいろいろ
     43   ...
     44   // output processed stuff to output variable
     45   // 出力変数に処理したものを出力
     46   out_variable_name = weird_stuff_we_processed;
     47 }
     48 </code></pre>
     49 
     50 <p>
     51   When we're talking specifically about the vertex shader each input variable is also known as a <def>vertex attribute</def>. There is a maximum number of vertex attributes we're allowed to declare limited by the hardware. OpenGL guarantees there are always at least 16 4-component vertex attributes available, but some hardware may allow for more which you can retrieve by querying <var>GL_MAX_VERTEX_ATTRIBS</var>:
     52 頂点シェーダーにおいて、入力変数は<def>頂点属性</def>と呼ばれます。宣言できる頂点属性の最大数はハードウェアにより決定されます。OpenGLは最低でも、4要素からなる頂点属性を16個利用できることを保証していますが、ハードウェアによってはもっと利用できます。その最大数は<var>GL_MAX_VERTEX_ATTRIBS</var>から確認できます:
     53 </p>
     54 
     55 <pre><code>
     56 int nrAttributes;
     57 glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &nrAttributes);
     58 std::cout &lt;&lt; "Maximum nr of vertex attributes supported: " &lt;&lt; nrAttributes &lt;&lt; std::endl;
     59 </code></pre>
     60  
     61 <p>
     62   This often returns the minimum of <code>16</code> which should be more than enough for most purposes.
     63 多くの場合この数字はOpenGLが定める最低である<code>16</code>になっていますが、ほとんどの場合それで十分です。
     64 </p>
     65 
     66 <h2>Types</h2>
     67 <h2>型</h2>
     68 <p>
     69   GLSL has, like any other programming language, data types for specifying what kind of variable we want to work with. GLSL has most of the default basic types we know from languages like C:  <code>int</code>, <code>float</code>, <code>double</code>, <code>uint</code> and <code>bool</code>. GLSL also features two container types that we'll be using a lot, namely <code>vectors</code> and <code>matrices</code>. We'll discuss matrices in a later chapter.
     70 他のプログラミング言語と同様に、GLSLにも変数の型があります。C言語のようなプログラミング言語において、基本的な型といえば、<code>int</code>、<code>float</code>、<code>double</code>、<code>uint</code>そして<code>bool</code>といったものですが、GLSLはこのような基本的な型に加えて<code>ベクトル</code>と<code>行列</code>という容器のような型があります。この二つの型はこれから頻繁に利用するものです。行列については別の章で説明します。
     71 </p>
     72 
     73 <h3>Vectors</h3>
     74 <h3>ベクトル</h3>
     75 <p>
     76   A vector in GLSL is a 1,2,3 or 4 component container for any of the basic types just mentioned.  They can take the following form (<code>n</code> represents the number of components):
     77 GLSLにおけるベクトルは上に記した基本的な型の数値を1から4個、要素として持つ容器です。これには以下のような形式があります(<code>n</code>は要素数です):
     78 </p>
     79   
     80   <ul>
     81     <li><code>vecn</code>: the default vector of <code>n</code> floats.</li>
     82     <li><code>bvecn</code>: a vector of <code>n</code> booleans.</li>
     83     <li><code>ivecn</code>: a vector of <code>n</code> integers.</li>
     84   	<li><code>uvecn</code>: a vector of <code>n</code> unsigned integers.</li>
     85   	<li><code>dvecn</code>: a vector of <code>n</code> double components.</li>
     86   </ul>
     87   <ul>
     88     <li><code>vecn</code>: <code>n</code>個の浮動小数点数からなるデフォルトのベクトル。</li>
     89     <li><code>bvecn</code>: <code>n</code>個の真偽値からなるベクトル。</li>
     90     <li><code>ivecn</code>: <code>n</code>個の整数からなるベクトル。</li>
     91   	<li><code>uvecn</code>: <code>n</code>個の符号なし整数からなるベクトル。</li>
     92   	<li><code>dvecn</code>: <code>n</code>個の倍精度浮動小数点数からなるベクトル。</li>
     93   </ul>
     94   
     95 <p>
     96   Most of the time we will be using the basic <code>vecn</code> since floats are sufficient for most of our purposes.
     97 本書においては浮動小数点数で十分間に合うので、主に<code>vecn</code>を利用します。
     98 </p>
     99   
    100   <p>
    101     Components of a vector can be accessed via <code>vec.x</code> where <code>x</code> is the first component of the vector. You can use <code>.x</code>, <code>.y</code>, <code>.z</code> and <code>.w</code> to access their first, second, third and fourth component respectively. GLSL also allows you to use <code>rgba</code> for colors or <code>stpq</code> for texture coordinates, accessing the same components.
    102 ベクトルの要素には<code>vec.x</code>のような形でアクセスできます。1〜4番目の要素はそれぞれ<code>.x</code>、<code>.y</code>、<code>.z</code>、<code>.w</code>と記述します。GLSLでは他にも、ベクトルを色の表現として利用する場合には<code>rgba</code>により、またテクスチャの座標には<code>stpq</code>により、各要素にアクセスすることができます。
    103   </p>
    104   
    105   <p>
    106     The vector datatype allows for some interesting and flexible component selection called <def>swizzling</def>. Swizzling allows us to use syntax like this:
    107 ベクトルの要素を選択する、おもしろくて柔軟性の高い方法があります。<def>スウィズリング</def>と呼ばれるこの方法は以下のように利用します:
    108   </p>
    109   
    110 <pre><code>
    111 vec2 someVec;
    112 vec4 differentVec = someVec.xyxx;
    113 vec3 anotherVec = differentVec.zyw;
    114 vec4 otherVec = someVec.xxxx + anotherVec.yxzy;
    115 </code></pre>
    116   
    117 <p>
    118 	You can use any combination of up to 4 letters to create a new vector (of the same type) as long as the original vector has those components; it is not allowed to access the <code>.z</code> component of a <code>vec2</code> for example. We can also pass vectors as arguments to different vector constructor calls, reducing the number of arguments required:    
    119 あるベクトルを用いて、その要素から好きな組み合わせにより同じ型の新しいベクトルを作れるのです。ただし例えば<code>vec2</code>の<code>.z</code>要素は存在しないので利用できません。あるいは以下のように、あるベクトルを他のベクトルのコンストラクタに渡すこともできます:
    120 </p>
    121   
    122 <pre><code>
    123 vec2 vect = vec2(0.5, 0.7);
    124 vec4 result = vec4(vect, 0.0, 0.0);
    125 vec4 otherResult = vec4(result.xyz, 1.0);
    126 </code></pre>
    127 
    128 <p>
    129 	Vectors are thus a flexible datatype that we can use for all kinds of input and output. Throughout the book you'll see plenty of examples of how we can creatively manage vectors.  
    130 このようにベクトルは柔軟性の高い型で、いつでも入力や出力として用いることができます。本書を通じて創造的にベクトルを使う例がたくさん見られます。
    131 </p>
    132   
    133 <h2>Ins and outs</h2>
    134 <h2>入力と出力</h2>
    135 <p>
    136   Shaders are nice little programs on their own, but they are part of a whole and for that reason we want to have inputs and outputs on the individual shaders so that we can move stuff around. GLSL defined the <code>in</code> and <code>out</code> keywords specifically for that purpose. Each shader can specify inputs and outputs using those keywords and wherever an output variable matches with an input variable of the next shader stage they're passed along. The vertex and fragment shader differ a bit though.
    137 シェーダーはそれだけで完結した小さなプログラムですが、それらを組み合わて全体としてうまく機能する必要があります。そのため、互いにデータをやりとりするための入力と出力がそれぞれのシェーダーで必要になります。GLSLには入出力を宣言するための<code>in</code>と<code>out</code>というキーワードが用意されています。各シェーダーではこれらのキーワードを使って入力と出力を定義し、あるシェーダーの出力が次のシェーダーの入力と合致すればそこでデータの受け渡しが行われます。ただし頂点シェーダーとフラグメントシェーダーは少し事情が異なります。
    138 </p>
    139   
    140 <p>
    141   The vertex shader <strong>should</strong> receive some form of input otherwise it would be pretty ineffective. The vertex shader differs in its input, in that it receives its input straight from the vertex data. To define how the vertex data is organized we specify the input variables with location metadata so we can configure the vertex attributes on the CPU. We've seen this in the previous chapter as <code>layout (location = 0)</code>. The vertex shader thus requires an extra layout specification for its inputs so we can link it with the vertex data.
    142 頂点シェーダーは特定の形式の入力を持つことが<strong>強く推奨</strong>されます。そうしないとかなり効率が落ちます。頂点シェーダーは他のシェーダーとは違い、入力として頂点データを直接受け取ります。CPU上で設定した頂点データがどのように構成されているかをシェーダーに伝えるため、入力に対してデータの位置というメタデータを記述します。前章で<code>layout (location = 0)</code>というコードを記述したのがそれです。頂点シェーダーへの入力を頂点データと紐付けるため、このようにデータの配置を特定する必要があるのです。
    143   </p>
    144   
    145 <note>
    146   It is also possible to omit the <code>layout (location = 0)</code> specifier and query for the attribute locations in your OpenGL code via <fun><function id='104'>glGetAttribLocation</function></fun>, but I'd prefer to set them in the vertex shader. It is easier to understand and saves you (and OpenGL) some work.
    147 <code>layout (location = 0)</code>を使わず、<fun><function id='104'>glGetAttribLocation</function></fun>によって属性の位置を特定することも可能ですが、著者は前述の方法を好みます。理解しやすく開発者(とOpenGL)の労力を節約できるからです。
    148   </note>
    149   
    150 <p>
    151   The other exception is that the fragment shader requires a <code>vec4</code> color output variable, since the fragment shaders needs to generate a final output color. If you fail to specify an output color in your fragment shader, the color buffer output for those fragments will be undefined (which usually means OpenGL will render them either black or white).
    152 もう一つの例外であるフラグメントシェーダーはその出力が<code>vec4</code>型の色でなければなりません。このシェーダーは最終的な色を生成する必要があるからです。フラグメントシェーダーにおいてあるフラグメントに対して色を出力できなかった場合、そのフラグメントの色バッファがどうなるかは不定です(多くの場合黒または白で描画されます)。
    153   </p>
    154 
    155 <p>
    156 	So if we want to send data from one shader to the other we'd have to declare an output in the sending shader and a similar input in the receiving shader. When the types and the names are equal on both sides OpenGL will link those variables together and then it is possible to send data between shaders (this is done when linking a program object). To show you how this works in practice we're going to alter the shaders from the previous chapter to let the vertex shader decide the color for the fragment shader.
    157 あるシェーダーから次のシェーダーにデータを送信する場合、送信元の出力と受信先の入力を同じ形式で宣言しなければいけません。双方の型と名前が一致した場合、OpenGLがそれを接続しデータの受け渡しが可能になります(この作業はプログラムオブジェクトのリンク時に行われます)。実際の動作を確認するために、前章で作成したシェーダーを変更して、頂点シェーダーがフラグメントシェーダーの色を決定するようにしましょう。
    158   </p>
    159 
    160 <strong>Vertex shader</strong>
    161 <strong>頂点シェーダー</strong>
    162 <pre><code>
    163 #version 330 core
    164 layout (location = 0) in vec3 aPos; // the position variable has attribute position 0
    165 layout (location = 0) in vec3 aPos; // 位置変数は位置0に存在
    166   
    167 out vec4 vertexColor; // specify a color output to the fragment shader
    168 out vec4 vertexColor; // フラグメントシェーダーへの出力として色を指定
    169 
    170 void main()
    171 {
    172     gl_Position = vec4(aPos, 1.0); // see how we directly give a vec3 to vec4's constructor
    173     vertexColor = vec4(0.5, 0.0, 0.0, 1.0); // set the output variable to a dark-red color
    174     gl_Position = vec4(aPos, 1.0); // このようにすればvec3からvec4を直接作成できます
    175     vertexColor = vec4(0.5, 0.0, 0.0, 1.0); // 出力する色を暗い赤に
    176 }
    177 </code></pre>  
    178   
    179 <strong>Fragment shader</strong>
    180 <strong>フラグメントシェーダー</strong>
    181 <pre><code>
    182 #version 330 core
    183 out vec4 FragColor;
    184   
    185 in vec4 vertexColor; // the input variable from the vertex shader (same name and same type)  
    186 in vec4 vertexColor; // 頂点シェーダーからの入力(同じ名前かつ同じ型)
    187 
    188 void main()
    189 {
    190     FragColor = vertexColor;
    191 } 
    192 </code></pre>
    193   
    194 <p>
    195   You can see we declared a <var>vertexColor</var> variable as a <code>vec4</code> output that we set in the vertex shader and we declare a similar <var>vertexColor</var> input in the fragment shader. Since they both have the same type and name, the <var>vertexColor</var> in the fragment shader is linked to the <var>vertexColor</var> in the vertex shader. Because we set the color to a dark-red color in the vertex shader, the resulting fragments should be dark-red as well. The following image shows the output:
    196 頂点シェーダーにおいて<code>vec4</code>の変数<var>vertexColor</var>を宣言し、フラグメントシェーダーでも同様のものを定義します。双方が同じ名前で同じ型の変数なので、フラグメントシェーダーの<var>vertexColor</var>は頂点シェーダーの<var>vertexColor</var>と接続されます。頂点シェーダーにおいて暗い赤に設定したので、フラグメントシェーダーからも暗い赤が出力されます。その結果以下のような画像が生成されます:
    197   </p>
    198  
    199   <img src="/img/getting-started/shaders.png" class="clean"/>
    200   
    201 <p>
    202    There we go! We just managed to send a value from the vertex shader to the fragment shader. Let's spice it up a bit and see if we can send a color from our application to the fragment shader!
    203 いかがでしょう。頂点シェーダーからフラグメントシェーダーに変数を送信できました。これに少し味付けして、アプリケーションからフラグメントシェーダーに色の情報を送信するようにしましょう。
    204   </p>
    205   
    206   <h2>Uniforms</h2>
    207   <h2>ユニフォーム</h2>
    208   <p>
    209     <def>Uniforms</def> are another way to pass data from our application on the CPU to the shaders on the GPU. Uniforms are however slightly different compared to vertex attributes. First of all, uniforms are <def>global</def>. Global, meaning that a uniform variable is unique per shader program object, and can be accessed from any shader at any stage in the shader program. Second, whatever you set the uniform value to, uniforms will keep their values until they're either reset or updated.
    210 <def>ユニフォーム</def>はCPU上のアプリケーションからGPU上のシェーダーにデータを送信するもうひとつの方法です。ユニフォームは頂点属性と少し違います。第一にユニフォームは<def>グローバル</def>です。すなわち、ユニフォームは各シェーダープログラムオブジェクトにおいて一意であり、同シェーダープログラム中の他のシェーダーからもアクセスできます。第二に、ユニフォームの値がセットされた場合、その値はリセットあるいは更新されるまで一定です。
    211   </p>
    212   
    213   <p>
    214     To declare a uniform in GLSL we simply add the <code>uniform</code> keyword to a shader with a type and a name. From that point on we can use the newly declared uniform in the shader. Let's see if this time we can set the color of the triangle via a uniform:
    215 GLSLにおいてユニフォームを宣言するには<code>uniform</code>キーワードを変数の型と名前に添えてシェーダーに記述するだけです。以降そのシェーダーにおいてこのユニフォームが利用できます。それではユニフォームを使って三角形の色が変更できるか試してみましょう:
    216   </p>
    217   
    218 <pre><code>
    219 #version 330 core
    220 out vec4 FragColor;
    221   
    222 uniform vec4 ourColor; // we set this variable in the OpenGL code.
    223 uniform vec4 ourColor; // この変数の値をOpenGLのコードで設定します。
    224 
    225 void main()
    226 {
    227     FragColor = ourColor;
    228 }   
    229 </code></pre>
    230   
    231   <p>
    232     We declared a uniform <code>vec4</code> <var>ourColor</var> in the fragment shader and set the fragment's output color to the content of this uniform value. Since uniforms are global variables, we can define them in any shader stage we'd like so no need to go through the vertex shader again to get something to the fragment shader. We're not using this uniform in the vertex shader so there's no need to define it there.
    233 <code>vec4</code> <var>ourColor</var>というユニフォームをフラグメントシェーダーで宣言し、フラグメントシェーダーの出力の色をこのユニフォームの値にしました。ユニフォームはグローバル変数なので、どのシェーダーにおいて定義することも可能です。そのため頂点シェーダーにおいてフラグメントシェーダーのために何かする必要はありません。頂点シェーダーにおいてこのユニフォームを利用しないので、そこで定義する必要もありません。
    234   </p>
    235   
    236   <warning>
    237     If you declare a uniform that isn't used anywhere in your GLSL code the compiler will silently remove the variable from the compiled version which is the cause for several frustrating errors; keep this in mind!
    238 定義したユニフォームがどのGLSLのコードにおいて全く利用されない場合、コンパイラは黙ってその変数を削除します。この仕様は時にうっとうしいエラーの原因になります。頭の片隅に置いておいて下さい 。
    239   </warning>
    240   
    241   <p>
    242     The uniform is currently empty; we haven't added any data to the uniform yet so let's try that. We first need to find the index/location of the uniform attribute in our shader. Once we have the index/location of the uniform, we can update its values. Instead of passing a single color to the fragment shader, let's spice things up by gradually changing color over time:
    243 このユニフォームはまだデータを追加していないので空っぽです。ここにデータを渡す方法を見ていきましょう。まずシェーダーにおけるユニフォームの場所を探す必要があります。ユニフォームが見つかったらその値を更新します。一色だけ指定するのでは面白くないので、時間と共に色を変化させてみましょう。
    244   </p>
    245   
    246 <pre><code>
    247 float timeValue = <function id='47'>glfwGetTime</function>();
    248 float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
    249 int vertexColorLocation = <function id='45'>glGetUniformLocation</function>(shaderProgram, "ourColor");
    250 <function id='28'>glUseProgram</function>(shaderProgram);
    251 <function id='44'>glUniform</function>4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
    252 </code></pre>
    253   
    254   <p>
    255     First, we retrieve the running time in seconds via <fun><function id='47'>glfwGetTime</function>()</fun>. Then we vary the color in the range of <code>0.0</code> - <code>1.0</code> by using the <fun>sin</fun> function and store the result in <var>greenValue</var>. 
    256 最初に<fun><function id='47'>glfwGetTime</function>()</fun>により実行時間を秒単位で取得します。次に<fun>sin</fun>関数を利用し色の数値を<code>0.0</code>と<code>1.0</code>の間で設定し、その値を<var>greenValue</var>に保存します。
    257   </p>
    258   
    259   <p>
    260     Then we query for the location of the <var>ourColor</var> uniform using <fun><function id='45'>glGetUniformLocation</function></fun>. We supply the shader program and the name of the uniform (that we want to retrieve the location from) to the query function. If <fun><function id='45'>glGetUniformLocation</function></fun> returns <code>-1</code>, it could not find the location. Lastly we can set the uniform value using the <fun><function id='44'>glUniform</function>4f</fun> function. Note that finding the uniform location does not require you to use the shader program first, but updating a uniform <strong>does</strong> require you to first use the program (by calling <fun><function id='28'>glUseProgram</function></fun>), because it sets the uniform on the currently active shader program.
    261 そして<var>ourColor</var>ユニフォームの場所を<fun><function id='45'>glGetUniformLocation</function></fun>により割り出します。シェーダープログラムと、場所が知りたいユニフォームの名前をこの関数に渡します。<fun><function id='45'>glGetUniformLocation</function></fun>が<code>-1</code>を返した場合、ユニフォームが見つからなかったということです。最後に<fun><function id='44'>glUniform</function>4f</fun>を用いてユニフォームの値を設定します。ユニフォームを探す際にシェーダープログラムの利用を宣言する必要はありません。しかしユニフォームの値の更新は、現在アクティブなシェーダープログラムに対して行われるので、<fun><function id='28'>glUseProgram</function></fun>によりシェーダープログラムの利用を宣言することが<strong>必要</strong>です。このことに留意してください。
    262   </p>
    263   
    264 <note>
    265 <p>
    266     Because OpenGL is in its core a C library it does not have native support for function overloading, so wherever a function can be called with different types OpenGL defines new functions for each type required; <fun><function id='44'>glUniform</function></fun> is a perfect example of this. The function requires a specific postfix for the type of the uniform you want to set. A few of the possible postfixes are:
    267 OpenGLの核となる部分はC言語のライブラリであり、関数のオーバーロードができないので、違う型の引数に対して同じことを行う関数が必要な場合、それぞれの型に対して別の関数として定義されています。<fun><function id='44'>glUniform</function></fun>はそのいい例です。値を設定するユニフォームの型に応じて、関数名に接尾語がつきます。例として以下のようなものが挙げられます:
    268   <ul>
    269     <li><code>f</code>: the function expects a <code>float</code> as its value.</li>  
    270     <li><code>i</code>: the function expects an <code>int</code> as its value.</li>  
    271   	<li><code>ui</code>: the function expects an <code>unsigned int</code> as its value.</li>  
    272     <li><code>3f</code>: the function expects 3 <code>float</code>s as its value.</li> 
    273     <li><code>fv</code>: the function expects a <code>float</code> vector/array as its value.</li> 
    274   </ul>
    275   <ul>
    276     <li><code>f</code>: 関数は1つの<code>float</code>を引数に取ります。</li>  
    277     <li><code>i</code>: 関数は1つの<code>int</code>を引数に取ります。</li>  
    278   	<li><code>ui</code>: 関数は1つの<code>unsigned int</code>を引数に取ります。</li>  
    279     <li><code>3f</code>: 関数は3つの<code>float</code>を引数に取ります。</li> 
    280     <li><code>fv</code>: 関数は<code>float</code>のベクトル(配列)1つを引数に取ります。</li> 
    281   </ul>
    282   Whenever you want to configure an option of OpenGL simply pick the overloaded function that corresponds with your type. In our case we want to set 4 floats of the uniform individually so we pass our data via <fun><function id='44'>glUniform</function>4f</fun> (note that we also could've used the <code>fv</code> version).
    283 OpenGLのオプションを設定したい場合、単にオーバーロードされた関数から適合する型のものを呼び出して下さい。今回はユニフォームに対して4つの浮動小数点数を割り当てたいので<fun><function id='44'>glUniform</function>4f</fun>を利用します(色を配列に格納すれば<code>fv</code>を利用することも可能です)。
    284 </p>
    285 </note>
    286     
    287  <p>    
    288    Now that we know how to set the values of uniform variables, we can use them for rendering. If we want the color to gradually change, we want to update this uniform every frame, otherwise the triangle would maintain a single solid color if we only set it once. So we calculate the <var>greenValue</var> and update the uniform each render iteration:
    289 ユニフォーム変数に値をセットしたので、これを描画に利用できるようになりました。時間と共に色を変化させるには、フレーム毎にユニフォームを更新しなければなりません。そうしないと三角形は最初に設定した色のままになります。そのため<var>greenValue</var>の計算とユニフォームの更新は描画ループの中に配置します:
    290   </p>
    291   
    292 <pre><code>
    293 while(!<function id='14'>glfwWindowShouldClose</function>(window))
    294 {
    295     // input
    296     // 入力
    297     processInput(window);
    298 
    299     // render
    300     // 描画
    301     // clear the colorbuffer
    302     // 色バッファの削除
    303     <function id='13'><function id='10'>glClear</function>Color</function>(0.2f, 0.3f, 0.3f, 1.0f);
    304     <function id='10'>glClear</function>(GL_COLOR_BUFFER_BIT);
    305 
    306     // be sure to activate the shader
    307     // シェーダーのアクティベート
    308     <function id='28'>glUseProgram</function>(shaderProgram);
    309   
    310     // update the uniform color
    311     // ユニフォームの色を更新
    312     float timeValue = <function id='47'>glfwGetTime</function>();
    313     float greenValue = sin(timeValue) / 2.0f + 0.5f;
    314     int vertexColorLocation = <function id='45'>glGetUniformLocation</function>(shaderProgram, "ourColor");
    315     <function id='44'>glUniform</function>4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f);
    316 
    317     // now render the triangle
    318     // 三角形の描画
    319     <function id='27'>glBindVertexArray</function>(VAO);
    320     <function id='1'>glDrawArrays</function>(GL_TRIANGLES, 0, 3);
    321   
    322     // swap buffers and poll IO events
    323     // バッファの入替えとイベントの処理
    324     <function id='24'>glfwSwapBuffers</function>(window);
    325     <function id='23'>glfwPollEvents</function>();
    326 }
    327 </code></pre>
    328   
    329   <p>
    330     The code is a relatively straightforward adaptation of the previous code. This time, we update a  uniform value each frame before drawing the triangle. If you update the uniform correctly you should see the color of your triangle gradually change from green to black and back to green.
    331 コードには以前のものをほとんどそのまま適応しただけです。今回は各フレームにおいて三角形を描画する前にユニフォームの値を更新しています。ユニフォームの更新がうまく行われていれば、三角形の色が緑から黒へ、黒から緑へ変化していくのが確認できるでしょう。
    332   </p>
    333   
    334 <div class="video paused" onclick="ClickVideo(this)">
    335   <video width="600" height="450" loop>
    336     <source src="/video/getting-started/shaders.mp4" type="video/mp4" />
    337     <img src="/img/getting-started/shaders2.png" class="clean"/>
    338   </video>
    339 </div>
    340 
    341   
    342 <p>
    343     Check out the source code <a href="/code_viewer_gh.php?code=src/1.getting_started/3.1.shaders_uniform/shaders_uniform.cpp" target="_blank">here</a> if you're stuck.
    344 問題があれば<a href="/code_viewer_gh.php?code=src/1.getting_started/3.1.shaders_uniform/shaders_uniform.cpp" target="_blank">ここ</a>でソースコードを確認してください。
    345   </p>
    346   
    347 <p>
    348   As you can see, uniforms are a useful tool for setting attributes that may change every frame, or for interchanging data between your application and your shaders, but what if we want to set a color for each vertex? In that case we'd have to declare as many uniforms as we have vertices. A better solution would be to include more data in the vertex attributes which is what we're going to do now.
    349 ご覧のように、ユニフォームはフレームごとに値を変更したり、アプリケーションとシェーダーの間でデータをやりとりするうえで便利です。しかし各頂点に対してそれぞれの色を設定したい場合はどうでしょう。この場合、頂点の数だけユニフォームを宣言する必要があります。より良い方法としては頂点属性に追加の情報を含めることが挙げられます。以下、その方法を見ていきましょう。
    350   </p>
    351     
    352   <h2>More attributes!</h2>
    353   <h2>属性の追加</h2>
    354   <p>
    355     We saw in the previous chapter how we can fill a VBO, configure vertex attribute pointers and store it all in a VAO. This time, we also want to add color data to the vertex data. We're going to add color data as 3 <code>float</code>s to the <var>vertices</var> array. We assign a red, green and blue color to each of the corners of our triangle respectively:
    356 前章で、VBOにデータを割り当て、頂点属性のポインタを設定し、それをVAOに保存する方法を説明しました。ここでは頂点データに色の情報も追加しましょう。<var>頂点</var>配列に色の情報を3つの<code>float</code>として追加します。三角形の各頂点に赤、緑、青の色を割り当てましょう:
    357   </p>
    358   
    359 <pre><code>
    360 float vertices[] = {
    361     // positions         // colors
    362     // 位置              // 色
    363      0.5f, -0.5f, 0.0f,  1.0f, 0.0f, 0.0f,   // 右下
    364     -0.5f, -0.5f, 0.0f,  0.0f, 1.0f, 0.0f,   // 左下
    365      0.0f,  0.5f, 0.0f,  0.0f, 0.0f, 1.0f    // 上 
    366 };    
    367 </code></pre>
    368   
    369 <p>
    370   Since we now have more data to send to the vertex shader, it is necessary to adjust the vertex shader to also receive our color value as a vertex attribute input. Note that we set the location of the <var>aColor</var> attribute to 1 with the layout specifier:
    371 頂点シェーダーに送信するデータが増えたため、頂点シェーダーが頂点属性の入力から色の情報を受け取れるように設定する必要があります。<var>aColor</var>の属性位置を1に設定していることに注意して下さい:
    372 </p>
    373   
    374 <pre><code>
    375 #version 330 core
    376 layout (location = 0) in vec3 aPos;   // the position variable has attribute position 0
    377 layout (location = 1) in vec3 aColor; // the color variable has attribute position 1
    378 layout (location = 0) in vec3 aPos;   // 位置の変数の属性位置は0
    379 layout (location = 1) in vec3 aColor; // 色の変数の属性位置は1
    380   
    381 out vec3 ourColor; // output a color to the fragment shader
    382 out vec3 ourColor; // フラグメントシェーダーへ色を出力
    383 
    384 void main()
    385 {
    386     gl_Position = vec4(aPos, 1.0);
    387     ourColor = aColor; // set ourColor to the input color we got from the vertex data
    388     ourColor = aColor; // ourColorを頂点データから取得した色に設定
    389 }       
    390 </code></pre>
    391   
    392   <p>
    393     Since we no longer use a uniform for the fragment's color, but now use the <var>ourColor</var> output variable we'll have to change the fragment shader as well:
    394 フラグメントの色としてユニフォームではなく<var>ourColor</var>の出力を利用するので、フラグメントシェーダーを以下のように変更します:
    395   </p>
    396   
    397 <pre><code>
    398 #version 330 core
    399 out vec4 FragColor;  
    400 in vec3 ourColor;
    401   
    402 void main()
    403 {
    404     FragColor = vec4(ourColor, 1.0);
    405 }
    406 </code></pre>
    407   
    408 <p>
    409   Because we added another vertex attribute and updated the VBO's memory we have to re-configure the vertex attribute pointers. The updated data in the VBO's memory now looks a bit like this: 
    410 頂点属性を追加し、VBOのメモリを更新したので、頂点属性ポインタを再設定します。更新されたデータはVBOのメモリ内で以下のような配置になっています:
    411   </p>
    412   
    413   <img src="/img/getting-started/vertex_attribute_pointer_interleaved.png" class="clean" alt="Interleaved data of position and color within VBO to be configured wtih <function id='30'>glVertexAttribPointer</function>"/>
    414     
    415 <p>
    416   Knowing the current layout we can update the vertex format with <fun><function id='30'>glVertexAttribPointer</function></fun>:
    417 これお踏まえ、頂点のフォーマットを<fun><function id='30'>glVertexAttribPointer</function></fun>により以下のように変更します:
    418 </p>
    419   
    420 <pre><code>
    421 // position attribute
    422 // 位置属性
    423 <function id='30'>glVertexAttribPointer</function>(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
    424 <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(0);
    425 // color attribute
    426 // 色属性
    427 <function id='30'>glVertexAttribPointer</function>(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float)));
    428 <function id='29'><function id='60'>glEnable</function>VertexAttribArray</function>(1);
    429 </code></pre>
    430 
    431 <p> 
    432   The first few arguments of <fun><function id='30'>glVertexAttribPointer</function></fun> are relatively straightforward. This time we are configuring the vertex attribute on attribute location <code>1</code>. The color values have a size of <code>3</code> <code>float</code>s and we do not normalize the values. 
    433 <fun><function id='30'>glVertexAttribPointer</function></fun>の前半の引数は分かりやすいと思います。今回は、属性位置<code>1</code>の頂点属性を設定します。色の情報は<code>float</code>が<code>3</code>つ分のサイズで、正規化はしません。
    434   </p>
    435   
    436   <p>
    437     Since we now have two vertex attributes we have to re-calculate the <em>stride</em> value. To get the next attribute value (e.g. the next <code>x</code> component of the position  vector) in the data array we have to move <code>6</code> <code>float</code>s to the right, three for the position values and three for the color values. This gives us a stride value of 6 times the size of a <code>float</code> in bytes (= <code>24</code> bytes). <br/>
    438   Also, this time we have to specify an offset. For each vertex, the position vertex attribute is first so we declare an offset of <code>0</code>. The color attribute starts after the position data so the offset is <code>3 * sizeof(float)</code> in bytes (= <code>12</code> bytes).
    439 頂点属性が二種類になったので<em>ストライド</em>も計算しなおします。次の属性の値(つまり、次の位置属性の<code>x</code>要素)を得るために、データの配列上で<code>float</code><code>6</code>つ分右に移動する必要があります。位置属性の値3つ分と色属性の値3つ分です。つまりストライドは<code>float</code>のサイズの6倍(=<code>24</code>バイト)です。<br/>
    440 加えて今回はデータの開始位置も設定しないといけません。各頂点において、位置を表わす頂点属性は先頭に位置するので、開始位置は<code>0</code>です。色を表わす頂点属性は位置のデータの後ろにあるので、開始位置は<code>3 * sizeof(float)</code>バイト(=<code>12</code>バイト)です。
    441 </p>
    442   
    443 <p> 
    444     Running the application should result in the following image:
    445 アプリケーションを実行すると以下のようになります:
    446 </p>
    447   
    448   <img src="/img/getting-started/shaders3.png" class="clean"/>
    449   
    450   <p>
    451     Check out the source code <a href="/code_viewer_gh.php?code=src/1.getting_started/3.2.shaders_interpolation/shaders_interpolation.cpp" target="_blank">here</a> if you're stuck.
    452 問題があれば<a href="/code_viewer_gh.php?code=src/1.getting_started/3.2.shaders_interpolation/shaders_interpolation.cpp" target="_blank">ここ</a>でソースコードを確認して下さい。
    453   </p>
    454   
    455  <p>
    456    The image may not be exactly what you would expect, since we only supplied 3 colors, not the huge color palette we're seeing right now. This is all the result of something called <def>fragment interpolation</def> in the fragment shader. When rendering a triangle the rasterization stage usually results in a lot more fragments than vertices originally specified. The rasterizer then determines the positions of each of those fragments based on where they reside on the triangle shape.<br/>
    457    Based on these positions, it <def>interpolates</def> all the fragment shader's input variables. Say for example we have a line where the upper point has a green color and the lower point a blue color. If the fragment shader is run at a fragment that resides around a position at <code>70%</code> of the line, its resulting color input attribute would then be a linear combination of green and blue; to be more precise: <code>30%</code> blue and <code>70%</code> green.
    458 画像は読者が想像したものと少し違うと思います。3つの色を設定したのであり、この画像のようにたくさんの色のパターンを指定したわけではないからです。この結果はフラグメントシェーダーの<def>フラグメント補完</def>と呼ばれる機能によるものです。三角形を描画する時、ラスタリゼーションステージでは最初に用意した頂点の数より多くのフラグメントが生成されています。ラスタライザはそれらのフラグメントの位置を三角形内での位置に基づいて決定します。<br/>
    459 この位置に基づき、フラグメントシェーダーの入力が<def>補完</def>されます。例えばここに一本の線があり、上端が緑で下端が青だとしましょう。この線上の下から70%の位置にあるフラグメントに対してフラグメントシェーダーが実行された場合、このフラグメントの色は緑と青の線形補完になります。もう少し詳しく言うと、<code>30%</code>の青と<code>70%</code>の緑になります。
    460   </p>
    461   
    462   <p>
    463     This is exactly what happened at the triangle. We have 3 vertices and thus 3 colors, and judging from the triangle's pixels it probably contains around 50000 fragments, where the fragment shader interpolated the colors among those pixels. If you take a good look at the colors you'll see it all makes sense: red to blue first gets to purple and then to blue. Fragment interpolation is applied to all the fragment shader's input attributes.
    464 これがまさに三角形で起こったことです。3つの頂点と3つの色があります。三角形のピクセル数から考えて、おそらくフラグメントは50000個程度存在します。このフラグメントそれぞれに対してフラグメントシェーダーが色を補完します。三角形の色をよくみると理解できるはずです。赤から青への変化は途中で紫を経由しています。フラグメント補完はフラグメントシェーダーの入力となる属性すべてに対して適応されます。
    465   </p>   
    466   
    467 <h1>Our own shader class</h1>
    468 <h1>独自のシェーダークラス</h1>
    469     <p> 
    470     Writing, compiling and managing shaders can be quite cumbersome. As a final touch on the shader subject we're going to make our life a bit easier by building a shader class that reads shaders from disk, compiles and links them, checks for errors and is easy to use. This also gives you a bit of an idea how we can encapsulate some of the knowledge we learned so far into useful abstract objects.
    471 シェーダーの作成、コンパイルそして管理は面倒です。シェーダーの章の最後として、独自のシェーダークラスを作成し、扱いやすくしましょう。このシェーダークラスはシェーダーをディスクから読み込み、コンパイル、リンクし、エラーを検出するものです。こうすることでここまで学んだ知識を抽象的なオブジェクトに詰め込み利便性を高める方法にも慣れることができます。
    472   </p>
    473   
    474   <p>
    475     We will create the shader class entirely in a header file, mainly for learning purposes and portability. Let's start by adding the required includes and by defining the class structure:
    476 学習のため、そして移植性を高めるため、シェーダークラスをひとつのヘッダーファイルに構築します。まずはじめに必要なものをインクルードしクラスの構造を定義しましょう:
    477   </p>
    478   
    479 <pre><code>
    480 #ifndef SHADER_H
    481 #define SHADER_H
    482 
    483 #include &lt;glad/glad.h&gt; // include glad to get all the required OpenGL headers
    484 #include &lt;glad/glad.h&gt; // gladをインクルードして必要なOpenGLヘッダーを取得
    485   
    486 #include &lt;string&gt;
    487 #include &lt;fstream&gt;
    488 #include &lt;sstream&gt;
    489 #include &lt;iostream&gt;
    490   
    491 
    492 class Shader
    493 {
    494 public:
    495     // the program ID
    496     // プログラムID
    497     unsigned int ID;
    498   
    499     // constructor reads and builds the shader
    500     // コンストラクタがシェーダーを読み込んでビルド
    501     Shader(const char* vertexPath, const char* fragmentPath);
    502     // use/activate the shader
    503     // シェーダーをアクティベート
    504     void use();
    505     // utility uniform functions
    506     // ユニフォーム設定用関数
    507     void setBool(const std::string &name, bool value) const;  
    508     void setInt(const std::string &name, int value) const;   
    509     void setFloat(const std::string &name, float value) const;
    510 };
    511   
    512 #endif
    513 </code></pre>
    514   
    515   <note>
    516     We used several <def>preprocessor directives</def> at the top of the header file. Using these  little lines of code informs your compiler to only include and compile this header file if it hasn't been included yet, even if multiple files include the shader header. This prevents linking conflicts.
    517 ヘッダファイルの先頭で<def>プリプロセッサ・ディレクティブ</def>を利用しています。これは複数のファイルからこのヘッダをインクルードしようとした時でも、このファイルがまだインクルードされていない場合に限りインクルードしてコンパイルするようにコンパイラに伝えるものです。これによりリンク時の競合を防げます。
    518   </note>
    519   
    520   <p>
    521     The shader class holds the ID of the shader program. Its constructor requires the file paths of the source code of the vertex and fragment shader respectively that we can store on disk as simple text files. To add a little extra we also add several utility functions to ease our lives a little: <fun>use</fun> activates the shader program, and all <fun>set...</fun> functions query a uniform location and set its value.
    522 シェーダークラスはシェーダープログラムのIDを記憶しています。頂点シェーダーとフラグメントシェーダーはディスク上にテキストファイルとして保存しておきます。シェーダークラスのコンストラクタはこれらのシェーダーのソースコードのファイルパスを必要とします。さらに利便性を高めるためいくつかの関数を追加します。シェーダープログラムをアクティベートする<fun>use</fun>関数と、ユニフォームの場所を特定してその数値を変更する<fun>set...</fun>関数です。
    523   </p>
    524   
    525 <h2>Reading from file</h2>
    526 <h2>ファイルからの読込み</h2>
    527 <p>
    528     We're using C++ filestreams to read the content from the file into several <code>string</code> objects:
    529 ファイルの内容を読み込み<code>string</code>オブジェクトに格納するため、C++のファイルストリームを利用します:
    530 </p>
    531   
    532 <pre><code>
    533 Shader(const char* vertexPath, const char* fragmentPath)
    534 {
    535     // 1. retrieve the vertex/fragment source code from filePath
    536     // 1. 頂点シェーダーとフラグメントシェーダーのソースコードをfilePathから読込み
    537     std::string vertexCode;
    538     std::string fragmentCode;
    539     std::ifstream vShaderFile;
    540     std::ifstream fShaderFile;
    541     // ensure ifstream objects can throw exceptions:
    542     // ifstreamオブジェクトがエラーを出せるかどうか確認:
    543     vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
    544     fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
    545     try 
    546     {
    547         // open files
    548 	// ファイルを開く
    549         vShaderFile.open(vertexPath);
    550         fShaderFile.open(fragmentPath);
    551         std::stringstream vShaderStream, fShaderStream;
    552         // read file's buffer contents into streams
    553 	// ファイルのバッファをストリームに読込み
    554         vShaderStream &lt;&lt; vShaderFile.rdbuf();
    555         fShaderStream &lt;&lt; fShaderFile.rdbuf();		
    556         // close file handlers
    557 	// ファイルを閉じる
    558         vShaderFile.close();
    559         fShaderFile.close();
    560         // convert stream into string
    561 	// ストリームを文字列に変換
    562         vertexCode   = vShaderStream.str();
    563         fragmentCode = fShaderStream.str();		
    564     }
    565     catch(std::ifstream::failure e)
    566     {
    567         std::cout &lt;&lt; "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" &lt;&lt; std::endl;
    568     }
    569     const char* vShaderCode = vertexCode.c_str();
    570     const char* fShaderCode = fragmentCode.c_str();
    571     [...]
    572 </code></pre> 
    573   
    574   <p>
    575     Next we need to compile and link the shaders. Note that we're also reviewing if compilation/linking failed and if so, print the compile-time errors. This is extremely useful when debugging (you are going to need those error logs eventually):
    576 続いてシェーダーをコンパイル及びリンクします。加えて、コンパイル及びリンクが成功したかどうか確認し、失敗していた場合コンパイル時のエラーを表示させています。これはデバッグ時に非常に便利です(開発においてエラーログは必須です)。
    577   </p>
    578   
    579 <pre><code>
    580 // 2. compile shaders
    581 // 2. シェーダーのコンパイル
    582 unsigned int vertex, fragment;
    583 int success;
    584 char infoLog[512];
    585    
    586 // vertex Shader
    587 // 頂点シェーダー
    588 vertex = <function id='37'>glCreateShader</function>(GL_VERTEX_SHADER);
    589 <function id='42'>glShaderSource</function>(vertex, 1, &amp;vShaderCode, NULL);
    590 <function id='38'>glCompileShader</function>(vertex);
    591 // print compile errors if any
    592 // コンパイルエラーの表示
    593 <function id='39'>glGetShaderiv</function>(vertex, GL_COMPILE_STATUS, &amp;success);
    594 if(!success)
    595 {
    596     <function id='40'>glGetShaderInfoLog</function>(vertex, 512, NULL, infoLog);
    597     std::cout &lt;&lt; "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" &lt;&lt; infoLog &lt;&lt; std::endl;
    598 };
    599   
    600 // similiar for Fragment Shader
    601 // フラグメントシェーダーに対しても同様
    602 [...]
    603   
    604 // shader Program
    605 // シェーダープログラム
    606 ID = <function id='36'>glCreateProgram</function>();
    607 <function id='34'>glAttachShader</function>(ID, vertex);
    608 <function id='34'>glAttachShader</function>(ID, fragment);
    609 <function id='35'>glLinkProgram</function>(ID);
    610 // print linking errors if any
    611 <function id='41'>glGetProgramiv</function>(ID, GL_LINK_STATUS, &amp;success);
    612 if(!success)
    613 {
    614     glGetProgramInfoLog(ID, 512, NULL, infoLog);
    615     std::cout &lt;&lt; "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" &lt;&lt; infoLog &lt;&lt; std::endl;
    616 }
    617   
    618 // delete the shaders as they're linked into our program now and no longer necessary
    619 // リンクが終わりで必要がなくなったシェーダーを削除。
    620 <function id='46'>glDeleteShader</function>(vertex);
    621 <function id='46'>glDeleteShader</function>(fragment);
    622 </code></pre> 
    623 
    624   <p>
    625     The <fun>use</fun> function is straightforward:
    626   </p>
    627   
    628 <pre><code>
    629 void use() 
    630 { 
    631     <function id='28'>glUseProgram</function>(ID);
    632 }  
    633 </code></pre>
    634     
    635 <p>
    636   Similarly for any of the uniform setter functions:
    637 </p>
    638     
    639 <pre><code>
    640 void setBool(const std::string &name, bool value) const
    641 {         
    642     <function id='44'>glUniform</function>1i(<function id='45'>glGetUniformLocation</function>(ID, name.c_str()), (int)value); 
    643 }
    644 void setInt(const std::string &name, int value) const
    645 { 
    646     <function id='44'>glUniform</function>1i(<function id='45'>glGetUniformLocation</function>(ID, name.c_str()), value); 
    647 }
    648 void setFloat(const std::string &name, float value) const
    649 { 
    650     <function id='44'>glUniform</function>1f(<function id='45'>glGetUniformLocation</function>(ID, name.c_str()), value); 
    651 } 
    652 </code></pre>
    653   
    654   <p>
    655     And there we have it, a completed <a href="/code_viewer_gh.php?code=includes/learnopengl/shader_s.h" target="_blank">shader class</a>. Using the shader class is fairly easy; we create a shader object once and from that point on simply start using it:
    656 これで<a href="/code_viewer_gh.php?code=includes/learnopengl/shader_s.h" target="_blank">シェーダークラス</a>の構築は完了です。このクラスを利用するのは簡単です。シェーダーオブジェクトを作成するだけで使えるのです:
    657   </p>
    658   
    659 <pre><code>
    660 Shader ourShader("path/to/shaders/shader.vs", "path/to/shaders/shader.fs");
    661 [...]
    662 while(...)
    663 {
    664     ourShader.use();
    665     ourShader.setFloat("someUniform", 1.0f);
    666     DrawStuff();
    667 }
    668 </code></pre>
    669     
    670 <p>
    671   Here we stored the vertex and fragment shader source code in two files called <code>shader.vs</code> and <code>shader.fs</code>. You're free to name your shader files however you like; I personally find the extensions <code>.vs</code> and <code>.fs</code> quite intuitive.
    672 それでは頂点シェーダーとフラグメントシェーダーを<code>shader.vs</code>と<code>shader.fs</code>というファイルにそれぞれ保存しましょう。ファイルの名前は好きなものにしてください。個人的に<code>.vs</code>と<code>.fs</code>という拡張子が分かりやすいのでこのような名前にしています。
    673 </p>
    674   
    675   <p>
    676     You can find the source code <a href="/code_viewer_gh.php?code=src/1.getting_started/3.3.shaders_class/shaders_class.cpp" target="_blank">here</a> using our newly created <a href="/code_viewer_gh.php?code=includes/learnopengl/shader_s.h" target="_blank">shader class</a>. Note that you can click the shader file paths to find the shaders' source code.
    677 今作った<a href="/code_viewer_gh.php?code=includes/learnopengl/shader_s.h" target="_blank">シェーダークラス</a>を利用したソースコードは<a href="/code_viewer_gh.php?code=src/1.getting_started/3.3.shaders_class/shaders_class.cpp" target="_blank">ここ</a>にあります。シェーダーファイルのパスをクリックすることでシェーダーのソースコードにもアクセスできます。
    678   </p>
    679 
    680 <h1>Exercises</h1>
    681 <h1>演習</h1>
    682   <ol>
    683     <li>Adjust the vertex shader so that the triangle is upside down: <a href="/code_viewer_gh.php?code=src/1.getting_started/3.4.shaders_exercise1/shaders_exercise1.cpp" target="_blank">solution</a>.</li>
    684     <li>頂点シェーダーを変更して三角形を逆さに表示してください: <a href="/code_viewer_gh.php?code=src/1.getting_started/3.4.shaders_exercise1/shaders_exercise1.cpp" target="_blank">回答</a></li>
    685     <li>Specify a horizontal offset via a uniform and move the triangle to the right side of the screen in the vertex shader using this offset value: <a href="/code_viewer_gh.php?code=src/1.getting_started/3.5.shaders_exercise2/shaders_exercise2.cpp" target="_blank">solution</a>.</li>
    686     <li>水平方向の位置をユニフォームで設定し、この値を頂点シェーダーから利用して三角形をスクリーンの右に寄せて下さい: <a href="/code_viewer_gh.php?code=src/1.getting_started/3.5.shaders_exercise2/shaders_exercise2.cpp" target="_blank">solution</a>.</li>
    687     <li>Output the vertex position to the fragment shader using the <code>out</code> keyword and set the fragment's color equal to this vertex position (see how even the vertex position values are interpolated across the triangle). Once you managed to do this; try to answer the following question: why is the bottom-left side of our triangle black?: <a href="/code_viewer_gh.php?code=src/1.getting_started/3.6.shaders_exercise3/shaders_exercise3.cpp" target="_blank">solution</a>.</li>
    688     <li><code>out</code>キーワードを用いて頂点の座標をフラグメントシェーダーに出力し、フラグメントの色をその座標と同じになるようにして下さい(頂点の座標であっても補完されることを確認して下さい)。これができたら、左下がどうして黒くなっているのか考えて下さい: <a href="/code_viewer_gh.php?code=src/1.getting_started/3.6.shaders_exercise3/shaders_exercise3.cpp" target="_blank">solution</a>.</li>
    689   </ol>
    690          
    691 
    692     </div>
    693 </body>
    694 </html>