Advertisement
Guest User

mygl.js

a guest
Mar 25th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var mygl=mygl||(function(){
  2.  
  3.   function copy_object(a) {
  4.     var b={};
  5.  
  6.     for(var k in a) {
  7.       if(a[k] instanceof Array) {
  8.         b[k]=a[k].slice();
  9.       } else {
  10.         b[k]=a[k];
  11.       }
  12.     }
  13.  
  14.     return b;
  15.   }
  16.  
  17.   var state_initial = {
  18.     "cull_face" : false,
  19.     "blend" : false,
  20.     "stencil_test" : false,
  21.     "depth_test" : false,
  22.     "polygon_offset_fill" : false,
  23.  
  24.     "dither" : true,
  25.     "scissor_test" : false,
  26.  
  27.     "blend_color" : [0,0,0,0],
  28.     "blend_equation_rgb" : 0x8006, //gl.FUNC_ADD
  29.     "blend_equation_alpha" : 0x8006, //gl.FUNC_ADD
  30.     "blend_src_rgb" : 1, //gl.ONE
  31.     "blend_dst_rgb" : 0, //gl.ZERO
  32.     "blend_src_alpha" : 1, //gl.ONE
  33.     "blend_dst_alpha" : 0, //gl.ZERO
  34.     "color_writemask" : [true,true,true,true],
  35.     "cull_face_mode" : 0x0405, //gl.BACK
  36.     "depth_func" : 0x0201, //gl.LESS
  37.     "depth_writemask" : true,
  38.     "depth_range" : [0,1],
  39.     "front_face" : 0x0901, //gl.CCW
  40.     "polygon_offset_factor" : 0,
  41.     "polygon_offset_units" : 0,
  42.     "stencil_back_func" : 0x0207, //gl.ALWAYS
  43.     "stencil_back_ref" : 0,
  44.     "stencil_back_value_mask" : 0xffffffff,
  45.     "stencil_func" : 0x0207, //gl.ALWAYS
  46.     "stencil_ref" : 0,
  47.     "stencil_value_mask" : 0xffffffff,
  48.     "stencil_back_writemask" : 0xffffffff,
  49.     "stencil_writemask" : 0xffffffff,
  50.     "stencil_back_pass_depth_fail" : 0x1E00, //gl.KEEP
  51.     "stencil_back_pass_depth_pass" : 0x1E00, //gl.KEEP
  52.     "stencil_back_fail" : 0x1E00, //gl.KEEP
  53.     "stencil_pass_depth_fail" : 0x1E00, //gl.KEEP
  54.     "stencil_pass_depth_pass" : 0x1E00, //gl.KEEP
  55.     "stencil_fail" : 0x1E00, //gl.KEEP
  56.  
  57.     "scissor_box" : [0,0,0,0],
  58.     "line_width" : 1,
  59.   };
  60.  
  61.   var state_current = copy_object(state_initial);
  62.   var state_to = copy_object(state_initial);
  63.  
  64.   function state_clear() {
  65.     //console.log(state_to.depth_test);
  66.     state_to = copy_object(state_initial);
  67.     //console.log(state_to.depth_test);
  68.   }
  69.  
  70.   function state_sync() {
  71.     //
  72.     state_current.cull_face=gl.getParameter(gl.CULL_FACE);
  73.     state_current.blend=gl.getParameter(gl.BLEND);
  74.     state_current.stencil_test=gl.getParameter(gl.STENCIL_TEST);
  75.     state_current.depth_test=gl.getParameter(gl.DEPTH_TEST);
  76.     state_current.polygon_offset_fill=gl.getParameter(gl.POLYGON_OFFSET_FILL);
  77.  
  78.     state_current.dither=gl.getParameter(gl.DITHER);
  79.     state_current.scissor_test=gl.getParameter(gl.SCISSOR_TEST);
  80.  
  81.     //
  82.     state_current.blend_color=gl.getParameter(gl.BLEND_COLOR);
  83.  
  84.     //blend equation
  85.     state_current.blend_equation_rgb=gl.getParameter(gl.BLEND_EQUATION_RGB);
  86.     state_current.blend_equation_alpha=gl.getParameter(gl.BLEND_EQUATION_ALPHA);
  87.  
  88.     //blend func
  89.     state_current.blend_src_rgb=gl.getParameter(gl.BLEND_SRC_RGB);
  90.     state_current.blend_dst_rgb=gl.getParameter(gl.BLEND_DST_RGB);
  91.     state_current.blend_src_alpha=gl.getParameter(gl.BLEND_SRC_ALPHA);
  92.     state_current.blend_dst_alpha=gl.getParameter(gl.BLEND_DST_ALPHA);
  93.  
  94.     //
  95.     state_current.color_writemask=gl.getParameter(gl.COLOR_WRITEMASK);
  96.     state_current.cull_face_mode=gl.getParameter(gl.CULL_FACE_MODE);
  97.     state_current.depth_func=gl.getParameter(gl.DEPTH_FUNC);
  98.     state_current.depth_writemask=gl.getParameter(gl.DEPTH_WRITEMASK);
  99.     state_current.depth_range=gl.getParameter(gl.DEPTH_RANGE);
  100.     state_current.front_face=gl.getParameter(gl.FRONT_FACE);
  101.  
  102.     //polygonOffset
  103.     state_current.polygon_offset_factor=gl.getParameter(gl.POLYGON_OFFSET_FACTOR);
  104.     state_current.polygon_offset_units=gl.getParameter(gl.POLYGON_OFFSET_UNITS);
  105.  
  106.     //stencilFuncBack
  107.     state_current.stencil_back_func=gl.getParameter(gl.STENCIL_BACK_FUNC);
  108.     state_current.stencil_back_ref=gl.getParameter(gl.STENCIL_BACK_REF);
  109.     state_current.stencil_back_value_mask=gl.getParameter(gl.STENCIL_BACK_VALUE_MASK);
  110.  
  111.     //stencilFuncFront
  112.     state_current.stencil_func=gl.getParameter(gl.STENCIL_FUNC);
  113.     state_current.stencil_ref=gl.getParameter(gl.STENCIL_REF);
  114.     state_current.stencil_value_mask=gl.getParameter(gl.STENCIL_VALUE_MASK);
  115.  
  116.     //stencilMaskBack
  117.     state_current.stencil_back_writemask=gl.getParameter(gl.STENCIL_BACK_WRITEMASK);
  118.  
  119.     //stencilMaskFront
  120.     state_current.stencil_writemask=gl.getParameter(gl.STENCIL_WRITEMASK);
  121.  
  122.     //stencilOpBack
  123.     state_current.stencil_back_pass_depth_fail=gl.getParameter(gl.STENCIL_BACK_PASS_DEPTH_FAIL);
  124.     state_current.stencil_back_pass_depth_pass=gl.getParameter(gl.STENCIL_BACK_PASS_DEPTH_PASS);
  125.     state_current.stencil_back_fail=gl.getParameter(gl.STENCIL_BACK_FAIL);
  126.  
  127.     //stencilOpFront
  128.     state_current.stencil_pass_depth_fail=gl.getParameter(gl.STENCIL_PASS_DEPTH_FAIL);
  129.     state_current.stencil_pass_depth_pass=gl.getParameter(gl.STENCIL_PASS_DEPTH_PASS);
  130.     state_current.stencil_fail=gl.getParameter(gl.STENCIL_FAIL);
  131.  
  132.     //scissor
  133.     state_current.scissor_box=gl.getParameter(gl.SCISSOR_BOX);
  134.  
  135.     //lineWidth
  136.     state_current.line_width=gl.getParameter(gl.LINE_WIDTH);
  137.  
  138.     //
  139.     state_to = copy_object(state_current);
  140.  
  141.     //
  142.     //for(var k in state_current) { console.log("\"" +k + "\" : " + state_current[k] + "\n"); }
  143.   }
  144.  
  145.   function state_func_enable(cap) {
  146.     if(cap==gl.CULL_FACE) {
  147.       state_to.cull_face=true;
  148.     } else if(cap==gl.BLEND) {
  149.       state_to.blend=true;
  150.     } else if(cap==gl.DEPTH_TEST) {
  151.       state_to.depth_test=true;
  152.     } else if(cap==gl.STENCIL_TEST) {
  153.       state_to.stencil_test=true;
  154.     } else if(cap==gl.POLYGON_OFFSET_FILL) {
  155.       state_to.polygon_offset_fill=true;
  156.  
  157.     } else if(cap==gl.DITHER) {
  158.       state_to.dither=true;
  159.     } else if(cap==gl.SCISSOR_TEST) {
  160.       state_to.scissor_test=true;
  161.     }
  162.   }
  163.  
  164.   function state_func_disable(cap) {
  165.     if(cap==gl.CULL_FACE) {
  166.       state_to.cull_face=false;
  167.     } else if(cap==gl.BLEND) {
  168.       state_to.blend=false;
  169.     } else if(cap==gl.DEPTH_TEST) {
  170.       state_to.depth_test=false;
  171.     } else if(cap==gl.STENCIL_TEST) {
  172.       state_to.stencil_test=false;
  173.     } else if(cap==gl.POLYGON_OFFSET_FILL) {
  174.       state_to.polygon_offset_fill=false;
  175.     } else if(cap==gl.DITHER) {
  176.       state_to.dither=false;
  177.     } else if(cap==gl.SCISSOR_TEST) {
  178.       state_to.scissor_test=false;
  179.     }
  180.   }
  181.  
  182.   function state_func_blendColor(red,green,blue,alpha) {
  183.     state_to.blend_color[0]=red;
  184.     state_to.blend_color[1]=green;
  185.     state_to.blend_color[2]=blue;
  186.     state_to.blend_color[3]=alpha;
  187.   }
  188.  
  189.   function state_func_blendEquation(mode) {
  190.     state_to.blend_equation_rgb=mode;
  191.     state_to.blend_equation_alpha=mode;
  192.   }
  193.  
  194.   function state_func_blendEquationSeparate(modeRGB,modeAlpha) {
  195.     state_to.blend_equation_rgb=modeRGB;
  196.     state_to.blend_equation_alpha=modeAlpha;
  197.   }
  198.  
  199.   function state_func_blendFunc(sfactor,dfactor) {
  200.     state_to.blend_src_rgb=sfactor;
  201.     state_to.blend_dst_rgb=dfactor;
  202.     state_to.blend_src_alpha=sfactor;
  203.     state_to.blend_dst_alpha=dfactor;
  204.   }
  205.  
  206.   function state_func_blendFuncSeparate(srcRGB,dstRGB,srcAlpha,dstAlpha) {
  207.     state_to.blend_src_rgb=srcRGB;
  208.     state_to.blend_dst_rgb=dstRGB;
  209.     state_to.blend_src_alpha=srcAlpha;
  210.     state_to.blend_dst_alpha=dstAlpha;
  211.   }
  212.  
  213.   function state_func_colorMask(red,green,blue,alpha) {
  214.     state_to.color_writemask[0]=red;
  215.     state_to.color_writemask[1]=green;
  216.     state_to.color_writemask[2]=blue;
  217.     state_to.color_writemask[3]=alpha;
  218.   }
  219.  
  220.   function state_func_cullFace(mode) {
  221.     state_to.cull_face_mode=mode;
  222.   }
  223.  
  224.   function state_func_depthFunc(func) {
  225.     state_to.depth_func=func;
  226.   }
  227.  
  228.   function state_func_depthMask(flag) {
  229.     state_to.depth_writemask=flag;
  230.   }
  231.  
  232.   function state_func_depthRange(zNear,zFar) {
  233.     state_to.depth_range[0]=zNear;
  234.     state_to.depth_range[1]=zFar;
  235.   }
  236.  
  237.   function state_func_frontFace(mode) {
  238.     state_to.front_face=mode;
  239.   }
  240.  
  241.   function state_func_polygonOffset(factor,units) {
  242.     state_to.polygon_offset_factor=factor;
  243.     state_to.polygon_offset_units=units;
  244.   }
  245.  
  246.   function state_func_stencilFunc(func,ref,mask) {
  247.     state_to.stencil_back_func=func;
  248.     state_to.stencil_back_ref=ref;
  249.     state_to.stencil_back_value_mask=mask;
  250.    
  251.     state_to.stencil_func=func;
  252.     state_to.stencil_ref=ref;
  253.     state_to.stencil_value_mask=mask;
  254.   }
  255.  
  256.   function state_func_stencilFuncSeparate(face,func,ref,mask) {
  257.     if(face==gl.BACK) {
  258.       state_to.stencil_back_func=func;
  259.       state_to.stencil_back_ref=ref;
  260.       state_to.stencil_back_value_mask=mask;
  261.     } else if(face==gl.FRONT) {
  262.       state_to.stencil_func=func;
  263.       state_to.stencil_ref=ref;
  264.       state_to.stencil_value_mask=mask;
  265.     }
  266.   }
  267.  
  268.   function state_func_stencilMask(mask) {
  269.     state_to.stencil_back_writemask=mask;
  270.     state_to.stencil_writemask=mask;
  271.   }
  272.  
  273.   function state_func_stencilMaskSeparate(face,mask) {
  274.     if(face==gl.BACK) {
  275.       state_to.stencil_back_writemask=mask;
  276.     } else if(face==gl.FRONT) {
  277.       state_to.stencil_writemask=mask;
  278.     }
  279.   }
  280.  
  281.   function state_func_stencilOp(fail,zfail,zpass) {
  282.     state_to.stencil_back_fail=fail;
  283.     state_to.stencil_back_pass_depth_fail=zfail;
  284.     state_to.stencil_back_pass_depth_pass=zpass;
  285.    
  286.     state_to.stencil_fail=fail;
  287.     state_to.stencil_pass_depth_fail=zfail;
  288.     state_to.stencil_pass_depth_pass=zpass;
  289.   }
  290.  
  291.   function state_func_stencilOpSeparate(face,fail,zfail,zpass) {
  292.     if(face==gl.BACK) {
  293.       state_to.stencil_back_fail=fail;
  294.       state_to.stencil_back_pass_depth_fail=zfail;
  295.       state_to.stencil_back_pass_depth_pass=zpass;
  296.     } else if(face==gl.FRONT) {
  297.       state_to.stencil_fail=fail;
  298.       state_to.stencil_pass_depth_fail=zfail;
  299.       state_to.stencil_pass_depth_pass=zpass;
  300.     }
  301.   }
  302.  
  303.   function state_func_scissor(x,y,width,height) {
  304.     state_to.scissor_box[0]=x;
  305.     state_to.scissor_box[1]=y;
  306.     state_to.scissor_box[2]=width;
  307.     state_to.scissor_box[3]=height;
  308.   }
  309.  
  310.   function state_func_lineWidth(width) {
  311.     state_to.line_width=width;
  312.   }
  313.  
  314.   function state_apply() {
  315.     //cull_face
  316.     if(state_current.cull_face!=state_to.cull_face) {
  317.       state_current.cull_face=state_to.cull_face;
  318.  
  319.       if(state_current.cull_face) {
  320.         gl.enable(gl.CULL_FACE);
  321.       } else {
  322.         gl.disable(gl.CULL_FACE);
  323.       }
  324.     }
  325.  
  326.     //blend
  327.     if(state_current.blend!=state_to.blend) {
  328.       state_current.blend=state_to.blend;
  329.  
  330.       if(state_current.blend) {
  331.         gl.enable(gl.BLEND);
  332.       } else {
  333.         gl.disable(gl.BLEND);
  334.       }
  335.     }
  336.  
  337.     //stencil_test
  338.     if(state_current.stencil_test!=state_to.stencil_test) {
  339.       state_current.stencil_test=state_to.stencil_test;
  340.  
  341.       if(state_current.stencil_test) {
  342.         gl.enable(gl.STENCIL_TEST);
  343.       } else {
  344.         gl.disable(gl.STENCIL_TEST);
  345.       }
  346.     }
  347.  
  348.     //enable/disable depth_test
  349.     if(state_current.depth_test!=state_to.depth_test) {
  350.       state_current.depth_test=state_to.depth_test;
  351.  
  352.       if(state_current.depth_test) {
  353.         gl.enable(gl.DEPTH_TEST);
  354.       } else {
  355.         gl.disable(gl.DEPTH_TEST);
  356.       }
  357.     }
  358.  
  359.     //polygon_offset_fill
  360.     if(state_current.polygon_offset_fill!=state_to.polygon_offset_fill) {
  361.       state_current.polygon_offset_fill=state_to.polygon_offset_fill;
  362.  
  363.       if(state_current.polygon_offset_fill) {
  364.         gl.enable(gl.POLYGON_OFFSET_FILL);
  365.       } else {
  366.         gl.disable(gl.POLYGON_OFFSET_FILL);
  367.       }
  368.     }
  369.  
  370.     //dither
  371.     if(state_current.dither!=state_to.dither) {
  372.       state_current.dither=state_to.dither;
  373.  
  374.       if(state_current.dither) {
  375.         gl.enable(gl.DITHER);
  376.       } else {
  377.         gl.disable(gl.DITHER);
  378.       }
  379.     }
  380.  
  381.     //scissor_test
  382.     if(state_current.scissor_test!=state_to.scissor_test) {
  383.       state_current.scissor_test=state_to.scissor_test;
  384.  
  385.       if(state_current.scissor_test) {
  386.         gl.enable(gl.SCISSOR_TEST);
  387.       } else {
  388.         gl.disable(gl.SCISSOR_TEST);
  389.       }
  390.     }
  391.  
  392.     //blendColor
  393.     if(state_to.blend &&
  394.        (state_current.blend_color[0]!=state_to.blend_color[0] ||
  395.         state_current.blend_color[1]!=state_to.blend_color[1] ||
  396.         state_current.blend_color[2]!=state_to.blend_color[2] ||
  397.         state_current.blend_color[3]!=state_to.blend_color[3])) {
  398.  
  399.       state_current.blend_color[0]=state_to.blend_color[0];
  400.       state_current.blend_color[1]=state_to.blend_color[1];
  401.       state_current.blend_color[2]=state_to.blend_color[2];
  402.       state_current.blend_color[3]=state_to.blend_color[3];
  403.  
  404.       gl.blendColor(state_current.blendColor[0],
  405.                     state_current.blendColor[1],
  406.                     state_current.blendColor[2],
  407.                     state_current.blendColor[3]);
  408.     }
  409.  
  410.     //blendEquationSeparate
  411.     if(state_to.blend &&
  412.        (state_current.blend_equation_rgb != state_to.blend_equation_rgb ||
  413.         state_current.blend_equation_alpha != state_to.blend_equation_alpha)) {
  414.  
  415.       state_current.blend_equation_rgb = state_to.blend_equation_rgb;
  416.       state_current.blend_equation_alpha = state_to.blend_equation_alpha
  417.  
  418.       gl.blendEquationSeparate(state_current.blend_equation_rgb,
  419.                                state_current.blend_equation_alpha);
  420.     }
  421.  
  422.     //blendFuncSeparate
  423.     if(state_to.blend &&
  424.        (state_current.blend_src_rgb != state_to.blend_src_rgb ||
  425.         state_current.blend_dst_rgb != state_to.blend_dst_rgb ||
  426.         state_current.blend_src_alpha != state_to.blend_src_alpha ||
  427.         state_current.blend_dst_alpha != state_to.blend_dst_alpha)) {
  428.  
  429.       state_current.blend_src_rgb = state_to.blend_src_rgb;
  430.       state_current.blend_dst_rgb = state_to.blend_dst_rgb;
  431.       state_current.blend_src_alpha = state_to.blend_src_alpha;
  432.       state_current.blend_dst_alpha = state_to.blend_dst_alpha;
  433.  
  434.       gl.blendFuncSeparate(state_current.blend_src_rgb,
  435.                            state_current.blend_dst_rgb,
  436.                            state_current.blend_src_alpha,
  437.                            state_current.blend_dst_alpha);
  438.     }
  439.  
  440.     //colorMask
  441.     if(state_current.color_writemask[0] != state_to.color_writemask[0] ||
  442.        state_current.color_writemask[1] != state_to.color_writemask[1] ||
  443.        state_current.color_writemask[2] != state_to.color_writemask[2] ||
  444.        state_current.color_writemask[3] != state_to.color_writemask[3]) {
  445.  
  446.       state_current.color_writemask[0] = state_to.color_writemask[0];
  447.       state_current.color_writemask[1] = state_to.color_writemask[1];
  448.       state_current.color_writemask[2] = state_to.color_writemask[2];
  449.       state_current.color_writemask[3] = state_to.color_writemask[3];
  450.  
  451.       gl.colorMask(state_current.color_writemask[0],
  452.                    state_current.color_writemask[1],
  453.                    state_current.color_writemask[2],
  454.                    state_current.color_writemask[3]);
  455.     }
  456.  
  457.     //stencilFuncSeparate front
  458.     if(//state_to.stencil_test &&
  459.        (state_current.stencil_func != state_to.stencil_func ||
  460.         state_current.stencil_ref != state_to.stencil_ref ||
  461.         state_current.stencil_value_mask != state_to.stencil_value_mask)) {
  462.  
  463.       state_current.stencil_func = state_to.stencil_func;
  464.       state_current.stencil_ref = state_to.stencil_ref;
  465.       state_current.stencil_value_mask = state_to.stencil_value_mask;
  466.  
  467.       gl.stencilFuncSeparate(gl.FRONT,
  468.                              state_current.stencil_func,
  469.                              state_current.stencil_ref,
  470.                              state_current.stencil_value_mask);
  471.     }
  472.  
  473.     //stencilFuncSeparate back
  474.     if(//state_to.stencil_test &&
  475.        (state_current.stencil_back_func != state_to.stencil_back_func ||
  476.         state_current.stencil_back_ref != state_to.stencil_back_ref ||
  477.         state_current.stencil_back_value_mask != state_to.stencil_back_value_mask)) {
  478.  
  479.       state_current.stencil_back_func = state_to.stencil_back_func;
  480.       state_current.stencil_back_ref = state_to.stencil_back_ref;
  481.       state_current.stencil_back_value_mask = state_to.stencil_back_value_mask;
  482.  
  483.       gl.stencilFuncSeparate(gl.BACK,
  484.                              state_current.stencil_back_func,
  485.                              state_current.stencil_back_ref,
  486.                              state_current.stencil_back_value_mask);
  487.     }
  488.  
  489.     //stencilMaskSeparate front
  490.     if(//state_to.stencil_test &&
  491.       state_current.stencil_writemask != state_to.stencil_writemask) {
  492.       state_current.stencil_writemask = state_to.stencil_writemask;
  493.       gl.stencilMaskSeparate(gl.FRONT,state_current.stencil_writemask);
  494.     }
  495.  
  496.     //stencilMaskSeparate back
  497.     if(//state_to.stencil_test &&
  498.       state_current.stencil_back_writemask != state_to.stencil_back_writemask) {
  499.       state_current.stencil_back_writemask = state_to.stencil_back_writemask;
  500.       gl.stencilMaskSeparate(gl.BACK,state_current.stencil_back_writemask);
  501.     }
  502.  
  503.     //stencilOpSeparate front
  504.     if(//state_to.stencil_test &&
  505.        (state_current.stencil_fail != state_to.stencil_fail ||
  506.         state_current.stencil_pass_depth_fail != state_to.stencil_pass_depth_fail ||
  507.         state_current.stencil_pass_depth_pass != state_to.stencil_pass_depth_pass)) {
  508.  
  509.       state_current.stencil_fail = state_to.stencil_fail;
  510.       state_current.stencil_pass_depth_fail = state_to.stencil_pass_depth_fail;
  511.       state_current.stencil_pass_depth_pass = state_to.stencil_pass_depth_pass;
  512.  
  513.       gl.stencilOpSeparate(gl.FRONT,
  514.                            state_current.stencil_fail,
  515.                            state_current.stencil_pass_depth_fail,
  516.                            state_current.stencil_pass_depth_pass);
  517.     }
  518.  
  519.     //stencilOpSeparate back
  520.     if(//state_to.stencil_test &&
  521.        (state_current.stencil_back_fail != state_to.stencil_back_fail ||
  522.         state_current.stencil_back_pass_depth_fail != state_to.stencil_back_pass_depth_fail ||
  523.         state_current.stencil_back_pass_depth_pass != state_to.stencil_back_pass_depth_pass)) {
  524.  
  525.       state_current.stencil_back_fail = state_to.stencil_back_fail;
  526.       state_current.stencil_back_pass_depth_fail = state_to.stencil_back_pass_depth_fail;
  527.       state_current.stencil_back_pass_depth_pass = state_to.stencil_back_pass_depth_pass;
  528.  
  529.       gl.stencilOpSeparate(gl.BACK,
  530.                            state_current.stencil_back_fail,
  531.                            state_current.stencil_back_pass_depth_fail,
  532.                            state_current.stencil_back_pass_depth_pass);
  533.     }
  534.  
  535.     //cullFace
  536.     if(state_to.cull_face &&
  537.        state_current.cull_face_mode != state_to.cull_face_mode) {
  538.       state_current.cull_face_mode=state_to.cull_face_mode;
  539.       gl.cullFace(state_current.cull_face_mode);
  540.     }
  541.  
  542.     //frontFace
  543.     if(state_current.front_face != state_to.front_face) {
  544.       state_current.front_face = state_to.front_face;
  545.       gl.frontFace(state_current.front_face);
  546.     }
  547.  
  548.     //polygonOffset
  549.     if(state_to.polygon_offset_fill &&
  550.        (state_current.polygon_offset_factor != state_to.polygon_offset_factor ||
  551.         state_current.polygon_offset_units != state_to.polygon_offset_units)) {
  552.  
  553.       state_current.polygon_offset_factor = state_to.polygon_offset_factor;
  554.       state_current.polygon_offset_units = state_to.polygon_offset_units;
  555.  
  556.       gl.polygonOffset(state_current.polygon_offset_factor,state_current.polygon_offset_units);
  557.     }
  558.  
  559.     //depthFunc
  560.     if(//state_to.depth_test &&
  561.        state_current.depth_func != state_to.depth_func) {
  562.       state_current.depth_func=state_to.depth_func;
  563.       gl.depthFunc(state_current.depth_func);
  564.     }
  565.  
  566.     //depthMask
  567.     if(//state_to.depth_test &&
  568.       state_current.depth_writemask != state_to.depth_writemask) {
  569.       state_current.depth_writemask = state_to.depth_writemask;
  570.       gl.depthMask(state_current.depth_writemask);
  571.     }
  572.  
  573.     //depthRange
  574.     if(//state_to.depth_test &&
  575.        (state_current.depth_range[0] != state_to.depth_range[0] ||
  576.         state_current.depth_range[1] != state_to.depth_range[1])) {
  577.  
  578.       state_current.depth_range[0] = state_to.depth_range[0];
  579.       state_current.depth_range[1] = state_to.depth_range[1];
  580.  
  581.       gl.depthRange(state_current.depth_range[0],
  582.                     state_current.depth_range[1]);
  583.     }
  584.  
  585.     //scissor
  586.     if(state_to.scissor_test &&
  587.        (state_current.scissor_box[0] != state_to.scissor_box[0] ||
  588.         state_current.scissor_box[1] != state_to.scissor_box[1] ||
  589.         state_current.scissor_box[2] != state_to.scissor_box[2] ||
  590.         state_current.scissor_box[3] != state_to.scissor_box[3])) {
  591.  
  592.       state_current.scissor_box[0] = state_to.scissor_box[0];
  593.       state_current.scissor_box[1] = state_to.scissor_box[1];
  594.       state_current.scissor_box[2] = state_to.scissor_box[2];
  595.       state_current.scissor_box[3] = state_to.scissor_box[3];
  596.  
  597.       gl.scissor(state_current.scissor_box[0],
  598.                  state_current.scissor_box[1],
  599.                  state_current.scissor_box[2],
  600.                  state_current.scissor_box[3]);
  601.     }
  602.  
  603.     //lineWidth
  604.     if(state_current.line_width != state_to.line_width) {
  605.       state_current.line_width = state_to.line_width;
  606.  
  607.       gl.lineWidth(state_current.line_width);
  608.     }
  609.   }
  610.  
  611.   //
  612.   function state_func_all_apply(s,clear) {
  613.     if(clear) {
  614.       state_clear();
  615.     }
  616.    
  617.     //todo: dither, scissor ...
  618.  
  619.     function enums(x) {return x;}
  620.    
  621.     //cull_face
  622.     if(s.cull_face!=null) {
  623.       state_to.cull_face=s.cull_face;
  624.     }
  625.  
  626.     //blend
  627.     if(s.blend!=null) {
  628.       state_to.blend=s.blend;
  629.     }
  630.  
  631.     //stencil_test
  632.     if(s.stencil_test!=null) {
  633.       state_to.stencil_test=s.stencil_test;
  634.     }
  635.  
  636.     //depth_test
  637.     if(s.depth_test!=null) {
  638.       state_to.depth_test=s.depth_test;
  639.     }
  640.  
  641.     //polygon_offset_fill
  642.     if(s.polygon_offset_fill!=null) {
  643.       state_to.polygon_offset_fill=s.polygon_offset_fill;
  644.     }
  645.    
  646.     //blend_color
  647.     if(s.blend_color!=null) {
  648.       state_to.blend_color[0]=s.blend_color[0];
  649.       state_to.blend_color[1]=s.blend_color[1];
  650.       state_to.blend_color[2]=s.blend_color[2];
  651.       state_to.blend_color[3]=s.blend_color[3];
  652.     }
  653.    
  654.     //blend_color_red
  655.     if(s.blend_color_red!=null) {
  656.       state_to.blend_color[0]=s.blend_color_red;
  657.     }
  658.  
  659.     //blend_color_red
  660.     if(s.blend_color_green!=null) {
  661.       state_to.blend_color[1]=s.blend_color_green;
  662.     }
  663.  
  664.     //blend_color_blue
  665.     if(s.blend_color_blue!=null) {
  666.       state_to.blend_color[2]=s.blend_color_blue;
  667.     }
  668.  
  669.     //blend_color_alpha
  670.     if(s.blend_color_alpha!=null) {
  671.       state_to.blend_color[3]=s.blend_color_alpha;
  672.     }
  673.    
  674.     //blend_equation
  675.     if(s.blend_equation!=null) {
  676.       state_to.blend_equation_rgb=enums(s.blend_equation);
  677.       state_to.blend_equation_alpha=enums(s.blend_equation);
  678.     }
  679.  
  680.     //blend_equation_rgb
  681.     if(s.blend_equation_rgb!=null) {
  682.       state_to.blend_equation_rgb=enums(s.blend_equation_rgb);
  683.     }
  684.  
  685.     //blend_equation_alpha
  686.     if(s.blend_equation_alpha!=null) {
  687.       state_to.blend_equation_alpha=enums(s.blend_equation_alpha);
  688.     }
  689.    
  690.     //blend_src
  691.     if(s.blend_src!=null) {
  692.       state_to.blend_src_rgb=enums(s.blend_src);
  693.       state_to.blend_src_alpha=enums(s.blend_src);
  694.     }
  695.  
  696.     //blend_src_rgb
  697.     if(s.blend_src_rgb!=null) {
  698.       state_to.blend_src_rgb=enums(s.blend_src_rgb);
  699.     }
  700.  
  701.     //blend_src_alpha
  702.     if(s.blend_src_alpha!=null) {
  703.       state_to.blend_src_alpha=enums(s.blend_src_alpha);
  704.     }
  705.  
  706.     //blend_dst
  707.     if(s.blend_src!=null) {
  708.       state_to.blend_dst_rgb=enums(s.blend_dst);
  709.       state_to.blend_dst_alpha=enums(s.blend_dst);
  710.     }
  711.    
  712.     //blend_dst_rgb
  713.     if(s.blend_dst_rgb!=null) {
  714.       state_to.blend_dst_rgb=enums(s.blend_dst_rgb);
  715.     }
  716.  
  717.     //blend_dst_alpha
  718.     if(s.blend_dst_alpha!=null) {
  719.       state_to.blend_dst_alpha=enums(s.blend_dst_alpha);
  720.     }
  721.    
  722.     //color_writemask
  723.     if(s.color_writemask!=null) {
  724.       state_to.color_writemask[0]=s.color_writemask[0];
  725.       state_to.color_writemask[1]=s.color_writemask[1];
  726.       state_to.color_writemask[2]=s.color_writemask[2];
  727.       state_to.color_writemask[3]=s.color_writemask[3];
  728.     }
  729.    
  730.     //color_writemask_red
  731.     if(s.color_writemask_red!=null) {
  732.       state_to.color_writemask[0]=s.color_writemask_red;
  733.     }
  734.    
  735.     //color_writemask_green
  736.     if(s.color_writemask_green!=null) {
  737.       state_to.color_writemask[1]=s.color_writemask_green;
  738.     }
  739.    
  740.     //color_writemask_blue
  741.     if(s.color_writemask_blue!=null) {
  742.       state_to.color_writemask[2]=s.color_writemask_blue;
  743.     }
  744.  
  745.     //color_writemask_alpha
  746.     if(s.color_writemask_alpha!=null) {
  747.       state_to.color_writemask[3]=s.color_writemask_alpha;
  748.     }
  749.  
  750.     //cull_face_mode
  751.     if(s.cull_face_mode!=null) {
  752.       state_to.cull_face_mode=enums(s.cull_face_mode);
  753.     }
  754.  
  755.     //depth_func
  756.     if(s.depth_func!=null) {
  757.       state_to.depth_func=enums(s.depth_func);
  758.     }
  759.  
  760.     //depth_func
  761.     if(s.depth_writemask!=null) {
  762.       state_to.depth_writemask=s.depth_writemask;
  763.     }
  764.    
  765.     //depth_range
  766.     if(s.depth_range!=null) {
  767.       state_to.depth_range[0]=s.depth_range[0];
  768.       state_to.depth_range[1]=s.depth_range[1];
  769.     }
  770.  
  771.     //depth_range_znear
  772.     if(s.depth_range_znear!=null) {
  773.       state_to.depth_range[0]=s.depth_range_znear;
  774.     }
  775.  
  776.     //depth_func
  777.     if(s.depth_range_zfar!=null) {
  778.       state_to.depth_range[1]=s.depth_range_zfar;
  779.     }
  780.  
  781.     //front_face
  782.     if(s.front_face!=null) {
  783.       state_to.front_face=enums(s.front_face);
  784.     }
  785.  
  786.     //polygon_offset_factor
  787.     if(s.polygon_offset_factor!=null) {
  788.       state_to.polygon_offset_factor=s.polygon_offset_factor;
  789.     }
  790.  
  791.     //polygon_offset_units
  792.     if(s.polygon_offset_units!=null) {
  793.       state_to.polygon_offset_units=s.polygon_offset_units;
  794.     }
  795.  
  796.     //stencil_func
  797.     if(s.stencil_func!=null) {
  798.       state_to.stencil_back_func=enums(s.stencil_func);
  799.       state_to.stencil_func=enums(s.stencil_func);
  800.     }
  801.    
  802.     //stencil_back_func
  803.     if(s.stencil_back_func!=null) {
  804.       state_to.stencil_back_func=enums(s.stencil_back_func);
  805.     }
  806.  
  807.     //stencil_front_func
  808.     if(s.stencil_front_func!=null) {
  809.       state_to.stencil_func=enums(s.stencil_front_func);
  810.     }
  811.  
  812.     //stencil_ref
  813.     if(s.stencil_ref!=null) {
  814.       state_to.stencil_back_ref=s.stencil_ref;
  815.       state_to.stencil_ref=s.stencil_ref;
  816.     }
  817.    
  818.     //stencil_back_ref
  819.     if(s.stencil_back_ref!=null) {
  820.       state_to.stencil_back_ref=s.stencil_back_ref;
  821.     }
  822.    
  823.     //stencil_front_ref
  824.     if(s.stencil_front_ref!=null) {
  825.       state_to.stencil_ref=s.stencil_front_ref;
  826.     }
  827.  
  828.     //stencil_value_mask
  829.     if(s.stencil_value_mask!=null) {
  830.       state_to.stencil_back_value_mask=s.stencil_value_mask;
  831.       state_to.stencil_value_mask=s.stencil_value_mask;
  832.     }
  833.    
  834.     //stencil_back_value_mask
  835.     if(s.stencil_back_value_mask!=null) {
  836.       state_to.stencil_back_value_mask=s.stencil_back_value_mask;
  837.     }
  838.    
  839.     //stencil_front_value_mask
  840.     if(s.stencil_front_value_mask!=null) {
  841.       state_to.stencil_value_mask=s.stencil_front_value_mask;
  842.     }
  843.    
  844.     //stencil_writemask
  845.     if(s.stencil_writemask!=null) {
  846.       state_to.stencil_back_writemask=s.stencil_writemask;
  847.       state_to.stencil_writemask=s.stencil_writemask;
  848.     }
  849.    
  850.     //stencil_back_writemask
  851.     if(s.stencil_back_writemask!=null) {
  852.       state_to.stencil_back_writemask=s.stencil_back_writemask;
  853.     }
  854.  
  855.     //stencil_front_writemask
  856.     if(s.stencil_writemask!=null) {
  857.       state_to.stencil_writemask=s.stencil_front_writemask;
  858.     }
  859.    
  860.     //stencil_pass_depth_fail
  861.     if(s.stencil_pass_depth_fail!=null) {
  862.       state_to.stencil_back_pass_depth_fail=enums(s.stencil_pass_depth_fail);
  863.       state_to.stencil_pass_depth_fail=enums(s.stencil_pass_depth_fail);
  864.     }
  865.        
  866.     //stencil_back_pass_depth_fail
  867.     if(s.stencil_back_pass_depth_fail!=null) {
  868.       state_to.stencil_back_pass_depth_fail=enums(s.stencil_back_pass_depth_fail);
  869.     }
  870.  
  871.     //stencil_front_pass_depth_fail
  872.     if(s.stencil_front_pass_depth_fail!=null) {
  873.       state_to.stencil_pass_depth_fail=enums(s.stencil_front_pass_depth_fail);
  874.     }
  875.  
  876.     //stencil_pass_depth_pass
  877.     if(s.stencil_pass_depth_pass!=null) {
  878.       state_to.stencil_back_pass_depth_pass=enums(s.stencil_pass_depth_pass);
  879.       state_to.stencil_pass_depth_pass=enums(s.stencil_pass_depth_pass);
  880.     }
  881.    
  882.     //stencil_back_pass_depth_pass
  883.     if(s.stencil_back_pass_depth_pass!=null) {
  884.       state_to.stencil_back_pass_depth_pass=enums(s.stencil_back_pass_depth_pass);
  885.     }
  886.    
  887.     //stencil_front_pass_depth_pass
  888.     if(s.stencil_front_pass_depth_pass!=null) {
  889.       state_to.stencil_pass_depth_pass=enums(s.stencil_front_pass_depth_pass);
  890.     }
  891.  
  892.     //stencil_fail
  893.     if(s.stencil_fail!=null) {
  894.       state_to.stencil_back_fail=enums(s.stencil_fail);
  895.       state_to.stencil_fail=enums(s.stencil_fail);
  896.     }
  897.  
  898.     //stencil_back_fail
  899.     if(s.stencil_back_fail!=null) {
  900.       state_to.stencil_back_fail=enums(s.stencil_back_fail);
  901.     }
  902.  
  903.     //stencil_front_fail
  904.     if(s.stencil_front_fail!=null) {
  905.       state_to.stencil_fail=enums(s.stencil_front_fail);
  906.     }
  907.    
  908.     state_apply();
  909.   }
  910.  
  911.   //===========================================================================
  912.  
  913.   return {
  914.     "enable" : state_func_enable,
  915.     "disable" : state_func_disable,
  916.     "blendColor" : state_func_blendColor,
  917.     "blendEquation" : state_func_blendEquation,
  918.     "blendEquationSeparate" : state_func_blendEquationSeparate,
  919.     "blendFunc" : state_func_blendFunc,
  920.     "blendFuncSeparate" : state_func_blendFuncSeparate,
  921.     "colorMask" : state_func_colorMask,
  922.     "cullFace" : state_func_cullFace,
  923.     "depthFunc" : state_func_depthFunc,
  924.     "depthMask" : state_func_depthMask,
  925.     "depthRange" : state_func_depthRange,
  926.     "frontFace" : state_func_frontFace,
  927.     "polygonOffset" : state_func_polygonOffset,
  928.     "stencilFunc" : state_func_stencilFunc,
  929.     "stencilFuncSeparate" : state_func_stencilFuncSeparate,
  930.     "stencilMask" : state_func_stencilMask,
  931.     "stencilMaskSeparate" : state_func_stencilMaskSeparate,
  932.     "stencilOp" : state_func_stencilOp,
  933.     "stencilOpSeparate" : state_func_stencilOpSeparate,
  934.     "scissor" : state_func_scissor,
  935.     "lineWidth" : state_func_lineWidth,
  936.    
  937.     "syncStates" : state_sync,
  938.     "clearStates" : state_clear,
  939.     "applyStates" : state_apply,
  940.    
  941.     "states" : state_func_all_apply
  942.   };
  943. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement