c++ - OpenGL 3.2 纹理问题

标签 c++ opengl glsl opengl-3 texturing

我目前正在使用 C++ 开发 2D 引擎。

我遇到了一个问题,我似乎相信我以前遇到过一次,但后来忘记了我是如何解决它的。

该引擎是跨平台的(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 );

-渲染代码

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 );

-垂直着色器

#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 );
}

-片段着色器

#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;
} 

最佳答案

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

这意味着位置和纹理坐标都来自缓冲区中的相同位置。它们重叠。

您似乎希望它们来自不同的缓冲区对象。这意味着您必须在第一次和第二次 glVertexAttribPointer 调用之间将新缓冲区绑定(bind)到 GL_ARRAY_BUFFER

关于c++ - OpenGL 3.2 纹理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658560/

相关文章:

c++ - 捕获未分配的 r 值的编译器选项

c++ - 平面拟合图像处理算法的 super 神秘逻辑错误

c++ - C++模板中的百分号(%)和尖锐符号(#)是什么意思

python - 对象在 AR (Aruco + OpenGL) 中错位

c++ - 如何使用 C++ 软件实际交付 GLSL 着色器

c++ - 如何从父类调用子方法

python - PyOpenGL: glutTimerFunc 回调缺少必需的参数 'value'

opengl - OpenGL 'Bind' 函数背后的概念

android - texture2D().r 和 texture2D().a 是什么意思?

c++ - 如何将一组统一缓冲区对象加载到着色器中?