2013-02-07 45 views
0

我已经成功地通过着色器渲染具有颜色分量的原语,并且翻译它们。然而,在尝试通过着色器来加载纹理和渲染它的原始,原语毛刺,他们应该是正方形:OpenGL ES 2.0纹理加载视觉故障

enter image description here

正如你所看到的,它成功地加载,并与应用纹理颜色分量添加到场景中的单个基元。

如果我然后删除颜色分量,我又有原语,但奇怪的是,他们通过更改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

你确定你的三角形有相同的绕组吗?绕组是三角点列出的顺序(顺时针或逆时针)。绕组用于面部剔除以确定三角形是朝向还是朝后。

您可以通过禁用脸部剔除来轻松检查您的三角是否被错误地缠绕。

glDisable(GL_CULL_FACE); 

更多的信息在这里(http://db-in.com/blog/2011/02/all-about-opengl-es-2-x-part-23/#face_culling

+0

我做glDisable(GL_CULL_FACE),什么都没有发生。但glEnable(GL_CULL_FACE),我又得到一个模糊的故障 – GenericController

+0

我也发现,如果我改变了我的顶点结构中的颜色成分,它改变了原始几何。他们可能混合起来了吗? – GenericController

+0

你可以添加SpriteVertices定义吗? – crazyjul