summaryrefslogtreecommitdiffstats
path: root/src/javax/media/j3d/TextureCubeMapRetained.java
blob: 97fa9ffa7398cc40b6a39b8eb9a0dfc9bc81d449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright 2001-2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package javax.media.j3d;


/**
 * TextureCubeMap is a subclass of Texture class.
 */
class TextureCubeMapRetained extends TextureRetained {


    static final int NUMFACES = 6;


    @Override
    void initialize(int	format, int width, int widPower,
			int height, int heiPower, int mipmapMode,
			int boundaryWidth) {

	this.numFaces = 6;

	super.initialize(format, width, widPower, height, heiPower,
				mipmapMode, boundaryWidth);
    }


    /**
     * Sets a specified mipmap level for a particular face of the cubemap.
     */
    void initImage(int level, int face, ImageComponent image) {

        // Issue 172 : call checkImageSize even for non-live setImage calls
        checkImageSize(level, image);

	if (this.images == null) {
            throw new IllegalArgumentException(
			J3dI18N.getString("TextureRetained0"));
	}

        if (image instanceof ImageComponent3D) {
            throw new IllegalArgumentException(
			J3dI18N.getString("TextureCubeMap3"));
        }


	if (face < TextureCubeMap.POSITIVE_X ||
			face > TextureCubeMap.NEGATIVE_Z) {
            throw new IllegalArgumentException(
			J3dI18N.getString("TextureCubeMap4"));
	}

	if (this.source.isLive()) {
	    if (this.images[face][level] != null) {
		this.images[face][level].clearLive(refCount);
	    }


	    if (image != null) {
		((ImageComponentRetained)image.retained).setLive(
			inBackgroundGroup, refCount);
	    }
	}

        /*  Don't think this is needed.   --- Chien.
         ((ImageComponent2DRetained)image.retained).setTextureRef();
        */

	if (image != null) {
	    this.images[face][level] = (ImageComponentRetained)image.retained;
	} else {
	    this.images[face][level] = null;
	}
    }

    final void setImage(int level, int face, ImageComponent image) {

	initImage(level, face, image);

        Object arg[] = new Object[3];
	arg[0] = new Integer(level);
	arg[1] = image;
	arg[2] = new Integer(face);
	sendMessage(IMAGE_CHANGED, arg);

	// If the user has set enable to true, then if the image is null
	// turn off texture enable
	if (userSpecifiedEnable) {
	    enable = userSpecifiedEnable;
	    if (image != null && level < maxLevels) {
		ImageComponentRetained img= (ImageComponentRetained)image.retained;
		if (img.isByReference()) {
		    if (img.getRefImage(0) == null) {
			enable = false;
		    }
		}
		else {
		    if (img.getImageData(isUseAsRaster()).get() == null) {
			enable = false;
		    }
		}
		if (!enable)
		    sendMessage(ENABLE_CHANGED, Boolean.FALSE);
	    }
	}
    }

    void initImages(int face, ImageComponent[] images) {

	if (images.length != maxLevels)
            throw new IllegalArgumentException(J3dI18N.getString("Texture20"));

	for (int i = 0; i < images.length; i++) {
	     initImage(i, face, images[i]);
	}
    }

    final void setImages(int face, ImageComponent[] images) {

        int i;

        initImages(face, images);

	ImageComponent [] imgs = new ImageComponent[images.length];
	for (i = 0; i < images.length; i++) {
	     imgs[i] = images[i];
	}

        Object args[] = new Object[2];
        args[0] = imgs;
        args[1] = new Integer(face);

	sendMessage(IMAGES_CHANGED, args);
	// If the user has set enable to true, then if the image is null
	// turn off texture enable
	if (userSpecifiedEnable) {
	    enable = userSpecifiedEnable;
	    i = 0;
	    while (enable && i < maxLevels) {
		if (images[i] != null) {
		    ImageComponentRetained img= (ImageComponentRetained)images[i].retained;
		    if (img.isByReference()) {
			if (img.getRefImage(0) == null) {
			    enable = false;
			}
		    }
		    else {
			if (img.getImageData(isUseAsRaster()).get() == null) {
			    enable = false;
			}
		    }
		}
		i++;
	    }
	    if (!enable) {
		sendMessage(ENABLE_CHANGED, Boolean.FALSE);
	    }
	}
    }




    /**
     * Gets a specified mipmap level of a particular face of the cube map.
     * @param level mipmap level to get
     * @param face face of the cube map
     * @return the pixel array object containing the texture image
     */
    final ImageComponent getImage(int level, int face) {

	if (face < TextureCubeMap.POSITIVE_X ||
			face > TextureCubeMap.NEGATIVE_Z) {
            throw new IllegalArgumentException(
			J3dI18N.getString("TextureCubeMap4"));
	}

	return  (((images != null) && (images[face][level] != null)) ?
		 (ImageComponent)images[face][level].source : null);
    }


