2012-07-25 117 views
0

我工作在C++此刻2D引擎。的OpenGL 3.2紋理問題

我遇到了一個我似乎相信我曾遇到過的問題,但之後卻忘記了我是如何修復它的。

該引擎是跨平臺(Win,OSX,Linux),爲了實現這一點,我使用GLFW作爲基礎。

在做正常的事情紋理我結束了這一點:

http://tinypic.com/r/263ec6o/6

正如你所看到的紋理是不正確(如圖像應該是我的一個簡單的圖像)。

使用gedebugger我可以確認圖像被緩存到GPU是正確的,但它有一些如何在圖像中看到結尾。

我會在下面介紹一些相關代碼,但是如果您希望獲得更多信息,請隨時提問。

- 緩衝代碼

glGenVertexArrays(1, &_vao); 
// Generate Buffers and so on. 
glBindVertexArray(_vao); 

glGenBuffers(1, &_vboVertices); 
glBindBuffer(GL_ARRAY_BUFFER, _vboVertices); 
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * _numVertices, _vertices, GL_STATIC_DRAW); 

glGenBuffers(1, &_vboTexCoords); 
glBindBuffer(GL_ARRAY_BUFFER, _vboTexCoords); 
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 2 * _numVertices, _texCoords, GL_STATIC_DRAW); 

glGenBuffers(1, &_vboIndices); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndices); 
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * _numIndices, _indices, GL_STATIC_DRAW); 

-Render代碼

glm::mat4 modelViewProjMatrix = projMatrix * viewMatrix * modelMatrix; 

ShaderResource * shader = ShaderManager::GetInstance()->GetShader(_shaderName.c_str()); 
TextureResource * texture = TextureManager::GetInstance()->GetTexture(_textureName.c_str()); 

shader->BindShader(); 

// Bind VAO 
glBindVertexArray(_vao); 

// Bind all VBO's 
glBindBuffer(GL_ARRAY_BUFFER, _vboVertices); 
GLint posLocation = shader->GetAttribLocation("in_position"); 
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); 
GLint texCoordLocation = shader->GetAttribLocation("in_texCoord"); 
glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); 
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vboIndices); 

// Enable VBO Pointers 
glEnableVertexAttribArray(posLocation); 
glEnableVertexAttribArray(texCoordLocation); 

// Find and assign Uniforms 
GLint MVPMatrixLocation = shader->GetUniformLocation("in_MVPMatrix"); 
GLint colourLocation = shader->GetUniformLocation("in_colour"); 
GLint textureLocation = shader->GetUniformLocation("texMap"); 

glUniformMatrix4fv(MVPMatrixLocation, 1, GL_FALSE, glm::value_ptr(modelViewProjMatrix)); 
glUniform4fv(colourLocation, 1, glm::value_ptr(_colour)); 

// Texture Uniform 
if(texture != 0) 
{ 
    glActiveTexture(GL_TEXTURE0); 
    glUniform1i(textureLocation, 0); 
    glBindTexture(GL_TEXTURE_2D, texture->GetTextureId()); 
} 

glDrawElements(GL_TRIANGLES, _numIndices, GL_UNSIGNED_INT, (void*) 0); 

glDisableVertexAttribArray(posLocation); 
glDisableVertexAttribArray(texCoordLocation); 

shader->UnbindShader(); 

glBindVertexArray(0); 

-Vert着色

#version 150 

in vec3 in_position; 
in vec2 in_texCoord; 

out vec4 out_colour; 
smooth out vec2 out_texCoord; 

uniform vec4 in_colour; 
uniform mat4 in_MVPMatrix; 

void main() 
{ 
out_colour = in_colour; 
out_texCoord = in_texCoord; 

    gl_Position = in_MVPMatrix * vec4(in_position, 1.0); 
} 

-Frag着色

#version 150 

in vec4 out_colour; 
smooth in vec2 out_texCoord; 

out vec4 fragColor; 

uniform sampler2D texMap; 

void main() 
{ 
    vec4 diffuseTexel = texture2D(texMap, out_texCoord); 

    fragColor = out_colour * diffuseTexel; 
} 

回答

3
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); 
glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); 

這意味着,無論是位置和紋理座標來自於緩衝區中的相同位置。它們重疊。

你似乎希望他們來自不同的緩衝區對象。這意味着你有一個新的緩衝區綁定到你的第一個和第二glVertexAttribPointer調用之間GL_ARRAY_BUFFER

+0

謝謝你的答案!!,這兩個都是對的,雖然我接受了第一個,但對我來說這是一個完全愚蠢的疏忽:/壓抑我花了幾個小時試圖解決這個問題......我需要睡覺。 – OpticFroggy 2012-07-25 21:30:41

3
// Bind all VBO's 
glBindBuffer(GL_ARRAY_BUFFER, _vboVertices); 
GLint posLocation = shader->GetAttribLocation("in_position"); 
glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); 
GLint texCoordLocation = shader->GetAttribLocation("in_texCoord"); 
glVertexAttribPointer(texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); 

texcoords的VertexAttribPointer應在綁定_vboTexCoord後設置,而不是_vboVertices。