我在使用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等)
有没有人看到任何可能的错误?
答案 0 :(得分:2)
我可以看到一些事情:
glEnable (GL_TEXTURE_2D);
这只会产生错误,因为此启用在GL核心配置文件中无效。
vec4 _tex = texture2D( texture_diffuse, texture_coordinate );
这不会编译,因为函数texture2D
在GLSL3.30中不可用。它只被称为texture
,它将从您调用它的sampler变量中派生出纹理类型。
您应检查GL错误,尤其是着色器的编译和链接状态(以及信息日志)。