我已成功通过着色器渲染带有颜色分量的图元并进行翻译。但是,在尝试加载纹理并通过着色器为基元渲染时,基元会出现故障,它们应该是正方形:
如您所见,它成功加载并将带有颜色分量的纹理应用于场景中的单个基元。
如果我然后移除颜色分量,我再次有原始,但奇怪的是,它们通过改变uvs来缩放 - 这不应该是这种情况,只有uvs应该缩放! (也是他们的起源被抵消)
我的着色器初始化代码:
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);
}
如果您想查看某些元素的更多细节,请询问。
非常感谢。
答案 0 :(得分:0)
你确定你的三角形有相同的绕组吗?绕组是列出三角形点的顺序(顺时针或逆时针)。绕组用于面部剔除,以确定三角形是面向还是背面。
您可以通过禁用面部剔除来轻松检查三角形是否错误缠绕。
glDisable( GL_CULL_FACE );
此处提供更多信息(http://db-in.com/blog/2011/02/all-about-opengl-es-2-x-part-23/#face_culling)