CapsAdmin

Untitled

Sep 23rd, 2013
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.33 KB | None | 0 0
  1.  
  2. local data = {
  3.     -- these are declared as uniform on all shaders
  4.     shared = {
  5.         uniform = {
  6.             time = 0,
  7.         },
  8.     },
  9.    
  10.     vertex = {
  11.         uniform = {
  12.             camera_matrix = "mat4",
  13.             model_matrix = "mat4",
  14.         },         
  15.         attributes = {
  16.             position = "vec2",
  17.         },
  18.         vertex_attributes = {
  19.             {pos = "vec2"},
  20.             {uv = "vec2"},
  21.             {color = "vec4"},
  22.         }, 
  23.         -- if main is not defined it will wrap void main() { *line here* } around the line
  24.         source = "gl_Position = camera_matrix * model_matrix * vec4(position, 0.0, 1.0);"
  25.     },
  26.    
  27.     fragment = {
  28.         uniform = {
  29.             add_color = 0, -- guess the type and use passed var as default if not passed
  30.             global_color = Color(1,1,1,1),
  31.             texture = Texture(16,16):Fill(function()
  32.                 return 255, 255, 255, 255
  33.             end),
  34.         },     
  35.         -- when attributes is used outside of vertex they are simply sent from vertex shader
  36.         -- as "__out_foo" and then grabbed from the other shader with a macro to turn its name
  37.         -- back to "foo" with #define
  38.         attributes = {
  39.             uv = "vec2",
  40.             color = "vec4",
  41.         },         
  42.         source = [[
  43.             out vec4 frag_color;
  44.  
  45.             vec4 texel = texture2D(texture, uv);
  46.  
  47.             void main()
  48.             {  
  49.                 if (add_color > 0.5)
  50.                 {
  51.                     frag_color = texel * color;
  52.                     frag_color.xyz = frag_color.xyz + global_color.xyz;
  53.                     frag_color.w = frag_color.w * global_color.w;
  54.                 }
  55.                 else
  56.                 {  
  57.                     frag_color = texel * color * global_color;
  58.                 }
  59.             }
  60.         ]]
  61.     }
  62. }
  63.  
  64. local mat = Material("2d_rect", data)
  65.  
  66. -- this creates buffer from the vertex_attributes field in our material
  67. local buffer = mat:CreateVertexBuffer({
  68.     {pos = {0, 0}, uv = {0, 1}, color = {1,1,1,1}},
  69.     {pos = {0, 1}, uv = {0, 0}, color = {1,1,1,1}},
  70.     {pos = {1, 1}, uv = {1, 0}, color = {1,1,1,1}},
  71.  
  72.     {pos = {1, 1}, uv = {1, 0}, color = {1,1,1,1}},
  73.     {pos = {1, 0}, uv = {1, 1}, color = {1,1,1,1}},
  74.     {pos = {0, 0}, uv = {0, 1}, color = {1,1,1,1}},
  75. })
  76.  
  77. event.AddListener("OnDraw2D", "hm", function()
  78.  
  79.     -- use the built in matrices cause we dont have our own (yet!)
  80.     gl.Translatef(50, 50, 0)
  81.     gl.Scalef(100, 100, 0)
  82.    
  83.     gl.GetFloatv(e.GL_MODELVIEW_MATRIX, render.model_matrix)
  84.     gl.GetFloatv(e.GL_PROJECTION_MATRIX, render.camera_matrix)
  85.    
  86.     --set the uniform fields
  87.     mat.model_matrix = render.model_matrix
  88.     mat.camera_matrix = render.camera_matrix
  89.     mat.global_color = HSVToColor(glfw.GetTime())
  90.  
  91.     buffer:Draw(mat)
  92. end)
Advertisement
Add Comment
Please, Sign In to add comment