纹理在OpenGL 3.3与GLSL中显示为黑色
Textures appear black in OpenGL 3.3 with GLSL
我有一个在OpenGL 3.3(核心配置文件)中显示GLSL纹理的问题。我把每件事都检查了三遍,还是找不到错误。我使用SDL进行窗口处理和纹理加载。
这是纹理加载函数
glEnable (GL_TEXTURE_2D);
glGenTextures(1, &generated_texture);
glBindTexture(GL_TEXTURE_2D, generated_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA, image->w, image->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, image->pixels);
传递给着色器
glActiveTexture(GL_TEXTURE0);
glUniform1i( glGetUniformLocation(shader, "texture_diffuse"), 0 );
glBindTexture(GL_TEXTURE_2D, texture_diffuse);
顶点着色器
#version 330 core
struct Light
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 position;
};
struct Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 emission;
float shininess;
float reflectivity;
float ior;
float opacity;
};
layout(location = 0) in vec4 VertexPosition;
layout(location = 1) in vec3 VertexNormal;
layout(location = 2) in vec2 VertexTexture;
uniform mat4 PMatrix; //Camera projection matrix
uniform mat4 VMatrix; //Camera view matrix
uniform mat3 NMatrix; //MVMatrix ... -> converted into normal matrix (inverse transpose operation)
uniform mat4 MVPMatrix;
uniform Light light;
uniform Material material;
uniform sampler2D texture_diffuse;
//The prefix ec means Eye Coordinates in the Eye Coordinate System
out vec4 ecPosition;
out vec3 ecLightDir;
out vec3 ecNormal;
out vec3 ecViewDir;
out vec2 texture_coordinate;
void main()
{
ecPosition = VMatrix * VertexPosition;
ecLightDir = vec3(VMatrix * light.position - ecPosition);
ecNormal = NMatrix * VertexNormal;
ecViewDir = -vec3(ecPosition);
texture_coordinate = VertexTexture;
gl_Position = PMatrix * ecPosition;
}
片段着色器
#version 330 core
in vec3 ecNormal;
in vec4 ecPosition;
in vec3 ecLightDir;
in vec3 ecViewDir;
in vec2 texture_coordinate;
out vec4 FragColor;
struct Light
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec4 position;
};
struct Material
{
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 emission;
float shininess;
float reflectivity;
float ior;
float opacity;
};
uniform Light light;
uniform Material material;
uniform sampler2D texture_diffuse;
void main()
{
vec3 N = normalize(ecNormal);
vec3 L = normalize(ecLightDir);
vec3 V = normalize(ecViewDir);
float lambert = dot(N,L);
vec4 _tex = texture2D( texture_diffuse, texture_coordinate );
FragColor = _tex;
}
也一切除了纹理工作(texture_coordinate等)
有人看到任何可能的错误吗?
我可以看到以下几点:
glEnable (GL_TEXTURE_2D);
这只会产生一个错误,因为这个启用在GL核心配置文件中无效。
vec4 _tex = texture2D( texture_diffuse, texture_coordinate );
这将无法编译,因为函数texture2D
在GLSL3.30中不可用。它被称为texture
,并将从您调用它的采样器变量中派生纹理类型。
你应该检查GL错误,特别是你的着色器的编译和链接状态(和信息日志)。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 纹理显示为黑色
- openCV中的Absdiff可以编译,但显示黑色图像
- 带有Clang的DirectX11显示黑色纹理(与Visual Studio配合使用)
- EVR在播放前固定后在窗口上显示几个黑色框架
- 从libpng加载的png纹理在OpenGL中显示为黑色
- 在opencv中使用imwrite保存图像时,会将图像全部写入黑色,但imshow会正确显示
- OpenGL3两组着色器,纹理显示为黑色
- 带有 GDI 的黑色突出显示条
- OpenGL位图程序只显示白色,黑色和黄色
- cocos2d-x ResolutionPolicy::SHOW_ALL显示漂亮的图像,而不是黑色的间隙
- SDL_LoadBMP()在引用表面时显示黑色表面
- 使用NDK从本机内存恢复图像会返回无显示的黑色图像
- 纹理在OpenGL 3.3与GLSL中显示为黑色
- Cimg显示呈现黑色