nabr

test_envmap.pde

May 22nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. //test_envmap.pde
  2. //05232017
  3. //@t_vaeringjarson
  4. //scetch i made for testing
  5.  
  6. //It is not thought be a physically-accurate reflection shader
  7. //you have to know about processing intern behavior to tweek values to fit your task
  8.  
  9. //vertex pre multiplication done by processing
  10. //forum.processing.org/two/discussion/11186/edit-stroke-position-and-stroke-color-of-a-pshape-using-shader
  11.  
  12.  
  13.  
  14. PShader bgShader, sphereShader;
  15.  
  16. PMatrix3D rotY=new PMatrix3D();
  17. PMatrix3D model = new PMatrix3D();
  18. PMatrix3D modelview = new PMatrix3D();
  19.  
  20.  
  21. void setup(){
  22. size(856,480,P3D);
  23.  
  24. //overloading the Matrix object from setup and than into draw (model Matrix)
  25. modelview=((PGraphicsOpenGL)g).modelview;
  26.  
  27. //Shader for Environment Mapping
  28. bgShader=new PShader(this,
  29. new String[]{"#version 150 \n"
  30. + "in vec4 position; "
  31. + "uniform mat4 Ry,view;"
  32. + "out vec4 refDir;"
  33. + "void main() {"
  34. + "gl_Position = vec4(position.xy,.0, 1.);"
  35. + "refDir =Ry*view*vec4(position.xy,1.,0);"
  36. + "}"
  37. }, new String[] {"#version 150 \n"
  38. + "in vec4 refDir;"
  39. + "uniform sampler2D envmap;"
  40. + "out vec4 fragColor;"
  41. + "const float PI ="+(double)PI+";"
  42. + "void main () {"
  43.  
  44. //reindelsoftware.com/Documents/Mapping/Mapping.html
  45. + "fragColor = texture(envmap,"
  46. + "vec2(.5+atan(refDir.z,refDir.x)/(2.0*PI),"
  47. + "acos(refDir.y/length(refDir))/PI));"
  48. + "}"
  49. }){
  50. PShader run(){
  51.  
  52. //mode: nearest
  53. ((PGraphicsOpenGL)g).textureSampling(2);
  54.  
  55. int x =0;
  56. //shader call chain: overload the texure at the higher shader object (bgShader)
  57. //background(0) =
  58. rectMode(RADIUS);
  59. fill(x);
  60. rect(x,x,width,height);
  61.  
  62. //draw grid
  63. stroke(254);
  64. while(x<width){
  65. x+=20; //at 1080p=96dpi
  66. line(x,0,x,height);
  67. line(0,x,width,x);
  68. }//x=0;
  69.  
  70. //copy pixels to texture
  71. this.set("envmap",get());
  72.  
  73. //enable for debug
  74. //this.set("modelview",((PGraphicsOpenGL)g).modelview);
  75.  
  76. return this;
  77. }
  78. }.run();
  79.  
  80. //Shader for the Processing Shape (sphere)
  81. sphereShader=new PShader(this,
  82. new String[]{ "#version 150 \n"
  83. + "in vec3 position,normal;"
  84. + "uniform mat4 modelviewInv,view,projection;"
  85. + "uniform float time;"
  86. + "out vec3 refDir;"
  87. + "void main () {"
  88. + "vec4 camPos = view*vec4(position, 1);"
  89. + "vec3 eye = normalize(position.xyz-modelviewInv[3].xyz/modelviewInv[3].w);"
  90.  
  91. //more accurate would be refDir=-normalize(normal);
  92. + "refDir = reflect(eye, normal);"
  93.  
  94. //vertex animation
  95. + "camPos.z*=clamp(sin(time)*.5+.5,0,1.);"
  96.  
  97. + "gl_Position = projection*camPos;"
  98. + "}"
  99. },new String[]{"#version 150 \n"
  100. + "in vec3 refDir;"
  101. + "uniform sampler2D envmap;"
  102. + "out vec4 fragColor;"
  103. + "const float PI ="+(double)PI+";"
  104. + "void main () {"
  105.  
  106. //mvps.org/directx/articles/spheremap.htm
  107. + "fragColor = texture(envmap,vec2( .5+asin(refDir.x/PI) ,.5+ asin(refDir.y/PI) ));"
  108. +"}"
  109. }){
  110. PShader run(){
  111. this.set("modelviewInv",((PGraphicsOpenGL)g).modelviewInv);
  112.  
  113. //enable for debug
  114. //this.set("modelview", ((PGraphicsOpenGL)g).modelview);
  115.  
  116. this.set("projection", ((PGraphicsOpenGL)g).projection);
  117. return this;
  118. }
  119. }.run();
  120.  
  121. //debug: both options off/on
  122. //enable for debug
  123.  
  124. //hint(DISABLE_OPTIMIZED_STROKE);
  125.  
  126. //wireframe Mode
  127. noStroke();
  128. }
  129. float t=0; //time variable
  130.  
  131. void draw(){
  132.  
  133. //millis()*.001f will fail becourse of a floating point error as I suspect rotateX(cos(
  134. //for better processing internal behavior use frameCount
  135. t=frameCount*.01f;
  136.  
  137. beginCamera();
  138. camera();
  139. //rotate Camera
  140. rotateX(cos(t));
  141. rotateZ(sin(t*.01f)/TWO_PI);
  142. endCamera();
  143.  
  144. //rotation pitch
  145. bgShader.set("Ry", rotY);
  146.  
  147. //reset to IdentityMatrix & reset the stack
  148. rotY.reset();
  149. rotY.apply(cos(t), 0,sin(t), 0, 0, 1, 0, 0, -sin(t), 0, cos(t), 0, 0, 0, 0, 1);
  150.  
  151. //set time
  152. sphereShader.set("time",t);
  153.  
  154. //no depth testing
  155. hint(DISABLE_DEPTH_MASK);
  156. shader(bgShader);
  157.  
  158. //enable for debug
  159. //background(0);
  160.  
  161. rect(0, 0, width, height);
  162.  
  163. //readPixels for the previous shader bgShader
  164. sphereShader.set("envmap",get());
  165.  
  166. //enable for debug
  167. //hint(ENABLE_DEPTH_MASK);
  168.  
  169. shader(sphereShader);
  170.  
  171. translate(width/2, height/2,-10);
  172. sphere(120);
  173.  
  174. //after we apply the transformation to the sphere we update the matrix
  175. //and going back to the begining of the draw loop -> camera
  176.  
  177. //reset to IdentityMatrix & reset the stack
  178. model.reset();
  179. model.apply(modelview.m00, modelview.m10, modelview.m20, modelview.m30,
  180. modelview.m01, modelview.m11, modelview.m21, modelview.m31,
  181. modelview.m02, modelview.m12, modelview.m22, modelview.m32,
  182. modelview.m03, modelview.m13, modelview.m23, modelview.m33);
  183.  
  184. //load the modelview from stack & update it
  185. bgShader.set("view",model);
  186. sphereShader.set("view",model);
  187.  
  188.  
  189. //enable for debug
  190. //if(keyPressed||mousePressed)exit();
  191. }
Advertisement
Add Comment
Please, Sign In to add comment