Advertisement
Guest User

test.vertext.glsl

a guest
Jul 11th, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.34 KB | None | 0 0
  1. #define positionFlag
  2. #define normalFlag
  3. #define lightingFlag
  4. #define ambientCubemapFlag
  5. #define numDirectionalLights 2
  6. #define numPointLights 5
  7. #define environmentCubemapFlag
  8. #define texCoord0Flag
  9. #define diffuseTextureFlag
  10. #define diffuseTextureCoord texCoord0
  11. #define diffuseColorFlag
  12. #define specularColorFlag
  13. #define emissiveColorFlag
  14. #define shininessFlag
  15. #define nop() {}
  16.  
  17. ////////////////////////////////////////////////////////////////////////////////////
  18. ////////// POSITION ATTRIBUTE - VERTEX
  19. ////////////////////////////////////////////////////////////////////////////////////
  20. #ifdef positionFlag
  21. attribute vec3 a_position;
  22. #endif //positionFlag
  23.  
  24. varying vec4 v_position;
  25. #define pushPositionValue(value) (v_position = (value))
  26. #if defined(positionFlag)
  27. vec4 g_position = vec4(a_position, 1.0);
  28. #define passPositionValue(value) pushPositionValue(value)
  29. #else
  30. vec4 g_position = vec4(0.0, 0.0, 0.0, 1.0);
  31. #define passPositionValue(value) nop()
  32. #endif
  33. #define passPosition() passPositionValue(g_position)
  34. #define pushPosition() pushPositionValue(g_position)
  35.  
  36. ////////////////////////////////////////////////////////////////////////////////////
  37. ////////// COLOR ATTRIBUTE - VERTEX
  38. ///////////////////////////////////////////////////////////////////////////////////
  39. #ifdef colorFlag
  40. attribute vec4 a_color;
  41. #endif //colorFlag
  42.  
  43. varying vec4 v_color;
  44. #define pushColorValue(value) (v_color = (value))
  45. #if defined(colorFlag)
  46. vec4 g_color = a_color;
  47. #define passColorValue(value) pushColorValue(value)
  48. #else
  49. vec4 g_color = vec4(1.0, 1.0, 1.0, 1.0);
  50. #define passColorValue(value) nop()
  51. #endif
  52. #define passColor() passColorValue(g_color)
  53. #define pushColor() pushColorValue(g_color)
  54.  
  55. ////////////////////////////////////////////////////////////////////////////////////
  56. ////////// NORMAL ATTRIBUTE - VERTEX
  57. ///////////////////////////////////////////////////////////////////////////////////
  58. #ifdef normalFlag
  59. attribute vec3 a_normal;
  60. #endif //normalFlag
  61.  
  62. varying vec3 v_normal;
  63. #define pushNormalValue(value) (v_normal = (value))
  64. #if defined(normalFlag)
  65. vec3 g_normal = a_normal;
  66. #define passNormalValue(value) pushNormalValue(value)
  67. #else
  68. vec3 g_normal = vec3(0.0, 0.0, 1.0);
  69. #define passNormalValue(value) nop()
  70. #endif
  71. #define passNormal() (passNormalValue(g_normal))
  72. #define pushNormal() (pushNormalValue(g_normal))
  73.  
  74. ////////////////////////////////////////////////////////////////////////////////////
  75. ////////// BINORMAL ATTRIBUTE - VERTEX
  76. ///////////////////////////////////////////////////////////////////////////////////
  77. #ifdef binormalFlag
  78. attribute vec3 a_binormal;
  79. #endif //binormalFlag
  80.  
  81. varying vec3 v_binormal;
  82. #define pushBinormalValue(value) (v_binormal = (value))
  83. #if defined(binormalFlag)
  84. vec3 g_binormal = a_binormal;
  85. #define passBinormalValue(value) pushBinormalValue(value)
  86. #else
  87. vec3 g_binormal = vec3(0.0, 1.0, 0.0);
  88. #define passBinormalValue(value) nop()
  89. #endif // binormalFlag
  90. #define passBinormal() passBinormalValue(g_binormal)
  91. #define pushBinormal() pushBinormalValue(g_binormal)
  92.  
  93. ////////////////////////////////////////////////////////////////////////////////////
  94. ////////// TANGENT ATTRIBUTE - VERTEX
  95. ///////////////////////////////////////////////////////////////////////////////////
  96. #ifdef tangentFlag
  97. attribute vec3 a_tangent;
  98. #endif //tangentFlag
  99.  
  100. varying vec3 v_tangent;
  101. #define pushTangentValue(value) (v_tangent = (value))
  102. #if defined(tangentFlag)
  103. vec3 g_tangent = a_tangent;
  104. #define passTangentValue(value) pushTangentValue(value)
  105. #else
  106. vec3 g_tangent = vec3(1.0, 0.0, 0.0);
  107. #define passTangentValue(value) nop()
  108. #endif // tangentFlag
  109. #define passTangent() passTangentValue(g_tangent)
  110. #define pushTangent() pushTangentValue(g_tangent)
  111.  
  112. ////////////////////////////////////////////////////////////////////////////////////
  113. ////////// TEXCOORD0 ATTRIBUTE - VERTEX
  114. ///////////////////////////////////////////////////////////////////////////////////
  115. #ifdef texCoord0Flag
  116. #ifndef texCoordsFlag
  117. #define texCoordsFlag
  118. #endif
  119. attribute vec2 a_texCoord0;
  120. #endif
  121.  
  122. varying vec2 v_texCoord0;
  123. #define pushTexCoord0Value(value) (v_texCoord0 = value)
  124. #if defined(texCoord0Flag)
  125. vec2 g_texCoord0 = a_texCoord0;
  126. #define passTexCoord0Value(value) pushTexCoord0Value(value)
  127. #else
  128. vec2 g_texCoord0 = vec2(0.0, 0.0);
  129. #define passTexCoord0Value(value) nop()
  130. #endif // texCoord0Flag
  131. #define passTexCoord0() passTexCoord0Value(g_texCoord0)
  132. #define pushTexCoord0() pushTexCoord0Value(g_texCoord0)
  133.  
  134. // Uniforms which are always available
  135. uniform mat4 u_projViewTrans;
  136. uniform mat4 u_worldTrans;
  137. uniform vec4 u_cameraPosition;
  138. uniform mat3 u_normalMatrix;
  139.  
  140. // Other uniforms
  141. #ifdef blendedFlag
  142. uniform float u_opacity;
  143. #else
  144. const float u_opacity = 1.0;
  145. #endif
  146.  
  147. #ifdef alphaTestFlag
  148. uniform float u_alphaTest;
  149. #else
  150. const float u_alphaTest = 0.0;
  151. #endif
  152.  
  153. #ifdef shininessFlag
  154. uniform float u_shininess;
  155. #else
  156. const float u_shininess = 20.0;
  157. #endif
  158.  
  159. #ifdef diffuseColorFlag
  160. uniform vec4 u_diffuseColor;
  161. #endif
  162.  
  163. #ifdef diffuseTextureFlag
  164. uniform sampler2D u_diffuseTexture;
  165. #endif
  166.  
  167. #ifdef specularColorFlag
  168. uniform vec4 u_specularColor;
  169. #endif
  170.  
  171. #ifdef specularTextureFlag
  172. uniform sampler2D u_specularTexture;
  173. #endif
  174.  
  175. #ifdef normalTextureFlag
  176. uniform sampler2D u_normalTexture;
  177. #endif
  178.  
  179. ////////////////////////////////////////////////////////////////////////////////////
  180. ////////// SKINNING
  181. ///////////////////////////////////////////////////////////////////////////////////
  182. #ifdef boneWeight0Flag
  183. #ifndef boneWeightsFlag
  184. #define boneWeightsFlag
  185. #endif
  186. attribute vec2 a_boneWeight0;
  187. #endif //boneWeight0Flag
  188.  
  189. #ifdef boneWeight1Flag
  190. #ifndef boneWeightsFlag
  191. #define boneWeightsFlag
  192. #endif
  193. attribute vec2 a_boneWeight1;
  194. #endif //boneWeight1Flag
  195.  
  196. #ifdef boneWeight2Flag
  197. #ifndef boneWeightsFlag
  198. #define boneWeightsFlag
  199. #endif
  200. attribute vec2 a_boneWeight2;
  201. #endif //boneWeight2Flag
  202.  
  203. #ifdef boneWeight3Flag
  204. #ifndef boneWeightsFlag
  205. #define boneWeightsFlag
  206. #endif
  207. attribute vec2 a_boneWeight3;
  208. #endif //boneWeight3Flag
  209.  
  210. #ifdef boneWeight4Flag
  211. #ifndef boneWeightsFlag
  212. #define boneWeightsFlag
  213. #endif
  214. attribute vec2 a_boneWeight4;
  215. #endif //boneWeight4Flag
  216.  
  217. #ifdef boneWeight5Flag
  218. #ifndef boneWeightsFlag
  219. #define boneWeightsFlag
  220. #endif
  221. attribute vec2 a_boneWeight5;
  222. #endif //boneWeight5Flag
  223.  
  224. #ifdef boneWeight6Flag
  225. #ifndef boneWeightsFlag
  226. #define boneWeightsFlag
  227. #endif
  228. attribute vec2 a_boneWeight6;
  229. #endif //boneWeight6Flag
  230.  
  231. #ifdef boneWeight7Flag
  232. #ifndef boneWeightsFlag
  233. #define boneWeightsFlag
  234. #endif
  235. attribute vec2 a_boneWeight7;
  236. #endif //boneWeight7Flag
  237.  
  238. // Declare the bones that are available
  239. #if defined(numBones)
  240. #if numBones > 0
  241. uniform mat4 u_bones[numBones];
  242. #endif //numBones
  243. #endif
  244.  
  245. // If there are bones and there are bone weights, than we can apply skinning
  246. #if defined(numBones) && defined(boneWeightsFlag)
  247. #if (numBones > 0)
  248. #define skinningFlag
  249. #endif
  250. #endif
  251.  
  252. #ifdef skinningFlag
  253. mat4 skinningTransform = mat4(0.0)
  254. #ifdef boneWeight0Flag
  255. + (a_boneWeight0.y) * u_bones[int(a_boneWeight0.x)]
  256. #endif //boneWeight0Flag
  257. #ifdef boneWeight1Flag
  258. + (a_boneWeight1.y) * u_bones[int(a_boneWeight1.x)]
  259. #endif //boneWeight1Flag
  260. #ifdef boneWeight2Flag
  261. + (a_boneWeight2.y) * u_bones[int(a_boneWeight2.x)]
  262. #endif //boneWeight2Flag
  263. #ifdef boneWeight3Flag
  264. + (a_boneWeight3.y) * u_bones[int(a_boneWeight3.x)]
  265. #endif //boneWeight3Flag
  266. #ifdef boneWeight4Flag
  267. + (a_boneWeight4.y) * u_bones[int(a_boneWeight4.x)]
  268. #endif //boneWeight4Flag
  269. #ifdef boneWeight5Flag
  270. + (a_boneWeight5.y) * u_bones[int(a_boneWeight5.x)]
  271. #endif //boneWeight5Flag
  272. #ifdef boneWeight6Flag
  273. + (a_boneWeight6.y) * u_bones[int(a_boneWeight6.x)]
  274. #endif //boneWeight6Flag
  275. #ifdef boneWeight7Flag
  276. + (a_boneWeight7.y) * u_bones[int(a_boneWeight7.x)]
  277. #endif //boneWeight7Flag
  278. ;
  279. #endif //skinningFlag
  280.  
  281. #ifdef skinningFlag
  282. vec3 applySkinning(const in vec3 x) { return (skinningTransform * vec4(x, 0.0)).xyz; }
  283. vec4 applySkinning(const in vec4 x) { return (skinningTransform * x); }
  284. #else
  285. #define applySkinning(x) (x)
  286. #endif //skinningFlag
  287.  
  288. #if defined(diffuseTextureFlag) || defined(specularTextureFlag)
  289. #define textureFlag
  290. #endif
  291.  
  292. #if defined(specularTextureFlag) || defined(specularColorFlag)
  293. #define specularFlag
  294. #endif
  295.  
  296. #if defined(specularFlag) || defined(fogFlag)
  297. #define cameraPositionFlag
  298. #endif
  299.  
  300. #ifdef shadowMapFlag
  301. uniform mat4 u_shadowMapProjViewTrans;
  302. varying vec3 v_shadowMapUv;
  303. #define separateAmbientFlag
  304. #endif //shadowMapFlag
  305.  
  306. #if defined(normalFlag) && defined(binormalFlag) && defined(tangentFlag)
  307. #define calculateTangentVectors() nop()
  308. #elif defined(normalFlag) && defined(binormalFlag)
  309. #define calculateTangentVectors() (g_tangent = normalize(cross(g_normal, g_binormal)))
  310. #elif defined(normalFlag) && defined(tangentFlag)
  311. #define calculateTangentVectors() (g_binormal = normalize(cross(g_normal, g_tangent)))
  312. #elif defined(binormalFlag) && defined(tangentFlag)
  313. #define calculateTangentVectors() (g_normal = normalize(cross(g_binormal, g_tangent)))
  314. #elif defined(normalFlag) || defined(binormalFlag) || defined(tangentFlag)
  315. vec3 biggestAngle(const in vec3 base, const in vec3 v1, const in vec3 v2) {
  316. vec3 c1 = cross(base, v1);
  317. vec3 c2 = cross(base, v2);
  318. return (dot(c2, c2) > dot(c1, c1)) ? c2 : c1;
  319. }
  320. #if defined(normalFlag)
  321. void calculateTangentVectors() {
  322. g_binormal = normalize(cross(g_normal, biggestAngle(g_normal, vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0))));
  323. g_tangent = normalize(cross(g_normal, g_binormal));
  324. }
  325. #elif defined(binormalFlag)
  326. void calculateTangentVectors() {
  327. g_tangent = normalize(cross(g_binormal, biggestAngle(g_binormal, vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 0.0))));
  328. g_normal = normalize(cross(g_binormal, g_tangent));
  329. }
  330. #elif defined(tangentFlag)
  331. void calculateTangentVectors() {
  332. g_binormal = normalize(cross(g_tangent, biggestAngle(g_binormal, vec3(0.0, 0.0, 1.0), vec3(0.0, 1.0, 0.0))));
  333. g_normal = normalize(cross(g_tangent, g_binormal));
  334. }
  335. #endif
  336. #endif
  337.  
  338. //////////////////////////////////////////////////////
  339. ////// AMBIENT LIGHT
  340. //////////////////////////////////////////////////////
  341. #ifdef ambientLightFlag
  342. #ifndef ambientFlag
  343. #define ambientFlag
  344. #endif
  345. uniform vec3 u_ambientLight;
  346. #define getAmbientLight() (u_ambientLight)
  347. #else
  348. #define getAmbientLight() (vec3(0.0))
  349. #endif
  350.  
  351.  
  352. //////////////////////////////////////////////////////
  353. ////// AMBIENT CUBEMAP
  354. //////////////////////////////////////////////////////
  355. #ifdef ambientCubemapFlag
  356. #ifndef ambientFlag
  357. #define ambientFlag
  358. #endif
  359. uniform vec3 u_ambientCubemap[6];
  360. vec3 getAmbientCubeLight(const in vec3 normal) {
  361. vec3 squaredNormal = normal * normal;
  362. vec3 isPositive  = step(0.0, normal);
  363. return squaredNormal.x * mix(u_ambientCubemap[0], u_ambientCubemap[1], isPositive.x) +
  364. squaredNormal.y * mix(u_ambientCubemap[2], u_ambientCubemap[3], isPositive.y) +
  365. squaredNormal.z * mix(u_ambientCubemap[4], u_ambientCubemap[5], isPositive.z);
  366. }
  367. #else
  368. #define getAmbientCubeLight(normal) (vec3(0.0))
  369. #endif
  370.  
  371. #if defined(ambientLightFlag) && defined(ambientCubemapFlag)
  372. #define getAmbient(normal) (getAmbientLight() + getAmbientCubeLight(normal))
  373. #elif defined(ambientLightFlag)
  374. #define getAmbient(normal) getAmbientLight()
  375. #elif defined(ambientCubemapFlag)
  376. #define getAmbient(normal) getAmbientCubeLight(normal)
  377. #else
  378. #define getAmbient(normal) (vec3(0.0))
  379. #endif
  380.  
  381. //////////////////////////////////////////////////////
  382. ////// POINTS LIGHTS
  383. //////////////////////////////////////////////////////
  384. #ifdef lightingFlag
  385. #if defined(numPointLights) && (numPointLights > 0)
  386. #define pointLightsFlag
  387. #endif // numPointLights
  388. #endif //lightingFlag
  389.  
  390. #ifdef pointLightsFlag
  391. struct PointLight
  392. {
  393. vec3 color;
  394. vec3 position;
  395. float intensity;
  396. };
  397. uniform PointLight u_pointLights[numPointLights];
  398. #endif
  399.  
  400. //////////////////////////////////////////////////////
  401. ////// DIRECTIONAL LIGHTS
  402. //////////////////////////////////////////////////////
  403. #ifdef lightingFlag
  404. #if defined(numDirectionalLights) && (numDirectionalLights > 0)
  405. #define directionalLightsFlag
  406. #endif // numDirectionalLights
  407. #endif //lightingFlag
  408.  
  409. #ifdef directionalLightsFlag
  410. struct DirectionalLight
  411. {
  412. vec3 color;
  413. vec3 direction;
  414. };
  415. uniform DirectionalLight u_dirLights[numDirectionalLights];
  416. #endif
  417.  
  418. varying vec3 v_lightDir;
  419. varying vec3 v_lightCol;
  420. varying vec3 v_viewDir;
  421.  
  422. #ifdef environmentCubemapFlag
  423. varying vec3 v_reflect;
  424. #endif
  425.  
  426. varying vec3 v_ambientLight;
  427.  
  428. void main() {
  429. calculateTangentVectors();
  430.  
  431. g_position = applySkinning(g_position);
  432. g_normal = normalize(u_normalMatrix * applySkinning(g_normal));
  433. g_binormal = normalize(u_normalMatrix * applySkinning(g_binormal));
  434. g_tangent = normalize(u_normalMatrix * applySkinning(g_tangent));
  435.  
  436. g_position = u_worldTrans * g_position;
  437. gl_Position = u_projViewTrans * g_position;
  438.  
  439. #ifdef shadowMapFlag
  440. vec4 spos = u_shadowMapProjViewTrans * g_position;
  441. v_shadowMapUv.xy = (spos.xy / spos.w) * 0.5 + 0.5;
  442. v_shadowMapUv.z = min(spos.z * 0.5 + 0.5, 0.998);
  443. #endif //shadowMapFlag
  444.  
  445. mat3 worldToTangent;
  446. worldToTangent[0] = g_tangent;
  447. worldToTangent[1] = g_binormal;
  448. worldToTangent[2] = g_normal;
  449.  
  450. v_ambientLight = getAmbient(g_normal);
  451.  
  452. v_lightDir = normalize(-u_dirLights[0].direction) * worldToTangent;
  453. v_lightCol = u_dirLights[0].color;
  454. vec3 viewDir = normalize(u_cameraPosition.xyz - g_position.xyz);
  455. v_viewDir = viewDir * worldToTangent;
  456. #ifdef environmentCubemapFlag
  457. v_reflect = reflect(-viewDir, g_normal);
  458. #endif
  459.  
  460. pushColor();
  461. pushTexCoord0();
  462. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement