Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Play GLSL on mruby with OpenFrameworks

蒼時弦や
September 08, 2016

Play GLSL on mruby with OpenFrameworks

蒼時弦や

September 08, 2016
Tweet

More Decks by 蒼時弦や

Other Decks in Programming

Transcript

  1. Web Developer Game Developer Major in Digital Media Design Taiwanese

    who likes Anime, Manga, Game @elct9620 襝儗䓚⛲
  2. Who use the OpenFrameworks? What is the Shader mean? What

    is the GLSL? OpenFrameworks and GLSL
  3. Open Class to extend C++ class How to convert Ruby

    to GLSL The DSL limitation Arithmetic Problem Shader Generator
  4. mrb_define_method(mrb, klass, "draw", draw, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "vertex=", setVertexShader, MRB_ARGS_REQ(1));

    mrb_define_method(mrb, klass, "fragment=", setFragmentShader, MRB_ARGS_REQ(1)); mrb_define_method(mrb, klass, "bind", bind, MRB_ARGS_OPT(3)); mrb_define_method(mrb, klass, "apply", apply, MRB_ARGS_NONE()); Open Class
  5. vec4 lightIntensity = vec4(1, 1, 1, 1); lightIntensity = clamp(lightIntensity.x,

    0, 1); lightIntensity is Vec4 vec4(1, 1, 1, 1) lightIntensity is clamp(lightIntensity.x, 0, 1) Ruby Code GLSL Code Convert to GLSL
  6. def method_missing(name, *args, &block) value = args.first return assign(name, value)

    if value.is_a? (AssignNode) return [send(name.downcase), value] if is_const?(name) Variable.new(name) end Convert to GLSL
  7. class Shader::AssignNode def initialize(type, *args) @type = type @expression =

    args.shift unless type.is_a?(Type) && [email protected]? @expression = @type @type = nil end end end Convert to GLSL
  8. def to(name) name = "-#{name}" if @negative return "#{name} =

    #{expression};" if @type.nil? return "#{@type.to_typename} #{name} = #{expression};" unless is_const?(name) "const #{@type.to_typename} #{name} = #{expression};" end Convert to GLSL
  9. class Shader::FunctionCall < Shader::Variable def initialize(name, *args) super(name) @args =

    args end def to_funtion "#{@name}(#{@args.join(", ")})" end def to_s return "#{to_funtion}" if @suffix.empty? "#{to_funtion}.#{@suffix.join(".")}" end end Convert to GLSL
  10. main do @variable = 1234 # Captured by class variable

    = 1234 # Not captured end Limitation
  11. main do vec4 @variable = vec4(1, 1, 1, 1) @variable

    = vec4 vec4(1, 1, 1, 1) end Limitation
  12. main do variable is Vec4 vec4(1, 1, 1, 1) variable

    is Vec4 vec4(1, 1, 1, 1) end Limitation
  13. main do variable is vec4 vec4(1, 1, 1, 1) +

    1 variable is Vec4 vec4(1) * normal end Arithmetic Problem
  14. module Shader::Calculable def operation @operation ||= [] end def +(other)

    operation.push("+", other) self end end Arithmetic Problem
  15. main do variable is Vec4 ( 1 - delta )

    * normal end # => vec4 variable = 1 - delat * normal Arithmetic Problem
  16. module Shader::Calculable def operation @operation ||= [] end def +(other)

    operation.push("+", other) BracketsNode.new(self) end end Arithmetic Problem
  17. main do variable is Vec4 ( 1 - delta )

    * normal end # => vec4 variable = ((1 - delat) * normal) Arithmetic Problem
  18. How to define a class in C++ Save data inside

    ruby class in C++ Using mruby in C++
  19. void freeShader(mrb_state* mrb, void* ptr) { Shader* shader = static_cast<Shader*>(ptr);

    if(shader != NULL) { free(shader); } mrb_free(ptr); } Save data in Class
  20. mrb_value init(mrb_state* mrb, mrb_value self) { Shader* shader = (Shader*)

    DATA_PTR(self); if(shader) { mrb_free(mrb, shader); } DATA_TYPE(self) = &ShaderType; DATA_PTR(self) = NULL; shader = allocaShader(mrb); shader->instance = new ofShader; DATA_PTR(self) = shader; return self; } Save data in Class