~holger-seelig/cobweb.js/trunk

« back to all changes in this revision

Viewing changes to src/cobweb/Components/Texturing/X3DTexture2DNode.js

  • Committer: Holger Seelig
  • Date: 2017-08-22 04:53:24 UTC
  • Revision ID: holger.seelig@yahoo.de-20170822045324-4of4xxgt79669gbt
Switched to npm.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
 
2
 *******************************************************************************
 
3
 *
 
4
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
5
 *
 
6
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
 
7
 *
 
8
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
 
9
 *
 
10
 * The copyright notice above does not evidence any actual of intended
 
11
 * publication of such source code, and is an unpublished work by create3000.
 
12
 * This material contains CONFIDENTIAL INFORMATION that is the property of
 
13
 * create3000.
 
14
 *
 
15
 * No permission is granted to copy, distribute, or create derivative works from
 
16
 * the contents of this software, in whole or in part, without the prior written
 
17
 * permission of create3000.
 
18
 *
 
19
 * NON-MILITARY USE ONLY
 
20
 *
 
21
 * All create3000 software are effectively free software with a non-military use
 
22
 * restriction. It is free. Well commented source is provided. You may reuse the
 
23
 * source in any way you please with the exception anything that uses it must be
 
24
 * marked to indicate is contains 'non-military use only' components.
 
25
 *
 
26
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 
27
 *
 
28
 * Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
 
29
 *
 
30
 * This file is part of the Cobweb Project.
 
31
 *
 
32
 * Cobweb is free software: you can redistribute it and/or modify it under the
 
33
 * terms of the GNU General Public License version 3 only, as published by the
 
34
 * Free Software Foundation.
 
35
 *
 
36
 * Cobweb is distributed in the hope that it will be useful, but WITHOUT ANY
 
37
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 
38
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
 
39
 * details (a copy is included in the LICENSE file that accompanied this code).
 
40
 *
 
41
 * You should have received a copy of the GNU General Public License version 3
 
42
 * along with Cobweb.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
 
43
 * copy of the GPLv3 License.
 
44
 *
 
45
 * For Silvio, Joy and Adi.
 
46
 *
 
47
 ******************************************************************************/
 
