Example usage for javax.media.j3d GeometryArray getVertexFormat

List of usage examples for javax.media.j3d GeometryArray getVertexFormat

Introduction

In this page you can find the example usage for javax.media.j3d GeometryArray getVertexFormat.

Prototype

public int getVertexFormat() 

Source Link

Document

Retrieves the vertexFormat of this GeometryArray

Usage

From source file:MyJava3D.java

private int computeIntensity(GeometryArray geometryArray, int index, int numPoints) {
    int intensity = 0;

    if (computeIntensity != false) {
        // if we have a normal vector compute the intensity under the
        // lighting
        if ((geometryArray.getVertexFormat() & GeometryArray.NORMALS) == GeometryArray.NORMALS) {
            double cos_theta;
            double cos_alpha;
            double cos_beta;

            for (int n = 0; n < numPoints; n++)
                geometryArray.getNormal(index + n, normalsArray[n]);

            // take the average normal vector
            averageVector(surf_norm, normalsArray, numPoints);
            temp.set(view);/* w  w w .  java2s .  c o m*/
            temp.scale(1.0f, surf_norm);

            cos_beta = temp.x + temp.y + temp.z;

            if (cos_beta > 0.0) {
                cos_theta = surf_norm.dot(light);

                if (cos_theta <= 0.0) {
                    intensity = (int) (lightMax * lightAmbient);
                } else {
                    temp.set(surf_norm);
                    temp.scale((float) cos_theta);
                    temp.normalize();
                    temp.sub(light);
                    temp.normalize();

                    cos_alpha = view.dot(temp);

                    intensity = (int) (lightMax * (lightAmbient + lightDiffuse * cos_theta
                            + lightSpecular * Math.pow(cos_alpha, lightGlossiness)));
                }
            }
        }
    }

    return intensity;
}

From source file:IntersectTest.java

public void processStimulus(Enumeration criteria) {
    WakeupCriterion wakeup;/* ww  w  . ja v  a2  s. co m*/
    AWTEvent[] event;
    int eventId;

    while (criteria.hasMoreElements()) {
        wakeup = (WakeupCriterion) criteria.nextElement();
        if (wakeup instanceof WakeupOnAWTEvent) {
            event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
            for (int i = 0; i < event.length; i++) {
                eventId = event[i].getID();
                if (eventId == MouseEvent.MOUSE_PRESSED) {
                    int x = ((MouseEvent) event[i]).getX();
                    int y = ((MouseEvent) event[i]).getY();
                    pickCanvas.setShapeLocation(x, y);

                    Point3d eyePos = pickCanvas.getStartPosition();
                    pickResult = pickCanvas.pickAllSorted();
                    // Use this to do picking benchmarks
                    /*
                     * long start = System.currentTimeMillis(); for (int
                     * l=0;l <3;l++) { if (l == 0) System.out.print
                     * ("BOUNDS: "); if (l == 1) System.out.print
                     * ("GEOMETRY: "); if (l == 2) System.out.print
                     * ("GEOMETRY_INTERSECT_INFO: ");
                     * 
                     * for (int k=0;k <1000;k++) { if (l == 0) {
                     * pickCanvas.setMode(PickTool.BOUNDS); pickResult =
                     * pickCanvas.pickAllSorted(); } if (l == 1) {
                     * pickCanvas.setMode(PickTool.GEOMETRY); pickResult =
                     * pickCanvas.pickAllSorted(); } if (l == 2) {
                     * pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
                     * pickResult = pickCanvas.pickAllSorted(); } } long
                     * delta = System.currentTimeMillis() - start;
                     * System.out.println ("\t"+delta+" ms / 1000 picks"); }
                     */
                    if (pickResult != null) {

                        // Get closest intersection results
                        PickIntersection pi = pickResult[0].getClosestIntersection(eyePos);

                        GeometryArray curGeomArray = pi.getGeometryArray();

                        // Position sphere at intersection point
                        Vector3d v = new Vector3d();
                        Point3d intPt = pi.getPointCoordinatesVW();
                        v.set(intPt);
                        spht3.setTranslation(v);
                        sphTrans[0].setTransform(spht3);

                        // Position sphere at closest vertex
                        Point3d closestVert = pi.getClosestVertexCoordinatesVW();
                        v.set(closestVert);
                        spht3.setTranslation(v);
                        sphTrans[1].setTransform(spht3);

                        Point3d[] ptw = pi.getPrimitiveCoordinatesVW();
                        Point3d[] pt = pi.getPrimitiveCoordinates();
                        int[] coordidx = pi.getPrimitiveCoordinateIndices();
                        Point3d ptcoord = new Point3d();
                        for (int k = 0; k < pt.length; k++) {
                            v.set(ptw[k]);
                            spht3.setTranslation(v);
                            sphTrans[k + 2].setTransform(spht3);
                        }

                        // Get interpolated color (if available)
                        Color4f iColor4 = null;
                        Color3f iColor = null;
                        Vector3f iNormal = null;

                        if (curGeomArray != null) {
                            int vf = curGeomArray.getVertexFormat();

                            if (((vf & (GeometryArray.COLOR_3 | GeometryArray.COLOR_4)) != 0)
                                    && (null != (iColor4 = pi.getPointColor()))) {
                                iColor = new Color3f(iColor4.x, iColor4.y, iColor4.z);

                                // Change the point's color
                                redlook.setMaterial(new Material(iColor, new Color3f(0.0f, 0.0f, 0.0f), iColor,
                                        new Color3f(1.0f, 1.0f, 1.0f), 50.0f));
                            }
                            if (((vf & GeometryArray.NORMALS) != 0)
                                    && (null != (iNormal = pi.getPointNormal()))) {
                                System.out.println("Interpolated normal: " + iNormal);
                            }
                        }

                        System.out.println("=============");
                        System.out.println("Coordinates of intersection pt:" + intPt);
                        System.out.println("Coordinates of vertices: ");
                        for (int k = 0; k < pt.length; k++) {
                            System.out.println(k + ":" + ptw[k].x + " " + ptw[k].y + " " + ptw[k].z);
                        }
                        System.out.println("Closest vertex: " + closestVert);
                        if (iColor != null) {
                            System.out.println("Interpolated color: " + iColor);
                        }
                        if (iNormal != null) {
                            System.out.println("Interpolated normal: " + iNormal);
                        }
                    }
                }
            }
        }
    }
    wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED));
}