ãšã³ããªãŒ
ç§ã¯æ°ãããã¯ãããžãŒãåŠã³ãä»ãŸã§ãã£ãããšã®ãªãããšãããã®ã奜ãã§ãã ç§ãããã³ã奜ãã§ãã ãšããã§ãäž¡æ¹ã®æ ç»ã åŠçæ代ã«åœŒããèŠãåã§ãããç§ã¯ã¢ã«ãã²ããã³ããã¬ã€ããã©ã€ããã€ã¯ã§ã®ã¬ãŒã¹ã®ãã¡ã³ã§ããã TRONïŒLegacyãèŠãŠãçªç¶ãã°ãªãããšå圢ã§ããã³ãäœããããšæããŸããã ããèããããšãªããç§ã¯ææã®Visual Studio Expressãç«ã¡äžããŠèããŸãã-ç§ã®ãã®äœæã¯ãç座ã®ã¯ããŒã³ã®ãã³ããšã©ãéãã®ã§ããããïŒ ã¹ã¿ãžãªã¯ã¹ã ãŒãºã«éãŸããç§ã®ç±æã¯ããèœã¡çããŸããã ãŸãã«ãWebGLã«é¢ããèšäºã«åºäŒã£ãç¬éãŸã§ã 圌ã®ç®ã¯åã³æãããªãã圌ã®æã¯ç·šéè ã«æã䌞ã°ããã ããããŒãã®ãªãã»ããäžã®ãã¿ã³ãã¯ãªãã¯ããããã®JavaScriptãã³ãã©ãŒãååäœæãããšããã©ããããããç§ã¯æãããããªãã£ãã
ãããã£ãŠãä»æ¥ã®ããã°ã©ã ã§ã¯ïŒ
- äœã¬ãã«WebGLããã°ã©ãã³ã°ã
- åçŽãª3次å ãªããžã§ã¯ãã®ã¬ã³ããªã³ã°ã
- éçºããã»ã¹ã«é¢ãã詳现ãªã³ã¡ã³ãã
- å€ãã®æåãšJavaScriptã³ãŒãã
- ç¡æã®é ãšçŽ æµãªé³æ¥œã
ãã®èšäºã¯ãäœãããããšã¯ãªããä»ã®äººãã©ã®ããã«æããå€ã®å€ªéœã®äžã§æ©ãã®ã§ã¯ãªããã³ã³ãã¥ãŒã¿ãŒã§æéãéãããã«ã€ããŠèªã¿ãã人ã察象ãšããŠããŸãã
ååŸããããã®ã®è©³çŽ°
çµæã¯HTMLããŒãžã«ãªããŸããããŒããããšããŠãŒã¶ãŒã¯ã°ãªããã®ã©ããã«ããç¡éå¹³é¢ã«ããã©ã€ããµã€ã¯ã«ãå¶åŸ¡ã§ããŸãã å ã®ãµã€ã¯ã«ã¯ãå éãæžéãã¹ã ãŒãºãªæ¹å転æããããŠå ã®å£ãæ®ãããšãã§ããªããã°ãªããŸããã ç§ã¯ããŸã§ã¯ãªãã®ã§ããã¢ã¯1ã€ã®ãã¡ã€ã«ã§æ§æãããŸããã ããŒã¯ã¢ãããã¹ã¯ãªãããã·ã§ãŒããŒãã¢ãã«-ãã¹ãŠããã£ã¬ã¯ããªã«é 眮ãããŸãã 1ã€ã®å°ããªCSSã»ã¬ã¯ã¿ãŒã§ãããã¡ã€ã«å šäœãéžæãããŸãã å°çšã®ãµãŒããŒãæã£ãŠããªãããããã¢ã¯ãã¡ã€ã«ãã¹ãã£ã³ã°ãµãŒãã¹ãéããŠã¢ãŒã«ã€ãã§é åžãããŸãã
建ç¯
ãã®èšäºã§ã¯ãã¬ã³ããªã³ã°ã«æ¢è£œã®ã©ã€ãã©ãªãŒã䜿çšããŸããããªããªããnefigã ããã§ãã ããããçå£ã«ãåãtdl.jsãå¹æçã«äœ¿çšããããã«ã¯ãããããèšèªã®ããããã©ã€ãã©ãªãããããç®çã§äœ¿çšããããã«ãæåã«äžäœã¬ãã«ã§ã®åäœãç解ããå¿ èŠããããŸãã ãããããã®èšäºã¯ããµãŒãããŒãã£ã®ã©ã€ãã©ãªã䜿çšããã«äžäººã®åã§æçµçã«ãããããããªåçŽãªãã¢ã§ãããçæ§ã®æ¿ããè ããããšãæ確ã«ç€ºããŸãã
ããžãã¯ã¯1ã€ã®ãã¡ã€ã«ã«ãªããŸãã ãã®äžã«ããªãœãŒã¹ã®èªã¿èŸŒã¿ãã·ã§ãŒããŒã®ã³ã³ãã€ã«ãã¡ã€ã³ã«ãŒããã·ãŒã³ã¬ã³ããªã³ã°æ©èœãå«ããšã³ãžã³åæåã³ãŒããé 眮ããŸãã å®éã«ã¯ã«ãŒãã§ã¯ãªãã requestAnimateFrameã䜿çšããã³ãŒã«ããã¯ã§ããã¡ã€ã³ã«ãŒããããèšç®ãšã¬ã³ããªã³ã°ãé çªã«åŒã³åºãããŸãïŒã¬ã³ããªã³ã°ãšèšç®ã®ã©ã¡ãã§ããã»ãšãã©éãã¯ãããŸãããã»ãšãã©;ïŒã
ã¬ã³ããªã³ã°ã«ã¯ããã€ãã®æé ãå«ãŸããŸãã
- ç°å¢ãã€ãŸããç§ãã¡ãé転ããåºãšããªãã·ã§ã³ã§ç§ãã¡ã®äžã«ããã£ãŠãã空ã®ã¬ã³ããªã³ã°ã
- ã©ã€ããµã€ã¯ã«ã®ã¬ã³ããªã³ã°ãããã«ã¯ãç°ãªããããªã¢ã«ã®ããŒããã¬ã³ããªã³ã°ããããã®ããã€ãã®ãµãã¹ããããå«ãŸããŸãã
- ã©ã€ããµã€ã¯ã«ã«éããå ã®ã¹ããªããã®ã¬ã³ããªã³ã°ã
äžåºŠã«ãã¹ãŠãæããŠã¿ãŸãããïŒ OpenGLãã€ãã©ã€ã³ã®æ©èœã®ããã 詳现ã¯ä»¥äžãã芧ãã ããã
ããã«åºã¥ããŠæ§ç¯ããŸãã 詳现ã«èª¬æããã®ã¯æå³ããããŸãããå®è£ ã«ã€ããŠèª¬æãããšããæ¥ãŸããã
äžè¬çãªWebGLãšã¯äœã§ããïŒ
ãã®ãµã€ãã§ã¯åå¿è ã«åœ¹ç«ã€å€ãã®æ å ±ãèŠã€ãããŸã ã ãããã ãã®ãªã³ã¯ã¯WebGLæ©èœã«é¢ãããã³ãã®ããããŒãžã«ã€ãªãããŸãã
äžèšã§èšãã°ãWebGLã¯JavaScriptçšã®OpenGL ES 2.0ãã€ã³ãã£ã³ã°ã®ã»ããã§ãã ãã®æè¡ã¯çŸåšã掻çºã«éçºãããŠããããããŸã å¥å šã§å®å šãªããã¥ã¡ã³ãã¯ãããŸããã ããããæ奜家ã¯åãšã¡ã€ã³ã§ããã䜿çšããŠããŸãã
䜿ãæ¹ã¯ãšãŠãç°¡åã§ãã
<canvas />
ã¿ã°ãHTMLããã¥ã¡ã³ãã®æ¬æã«ãããããã
canvas.getContext("experimental-webgl")
èŠçŽ
canvas.getContext("experimental-webgl")
ã®ã¡ãœãããå®è¡ããã ãã§ãWebGLã®ã¬ã³ããªã³ã°ã«äœ¿çšããããªããžã§ã¯ããååŸã§ããŸãã ã³ãŒããæžãå§ããŸãããã
ãšã³ãžã³ã®åæå
ãã¹ãŠã¯ãããŒãžã®ããŒãããå§ãŸããŸãã ãããæãç°¡åãªæ¹æ³ã§ãããããjQueryã䜿çšããŠãã®ã€ãã³ããžã®ã³ãŒã«ããã¯ãèšå®ããŸãã ã³ãŒã«ããã¯ã§ã¯ããªãœãŒã¹ãããŒãããGLã³ã³ããã¹ããååŸããŠãšã³ãžã³ãèµ·åããæ©èœãåŒã³åºããŸãã
$(function() { loadResources(); var gl = $("#viewport")[0].getContext("experimental-webgl"); engineStartup(gl); });
次ã«ããã¢ã§äœ¿çšããã·ã§ãŒããŒããã°ã©ã ãã³ã³ãã€ã«ããŠãã«ãããå¿ èŠããããŸãã ãããè¡ãé¢æ°ã¯æ¬¡ã®ãšããã§ãã
function buildShaders(gl, count) { var shaders = []; for (var i = 0; i < 1; i++) { shaders[i] = composeProgram(gl, localStorage.getItem("step " + i + " vertex shader"), localStorage.getItem("step " + i + " fragment shader")); } return shaders; } // http://www.guciek.net/webgl_shortest/en function composeProgram(gl, vertex_shader, fragment_shader) { var program = gl.createProgram(); var addShader = function(type, source) { var shader = gl.createShader(type); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { throw "Could not compile " + (type == gl.VERTEX_SHADER ? "vertex" : "fragment") + " shader:\n\n" + gl.getShaderInfoLog(shader); } gl.attachShader(program, shader); }; addShader(gl.VERTEX_SHADER, vertex_shader); addShader(gl.FRAGMENT_SHADER, fragment_shader); gl.linkProgram(program); if (!gl.getProgramParameter(program, gl.LINK_STATUS)) { throw "Could not link the shader program."; } return program; }
ã¯ããã¯ããããŒã«ã«ãã©ãŠã¶ã¹ãã¬ãŒãžã䜿çšããŠããŒã«ã«ãã·ã³ãããã¡ã€ã«ãããŠã³ããŒãããŸãã ããã§ç§ã¯ãã®ãããªå€æ ã§ãã ã¡ãªã¿ã«ããæ°ã«å ¥ãã®Chromeã§ãããèš±å¯ããã«ã¯ã次ã®ããã«å®è¡ããå¿ èŠããããŸãã
"chrome --allow-file-access-from-files"
ã
ããããã³ãŒããšãããäœãæå³ããã®ãã«ã€ããŠã¯ãã話ããŠãã ããã æåã®é¢æ°ã¯ã誰ã«ãèå³ãèµ·ãããªããšæããŸã-ã·ã§ãŒããŒããã°ã©ã ã®é åãã³ã³ãã€ã«ãããŸãã ãããã2çªç®ã®é¢æ°ã§çºçããã·ã§ãŒããŒããã°ã©ã ã®äœæã¯ãã¯ããã«èå³æ·±ããã®ã§ãã æåã«ãWebGLã§ã·ã§ãŒããŒãäœã§ããããç解ããå¿ èŠããããŸãã
ã·ã§ãŒããŒã¯ãã®ãããªãã®ã§ã...
ã·ã§ãŒããŒã¯ããã¬ãŒã åŠçäžã«GPUã§å®è¡ãããããã°ã©ã ã§ãã OpenGL ES 2.0ã«ã¯ãé ç¹ãšãã¯ã»ã«ïŒããããé ç¹ãšãã©ã°ã¡ã³ãïŒã®2çš®é¡ã®ã·ã§ãŒããŒããããŸãã ããã§ã¯ãOpenGLãã€ãã©ã€ã³ã§ã®æäœã®é åºã«ã€ããŠè©³ãã説æããŸãããé ç¹ã·ã§ãŒããŒã¯ãã¯ã»ã«ã·ã§ãŒããŒãããæ©ãå®è¡ããããã€ãã©ã€ã³å ã®ãã¹ãŠã®é ç¹ã§åäœããããšãç¥ãå¿ èŠããããŸãã ãã¯ã»ã«ã·ã§ãŒããŒã¯ããã€ãã©ã€ã³ã®åãã¯ã»ã«ã®ãã¬ãŒã ãç»é¢ã«è¡šç€ºãããã»ãŒåã«å®è¡ãããé ç¹ã·ã§ãŒããŒã«ãã£ãŠéä¿¡ãããããŒã¿ã䜿çšã§ããŸãã ããã2ã€ã®ã·ã§ãŒããŒã®çµã¿åããã¯ãã·ã§ãŒããŒããã°ã©ã ãšåŒã°ããŸãã åæã«ãã°ã©ãã£ãã¯ããã»ããµã¯1ã€ã®ã·ã§ãŒããŒããã°ã©ã ã®ã¿ãå®è¡ã§ããŸãããããã¯ãã¹ãŠã«ã€ããŠ2ã€ã®ã·ã§ãŒããŒã«å¶éãããããšãæå³ãããã®ã§ã¯ãããŸããã ã·ã§ãŒããŒããã°ã©ã ã¯ã¬ã³ããªã³ã°äžã«åãæ¿ããããšãã§ããåŸç¶ã®ãã¹ãŠã®ããªããã£ãã®åŠçããžãã¯ãå€æŽã§ããŸãã ãããè¿ éã«è¡ãã«ã¯ã䜿çšãããã¹ãŠã®ã·ã§ãŒããŒãæåã«ã³ã³ãã€ã«ãããããããã·ã§ãŒããŒããã°ã©ã ããã«ãããå¿ èŠããããŸãã ããã°ã©ã ãä¿åããããã«ãé åãããè¯ããã®ãèŠã€ããããšãã§ããŸããã§ããã é åã€ã³ããã¯ã¹ã¯ãã·ã§ãŒããŒããã°ã©ã ã䜿çšããã¬ã³ããªã³ã°ã¹ãããã«å¯Ÿå¿ããŸãã ã·ã§ãŒããŒèªäœã®äœæã¯ãã°ãã延æããŸãããããã§ã¯ãœãŒã¹ã³ãŒãããã·ã§ãŒããŒããã°ã©ã ãäœæããæ©èœã«ã€ããŠè©³ããèŠãŠãããŸãã
ãŸãã
createProgram()
é¢æ°ãåŒã³åºãããã·ã§ãŒããŒããã°ã©ã ãäœæããããšãGLã«
createProgram()
ãŸãã 次ã«ããã®ããã°ã©ã ã«é ç¹ã·ã§ãŒããŒãšãã¯ã»ã«ã·ã§ãŒããŒãè¿œå ããŸãã è¿œå ã¯4ã€ã®ã¹ãããã§è¡ãããŸãã ãŸãã
gl.createShader()
ã䜿çšããŠã·ã§ãŒããŒã®ãªããžã§ã¯ããäœæãã次ã«
shaderSource()
é¢æ°
shaderSource()
ãœãŒã¹ã³ãŒãã
shaderSource()
ããŸãããã®åŸã
compileShader()
ã³ã³ãã€ã«ãããã³ã³ãã€ã«ãããã·ã§ãŒããŒã
attachShader()
ããã°ã©ã ã«è¿œå ãããŸãã 2ã€ã®é¢æ°åŒã³åºããš1ã€ã®ã·ã§ãŒããŒããã°ã©ã ã«ã¯ãããã«äœ¿çšã§ãã2ã€ã®ã·ã§ãŒããŒãå«ãŸããŠããŸãã ããã§ãããã°ã©ã ã¯
linkProgram()
ã䜿çšããŠ
linkProgram()
ããå¿ èŠããããŸã-ãããŠãããã䜿çšã§ããŸãã
ããã§ã¯ããªãœãŒã¹ã®èªã¿èŸŒã¿é¢æ°ãšããã€ãã®è£å©çãªæ®»ã瀺ããŸãã-ããã¯æåºãšããŠéå±ã§ãã å ã«é²ãã§ãã ããã
ã·ã§ãŒããŒãæžã
ã·ã§ãŒããŒã«é¢ããåºæ¬çãªçè«çæ å ±ã¯ãã§ã«æã£ãŠããã®ã§ãããã§ã¯ã·ã§ãŒããŒèªäœã®èšè¿°ã«ã€ããŠèª¬æããŸãã
ã·ã§ãŒããŒã¯Cã«äŒŒãèšèªã§æžãããŠããŸãã ããæ£ç¢ºã«ã¯ã2ã€ã®éåžžã«ãã䌌ãèšèª-é ç¹çšãšãã¯ã»ã«ã·ã§ãŒããŒçšã§ãã ãããã GLSLã®ä»æ§ãèªãããšãã§ããŸã ã ã¡ã€ã³ããã°ã©ã ã¯ãåäžå€æ°ã䜿çšããŠãã©ã¡ãŒã¿ãŒãã·ã§ãŒããŒã«æž¡ãããšãã§ããŸãã ãããã®å€æ°ã®äž»ãªæ©èœã¯ãããªããã£ãåŠçäžã«å€ãå€æŽã§ããªãããšã§ããããã«ããããããã®å€æ°ã¯ãã¡ã€ã³ããã°ã©ã ãšã·ã§ãŒããŒããã°ã©ã éã®äž»ãªéä¿¡æ¹æ³ã«ãªããŸãã é ç¹ã·ã§ãŒããŒã¯ãã¡ã€ã³ããã°ã©ã ã®åé ç¹ã«èšå®ãããå±æ§å€æ°ãåãå ¥ããããšãã§ããŸãã é ç¹ã·ã§ãŒããŒãšãã¯ã»ã«ã·ã§ãŒããŒéã®éä¿¡ã«ã¯ãé ç¹ã·ã§ãŒããŒã«ãã£ãŠåæåãããåŠçãããããªããã£ãå šäœã®é åã§è£éãããããŸããŸãªå€æ°ã䜿çšãããŸããè£éãããå€ã¯ãã¯ã»ã«ã·ã§ãŒããŒã§äœ¿çšã§ããŸãã
ããšãã°ã3çš®é¡ãã¹ãŠã®å€æ°ã䜿çšããã·ã³ãã«ãªã·ã§ãŒããŒããã°ã©ã ã瀺ããŸãã
// Vertex shader attribute vec3 aVertexPosition; attribute vec4 aVertexColor; uniform mat4 uMVMatrix; uniform mat4 uPMatrix; varying vec4 vColor; void main() { gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0); vColor = aVertexColor; } // Fragment shader #ifdef GL_ES precision highp float; #endif varying vec4 vColor; void main() { gl_FragColor = vColor; }
çµæã¯äºæž¬å¯èœã§ã-åŸé ã®ããããªããã£ãã åŠçããã»ã¹ã詳ããèŠãŠã¿ãŸãããã
ã·ã§ãŒããŒã®ããŒã¿åãšå€æ°
äžèŠãããšãå€æ°ã«å¥åŠãªããŒã¿åããããŸãã ã·ã§ãŒããŒèšèªã«ã¯å€ãã®åºæ¬çãªã¿ã€ãããããŸã-ããŒã«ãæŽæ°ãæµ®åå°æ°ç¹ãããã€ãã®æ¬¡å ã®ãã¯ãã«ãšãããªãã¯ã¹ããã¯ã¹ãã£ãã³ãã©ãŒïŒå éšäœ¿çšã®ã¿ïŒãæ§é ãšé åã ãããŸã§ã®ãšããç§ãã¡ã«ãšã£ãŠèå³æ·±ãã®ã¯ãã¯ãã«ãšè¡åã®ã¿ã§ããããã³ãã©ãŒã¯ç¹ã«æçšã§ã¯ãããŸããã
ãã¯ãã«ã¯2ã3ããŸãã¯4æåã§ãåæåã¯æµ®åå°æ°ç¹æ°ã§ãã æåãš2çªç®ã®ã¿ã€ãã¯ãæ°åŠã®åŠæ ¡ã®ã³ãŒã¹ã§ããç¥ãããŠããŸãããæåŸã®ã¿ã€ãã¯ç§ãã¡ã«äžæè°ã«æãããŸã-ããã¯4次å 空éã§å®çŸ©ãããŠããŸããïŒ äžè¬ã«ã4次å 座æšã®åŠçããžãã¯ãã·ã§ãŒããŒã«é§åããå ŽåããããããããªããŸãã 3次å 空éã«é©çšãããšããã¯ãã«ã®4çªç®ã®ã³ã³ããŒãã³ãã¯é ç¹ã®æ·±ãã®å€ãèšå®ããŸãã ã·ãŒã³ã®æ·±ãã«ã€ããŠ-åŸã§ã äžè¬ã«ãã·ã§ãŒããŒã®ãã¯ãã«ã¯ã空éã®åº§æšã決å®ããããã ãã«äœ¿çšãããŸããã ãããã¯ãè²ã®å€ãæ³ç·ããã¯ã¹ãã£åº§æšãç幎ææ¥ãªã©ãããªãã®å¿ãæããã®ãä¿åããããã«äœ¿çšã§ããŸãã ããã«ãå€ãã·ã§ãŒããŒã«è»¢éããããã®ãã¯ãã«ã®éèŠæ§ã«æ³šç®ãã䟡å€ããããŸãããããã®å€ãéä¿¡ããããã®ãããã¡ãŒå ã®ã¡ã¢ãªã«ã¯ã4ã³ã³ããŒãã³ããã¯ãã«ã®åæ°ãå²ãåœãŠãããŸãã ãããã£ãŠã2-3-4ã®æµ®åå°æ°ç¹æ°ãã·ã§ãŒããŒã«è»¢éããå¿ èŠãããå Žåãæãçµæžçã§æ£ããæ¹æ³ã¯ããããã1ã€ã®ãã¯ãã«ã«ããã·ã¥ããããšã§ãã ããã«æ»ããŸãã
è¡åã¯ãã¯ããã«èå³æ·±ãããŒã¿åã§ãã ãããªãã¯ã¹èšç®ãGPUã«ãã£ãŠæ®éçãªèšç®ããã1æ¡é«éã«å®è¡ãããããšãè€æ°åèããããšãããã§ããããïŒ ããã ãã§ãã ãããªãã¯ã¹ãéåžžã«éäžçã«äœ¿çšããŸãã ãããªãã¯ã¹ã®æäœãšãå°ãäœãã¬ã³ããªã³ã°ãããšãã®å¹æã«ã€ããŠã®è©³çŽ°ã ããã§ãé ç¹ã·ã§ãŒããŒã§äœ¿çšããã2ã€ã®é ç¹ã·ã§ãŒããŒãããŒã¹ãšãã£ã¹ãã¬ã€ã¹ã¡ã³ãã§ããããšãç¥ãã ãã§æžã¿ãŸãã éèŠè¡åã¯ãã«ã¡ã©ã®äœçœ®ãšèŠéè§ã決å®ããŸãã å€äœãããªãã¯ã¹ã¯ãé ç¹ã移åããå Žæã瀺ããŸãã ã·ã§ãŒããŒã¯ããªããã£ãã«å¯ŸããŠå®è¡ããããããçµæãšããŠãããªããã£ãå šäœã移åãããªãã·ã§ã³ã§ä»»æã®è§åºŠã§å転ããŸãã
確ãã«ãå€æ°
gl_Position
ããã³
gl_FragColor
ã ã·ã§ãŒããŒã¯ãå®è¡çµæããããã®å€æ°ã«èšé²ããŸãã
gl_Position
ã¯ã3次å 空éã§ã®é ç¹ã®äœçœ®ã決å®ããŸãã
gl_FragColor
ã¯ãç»é¢ã«è¡šç€ºããããã¯ã»ã«ã®è²ãèšå®ããŸãã ãã¯ã»ã«ã·ã§ãŒããŒã«å ¥ãåã«ãããªããã£ãå šäœã§åäžãªå€æ°ãè£éãããããšãèŠããŠããŸããïŒ ãã®ãããããªããã£ãã«åŸé ãçããŸãã ãã¢ã§äœ¿çšãããã·ã§ãŒããŒã¯ãäžãããããã®ãããè€éãªäŸã§ã¯ãªãããããã¹ãŠãå€ããå°ãªããæ確ã§ããããšãé¡ã£ãŠããŸãã
ã¬ã³ããªã³ã°ããããªãã¯ã¹ããã¹ãŠãã¹ãŠãã¹ãŠ
é ç¹ã·ã§ãŒããŒãå®è¡ãããåã«ãGPUã¯ã«ã¡ã©ã«ã€ããŠãããã«é ç¹ã®äœçœ®ã«ã€ããŠãããäœãç¥ããªãããšãæ¢ã«ãæ°ã¥ããããããŸããã ãããã£ãŠã圌ã«ãããæããããšãã§ããããšã¯éåžžã«éèŠã§ãã åè¿°ã®ããã«ãã«ã¡ã©ã¯ãããªãã¯ã¹ã«ãã£ãŠèšå®ãããŸãã å¥ã®ãããªãã¯ã¹ã¯ã空éå ã®ãã€ã³ãã®åãã決å®ããŸãã ããã2ã€ã®è¡åã®åäœåçãç解ããããã«ãé ã®ããæ°åŠã«é£ã³èŸŒã¿ãŸãã
çè«
æåã«ãã·ãŒã³ã®æ圱ãã©ã®ããã«æ§ç¯ãããããç解ããå¿ èŠããããŸãã è±èªã®è©³çŽ°ã¯ãã¡ãã§ãã äž»ãªã¢ã€ãã¢ã¯ããããªãã¯ã¹æäœã䜿çšããŠã空éå ã®ãã€ã³ãã衚瀺é¢äžã®ãã€ã³ãã«æ圱ããããšã§ãïŒã³ã³ãã€ãŒã®ãã®éšåã¯ãGdã®æ å ãšç§ãã¡ããé ãããŠããŸãïŒã åç §ããŠèšäºã泚ææ·±ãèªãã ããªãã®äººã ã¯æ¿æããŸã-圌ãã¯èšãããªãã«ã¡ã©ã«ã€ããŠäœãèšãããŠããªãã®ã§ããïŒ å®éãOpenGLã®ã«ã¡ã©ã¯åžžã«1ã€ã®ãã€ã³ãïŒåç¹ïŒã«ãããZ軞ã®è² ã®éšåã«äœçœ®åãããããŠããŸããä»ã®å šå¡ãæ¢ã«ã³ã¡ã³ãã§èè ãå°ããªããªãŽã³ã«åŒãè£ãããã«æãæ²ããŠããã®ã§ãç§ã¯æ¥ãã§ããªãå°éã¯ããªãã®é ãåããããšãã§ããŸããïŒãã å®éãGPUã䜿çšãããšäœæ¥ãç°¡åã«ãªããŸãã3次å ã®ç¹ãæ圱ããããã®åŒãéåžžã«åçŽåããããã®çµæãå¿ èŠãªèšç®ã®æ°ãæžããŸãã ãŸãããããããã°ã©ãã£ãã¯ã¢ã¯ã»ã©ã¬ãŒã¿ã®ã¡ãŒã«ãŒã¯ããã°ã©ãã®ç掻ãåçŽåããã«ã¡ã©ã®å®è£ ã«å®å šãªèªç±ãäžããŸãã ã€ãŸããå°éã§ããŠã¹ãåŒã£åŒµããšãå®éã«å転ããã®ã¯ã«ã¡ã©ã§ã¯ãªããåç¹ãäžå¿ã«å転ããã·ãŒã³å šäœã§ãã 審åå®ã¯ãããæ°ã«å ¥ã£ãŠããã ããã ãããã£ãŠãé è¿æ³è¡åãšå€äœè¡åã®éã«éãã¯ãããŸãããã©ã¡ãã空éå ã®ãã€ã³ãã移åããŸãã ãããããã£ã¹ãã¬ã€ã¹ã¡ã³ããããªãã¯ã¹ã¯ç©ºéå ã®ãªããžã§ã¯ãã移åããããã ãã«ãããè¡ããããŒã¹ãããªãã¯ã¹ã¯ã·ãŒã³ã®å¿ èŠãªéšåã®ã¿ããã¬ãŒã ã«å ¥ãããã«çµæã®ã¹ããŒã¹ãå€æŽããŸãã 空éã«ããªããã£ãã衚瀺ããã«ã¯2ã€ã®ãããªãã¯ã¹ãèŠæ±ããå¿ èŠãããããšãç¥ã£ãŠããã®ã§ã質åãããããšãã§ããŸã-ããã¯ãããšãã°ãããè€éãªã¢ãã«ããã£ãŒããããäžåºŠä¿åââããŠããããªããžã§ã¯ãå šäœã移åããŠãããªãã¯ã¹ãåã«ãŠã³ãã§ããããšãæå³ããŸããïŒ çãã¯ã€ãšã¹ã§ãã ããã«ãOpenGLã¯ãé ç¹ãããã¡ãŒã䜿çšããŠãããªã«ãŒãã¡ã¢ãªã«ãªããžã§ã¯ãã®é ç¹ã®ã»ãããçŽæ¥ä¿åããæ©äŒãæäŸãããããªã«ãŒããã¹ã®åž¯åå¹ ãç¯çŽããŸãã
ç·Žç¿ãã
ãããŠãããã§ãã·ãŒã³ã¬ã³ããªã³ã°ããã»ã¹ã®èª¬æã«ã¹ã ãŒãºã«ã¢ãããŒãããŸããã ããšãã°ãäžè§åœ¢ãšæ£æ¹åœ¢ã®ã°ã©ããŒã·ã§ã³ã䜿çšããŸãããç»é¢ã«éæ¢ç»åã衚瀺ããã ãã§ãªãããŠãŒã¶ãŒã«ã«ã¡ã©ã移åãããŸãã
ã·ã³ãã«ã§è€éãªãã®ãåããŸã
ã¬ã³ããªã³ã°ããåã«ãå¿ èŠãªã·ã§ãŒããŒããã°ã©ã ãäœæããé ç¹ãããã¡ãŒãåæåããæ圱é¢ã調æŽããã«ã¡ã©ã®åæèšå®ãšãªããžã§ã¯ãã®äœçœ®ãæå®ããå¿ èŠããããŸãã ãããŸã§ã®ãšããããªã¹ãã®æåã®é ç®ã®ã¿ã瀺ããŸããã ä¿®æ£ããŸããã
ãããã¡ããããã¡...
ãããã¡ã®æäœããå§ããŸãããã ã³ã³ã»ããèªäœã¯éåžžã«ã·ã³ãã«ã§ã-é ç¹ã·ã§ãŒããŒã®å±æ§ãããããããã®å±æ§ã®å€ãæã€é åããããŸãã 1ã€ã®ããªããã£ãã®é åå ã®èŠçŽ ã®æ°ã¯äžèŽããå¿ èŠããããŸãã
createBuffer()
é¢æ°ã䜿çšããŠãå€ã®é åã«ã¹ããŒã¹ãå²ãåœãŠãããšãGLãµãã·ã¹ãã ã«å®£èšããŸãã 次ã«
bindBuffer()
ããŠãäœæããã°ããã®ãããã¡ãŒ
bindBuffer()
éžæ
bindBuffer()
ãŸãã ããã¯éåžžã«éèŠã§ããäžåºŠã«éžæã§ããã®ã¯1ã€ã®ãããã¡ã ããªã®ã§ãè€æ°ã®ãããã¡ãåŠçããå¿ èŠãããå Žåã¯ãããããé çªã«éžæããŠå¿ èŠãªã¢ã¯ã·ã§ã³ãå®è¡ããå¿ èŠããããŸãã ãã ãããããã¡ã«äœããä¿åããå¿ èŠãããããã
bufferData()
ãåŒã³åºããŠãé åã®å€ãšãµã€ãºãæå®ããŸãã ã³ãŒãã§ã¯ã次ã®ããã«ãªããŸãïŒãããã¡ãäœæããæ©èœã®äžéšïŒïŒ
var buffers = []; buffers[0] = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, buffers[0]); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ -1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, -1.0, 0.0]), gl.STATIC_DRAW); buffers[0].itemSize = 3; buffers[0].itemCount = 3; buffers[0].attributeLocation = gl.getAttribLocation(shaders[0], "aVertexPosition"); gl.enableVertexAttribArray(buffers[0].attributeLocation);
å€æ°
itemSize
ã
itemCount
ããã³
attributeLocation
ã¯ãã¬ã³ããªã³ã°ã§äœ¿çšãããŸãã ä»ã®ãšãããããã«çŠç¹ãåãããŸããã
getAttribLocation()
é¢æ°ã䜿çšãããšãé ç¹ã·ã§ãŒããŒã®2ã€ã®å±æ§ã®äœçœ®ãä¿åãããåŸã§ã¬ã³ããªã³ã°ã§äœ¿çšã§ããŸãã
enableVertexAttribArray()
é¢æ°ã¯ãæåŸ ã©ããã«æ©èœããŸãã
ãã§ã«äœããæãã®ã§ããããïŒ
å®éãçŸæç¹ã§å¿ èŠãªã®ã¯1ã€ã®ã·ã§ãŒããŒããã°ã©ã ãš2ã€ã®ãããã¡ã ãã§ãã1ã€ã¯ããªããã£ãã®é ç¹ã®äœçœ®ãå«ã¿ããã1ã€ã¯RGBA float32圢åŒã®è²ãå«ãã§ããŸãïŒééããªãã£ãããã£ã³ãã«ããšã®32ãããæµ®åå°æ°ç¹æ°ïŒã ããããã¹ãŠã®ãããªãã¯ã¹ãšãã¥ãŒããŒãã§ã¹ã³ã¢ãä»ããŠãäžè§åœ¢ãæãããšãã§ããŸãã ãã¡ãããçµæã¯ããã»ã©ç±ããããŸããã
奜å¥å¿eyeçãªç®ã¯ãäžè§åœ¢ã®å·ŠåŽã«ãã巚倧ãªãšã€ãªã¢ã·ã³ã°ã«ããã«æ°ä»ãã§ãããã ããã«ããã©ãŠã¶ãŠã£ã³ããŠã®ãµã€ãºãå€æŽãããšãäžè§åœ¢ã®æ¯çãå€æŽãããŸãããããã¯ãŸã£ããåãå ¥ããããŸããã ãããã®åé¡ã¯ãã¹ãŠåŠçã§ããŸãããèå³ã®ããæ¹ã¯ãã®äŸã®ã³ãŒããã芧ãã ãã ã å€æã®è¿œå ãšå¥å šãªè¡šç€ºã®èšå®ã«ç§»ããŸãããã
åã³è¡å
ãããªãã¯ã¹ã®æäœã§ã¯ã glMatrix.jsã©ã€ãã©ãªãŒã䜿çšããŸããããã¯ããããªãã¯ã¹èšç®ã®ãšã©ãŒããããã°ããããªãããã§ãã
ãŸãã空éå ã®ãã€ã³ãã®åããç解ããå¿ èŠããããŸãã ããšãã°ããã€ã³ãAïŒ1; 1; 1ïŒãåãããããZ軞ã§+1移åããããšããŸããé ã®äžã§äœããæ°ããããã®é·ããŠèŠããè©Šã¿ã®åŸãé»åã䜿çšããŠãã€ã³ãAïŒ1; 1; 2ïŒãåŸãŸããã ããã¯éåžžã«ç°¡åãªæäœã§ãã1ã€ãŸãã¯è€æ°ã®è»žã«æ²¿ã£ãŠãã€ã³ãã移åããå Žåããã€ã³ãã®å¯Ÿå¿ããã³ã³ããŒãã³ãã«æ°å€ãè¿œå ããã ãã§ããããã§ãã 次ã«ãX軞ã®æ£ã®æ¹åã«å¯ŸããŠXYå¹³é¢ã§ç¹AïŒ1; 1; 0ïŒã45床å転ãããŠã¿ãŸããè³ãçã ããããã¿ãé«çæ°åŠãã䌌ããããªããšãæãåºãããšãã§ããŸãã æäŸãããŠãããªã³ã¯ã§ã¯ãã¯ã©ãŒã¿ããªã³ãšãã®é©çšã«ã€ããŠååã«è©³çŽ°ã«èª¬æãããŠããã®ã§ãèªãããšããå§ãããŸãã è±èªã®èªè ã¯ã ãŠã£ãããã£ã¢ã«ã¢ã¯ã»ã¹ããŠããã«çè«ãåŠã¶ããšãã§ããŸãã ãã ããäœæ¥ã®ããã«å¿ èŠãªã®ã¯ã4x4ãããªãã¯ã¹ã«3ã€ã®è»žã®ããããã«å¯Ÿããç¹ã®å転ãšç©ºéå ã®ç¹ã®ç§»åã«é¢ããæ å ±ãå«ããããšãã§ããããšã ãã§ãã ãããã®4ã€ã®å€æãçµã¿åãããããšã§ãå¿ èŠã«å¿ããŠä»»æã®ãã€ã³ãã移åã§ããŸãã ãããŠãåãªããã€ã³ãã§ã¯ãªããããªããã£ãå šäœãããã«ã¯ãªããžã§ã¯ãã§ãã
ãããã£ãŠãäžéãªãã€ã³ãAïŒ1; 1; 0ïŒããªã³ã«ããŸãããã³ãŒãå ã§ãããè¡ããŸãã æåã«ãå€æè¡åãäœæããå¿ èŠããããŸãã ãã®ããã»ã¹ã¯ããã€ãã®æ®µéã§æ§æãããŠããŸãã ãŸãã æçè¡åãäœæããŸããããã¯ããã€ã³ãã«å€æãé©çšãããªãããšãæå³ããŸãã ãã®è¡åã«ã¯ã䞻察è§ã«1ãå«ãŸããä»ã®äœçœ®ã«ãŒããå«ãŸããŸãã 次ã«ãå¿ èŠãªå€æãæçè¡åã«é 次é©çšããå¿ èŠããããŸã-å€ââäœïŒ
translate()
é¢æ°ïŒãå転ïŒ
rotate()
é¢æ°ïŒãã¹ã±ãŒãªã³ã°ïŒ
scale()
é¢æ°ïŒã ãã®çµæãé©çšããããã¹ãŠã®å€æãçµã¿åããããããªãã¯ã¹ãååŸããŸãã ã³ãŒãã§ã¯ã次ã®ããã«ãªããŸãã
var matrix = mat4.create(); mat4.identity(matrix); matrix.rotate(matrix, Math.PI / 4, [1, 0, 0]);
ãã®çµæã空éå ã®éå§ç¹ãè¡šããã¯ãã«ãšã®ä¹ç®ã«ããç®çã®ç¹ãåŸãããè¡åãåŸãããŸãã ãã®ã¢ã¯ã·ã§ã³ã¯é ç¹ã·ã§ãŒããŒã§å®è¡ããã2ã€ã®æ°å€ãä¹ç®ããã®ãšåããããç°¡åã§ãã çå£ã«ãGLSLã§ãããªãã¯ã¹ã«ãã¯ãã«ãïŒããã³ãããªãã¯ã¹ããããªãã¯ã¹ã«ããã¯ãã«ã«ããã¯ãã«ãïŒä¹ç®ããããšã¯ãåçŽãªã*ãæŒç®åã§è¡šãããŸãã
attribute vec3 aVertexPosition; attribute vec4 aVertexColor; uniform mat4 matrix; varying vec4 vColor; void main() { gl_Position = matrix * vec4(aVertexPosition, 1.0); vColor = aVertexColor; }
å€æãããªãã¯ã¹ã¯ãåäžå€æ°ãšããŠé ç¹ã·ã§ãŒããŒã«æž¡ãããããšãå¿ããªãã§ãã ããã
ååãšããŠã1ã€ã®ãããªãã¯ã¹ã䜿çšããŠãã«ã¡ã©ãšç©ºéå ã®ãªããžã§ã¯ããçŽæ¥ç§»åããããã®äž¡æ¹ã®ããªããã£ãã·ã§ãŒããŒãé ç¹ã·ã§ãŒããŒã«éä¿¡ã§ããŸãã ãããããããã¯è«ççã«å®å šã«ç°ãªããã®ã§ãã ã«ã¡ã©ã¯ãã®ç¹æ§ãå€æŽãããã¹ããŒãžäžã®ãªããžã§ã¯ãã移åããå¯èœæ§ããããŸãïŒéãåæ§ã§ãïŒããåãã¬ãŒã ãèšç®ãããšãã¯ãäž¡æ¹ã®ã¿ã€ãã®å€æãæ¯ååçµåããå¿ èŠããããŸãã ããã«ãã·ãŒã³ã«ã¯è€æ°ã®ãªããžã§ã¯ããååšããå¯èœæ§ããããèšç®ã®æ°ãèªåçã«å¢å ããŸãã ãã®ãããã«ã¡ã©ãšãªããžã§ã¯ãããšã«1ã€ã®ãããªãã¯ã¹ãä¿æããå¿ èŠããããŸããGPUã¯ããããä¹ç®ããŸããããã¯ã³ã³ãã¥ãŒã¿ãŒã§è¡ãããŸãã
ã«ã¡ã©ã®è©³çŽ°
ããªããã£ãã®ç§»åãããã»ã©é£ãããªãå Žåã¯ãã«ã¡ã©ãæäœãããšè³ãå®å šã«ãªããªãæãããããŸãã ããããæªéã¯åœŒãæãããŠããã»ã©ã²ã©ããã®ã§ã¯ãããŸããã å®éããããã¯ãã¹ãŠåããã€ãã£ãã®å€æã§ãããç°ãªã芳ç¹ããã®ã¿ã§ãïŒãããã¯ããããªããïŒã äžèšã®ããã«ãOpenGLã®ã«ã¡ã©ã¯åç¹ã«ãããŸãã ãªããžã§ã¯ããç»é¢ã«è¡šç€ºããã«ã¯ã空éå ã§ãªããžã§ã¯ããZ軞ã®è² ã®éšåã«ç§»åããå¿ èŠããããŸããã«ã¡ã©ã90床äžã«ïŒã€ãŸãX軞äžã§ïŒå転ããã«ã¯ãã·ãŒã³ãåãX軞ã«æ²¿ã£ãŠåç¹ãã90床äžã«å転ããå¿ èŠããããŸãããªããžã§ã¯ãããã®ç§»åã¯ãZ軞ã«æ²¿ã£ãŠåç¹ãã移åããã ãã§ã·ãã¥ã¬ãŒããããŸããããŸã ã¹ã±ãŒã«ã¢ããããå¿ èŠã¯ãããŸãããã«ã¡ã©ã®æãéèŠãªããããã£ã¯ãã«ã¡ã©ãããªãã¯ã¹ãã·ãŒã³å ã®ãã¹ãŠã®ãªããžã§ã¯ãã«é©çšãããããšã§ãã
glMatrix.jsã¯ãã«ã¡ã©ã®ãããªãã¯ã¹ãäœæããã®ã«åœ¹ç«ã¡ãŸããããã«å€ãã®ç°ãªãæ圱ã®ããã®3ã€ã®æ©èœãšããŠããã§ã«ä»¥äžã®ãšããã§ã
perspective()
ã
ortho()
ãš
frustrum()
ãä»ã®ãšããããã䜿çšããŸã
perspective()
ããããã
ãããŠãŸã 圌ã¯å転ããŸãïŒ
次ã®ã³ãŒãã¯ãäžè§åœ¢ãå転ãããŸããåæã«ãããªãã¯ãããããããå°ãããªã£ãŠããããšã«æ°ä»ãã§ããã-ããã¯é è¿æ³ã®å¹æã§ãã
// , , // . // . function drawFrame(gl, shaders, buffers, matrices) { gl.viewport(0, 0, gl.canvas.clientWidth, gl.canvas.clientHeight); gl.clearColor(0.0, 0.0, 0.0, 1); gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(shaders[0]); mat4.perspective(75, gl.canvas.clientWidth / gl.canvas.clientHeight, 0.1, 100.0, matrices[0]); mat4.rotate(matrices[1], Math.PI / 100, [1, 1, 1]); gl.bindBuffer(gl.ARRAY_BUFFER, buffers[0]); gl.vertexAttribPointer(buffers[0].attributeLocation, buffers[0].itemSize, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ARRAY_BUFFER, buffers[1]); gl.vertexAttribPointer(buffers[1].attributeLocation, buffers[1].itemSize, gl.FLOAT, false, 0, 0); gl.uniformMatrix4fv(matrices[0].uniformLocation, false, matrices[0]); gl.uniformMatrix4fv(matrices[1].uniformLocation, false, matrices[1]); gl.drawArrays(gl.TRIANGLES, 0, buffers[0].itemCount); }
æºåã¢ã¯ã·ã§ã³ã¯ãã§ã«èª¬æãããŠããŸããã1ã€ãé€ããŠ-
getUniformLocation()
ã·ã§ãŒããŒã䜿çšããŠåäžå€æ°ã®äœçœ®ãååŸããŸããããã®ã¢ããªã±ãŒã·ã§ã³ã¯éåžžã«åçŽãªã®ã§ãã¹ããŒããŠã³ããŸããããšããã§ããœãŒã¹ã¯ããã§ååŸã§ããŸãã
ã³ãŒããåæããŸããããæåã®é¢æ°åŒã³åºã
viewport()
ã¯ããã£ã³ãã¹äžã®ç»ååºåé åã®ãµã€ãºãèšå®ããŸããæå®ããè²ã§åºåé åãæ©èœãã
clearColor()
ã
clear()
ã¯ãªã¢ããŸããè¡åã®æäœã«ã€ããŠã¯åã«èª¬æããŸããããé¢æ°ã®ãã©ã¡ãŒã¿ãŒã«ã€ããŠã¯èª¬æããŸãã-ãããã¯ã©ã€ãã©ãªãŒããŒãžã«ãããŸãã䜿ãæ £ããé¢æ°ã䜿çšããŠ
bindBuffer()
ãé ç¹ã®åº§æšãšè²ãæã€é åãéžæã
vertexAttribPointer()
ãé ç¹ã·ã§ãŒããŒã®å±æ§å€æ°ã®ãœãŒã¹ãšããŠèšå®ããŸãããã®åŸã2ã€ã®åŒã³åºããç¶ããŸãã
uniformMatrix4fv()
ã·ã§ãŒããŒã®2ã€ã®åäžãªãããªãã¯ã¹ãå®çŸ©ããŸãã1ã€ç®ã¯ã«ã¡ã©ã®ãããªãã¯ã¹ã2ã€ç®ã¯ããªããã£ããªå€æã§ããæåŸã«
drawArrays()
ãäžè§åœ¢ãç»é¢ã«è¡šç€ºãããŸãã
æçµåé³
ãã®èšäºã¯ãäºæ³ä»¥äžã«å€§ããæé·ãããããå°ãªããšã2ã€ã®éšåã«åããå¿ èŠããããŸãããã ããèªè ãOpenGLäžã®å¥ã®å転ããäžè§åœ¢ã«ã€ããŠèªãæéãç¡é§ã«ããªãããã«ãæåŸã«è»œããµã€ã¯ã«ã®å°ããªã¢ãã«ãç»é¢ã«è¡šç€ºãããŸãã
OpenGLã§ã®ã¢ãã«ã®è¡šçŸ
å®éãOpenGLã¯ã¢ãã«ãäœã§ããããç¥ããŸãããããªããã£ããšãã®é åã§ã®ã¿åäœããŸããããã°ã©ããŒã®ã¿ã¹ã¯ã¯ãGLãã€ãã©ã€ã³ã«äŸçµŠããããšãã§ããããªããã£ãã®ã»ããã®åœ¢åŒã§ä»»æã®åœ¢åŒã®ã¢ãã«ãæ瀺ãããã®ã¢ãã«ãã¹ã¯ãªãŒã³ã«æ圱ãããšãã圢ã§æåŸ ãããçµæãååŸããããšã§ããä¿¡ããããªãã§ãããããè€éãªã¡ãã·ã¥ã衚瀺ããããã«ãã¬ã³ããªã³ã°ã³ãŒããå€æŽããå¿ èŠã¯ã»ãšãã©ãããŸãããå®éãã¢ãã«ãã¯ã€ã€ãã¬ãŒã ãšããŠè¡šç€ºããããã®ã¢ã¯ã·ã§ã³ãè¿œå ããå¿ èŠããããŸããããããæåã«ãé¢æ°ã詳ããèŠãŠã¿ãŸããã
drawArrays()
ã 3ã€ã®åŒæ°ãåããŸããæåã®åŒæ°ã¯éžæããé ç¹ãããã¡ãŒã®è§£éæ¹æ³ã決å®ãã2çªç®ã¯åŠçãéå§ããèŠçŽ ã®ã€ã³ããã¯ã¹ã瀺ãã3çªç®ã¯åŠçããé ç¹ã®æ°ãèšå®ããŸããç°¡åã«èšãã°ãã¢ãã«ã®ãã¹ãŠã®äžè§åœ¢ã®é ç¹ã®åº§æšã§é ç¹ãããã¡ãŒãåããã°ãåŠçãããé ç¹ã®æ°ãå¢ããã ãã§æžã¿ããã¹ãŠãæã«ãªããŸãã
çµéšè±å¯ãªéçºè ã¯ããã®ã¢ãããŒãã®æ¬ ç¹ãã€ãŸãéè€ããé ç¹ãä¿åããããã®ã¡ã¢ãªã®éå°ãªäœ¿çšãããã«ææããŸããå®éãOpenGLã«ã¯ãã¡ã¢ãªäœ¿çšéãåæžããããªããã£ãã®åºåãé«éåãã2ã€ã®æ¹æ³ããããŸãã 1ã€ç®ã¯ãããªããã£ãã衚瀺ããããã®å¥ã®é¢æ°ã§ã-
drawElements()
ãäž»ãªéãã¯ãé ç¹ã®åº§æšã ãã§ãªããé ç¹ã€ã³ããã¯ã¹ã®é åãšé©åãªé ç¹ã®é åã§åäœããããšã§ãããããã£ãŠã2ã€ã®å ±éã®é ç¹ãæã€2ã€ã®äžè§åœ¢ãåºåããã«ã¯ãæåã«é ç¹ã®é åããããªã«ãŒãã®ã¡ã¢ãªã«ä¿åããŠããã
drawElements()
é ç¹ã€ã³ããã¯ã¹ã®é åãããªããã£ãã®è¡šç€ºã«äœ¿çšããé¢æ°ã«æž¡ãå¿ èŠããããŸããã€ã³ããã¯ã¹ã®ãµã€ãºãæ倧2ãã€ãã«ãªãå Žåããããããã¡ã¢ãªæ¶è²»éã¯æžå°ããŸãããã®æ¹æ³ã¯é©åã§ãããç°ãªãã¢ãã«ã®éåžžã«å€æ°ã®éè€ããé ç¹ã«å¯ŸããŠã®ã¿æå¹ã§ãããã®å Žåã
TRIANGLE_STRIP
代ãã
TRIANGLES
ã«åŒã³åºãã§ãã©ã¡ãŒã¿ãŒã䜿çšããã®ãæåã§ã
drawArrays()
ãäžè§åœ¢ã®ã¹ããªããã¯ãé ç¹ãæã€èŠåçãªé åã§ããã解éãç°ãªããŸããæåã®äžè§åœ¢ã¯ãããã³ã®å Žåã®ããã«ãã€ã³ããã¯ã¹[0,1,2]ãæã€é ç¹ã§æ§æãããŸã
TRIANGLES
ããã ãã2çªç®ã®äžè§åœ¢ã®é ç¹ã¯[1,2,3]ã«ãªãã
TRIANGLES
2çªç®ã®äžè§åœ¢ã®å Žåãã€ã³ããã¯ã¹ä»ãã®é ç¹ã¯[3,4,5]ã«å®çŸ©ãããŸãããŸããåªããã¡ã¢ãªç¯çŽãç§ã䜿çšããŠããã¢ãã«ïŒããããååŸããŸãïŒã¯ãé ç¹ã®é åãšäžè§åœ¢ã®é åã§æ§æãããäžè§åœ¢ã®é ç¹ã¯æåã®é åã®ã€ã³ããã¯ã¹ã«ãã£ãŠã¢ãã¬ã¹æå®ãããŸãããããã£ãŠãæãæ£ãããªãã·ã§ã³ã¯
drawElements()
parameterãšå ±ã«äœ¿çšããããš
TRIANGLES
ã§ããã¿ã¹ã¯ãç°¡ç¥åããããã«ãäžè§åœ¢ã䞊ã¹æ¿ããŠã¹ããªãããäœæããŸããããå°æ¥çã«ã¯ãããå¿ èŠã«ãªããŸãã
ã¢ãã«ã¬ã³ããªã³ã°
JSON圢åŒã®ã¢ãã«ã¯ãé ç¹ã®é åãã€ã³ããã¯ã¹ã®é åãé ç¹ã®æ°ãäžè§åœ¢ã®æ°ã§æ§æãããŸãã解æã¯jQueryã«ãã£ãŠè¡ãããŸããã¬ã³ããªã³ã°ã³ãŒãã¯ããŸãå€æŽãããŠããŸãããéãã¯ããªããžã§ã¯ãã衚瀺ãããšãã®æåŸã«ããã ãã§ããã·ã§ãŒããŒã¯ãŸã£ããå€æŽãããŠããŸããã
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers[2]); gl.drawElements(gl.TRIANGLES, buffers[2].itemCount, gl.UNSIGNED_SHORT, 0);
3çªç®ã®ãããã¡ãŒã«ã¯ãã€ã³ããã¯ã¹ã®é åãå«ãŸããŸããåŒã³åºãã®åã«GLã§éžæããå¿ èŠããããŸã
drawElements()
ã解æãããJSONã䜿çšããŠé ç¹ãããã¡ãŒãšé ç¹ã«ã©ãŒãããã¡ãŒããã£ã±ãã«ãªã£ããšããŠããããã¯ã»ãšãã©ã³ãŒãã«åœ±é¿ããªãã£ãããšã«æ³šæããŠãã ãããé ç¹ã®è²ã¯ã©ã³ãã ã«ã«ãŠã³ããããŸãããã¹ãŠã®ã³ã³ããŒãã³ããã©ã³ãã ãªå€ã«èšå®ãããŠããå Žåãã¢ãã«ãé¢çœããã€ã³ãã§ããŸãããããããœãŒã¹ãå ¥æã§ããŸãããããŠãããã«åœŒãèŠããããšãã§ããŸãïŒ
ãã¹ã¢ããã³ãã£ãŒ
éçºã«é¢ããç§ã®èããåçåããæ¹æ³ãšããŠããã®èšäºãæžãå§ããŸããããã¥ãŒããªã¢ã«ãçè«çãªèšç®ãšã¯èããªãã§ãã ãããã©ã¡ããèšäºã®ãªã³ã¯ã«ãããŸãããããã説æãããŠãããã®ã誰ãã«ãšã£ãŠèå³æ·±ããšæãããå ŽåããŸãã¯èª°ãã«ãšã£ãŠæçšã§ããããšãå€æããå Žåã§ããç§ã¯åãã§ããŸããæ£çŽã«ã
ãããã¯ãããã«ãšã£ãŠèå³æ·±ããã®ã§ããå Žåã¯ãç¶ããäœæããŸããããã¯ããã§ã¯ãªãå¯èœæ§ããããŸã-ä»ç§ã¯è¶£å³ã®ããã®å€ãã®èªç±æéããªã-ããããç§ã¯æžããŸãã次ã®ã·ãªãŒãºã§ã¯ïŒ
- 深床ãããã¡ãšã¹ãã³ã·ã«ãããã¡ã
- åå°ãšãã¯ã¹ãã£ã
- 管çã
- WebGLã®é«ã¬ãã«ã©ã€ãã©ãªãæ¢çŽ¢ããŸãã
JavaScriptã®é人ãã³ã¡ã³ãã«è¿œå ãããå Žåãã³ãŒãã®æ¹åãšãããªãéçºã«é¢ããã¢ããã€ã¹ãåãã§èããŸãã
䜿çšãããè³æãžã®ãªã³ã¯ã¯ãããŸããããªããªãããããã¯èšäºã®æ¬æã«ããããããåŒãåºãã®ãé¢åã ããã§ãã