Advertisement
Guest User

Bump GLSL problem

a guest
Oct 31st, 2011
686
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. [Vertex Shader]
  2.  
  3.  
  4. varying vec3 lightVec;
  5. varying vec3 eyeVec;
  6. varying vec2 texCoord;
  7. attribute vec3 vTangent;
  8.  
  9.  
  10. void main(void)
  11. {
  12. gl_Position = ftransform();
  13. texCoord = gl_MultiTexCoord0.xy;
  14.  
  15. vec3 n = normalize(gl_NormalMatrix * gl_Normal);
  16. vec3 t = normalize(gl_NormalMatrix * vTangent);
  17. vec3 b = cross(n, t);
  18.  
  19. vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
  20. vec3 tmpVec = gl_LightSource[0].position.xyz - vVertex;
  21.  
  22. lightVec.x = dot(tmpVec, t);
  23. lightVec.y = dot(tmpVec, b);
  24. lightVec.z = dot(tmpVec, n);
  25.  
  26. tmpVec = -vVertex;
  27. eyeVec.x = dot(tmpVec, t);
  28. eyeVec.y = dot(tmpVec, b);
  29. eyeVec.z = dot(tmpVec, n);
  30. }
  31.  
  32.  
  33. [Pixel Shader]
  34.  
  35.  
  36.  
  37.  
  38. varying vec3 lightVec;
  39. varying vec3 eyeVec;
  40. varying vec2 texCoord;
  41. uniform sampler2D colorMap;
  42. uniform sampler2D normalMap;
  43. uniform float invRadius;
  44.  
  45. void main (void)
  46. {
  47. float distSqr = dot(lightVec, lightVec);
  48. float att = clamp(1.0 - invRadius * sqrt(distSqr), 0.0, 1.0);
  49. vec3 lVec = lightVec * inversesqrt(distSqr);
  50.  
  51. vec3 vVec = normalize(eyeVec);
  52.  
  53. vec4 base = texture2D(colorMap, texCoord);
  54.  
  55. vec3 bump = normalize( texture2D(normalMap, texCoord).xyz * 2.0 - 1.0);
  56.  
  57. vec4 vAmbient = gl_LightSource[0].ambient * gl_FrontMaterial.ambient;
  58.  
  59. float diffuse = max( dot(lVec, bump), 0.0 );
  60.  
  61. vec4 vDiffuse = gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse *
  62. diffuse;
  63.  
  64. float specular = pow(clamp(dot(reflect(-lVec, bump), vVec), 0.0, 1.0),
  65. gl_FrontMaterial.shininess );
  66.  
  67. vec4 vSpecular = gl_LightSource[0].specular * gl_FrontMaterial.specular *
  68. specular;
  69.  
  70. gl_FragColor = ( vAmbient*base +
  71. vDiffuse*base +
  72. vSpecular) * att;
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80. [OpenGL code (JOGL)]
  81.  
  82. public void drawModel(GL2 gl)
  83. {
  84.  
  85. ConstructInterleavedArray(gl);
  86.  
  87. gl.glEnable(GL2.GL_CULL_FACE);
  88. gl.glCullFace(GL2.GL_BACK);
  89. gl.glPolygonMode(GL2.GL_FRONT, GL2.GL_TRIANGLES);
  90.  
  91. gl.glActiveTexture(GL2.GL_TEXTURE0);
  92. texture.enable(gl);
  93. texture.bind(gl);
  94. gl.glUniform1i(texture.getTextureObject(gl),0);
  95.  
  96. gl.glActiveTexture(GL2.GL_TEXTURE1);
  97. normalMap.enable(gl);
  98. normalMap.bind(gl);
  99. gl.glUniform1i(normalMap.getTextureObject(gl),0);
  100.  
  101. gl.glDrawArrays(FaceFormat, 0, PolyCount * FaceMultiplier);
  102.  
  103. gl.glDisable(GL2.GL_CULL_FACE);
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement