gallery3d: Add new widget
[platform/framework/web/web-ui-fw.git] / src / js / widgets / components / webgl.js
1 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);\r
2 //>>description: Tizen WebGL component for gallery3d\r
3 //>>label: WebGL\r
4 //>>group: Tizen:Widgets:Lib\r
5 \r
6 define( [ ], function ( ) {\r
7 //>>excludeEnd("jqmBuildExclude");\r
8 \r
9 /* ***************************************************************************\r
10  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.\r
11  *\r
12  * Permission is hereby granted, free of charge, to any person obtaining a\r
13  * copy of this software and associated documentation files (the "Software"),\r
14  * to deal in the Software without restriction, including without limitation\r
15  * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
16  * and/or sell copies of the Software, and to permit persons to whom the\r
17  * Software is furnished to do so, subject to the following conditions:\r
18  *\r
19  * The above copyright notice and this permission notice shall be included in\r
20  * all copies or substantial portions of the Software.\r
21  *\r
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\r
27  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER\r
28  * DEALINGS IN THE SOFTWARE.\r
29  * ***************************************************************************\r
30  *\r
31  * Authors: Hyunsook Park <hyunsook.park@samsung.com>\r
32  *                      Wonseop Kim <wonseop.kim@samsung.com>\r
33 */\r
34 \r
35 ( function ( $, undefined ) {\r
36         $.webgl = {};\r
37 \r
38         $.webgl.shader = {\r
39                 _vertexShader: null,\r
40                 _fragmentShader: null,\r
41 \r
42                 deleteShaders: function ( gl ) {\r
43                         gl.deleteShader( this._vertexShader );\r
44                         gl.deleteShader( this._fragmentShader );\r
45                 },\r
46 \r
47                 addShaderProgram : function ( gl, vs, fs, isFile ) {\r
48                         var shaderProgram,\r
49                                 vertexShaderSource = {},\r
50                                 fragmentShaderSource = {};\r
51 \r
52                         if ( isFile ) {\r
53                                 vertexShaderSource = this.loadShaderFile( vs );\r
54                                 fragmentShaderSource = this.loadShaderFile( fs );\r
55                         } else {\r
56                                 vertexShaderSource.source = vs;\r
57                                 fragmentShaderSource.source = fs;\r
58                         }\r
59 \r
60                         this._vertexShader = this.getShader( gl, gl.VERTEX_SHADER, vertexShaderSource );\r
61                         this._fragmentShader = this.getShader( gl, gl.FRAGMENT_SHADER, fragmentShaderSource );\r
62 \r
63                         shaderProgram = gl.createProgram();\r
64                         gl.attachShader( shaderProgram, this._vertexShader);\r
65                         gl.attachShader( shaderProgram, this._fragmentShader);\r
66                         gl.linkProgram( shaderProgram );\r
67 \r
68                         if ( !gl.getProgramParameter( shaderProgram, gl.LINK_STATUS ) ) {\r
69                                 window.alert( "Could not initialize Shaders!" );\r
70                         }\r
71                         return shaderProgram;\r
72                 },\r
73 \r
74                 loadShaderFile : function ( path ) {\r
75                         var cache = null;\r
76                         $.ajax({\r
77                                 async : false,\r
78                                 url : path,\r
79                                 success : function ( result ) {\r
80                                         cache = {\r
81                                                 source: result\r
82                                         };\r
83                                 }\r
84                         });\r
85                         return cache;\r
86                 },\r
87 \r
88                 getShader: function ( gl, type, script ) {\r
89                         var shader;\r
90 \r
91                         if ( !gl || !type || !script ) {\r
92                                 return null;\r
93                         }\r
94 \r
95                         shader = gl.createShader( type );\r
96 \r
97                         gl.shaderSource( shader, script.source );\r
98                         gl.compileShader( shader );\r
99 \r
100                         if ( !gl.getShaderParameter( shader, gl.COMPILE_STATUS ) ) {\r
101                                 window.alert( gl.getShaderInfoLog( shader ) );\r
102                                 gl.deleteShader( shader );\r
103                                 return null;\r
104                         }\r
105                         return shader;\r
106                 }\r
107         };\r
108 \r
109         $.webgl.buffer = {\r
110                 attribBufferData: function ( gl, attribArray ) {\r
111                         var attribBuffer = gl.createBuffer();\r
112 \r
113                         gl.bindBuffer( gl.ARRAY_BUFFER, attribBuffer );\r
114                         gl.bufferData( gl.ARRAY_BUFFER, attribArray, gl.STATIC_DRAW );\r
115                         gl.bindBuffer( gl.ARRAY_BUFFER, null );\r
116 \r
117                         return attribBuffer;\r
118                 }\r
119         };\r
120 \r
121 } ( jQuery ) );\r
122 \r
123 //>>excludeStart("jqmBuildExclude", pragmas.jqmBuildExclude);\r
124 } );\r
125 //>>excludeEnd("jqmBuildExclude");\r