    /**
     * Gets an array of image for a particular face of the cube map.
     * @param face face of the cube map
     * @return the pixel array object containing the texture image
     */
    final ImageComponent[] getImages(int face) {

	if (images == null)
	    return null;

	if (face < TextureCubeMap.POSITIVE_X ||
			face > TextureCubeMap.NEGATIVE_Z) {
            throw new IllegalArgumentException(
			J3dI18N.getString("TextureCubeMap4"));
	}

	ImageComponent [] rImages = new ImageComponent[images[face].length];
	for (int i = 0; i < images[face].length; i++) {
	    if (images[face][i] != null)
	        rImages[i] = (ImageComponent)images[face][i].source;
	    else
		rImages[i] = null;
	}
	return rImages;
    }


    @Override
    void bindTexture(Context ctx, int objectId, boolean enable) {
        Pipeline.getPipeline().bindTextureCubeMap(ctx, objectId, enable);
    }

    @Override
    void updateTextureBoundary(Context ctx,
            int boundaryModeS, int boundaryModeT,
            float boundaryRed, float boundaryGreen,
            float boundaryBlue, float boundaryAlpha) {

        Pipeline.getPipeline().updateTextureCubeMapBoundary(ctx,
                boundaryModeS, boundaryModeT,
                boundaryRed, boundaryGreen,
                boundaryBlue, boundaryAlpha);
    }

    @Override
    void updateTextureFilterModes(Context ctx,
            int minFilter, int magFilter) {

        Pipeline.getPipeline().updateTextureCubeMapFilterModes(ctx,
                minFilter, magFilter);
    }

    @Override
    void updateTextureSharpenFunc(Context ctx,
            int numSharpenTextureFuncPts,
            float[] sharpenTextureFuncPts) {

        Pipeline.getPipeline().updateTextureCubeMapSharpenFunc(ctx,
            numSharpenTextureFuncPts, sharpenTextureFuncPts);
    }

    @Override
    void updateTextureFilter4Func(Context ctx,
            int numFilter4FuncPts,
            float[] filter4FuncPts) {

        Pipeline.getPipeline().updateTextureCubeMapFilter4Func(ctx,
                numFilter4FuncPts, filter4FuncPts);
    }

    @Override
    void updateTextureAnisotropicFilter(Context ctx, float degree) {
        Pipeline.getPipeline().updateTextureCubeMapAnisotropicFilter(ctx, degree);
    }


    @Override
    void updateTextureLodRange(Context ctx,
            int baseLevel, int maximumLevel,
            float minimumLod, float maximumLod) {

        Pipeline.getPipeline().updateTextureCubeMapLodRange(ctx, baseLevel, maximumLevel,
                minimumLod, maximumLod);
    }

    @Override
    void updateTextureLodOffset(Context ctx,
            float lodOffsetX, float lodOffsetY,
            float lodOffsetZ) {

        Pipeline.getPipeline().updateTextureCubeMapLodOffset(ctx,
                lodOffsetX, lodOffsetY, lodOffsetZ);
    }


    /**
     * Load level 0 explicitly with null data pointer to allow
     * mipmapping when level 0 is not the base level
     */
    @Override
    void updateTextureDimensions(Canvas3D cv) {
        if(images[0][0] != null) {
            // All faces should have the same image format and type.
            int imageFormat = images[0][0].getImageFormatTypeIntValue(false);
            int imageType = images[0][0].getImageDataTypeIntValue();

            for (int i = 0; i < 6; i++) {
                updateTextureImage(cv, i, maxLevels, 0,
                        format, imageFormat,
                        width, height, boundaryWidth,
                        imageType, null);
            }
        }
    }

    // This is just a wrapper of the native method.
    @Override
    void updateTextureImage(Canvas3D cv,
            int face, int numLevels, int level,
            int textureFormat, int imageFormat,
            int width, int height,
            int boundaryWidth, int imageDataType,
            Object imageData) {

        Pipeline.getPipeline().updateTextureCubeMapImage(cv.ctx,
                face, numLevels, level,
                textureFormat, imageFormat,
                width, height,
                boundaryWidth, imageDataType, imageData, useAutoMipMapGeneration(cv));
    }

    // This is just a wrapper of the native method.
    @Override
    void updateTextureSubImage(Canvas3D cv,
            int face, int level,
            int xoffset, int yoffset,
            int textureFormat, int imageFormat,
            int imgXOffset, int imgYOffset,
            int tilew, int width, int height,
            int imageDataType, Object imageData) {

        Pipeline.getPipeline().updateTextureCubeMapSubImage(cv.ctx,
                face, level, xoffset, yoffset,
                textureFormat, imageFormat,
                imgXOffset, imgYOffset,
                tilew, width, height,
                imageDataType, imageData, useAutoMipMapGeneration(cv));

    }
}