demo_shaders.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import moderngl_window as mglw
  2. import moderngl
  3. #import moderngl.window
  4. import time
  5. #print(dir(moderngl))
  6. #export DISPLAY=:99.0
  7. #Xvfb :99 -screen 0 640x480x24 &
  8. #redirects screen output to invisibel framebuffer
  9. # python3 main.py --window glfw --fullscreen --samples 16 --cursor false --size 800x600
  10. vertex_shader = """
  11. #version 430
  12. in vec3 in_position;
  13. void main(){
  14. gl_Position = vec4(in_position, 0.9);
  15. }
  16. """
  17. fragment_shader = """
  18. #version 430
  19. out vec4 fragColor;
  20. uniform vec2 resolution;
  21. uniform float time;
  22. vec2 rotate2D(vec2 uv, float a){
  23. float s = sin(a);
  24. float c = cos(a);
  25. return mat2(c,-s,s,c) * uv;
  26. }
  27. vec3 ring(vec3 col,vec2 uv, int z,float ang, float speed,float scaleX, float scaleY){
  28. //z =1; //count of star's
  29. //ang = 270;
  30. for( float i=0.0; i < z; i++){
  31. float a = 0;
  32. a = ang/z * i * (3.1415/180) ;
  33. a += time*speed;
  34. float dy = cos(a)/2;
  35. float dx = sin(a)/2;
  36. dy = dy * scaleY; // *.4; // scale y
  37. dx = dx * scaleX; //*.9; // scale x
  38. col += 0.01 / length(uv+vec2(dx+0.1,dy) );
  39. }
  40. col *= sin(vec3(0.2,0.8,0.9) * time) * 0.15 +0.25;
  41. fragColor = vec4(col, 1.0);
  42. return col;
  43. //return fragColor;
  44. }
  45. void main() {
  46. vec2 uv = (gl_FragCoord.xy - resolution.xy/2) / resolution.y;
  47. vec3 col = vec3(0.0);
  48. //uv = rotate2D(uv, 3.14/2.0+3.2);
  49. //uv = rotate2D(uv, 3.14/2.0);
  50. float r = 0.17;
  51. int z = 10;
  52. float ang = 360;
  53. float speed = .9;
  54. z = 50; //count of star's
  55. ang = 270;
  56. speed = -.1;
  57. col = ring(col,uv, z, ang, speed, .9, .4);
  58. z = 10;
  59. //col = ring(col,uv, z, 270,0.1, .7, .7);
  60. z =2; //count of star's
  61. ang = 90;
  62. speed = 10;
  63. col = ring(col,uv, z, ang, speed, .6, .2);
  64. z = 20; //count of star's
  65. ang = 360;
  66. speed = 0.5;
  67. col = ring(col,uv, z, ang, speed, .5, .5);
  68. /*
  69. float i = 0;
  70. float a = time+i;
  71. a = ang/z * i * (3.1415/180) ;
  72. a += time/4;
  73. float dx = 0.1;//cos(a)/2;
  74. float dy = 0.1;//sin(a)/2;
  75. //col += 0.01 / length(uv+vec2(dx+0.1,dy) );
  76. col += 0.03 / length(uv+vec2(dx+0.5,dy) );
  77. //col *= sin(vec3(0.8,0.2,0.9) * time) * 0.15 +0.25;
  78. fragColor = vec4(col, 1.0);
  79. dx = 0.2;
  80. dy = 0.42;
  81. col += 0.01 / length(uv+vec2(dx+0.5,dy) );
  82. fragColor = vec4(col, 1.0);
  83. uv + vec2(0,0);
  84. col = vec3(0.0);
  85. //fragColor = vec4(vec3(0,0,1), 1.0);
  86. //fragColor[0] = [0,0,1,1];
  87. */
  88. }
  89. """
  90. vertex_shader2 = """
  91. #version 450
  92. in vec3 in_position;
  93. void main(){
  94. gl_Position = vec4(in_position, 0.9);
  95. const vec3 positions[3] = vec3[3](
  96. vec3(1.f,1.f, 0.0f),
  97. vec3(-1.f,1.f, 0.0f),
  98. vec3(0.f,-1.f, 0.0f)
  99. );
  100. //output the position of each vertex
  101. //gl_Position = vec4(positions, 1.0f);
  102. //gl_Position = vec4(1.f,1.f, 0.0f, 1.0f);
  103. //gl_Position = vec4(-1.f,1.f, 0.0f, 1.0f);
  104. //gl_Position = vec4(1.f,-1.f, 0.0f, 1.0f);
  105. //gl_Position = vec4(positions[gl_VertexIndex], 1.0f);
  106. }
  107. """
  108. fragment_shader2 = """
  109. #version 450
  110. out vec4 fragColor;
  111. uniform vec2 resolution;
  112. uniform float time;
  113. void main(){
  114. vec2 uv = (gl_FragCoord.xy - resolution.xy/2) / resolution.y;
  115. vec3 col = vec3(0.0);
  116. fragColor = vec4(1.f,0.f,0.f,1.0f);
  117. }
  118. """
  119. class Screen(mglw.WindowConfig):
  120. window_size = 600, 300
  121. window_size = 900, 506
  122. #resource_dir = 'programms'
  123. def __init__(self,**args):
  124. super().__init__(**args)
  125. self.quad = mglw.geometry.quad_fs()
  126. # load shader's as string
  127. self.prog = self.ctx.program(vertex_shader=vertex_shader,
  128. fragment_shader=fragment_shader)
  129. # load shader's form file
  130. #self.prog = self.load_program(vertex_shader='vertex_shader.gls1',
  131. # #fragment_shader='fragment_shader.gls1')
  132. self.set_uniform('resolution',self.window_size)
  133. self.t = time.time()
  134. def set_uniform(self,u_name, u_value):
  135. try:
  136. self.prog[u_name] = u_value
  137. except KeyError:
  138. print(f'uniform: {u_name} - not used in shader')
  139. def render(self,_time,frame_time):
  140. self.ctx.clear()
  141. self.set_uniform('time',_time)
  142. if self.t+10 < time.time():
  143. print(_time)
  144. self.t = time.time()
  145. self.quad.render(self.prog)
  146. if __name__ == "__main__":
  147. mglw.run_window_config(Screen)