48
 
 
49
 
 
50
define ([
 
51
        "jquery",
 
52
        "cobweb/Fields",
 
53
        "cobweb/Components/Texturing/X3DTextureNode",
 
54
        "cobweb/Bits/X3DCast",
 
55
        "cobweb/Bits/X3DConstants",
 
56
],
 
57
function ($,
 
58
          Fields,
 
59
          X3DTextureNode,
 
60
          X3DCast,
 
61
          X3DConstants)
 
62
{
 
63
"use strict";
 
64
 
 
65
   var defaultData = new Uint8Array ([ 255, 255, 255, 255 ]);
 
66
 
 
67
        function X3DTexture2DNode (executionContext)
 
68
        {
 
69
                X3DTextureNode .call (this, executionContext);
 
70
 
 
71
                this .addType (X3DConstants .X3DTexture2DNode);
 
72
 
 
73
                this .width  = 0;
 
74
                this .height = 0;
 
75
                this .flipY  = false;
 
76
                this .data   = null;
 
77
        }
 
78
 
 
79
        X3DTexture2DNode .prototype = $.extend (Object .create (X3DTextureNode .prototype),
 
80
        {
 
81
                constructor: X3DTexture2DNode,
 
82
                initialize: function ()
 
83
                {
 
84
                        X3DTextureNode .prototype .initialize .call (this);
 
85
                        
 
86
                        var gl = this .getBrowser () .getContext ();
 
87
                        
 
88
                        this .target = gl .TEXTURE_2D;
 
89
 
 
90
                        this .repeatS_           .addInterest ("updateTextureProperties", this);
 
91
                        this .repeatT_           .addInterest ("updateTextureProperties", this);
 
92
                        this .textureProperties_ .addInterest ("set_textureProperties__", this);
 
93
 
 
94
                        gl .bindTexture (gl .TEXTURE_2D, this .getTexture ());
 
95
                        gl .texImage2D  (gl .TEXTURE_2D, 0, gl .RGBA, 1, 1, 0, gl .RGBA, gl .UNSIGNED_BYTE, defaultData);
 
96
                
 
97
                        this .set_textureProperties__ ();
 
98
                },
 
99
                set_textureProperties__: function ()
 
100
                {
 
101
                        if (this .texturePropertiesNode)
 
102
                                this .texturePropertiesNode .removeInterest ("updateTextureProperties", this);
 
103
 
 
104
                        this .texturePropertiesNode = X3DCast (X3DConstants .TextureProperties, this .textureProperties_);
 
105
 
 
106
                        if (! this .texturePropertiesNode)
 
107
                                this .texturePropertiesNode = this .getBrowser () .getDefaultTextureProperties ();
 
108
 
 
109
                        this .texturePropertiesNode .addInterest ("updateTextureProperties", this);
 
110
 
 
111
                        this .updateTextureProperties ();
 
112
                },
 
113
                getTarget: function ()
 
114
                {
 
115
                        return this .target;
 
116
                },
 
117
                getWidth: function ()
 
118
                {
 
119
                        return this .width;
 
120
                },
 
121
                getHeight: function ()
 
122
                {
 
123
                        return this .height;
 
124
                },
 
125
                getFlipY: function ()
 
126
                {
 
127
                        return this .flipY;
 
128
                },
 
129
                getData: function ()
 
130
                {
 
131
                        return this .data;
 
132
                },
 
133
                setTexture: function (width, height, transparent, data, flipY)
 
134
                {
 
135
                        try
 
136
                        {
 
137
                                this .transparent_ = transparent;
 
138
                                this .width        = width;
 
139
                                this .height       = height;
 
140
                                this .flipY        = flipY;
 
141
                                this .data         = data;
 
142
        
 
143
                                var gl = this .getBrowser () .getContext ();
 
144
        
 
145
                                gl .pixelStorei (gl .UNPACK_FLIP_Y_WEBGL, flipY);
 
146
                                gl .pixelStorei (gl .UNPACK_ALIGNMENT, 1);
 
147
                                gl .bindTexture (gl .TEXTURE_2D, this .getTexture ());
 
148
                                gl .texImage2D  (gl .TEXTURE_2D, 0, gl .RGBA, width, height, 0, gl .RGBA, gl .UNSIGNED_BYTE, data);
 
149
        
 
150
                                this .updateTextureProperties ();
 
151
                                this .addNodeEvent ();
 
152
                        }
 
153
                        catch (error)
 
154
                        { }
 
155
                },
 
156
                updateTexture: function (data, flipY)
 
157
                {
 
158
                        try
 
159
                        {
 
160
                                this .data = data;
 
161
        
 
162
                                var gl = this .getBrowser () .getContext ();
 
163
        
 
164
                                gl .pixelStorei (gl .UNPACK_FLIP_Y_WEBGL, flipY);
 
165
                                gl .bindTexture (gl .TEXTURE_2D, this .getTexture ());
 
166
                                gl .texSubImage2D (gl .TEXTURE_2D, 0, 0, 0, gl .RGBA, gl .UNSIGNED_BYTE, data);
 
167
        
 
168
                                if (this .texturePropertiesNode .generateMipMaps_ .getValue ())
 
169
                                        gl .generateMipmap (gl .TEXTURE_2D);
 
170
        
 
171
                                this .addNodeEvent ();
 
172
                        }
 
173
                        catch (error)
 
174
                        { }
 
175
                },
 
176
                updateTextureProperties: function ()
 
177
                {
 
178
                        var gl = this .getBrowser () .getContext ();
 
179
 
 
180
                        X3DTextureNode .prototype .updateTextureProperties .call (this,
 
181
                                                                                  gl .TEXTURE_2D,
 
182
                                                                                  this .textureProperties_ .getValue (),
 
183
                                                                                  this .texturePropertiesNode,
 
184
                                                                                  this .width,
 
185
                                                                                  this .height,
 
186
                                                                                  this .repeatS_ .getValue (),
 
187
                                                                                  this .repeatT_ .getValue (),
 
188
                                                                                  false);
 
189
                },
 
190
                clear: function ()
 
191
                {
 
192
                        this .setTexture (1, 1, false, defaultData, false);
 
193
                },
 
194
                resize: function (input, inputWidth, inputHeight, outputWidth, outputHeight)
 
195
                {
 
196
                   // Nearest neighbor scaling algorithm for very small images.
 
197
 
 
198
                        var
 
199
                                output = new Uint8Array (outputWidth * outputHeight * 4),
 
200
                                scaleX = outputWidth / inputWidth,
 
201
                                scaleY = outputHeight / inputHeight;
 
202
 
 
203
                        for (var y = 0; y < outputHeight; ++ y)
 
204
                        {
 
205
                                var
 
206
                                        inputW  = Math .floor (y / scaleY) * inputWidth,
 
207
                                        outputW = y * outputWidth;
 
208
 
 
209
                                for (var x = 0; x < outputWidth; ++ x)
 
210
                                {
 
211
                                        var
 
212
                                                index       = (inputW + Math.floor (x / scaleX)) * 4,
 
213
                                                indexScaled = (outputW + x) * 4;
 
214
 
 
215
                                        output [indexScaled]     = input [index];
 
216
                                        output [indexScaled + 1] = input [index + 1];
 
217
                                        output [indexScaled + 2] = input [index + 2];
 
218
                                        output [indexScaled + 3] = input [index + 3];
 
219
                                }
 
220
                        }
 
221
 
 
222
                        return output;
 
223
                },
 
224
                setShaderUniforms: function (gl, shaderObject, i)
 
225
                {
 
226
                        shaderObject .textureTypeArray [i] = 2;
 
227
                        gl .activeTexture (gl .TEXTURE2);
 
228
                        gl .bindTexture (gl .TEXTURE_2D, this .getTexture ());
 
229
                        gl .uniform1iv (shaderObject .x3d_TextureType, shaderObject .textureTypeArray); // TODO: Put this in X3DProgramableShaderObject
 
230
                },
 
231
        });
 
232
 
 
233
        return X3DTexture2DNode;
 
234
});
 
235
 
 
236