1
/* -*- Mode: JavaScript; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
*******************************************************************************
4
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6
* Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
8
* All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
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
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.
19
* NON-MILITARY USE ONLY
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.
26
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
28
* Copyright 2015, 2016 Holger Seelig <holger.seelig@yahoo.de>.
30
* This file is part of the Cobweb Project.
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.
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).
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.
45
* For Silvio, Joy and Adi.
47
******************************************************************************/
53
"cobweb/Components/Texturing/X3DTextureNode",
54
"cobweb/Bits/X3DCast",
55
"cobweb/Bits/X3DConstants",
65
var defaultData = new Uint8Array ([ 255, 255, 255, 255 ]);
67
function X3DTexture2DNode (executionContext)
69
X3DTextureNode .call (this, executionContext);
71
this .addType (X3DConstants .X3DTexture2DNode);
79
X3DTexture2DNode .prototype = $.extend (Object .create (X3DTextureNode .prototype),
81
constructor: X3DTexture2DNode,
82
initialize: function ()
84
X3DTextureNode .prototype .initialize .call (this);
86
var gl = this .getBrowser () .getContext ();
88
this .target = gl .TEXTURE_2D;
90
this .repeatS_ .addInterest ("updateTextureProperties", this);
91
this .repeatT_ .addInterest ("updateTextureProperties", this);
92
this .textureProperties_ .addInterest ("set_textureProperties__", this);
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);
97
this .set_textureProperties__ ();
99
set_textureProperties__: function ()
101
if (this .texturePropertiesNode)
102
this .texturePropertiesNode .removeInterest ("updateTextureProperties", this);
104
this .texturePropertiesNode = X3DCast (X3DConstants .TextureProperties, this .textureProperties_);
106
if (! this .texturePropertiesNode)
107
this .texturePropertiesNode = this .getBrowser () .getDefaultTextureProperties ();
109
this .texturePropertiesNode .addInterest ("updateTextureProperties", this);
111
this .updateTextureProperties ();
113
getTarget: function ()
117
getWidth: function ()
121
getHeight: function ()
125
getFlipY: function ()
133
setTexture: function (width, height, transparent, data, flipY)
137
this .transparent_ = transparent;
139
this .height = height;
143
var gl = this .getBrowser () .getContext ();
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);
150
this .updateTextureProperties ();
151
this .addNodeEvent ();
156
updateTexture: function (data, flipY)
162
var gl = this .getBrowser () .getContext ();
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);
168
if (this .texturePropertiesNode .generateMipMaps_ .getValue ())
169
gl .generateMipmap (gl .TEXTURE_2D);
171
this .addNodeEvent ();
176
updateTextureProperties: function ()
178
var gl = this .getBrowser () .getContext ();
180
X3DTextureNode .prototype .updateTextureProperties .call (this,
182
this .textureProperties_ .getValue (),
183
this .texturePropertiesNode,
186
this .repeatS_ .getValue (),
187
this .repeatT_ .getValue (),
192
this .setTexture (1, 1, false, defaultData, false);
194
resize: function (input, inputWidth, inputHeight, outputWidth, outputHeight)
196
// Nearest neighbor scaling algorithm for very small images.
199
output = new Uint8Array (outputWidth * outputHeight * 4),
200
scaleX = outputWidth / inputWidth,
201
scaleY = outputHeight / inputHeight;
203
for (var y = 0; y < outputHeight; ++ y)
206
inputW = Math .floor (y / scaleY) * inputWidth,
207
outputW = y * outputWidth;
209
for (var x = 0; x < outputWidth; ++ x)
212
index = (inputW + Math.floor (x / scaleX)) * 4,
213
indexScaled = (outputW + x) * 4;
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];
224
setShaderUniforms: function (gl, shaderObject, i)
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
233
return X3DTexture2DNode;