G'day Folks,

I'm really stuck here with this seemingly trivial problem. It's been
annoying me for the past week and I really don't know what the answer
is. As a backgrounder, I'm generating custom geometry for the j3d.org
toolkit therefore I do not want to use the NormalGenerator. It's a
common thing across all of the geometry, so there's some fundamental
flaw in my technique. I'm hoping someone will find it much easier than I
do.

What happens is that it seems to cull the front faces completely by
lighting them as black - almost as though the normals were negative. If
I negate them by swapping the order of the cross-product I get the back
faces of the geometry lit as you would expect - I see the "inside" of
the object. The only time I can see the front face properly lit is if I
put in CULL_FRONT.

Let's start with creating a cone as a bunch of points:

            for(i = facetCount; --i >=0;)
            {
                angle = segment_angle * (i + 1);

                x1 = (float)(bottomRadius * Math.cos(angle));
                z1 = (float)(bottomRadius * Math.sin(angle));

                //side coords
                coordinates[count++] = 0;
                coordinates[count++] = coneHeight;
                coordinates[count++] = 0;

                coordinates[count++] = x1;
                coordinates[count++] = 0;
                coordinates[count++] = z1;

                coordinates[count++] = x;
                coordinates[count++] = 0;
                coordinates[count++] = z;

                //bottom coordinates
                coordinates[count++] = x;
                coordinates[count++] = 0;
                coordinates[count++] = z;

                coordinates[count++] = x1;
                coordinates[count++] = 0;
                coordinates[count++] = z1;

                coordinates[count++] = 0;
                coordinates[count++] = 0;
                coordinates[count++] = 0;

                x = x1;
                z = z1;
            }

Then we generate normals for them:

            for(int w = 0; w < coordinates.length; w += 9)
            {
                p0.x = coordinates[w]     - coordinates[w + 3];
                p0.y = coordinates[w + 1] - coordinates[w + 4];
                p0.z = coordinates[w + 2] - coordinates[w + 5];

                p1.x = coordinates[w + 6] - coordinates[w + 3];
                p1.y = coordinates[w + 7] - coordinates[w + 4];
                p1.z = coordinates[w + 8] - coordinates[w + 5];

                norm.cross(p0, p1);
                norm.normalize();

                normals[count++] = norm.x;
                normals[count++] = norm.y;
                normals[count++] = norm.z;

                normals[count++] = norm.x;
                normals[count++] = norm.y;
                normals[count++] = norm.z;

                normals[count++] = norm.x;
                normals[count++] = norm.y;
                normals[count++] = norm.z;
            }

Then we display it:

int format = GeometryArray.COORDINATES | GeometryArray.NORMALS;
geom = new TriangleArray(vertex_count, format);
geom.setCoordinates(0, coords);
geom.setNormals(0, normals);

targetShape.setGeometry(geom);


The shape is generated with:

        targetMaterial =
           new Material(ambientBlue, black, blue, specular, 75.0f);
        targetMaterial.setLightingEnable(true);
        targetMaterial.setCapability(Material.ALLOW_COMPONENT_WRITE);

        Appearance blue_appearance = new Appearance();
        blue_appearance.setMaterial(targetMaterial);

        targetPolyAttr = new PolygonAttributes();

targetPolyAttr.setCapability(PolygonAttributes.ALLOW_MODE_WRITE);

targetPolyAttr.setCapability(PolygonAttributes.ALLOW_CULL_FACE_WRITE);
        targetPolyAttr.setPolygonMode(targetPolyAttr.POLYGON_FILL);
//        targetPolyAttr.setCullFace(PolygonAttributes.CULL_FRONT);
        blue_appearance.setPolygonAttributes(targetPolyAttr);

        targetShape = new Shape3D();
        targetShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
        targetShape.setAppearance(blue_appearance);

When I generate some output of the first couple of polygons, this is
what I get:
(ignore the first one because the values are a bit sucky due to errors
in the calculation)

Coordinates are:
 v1 0.0 2.0 0.0
 v2 1.0 0.0 -2.4492127E-16
 v3 1.0 0.0 0.0
 v4 1.0 0.0 0.0
 v5 1.0 0.0 -2.4492127E-16
 v6 0.0 0.0 0.0
Second facet:
 v1 0.0 2.0 0.0
 v2 0.9238795 0.0 -0.38268343
 v3 1.0 0.0 -2.4492127E-16
 v4 1.0 0.0 -2.4492127E-16
 v5 0.9238795 0.0 -0.38268343
 v6 0.0 0.0 0.0
Normals are:
 v1 0.89442724 0.44721362 -0.0
 v2 0.89442724 0.44721362 -0.0
 v3 0.89442724 0.44721362 -0.0
 v4 0.0 -1.0 0.0
 v5 0.0 -1.0 0.0
 v6 0.0 -1.0 0.0
Second facet:
 v1 0.880599 0.4402995 -0.1751621
 v2 0.880599 0.4402995 -0.1751621
 v3 0.880599 0.4402995 -0.1751621
 v4 0.0 -1.0 0.0
 v5 0.0 -1.0 0.0
 v6 0.0 -1.0 0.0
--
Justin Couch                         http://www.vlc.com.au/~justin/
Freelance Java Consultant                  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