2

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?

genpfault
  • 51,148
  • 11
  • 85
  • 139
DFDark
  • 219
  • 1
  • 2
  • 14
  • Looks reasonable at a glance. I'd run it through something like AMD CodeXL and see what's getting loaded to the gpu. – Nathan Monteleone Mar 26 '14 at 20:27
  • make sure `generated_texture` is the same as `texture_diffuse` and check for errors with `glGetError` – a.lasram Mar 26 '14 at 20:31
  • I checked that texture_diffuse is the same as generated_texture, as for errors it's "invalid enumerator" for the first execution of render cycle(number 1283), but "no error" for the rest. ( The rest have also same number of texture id ) – DFDark Mar 26 '14 at 22:51

1 Answers1

2

There are a few things I can see:

glEnable (GL_TEXTURE_2D);

This will just generate an error, as this enable is not valid in GL core profiles.

vec4 _tex   = texture2D( texture_diffuse, texture_coordinate );

This will not compile, as the function texture2D is not available in GLSL3.30. It is called just texture and will derive the texture type from the sampler variable you call it with.

You should check for GL errors and especially the compile and link states of you shaders (and the info logs).

derhass
  • 43,833
  • 2
  • 57
  • 78
  • Thanks for the insight, but it doesn't seem to make any difference. glGetError Returns "invalid enumerator" for the first execution of render cycle(number 1283), but "no error" for the rest. And changing texture2D in shader for texture does nothing – DFDark Mar 26 '14 at 22:18
  • @DFDark errors are reseted only when read. Knowing exactly which function is generating an error (in your init funct) is the first step. call glGetError after each glCall. – j-p Mar 27 '14 at 05:33