シェーダーを介してカラー コンポーネントを使用してプリミティブをレンダリングし、それらを変換することに成功しました。ただし、テクスチャをロードしてシェーダーを介してプリミティブ用にレンダリングしようとすると、プリミティブに不具合が生じ、正方形になるはずです。
ご覧のとおり、カラー コンポーネントを含むテクスチャがシーン内の 1 つのプリミティブに正常に読み込まれ、適用されています。
次にカラー コンポーネントを削除すると、再びプリミティブが作成されますが、奇妙なことに、それらは UV を変更することによってスケーリングされます。これは当てはまりません。UV のみがスケーリングされます。(また、それらの原点はオフセットされています)
私のシェーダー初期化コード:
void renderer::initRendererGfx()
{
shader->compilerShaders();
shader->loadAttribute(@"Position");
shader->loadAttribute(@"SourceColor");
shader->loadAttribute(@"TexCoordIn");
}
これが私のオブジェクトハンドラーレンダリング関数コードです:
void renderer::drawRender(glm::mat4 &view, glm::mat4 &projection)
{
//Loop through all objects of base type OBJECT
for(int i=0;i<SceneObjects.size();i++){
if(SceneObjects.size()>0){
shader->bind();//Bind the shader for the rendering of this object
SceneObjects[i]->mv = view * SceneObjects[i]->model;
shader->setUniform(@"modelViewMatrix", SceneObjects[i]->mv);//Calculate object model view
shader->setUniform(@"MVP", projection * SceneObjects[i]->mv);//apply projection transforms to object
glActiveTexture(GL_TEXTURE0); // unneccc in practice
glBindTexture(GL_TEXTURE_2D, SceneObjects[i]->_texture);
shader->setUniform(@"Texture", 0);//Apply the uniform for this instance
SceneObjects[i]->draw();//Draw this object
shader->unbind();//Release the shader for the next object
}
}
}
これが私のスプライトバッファの初期化と描画コードです:
void spriteObject::draw()
{
glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, sizeof(SpriteVertex), NULL);
glVertexAttribPointer((GLuint)1, 4, GL_FLOAT, GL_FALSE, sizeof(SpriteVertex) , (GLvoid*) (sizeof(GL_FLOAT) * 3));
glVertexAttribPointer((GLuint)2, 2, GL_FLOAT, GL_FALSE, sizeof(SpriteVertex) , (GLvoid*)(sizeof(GL_FLOAT) * 7));
glDrawElements(GL_TRIANGLE_STRIP, sizeof(SpriteIndices)/sizeof(SpriteIndices[0]), GL_UNSIGNED_BYTE, 0);
}
void spriteObject::initBuffers()
{
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(SpriteVertices), SpriteVertices, GL_STATIC_DRAW);
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(SpriteIndices), SpriteIndices, GL_STATIC_DRAW);
}
頂点シェーダーは次のとおりです。
attribute vec3 Position;
attribute vec4 SourceColor;
varying vec4 DestinationColor;
uniform mat4 projectionMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 MVP;
attribute vec2 TexCoordIn;
varying vec2 TexCoordOut;
void main(void) {
DestinationColor = SourceColor;
gl_Position = MVP * vec4(Position,1.0);
TexCoordOut = TexCoordIn;
}
そして最後にフラグメント シェーダー:
varying lowp vec4 DestinationColor;
varying lowp vec2 TexCoordOut;
uniform sampler2D Texture;
void main(void) {
gl_FragColor = DestinationColor * texture2D(Texture, TexCoordOut);
}
特定の要素の詳細を知りたい場合は、質問してください。
どうもありがとう。