3

OpenGL 1.1 コードを OpenGL 2.0 に変換したい。

私は Cocos2d v2.0 (2D ゲームを構築するためのフレームワーク) を使用しています。

4

1 に答える 1

2

まず、シェーダーをプログラムに取り込まなければなりません。以下に示すように、シェーダー コードを直接 const char * にコピーするか、実行時にファイルからシェーダーを読み込むことができます。これは、開発しているプラ​​ットフォームによって異なるため、その方法は示しません。

const char *vertexShader = "... Vertex Shader source ...";
const char *fragmentShader = "... Fragment Shader source ...";

頂点シェーダーとフラグメント シェーダーの両方のシェーダーを作成し、それらの ID を保存します。

GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

これは、const char * にコピーして貼り付けるか、ファイルからロードしたデータで、作成したばかりのシェーダーを埋める場所です。

glShaderSource(vertexShader, 1, &vertexShader, NULL);
glShaderSource(fragmentShader, 1, &fragmentShader, NULL);

次に、シェーダーをコンパイルするようにプログラムに指示します。

glCompileShader(vertexShader);
glCompileShader(fragmentShader);

OpenGL からシェーダーへのインターフェイスとなるシェーダー プログラムを作成します。

shaderProgram = glCreateProgram();

シェーダー プログラムにシェーダーをアタッチします。

glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);

シェーダー プログラムをプログラムにリンクします。

glLinkProgram(shaderProgram);

作成したシェーダー プログラムを使用することを OpenGL に伝えます。

glUseProgram(shaderProgram);

頂点シェーダー:

attribute vec4 a_Position; // Attribute is data send with each vertex. Position 
attribute vec2 a_TexCoord; // and texture coordinates is the example here

uniform mat4 u_ModelViewProjMatrix; // This is sent from within your program

varying mediump vec2 v_TexCoord; // varying means it is passed to next shader

void main()
{
    // Multiply the model view projection matrix with the vertex position
    gl_Position = u_ModelViewProjMatrix* a_Position;
    v_TexCoord = a_TexCoord; // Send texture coordinate data to fragment shader.
}

フラグメント シェーダー:

precision mediump float; // Mandatory OpenGL ES float precision

varying vec2 v_TexCoord; // Sent in from Vertex Shader

uniform sampler2D u_Texture; // The texture to use sent from within your program.

void main()
{
    // The pixel colors are set to the texture according to texture coordinates.
    gl_FragColor =  texture2D(u_Texture, v_TexCoord);
}
于 2012-04-04T07:42:26.733 に答える