I have a problem with displaying textures with GLSL in OpenGL 3.3 (Core profile). I have triple-checked everything and still can't find mistake. I'm using SDL for window handling and for texture loading.
This is my texture loading function
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);
Passing to shaders
glActiveTexture(GL_TEXTURE0);
glUniform1i( glGetUniformLocation(shader, "texture_diffuse"), 0 );
glBindTexture(GL_TEXTURE_2D, texture_diffuse);
Vertex Shader
#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;
}
Fragment Shader
#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;
}
Also everything except textures works ( texture_coordinate etc. )
Does anyone see any possible error?