Brak opisu

pntriangles.tese 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #version 450
  2. #extension GL_ARB_separate_shader_objects : enable
  3. #extension GL_ARB_shading_language_420pack : enable
  4. // PN patch data
  5. struct PnPatch
  6. {
  7. float b210;
  8. float b120;
  9. float b021;
  10. float b012;
  11. float b102;
  12. float b201;
  13. float b111;
  14. float n110;
  15. float n011;
  16. float n101;
  17. };
  18. layout (binding = 1) uniform UBO
  19. {
  20. mat4 projection;
  21. mat4 model;
  22. float tessAlpha;
  23. } ubo;
  24. layout(triangles, fractional_odd_spacing, ccw) in;
  25. layout(location = 0) in vec3 iNormal[];
  26. layout(location = 3) in vec2 iTexCoord[];
  27. layout(location = 6) in PnPatch iPnPatch[];
  28. layout(location = 0) out vec3 oNormal;
  29. layout(location = 1) out vec2 oTexCoord;
  30. #define uvw gl_TessCoord
  31. void main()
  32. {
  33. vec3 uvwSquared = uvw * uvw;
  34. vec3 uvwCubed = uvwSquared * uvw;
  35. // extract control points
  36. vec3 b210 = vec3(iPnPatch[0].b210, iPnPatch[1].b210, iPnPatch[2].b210);
  37. vec3 b120 = vec3(iPnPatch[0].b120, iPnPatch[1].b120, iPnPatch[2].b120);
  38. vec3 b021 = vec3(iPnPatch[0].b021, iPnPatch[1].b021, iPnPatch[2].b021);
  39. vec3 b012 = vec3(iPnPatch[0].b012, iPnPatch[1].b012, iPnPatch[2].b012);
  40. vec3 b102 = vec3(iPnPatch[0].b102, iPnPatch[1].b102, iPnPatch[2].b102);
  41. vec3 b201 = vec3(iPnPatch[0].b201, iPnPatch[1].b201, iPnPatch[2].b201);
  42. vec3 b111 = vec3(iPnPatch[0].b111, iPnPatch[1].b111, iPnPatch[2].b111);
  43. // extract control normals
  44. vec3 n110 = normalize(vec3(iPnPatch[0].n110, iPnPatch[1].n110, iPnPatch[2].n110));
  45. vec3 n011 = normalize(vec3(iPnPatch[0].n011, iPnPatch[1].n011, iPnPatch[2].n011));
  46. vec3 n101 = normalize(vec3(iPnPatch[0].n101, iPnPatch[1].n101, iPnPatch[2].n101));
  47. // compute texcoords
  48. oTexCoord = gl_TessCoord[2]*iTexCoord[0] + gl_TessCoord[0]*iTexCoord[1] + gl_TessCoord[1]*iTexCoord[2];
  49. // normal
  50. // Barycentric normal
  51. vec3 barNormal = gl_TessCoord[2]*iNormal[0] + gl_TessCoord[0]*iNormal[1] + gl_TessCoord[1]*iNormal[2];
  52. vec3 pnNormal = iNormal[0]*uvwSquared[2] + iNormal[1]*uvwSquared[0] + iNormal[2]*uvwSquared[1]
  53. + n110*uvw[2]*uvw[0] + n011*uvw[0]*uvw[1]+ n101*uvw[2]*uvw[1];
  54. oNormal = ubo.tessAlpha*pnNormal + (1.0-ubo.tessAlpha) * barNormal;
  55. // compute interpolated pos
  56. vec3 barPos = gl_TessCoord[2]*gl_in[0].gl_Position.xyz
  57. + gl_TessCoord[0]*gl_in[1].gl_Position.xyz
  58. + gl_TessCoord[1]*gl_in[2].gl_Position.xyz;
  59. // save some computations
  60. uvwSquared *= 3.0;
  61. // compute PN position
  62. vec3 pnPos = gl_in[0].gl_Position.xyz*uvwCubed[2]
  63. + gl_in[1].gl_Position.xyz*uvwCubed[0]
  64. + gl_in[2].gl_Position.xyz*uvwCubed[1]
  65. + b210*uvwSquared[2]*uvw[0]
  66. + b120*uvwSquared[0]*uvw[2]
  67. + b201*uvwSquared[2]*uvw[1]
  68. + b021*uvwSquared[0]*uvw[1]
  69. + b102*uvwSquared[1]*uvw[2]
  70. + b012*uvwSquared[1]*uvw[0]
  71. + b111*6.0*uvw[0]*uvw[1]*uvw[2];
  72. // final position and normal
  73. vec3 finalPos = (1.0-ubo.tessAlpha)*barPos + ubo.tessAlpha*pnPos;
  74. gl_Position = ubo.projection * ubo.model * vec4(finalPos,1.0);
  75. }