Hello Guest

[LWJGL 3] Binding textures to shader, black texture

  • 2 Replies
  • 2541 Views
[LWJGL 3] Binding textures to shader, black texture
« on: December 29, 2014, 07:58:11 »
I have a problem with binding the textures to the shader.

Not sure what is wrong with it, I have just ported the code from JOGL.
The textures are uploaded (I have successfully got them back).

Code: [Select]
int textureLocation = GL20.glGetUniformLocation(this.program.getProgramID(), "diffuseTexture");
int normalLocation = GL20.glGetUniformLocation(this.program.getProgramID(), "normalTexture");

if (texture.diffuse != null) {
GL20.glUniform1i(textureLocation, 0);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
texture.diffuse.bind();
GL11.glEnable(GL11.GL_TEXTURE_2D);
}

if (texture.normal != null) {
GL20.glUniform1i(normalLocation, 1);
GL13.glActiveTexture(GL13.GL_TEXTURE1);
texture.normal.bind();
GL11.glEnable(GL11.GL_TEXTURE_2D);
}

Vertex Shader
Code: [Select]
varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;
varying vec3 normal;
attribute vec3 tangent;

void main(void)
{
gl_Position = ftransform();
texCoord = gl_MultiTexCoord0.xy;
normal = gl_Normal.xyz;

vec3 n = normalize(gl_NormalMatrix * gl_Normal);
vec3 t = normalize(gl_NormalMatrix * cross(tangent,gl_Normal));
vec3 b = cross(n, t);

vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;

lightVec.x = dot(tmpVec, t);
lightVec.y = dot(tmpVec, b);
lightVec.z = dot(tmpVec, n);

tmpVec = -vVertex;
eyeVec.x = dot(tmpVec, t);
eyeVec.y = dot(tmpVec, b);
eyeVec.z = dot(tmpVec, n);
}

Fragment Shader
Code: [Select]
varying vec3 lightVec;
varying vec3 eyeVec;
varying vec2 texCoord;
varying vec3 normal;
uniform sampler2D diffuseTexture;
uniform sampler2D normalTexture;
uniform float invRadius;

void main (void)
{
float distSqr = dot(lightVec, lightVec);
float att = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);
vec3 lVec = lightVec * inversesqrt(distSqr);

vec3 vVec = normalize(eyeVec);

vec4 base = texture2D(diffuseTexture, texCoord);

//vec4 base = vec4(1, 1, 1, 1);

vec3 bump = normalize( texture2D(normalTexture, texCoord).xyz * 2.0 - 1.0);

//vec3 bump = vec3( 0.5, 0.5, 0.5);

vec4 vAmbient = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;

float diffuse = max( dot(lVec, bump), 0.0 );

vec4 vDiffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse *
diffuse;

float specular = pow(clamp(dot(reflect(-lVec, bump), vVec), 0.0, 1.0),
                 gl_FrontMaterial.shininess );

vec4 vSpecular = clamp(gl_LightSource[0].specular * gl_FrontMaterial.specular *
specular, 0.0, 1.0);


//gl_FragColor = vec4((normal.rbg+1)*0.5,1.0);
//gl_FragColor = (vAmbient*base + vDiffuse*base) * att;
//gl_FragColor = (vAmbient*base + vDiffuse*base) * att;
//gl_FragColor = vec4((bump.x+1)*0.5,(bump.y+1)*0.5,(bump.z+1)*0.5,1);
//gl_FragColor = vec4((normal+1)*0.5,1.0);


gl_FragColor = ( vAmbient*base + vDiffuse*base + vSpecular) * att;
gl_FragColor.a = base.a;
}

*

Offline Cornix

  • *****
  • 488
Re: [LWJGL 3] Binding textures to shader, black texture
« Reply #1 on: December 29, 2014, 09:15:37 »
Try it with a simpler shader. At the moment there are too many other variables that might mess things up.

Re: [LWJGL 3] Binding textures to shader, black texture
« Reply #2 on: December 29, 2014, 22:05:31 »
Thanks, there were actually few problems, the lightning wasn't initialized properly, I had to change the order of calls and fix the setup function.
Reducing the complexity of the shader helped me to find them.