我目前正在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);
...
Run Code Online (Sandbox Code Playgroud)
将纹理绑定到渲染
// 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 );
Run Code Online (Sandbox Code Playgroud)
片段着色器
uniform sampler2D diffusemap;
in vec2 passUV;
out vec3 outColour;
...
outColour = texture( diffusemap, passUV ).rgb;
Run Code Online (Sandbox Code Playgroud)
使用的所有纹理都是2的方形尺寸.
显示问题的图像.
GL3.1:http://i.imgur.com/NUgj6vA.png
GL3.3:http://i.imgur.com/oOc0jcd.png
顶点着色器
#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;
}
Run Code Online (Sandbox Code Playgroud)
在线:
glTexImage2D(GL_TEXTURE_2D, 0, texture_bpp / 8, texture_width, texture_height, 0, texture_type, GL_UNSIGNED_BYTE, texture_imageData);
Run Code Online (Sandbox Code Playgroud)
假设(texture_bpp/8)将返回正确的格式类型是不正确的.它应该是指定内部格式的GLenum值之一,例如GL_RGBA.
将其更正为(或与纹理文件的内部格式匹配的格式)完全解决了问题,并且适用于GL3.3和GL3.1:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texture_width, texture_height, 0, texture_type, GL_UNSIGNED_BYTE, texture_imageData);
Run Code Online (Sandbox Code Playgroud)