纹理在OpenGL 3.3与GLSL中显示为黑色

Textures appear black in OpenGL 3.3 with GLSL

本文关键字:显示 黑色 OpenGL 纹理 GLSL      更新时间:2023-10-16

我有一个在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错误,特别是你的着色器的编译和链接状态(和信息日志)。