C++ OpenGL纹理在3.3中都是黑色的,但在3.1中可以使用

C++ OpenGL纹理在3.3中都是黑色的,但在3.1中可以使用,c++,opengl,glsl,C++,Opengl,Glsl,我目前正在OpenGL3.3中处理一个简单的3D场景,但是当我尝试对对象进行纹理处理时,所有对象的纹理都是完全黑色的。但是,如果我将上下文版本更改为3.1;在模型上正确渲染纹理没有问题 我不确定这是否表明我使用了不推荐的功能/方法,但我很难看出问题出在哪里 设置纹理 (load texture from file) ... glGenTextures(1, &TexID); // Create The Texture ( CHANGE ) glBindText

我目前正在OpenGL3.3中处理一个简单的3D场景,但是当我尝试对对象进行纹理处理时,所有对象的纹理都是完全黑色的。但是,如果我将上下文版本更改为3.1;在模型上正确渲染纹理没有问题

我不确定这是否表明我使用了不推荐的功能/方法,但我很难看出问题出在哪里

设置纹理

(load texture from file)
...
glGenTextures(1, &TexID);               // Create The Texture ( CHANGE )
glBindTexture(GL_TEXTURE_2D, TexID);

glTexImage2D(GL_TEXTURE_2D, 0, texture_bpp / 8, texture_width, texture_height, 0, texture_type, GL_UNSIGNED_BYTE, texture_imageData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
...
绑定要渲染的纹理

// mLocation is the layout location in the shader, from glGetUniformLocation
// mTextureUnit is the specified texture unit to load into. Currently using 0.
// mTextureID is the ID of the loaded texture, as generated above.
glActiveTexture( GL_TEXTURE0 + mData.mTextureUnit );
glBindTexture( GL_TEXTURE_2D, mData.mTextureID );
glUniform1i( mLocation, mData.mTextureUnit );
uniform sampler2D diffusemap;
in vec2 passUV;
out vec3 outColour;
...
outColour = texture( diffusemap, passUV ).rgb;
#version 330 core

uniform mat4 p;
uniform mat4 v;
uniform mat4 m;

in vec3 vertex;
in vec3 normal;
in vec2 uv;

out vec3 passVertex;
out vec3 passNormal;
out vec2 passUV;

void main( void )
{
    gl_Position = p * v * m * vec4( vertex, 1.0 );

    passVertex = vec3( m * vec4( vertex, 1.0 ) );
    passNormal = vec3( m * vec4( normal, 1.0 ) );
    passUV = uv;
}
片段着色器

// mLocation is the layout location in the shader, from glGetUniformLocation
// mTextureUnit is the specified texture unit to load into. Currently using 0.
// mTextureID is the ID of the loaded texture, as generated above.
glActiveTexture( GL_TEXTURE0 + mData.mTextureUnit );
glBindTexture( GL_TEXTURE_2D, mData.mTextureID );
glUniform1i( mLocation, mData.mTextureUnit );
uniform sampler2D diffusemap;
in vec2 passUV;
out vec3 outColour;
...
outColour = texture( diffusemap, passUV ).rgb;
#version 330 core

uniform mat4 p;
uniform mat4 v;
uniform mat4 m;

in vec3 vertex;
in vec3 normal;
in vec2 uv;

out vec3 passVertex;
out vec3 passNormal;
out vec2 passUV;

void main( void )
{
    gl_Position = p * v * m * vec4( vertex, 1.0 );

    passVertex = vec3( m * vec4( vertex, 1.0 ) );
    passNormal = vec3( m * vec4( normal, 1.0 ) );
    passUV = uv;
}
所有使用的纹理都是2平方的幂


显示问题的图像

GL3.1:

GL3.3:


顶点着色器

// mLocation is the layout location in the shader, from glGetUniformLocation
// mTextureUnit is the specified texture unit to load into. Currently using 0.
// mTextureID is the ID of the loaded texture, as generated above.
glActiveTexture( GL_TEXTURE0 + mData.mTextureUnit );
glBindTexture( GL_TEXTURE_2D, mData.mTextureID );
glUniform1i( mLocation, mData.mTextureUnit );
uniform sampler2D diffusemap;
in vec2 passUV;
out vec3 outColour;
...
outColour = texture( diffusemap, passUV ).rgb;
#version 330 core

uniform mat4 p;
uniform mat4 v;
uniform mat4 m;

in vec3 vertex;
in vec3 normal;
in vec2 uv;

out vec3 passVertex;
out vec3 passNormal;
out vec2 passUV;

void main( void )
{
    gl_Position = p * v * m * vec4( vertex, 1.0 );

    passVertex = vec3( m * vec4( vertex, 1.0 ) );
    passNormal = vec3( m * vec4( normal, 1.0 ) );
    passUV = uv;
}
行中:

glTexImage2D(GL_TEXTURE_2D, 0, texture_bpp / 8, texture_width, texture_height, 0, texture_type, GL_UNSIGNED_BYTE, texture_imageData);
假设(texture_bpp/8)将返回正确的格式类型是不正确的。它应该是指定内部格式(如GL_RGBA)的GLenum值之一

将其更正为(或与纹理文件的内部格式匹配的任何格式)将完全修复该问题,并可在GL3.3和GL3.1上使用:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_width, texture_height, 0, texture_type, GL_UNSIGNED_BYTE, texture_imageData);

为了完整性起见,应该是枚举器。和一个大小的枚举数,而不是一个未大小的枚举数。当您可以使用
GL\u RGB8
时,请停止使用
GL\u RGB


答案正确地指出了问题所在,但解释一下为什么之前的假设适用于3.1而不适用于3.3会有帮助

在OpenGL 3.0中不推荐使用[1,4]范围内的数字,并在OpenGL 3.1中删除了该功能。然而,当时没有办法说,“给我OpenGL 3.1版的实际核心概要文件”;
WGL/GLX\u CONTEXT\u CORE\u PROFILE\u BIT\u ARB
s不存在。因此,当您获得3.1上下文时,实现导出是完全合法的,它仍然允许所有删除的功能


在3.2中,OpenGL中添加了。在这一点上,您不会让某人在核心概要文件中公开ARB\U兼容性。这就是为什么当您要求3.1时,代码可以工作(因为它可以免费为您提供3.1兼容性),但当您要求3.3核心配置文件时,代码就不能工作了。

顶点着色器看起来像什么?在底部为您添加了它。但就纹理而言,它只是将uv数据传递给片段着色器,而不进行任何转换。只是为了确保UV正确通过/不是问题所在-我已将UV快速渲染为颜色(红色、绿色)。这似乎表明UV数据进入frag着色器时没有问题:答案正确地识别了问题,但解释一下之前的假设为什么适用于3.1而不适用于3.3会很有帮助。可能3.1内部使用默认格式,3.3在这方面更严格。无论如何,答案不错。