Dola Woolfe wrote:

Thank you, Vladimir. What I nice idea!..

I think I understand it, but I must admit that I
couldn't adjust it to my situation. With all my
rotations at stuff the background was either appearing
in the wrong location or not at all.

What we've done with Xj3D works much nicer than these. Since we had to follow the X3D and VRML97 specifications, we had to deal with colours and angles. That is - the gradient must be a specific colour at the given angle from the nadir. This required us to implement the background gradient as a geometry sphere that is rendered as the Background geometry. A true VRML implementation is far more complex though as it requires two spheres (sky and ground) as well as a box structure. I've cut & pasted the appropriate code that generates the sphere. Only one other note is that it uses a colour interpolator utility class (org.j3d.terrain.ColorRampGenerator) to generate the correct colours at the specific vertex positions (we use color-per-vertex geometry tesselated evenly at 32 faces from top to bottom of the sphere) and a utility class to generate the evenly tessalate sphere (org.j3d.geom.SphereGenerator):

If you would like to see the full source for this, let me know or look
for the class org.web3d.vrml.renderer.j3d.browser.GlobalEffectsGroup


PolygonAttributes sky_attr = new PolygonAttributes(); sky_attr.setCullFace(PolygonAttributes.CULL_FRONT); sky_attr.setBackFaceNormalFlip(true); sky_attr.setPolygonOffset(-1f);

Material mat = new Material();
mat.setLightingEnable(false);

Appearance sky_app = new Appearance();
sky_app.setPolygonAttributes(sky_attr);
sky_app.setMaterial(mat);

skyShape = new Shape3D();
skyShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
        skyShape.clearCapabilityIsFrequent(Shape3D.ALLOW_GEOMETRY_WRITE);
skyShape.setAppearance(sky_app);

bgGroup.addChild(skyShape);

/**
 * Create the array holding the coordinates, normals and colors for the
 * sky sphere.
 *
 * @param color The color values to use as a flat array
 * @param angles The angles to use at each colour boundary
 * @param num The number of color values to read from the arrays
 */
private void updateSkySphereGeom(float[] color, float[] angles, int num) {

    // Update the color interpolator for the sky. First color is always
    // straight down the -Y axis, so assumed angle of zero.
    if(skyColorCreator == null)
        skyColorCreator = new ColorRampGenerator();

boolean extra_color = (angles[num - 1] < Math.PI);

    int num_colors = num;
    if(extra_color)
        num_colors++;

    if((skyHeights == null) || (skyHeights.length != num_colors)) {
        skyHeights = new float[num_colors];
        skyColors = new float[num_colors][3];
    }

    int ci = 0;
    skyHeights[0] = 0;
    skyColors[0][0] = color[ci++];
    skyColors[0][1] = color[ci++];
    skyColors[0][2] = color[ci++];

    for(int i = 1; i < num_colors - 1; i++) {
        skyHeights[i] = (float)Math.cos(angles[i - 1]);
        skyColors[i][0] = color[ci++];
        skyColors[i][1] = color[ci++];
        skyColors[i][2] = color[ci++];
    }

    int last = num_colors - 1;
    skyHeights[last] = -1;

    if(extra_color) {
        skyColors[last][0] = color[ci - 3];
        skyColors[last][1] = color[ci - 2];
        skyColors[last][2] = color[ci - 1];
    } else {
        skyColors[last][0] = color[ci++];
        skyColors[last][1] = color[ci++];
        skyColors[last][2] = color[ci++];
    }

skyColorCreator.setColorRamp(skyHeights, skyColors);

    // Do we need to completely regenerate the geometry, or just update the
    // existing collection. So long as the total number of angles hasn't
    // changed, we can reuse the geometry array. The assumption is that
    // we don't change the number of colors/angles very often, so it's
    // alright to be costly here, because we want to save on memory in
    // the general case.
    if(num > maxSkyColors) {
        maxSkyColors = num > DEFAULT_NUM_SPHERE_FACES ?
                       num :
                       DEFAULT_NUM_SPHERE_FACES;

skyAngleInc = (float)Math.PI / maxSkyColors;

        SphereGenerator gen = new SphereGenerator(1, maxSkyColors);
        gen.generate(skyData);

        // Allocate the color array directly because the generate()
        // call will not have done that. We'll need it for later on
        // anyway.
        skyData.colors = new float[skyData.vertexCount * 3];

        int format = IndexedTriangleStripArray.COORDINATES |
                     IndexedTriangleStripArray.NORMALS |
                     IndexedTriangleStripArray.COLOR_3;

        skyGeometry =
            new IndexedTriangleStripArray(skyData.vertexCount,
                                          format,
                                          skyData.indexesCount,
                                          skyData.stripCounts);
        skyGeometry.setCapability(GeometryArray.ALLOW_COLOR_WRITE);
        skyGeometry.setCoordinates(0, skyData.coordinates);
        skyGeometry.setNormals(0, skyData.normals);
        skyGeometry.setCoordinateIndices(0, skyData.indexes);
        skyGeometry.setColorIndices(0, skyData.indexes);
        skyGeometry.setNormalIndices(0, skyData.indexes);
    }

    // update the geometry color value here.
    skyColorCreator.generate(skyData);
    skyGeometry.setColors(0, skyData.colors);
}

--
Justin Couch                         http://www.vlc.com.au/~justin/
Java Architect & Bit Twiddler              http://www.yumetech.com/
Author, Java 3D FAQ Maintainer                  http://www.j3d.org/
-------------------------------------------------------------------
"Humanism is dead. Animals think, feel; so do machines now.
Neither man nor woman is the measure of all things. Every organism
processes data according to its domain, its environment; you, with
all your brains, would be useless in a mouse's universe..."
                                              - Greg Bear, Slant
-------------------------------------------------------------------

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA3D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to