Search in sources :

Example 86 with RenderProps

use of maspack.render.RenderProps in project artisynth_core by artisynth.

the class WavefrontReader method processLine.

protected boolean processLine(ReaderTokenizer rtok) throws IOException {
    if (curve != null || surface != null) {
        if (!rtok.sval.equals("parm") && !rtok.sval.equals("end")) {
            throw new IOException("unexpected keyword '" + rtok.sval + "' between curv/surf and end, line " + rtok.lineno());
        }
    }
    int lineno = rtok.lineno();
    if (rtok.sval.equals("v")) {
        Vector4d pnt = new Vector4d();
        if ((pnt.x = scanDouble(rtok)) != pnt.x || (pnt.y = scanDouble(rtok)) != pnt.y || (pnt.z = scanDouble(rtok)) != pnt.z) {
            throw new IOException("vertex coordinate expected, line " + lineno);
        }
        pnt.w = scanOptionalNumber(rtok, "vertex w coordinate", 1);
        vertexList.add(pnt);
        // 3 or 4, so flush any extra numbers
        if (toEOL(rtok)) {
            // extra numbers, so number 4 probably wasn't w
            pnt.w = 1;
        }
    } else if (rtok.sval.equals("vn")) {
        Vector3d nrm = new Vector3d();
        if ((nrm.x = scanDouble(rtok)) != nrm.x || (nrm.y = scanDouble(rtok)) != nrm.y || (nrm.z = scanDouble(rtok)) != nrm.z) {
            throw new IOException("normal coordinate expected, line " + lineno);
        }
        normalList.add(nrm);
    } else if (rtok.sval.equals("vt")) {
        Vector3d txt = new Vector3d();
        if ((txt.x = scanDouble(rtok)) != txt.x) {
            throw new IOException("texture vertex u coordinate expected, line " + lineno);
        }
        txt.y = scanOptionalNumber(rtok, "texture vertex v coordinate", 0);
        txt.z = scanOptionalNumber(rtok, "texture vertex w coordinate", 0);
        textureVertexList.add(txt);
    } else if (rtok.sval.equals("voff")) {
        // non-standard - used when we are reading a snippet from an obj file
        myVertexOffset = rtok.scanInteger();
    } else if (rtok.sval.equals("vtoff")) {
        // non-standard - used when we are reading a snippet from an obj file
        myVertexTextureOffset = rtok.scanInteger();
    } else if (rtok.sval.equals("vnoff")) {
        // non-standard - used when we are reading a snippet from an obj file
        myVertexNormalOffset = rtok.scanInteger();
    } else if (rtok.sval.equals("f")) {
        Face face = new Face();
        scanFaceIndices(face, rtok);
        myCurrentGroup.faceList.add(face);
    } else if (rtok.sval.equals("l")) {
        Line line = new Line();
        scanLineIndices(line, rtok);
        myCurrentGroup.lineList.add(line);
    } else if (rtok.sval.equals("deg")) {
        double num = scanDouble(rtok);
        if (num != num || num != (int) num) {
            throw new IOException("u curve degree expected, line " + lineno);
        }
        degreeu = (int) num;
        num = scanOptionalNumber(rtok, "v curve degree", -1);
        if (num != (int) num) {
            throw new IOException("v curve degree expected, line " + lineno);
        }
        degreev = (int) num;
    } else if (rtok.sval.equals("curv")) {
        curve = new Curve();
        curve.lineNum = lineno;
        curve.isRational = true;
        curve.type = BSPLINE;
        if ((curve.u0 = scanDouble(rtok)) != curve.u0 || (curve.u1 = scanDouble(rtok)) != curve.u1) {
            throw new IOException("u start and end values expected, line " + lineno);
        }
        curve.indices = scanIndexList(rtok, "control point index");
        if (degreeu == -1) {
            throw new IOException("degree not specified for curv, line " + lineno);
        }
        curve.degree = degreeu;
    } else if (rtok.sval.equals("surf")) {
        surface = new Surface();
        surface.lineNum = lineno;
        surface.isRational = true;
        surface.type = BSPLINE;
        if ((surface.u0 = scanDouble(rtok)) != surface.u0 || (surface.u1 = scanDouble(rtok)) != surface.u1 || (surface.v0 = scanDouble(rtok)) != surface.v0 || (surface.v1 = scanDouble(rtok)) != surface.v1) {
            throw new IOException("u and v start and end values expected, line " + lineno);
        }
        Face face = new Face();
        scanFaceIndices(face, rtok);
        surface.indices = face.indices;
        surface.textureIndices = face.textureIndices;
        surface.normalIndices = face.normalIndices;
        if (degreeu == -1) {
            throw new IOException("u degree not specified for surf, line " + lineno);
        }
        if (degreev == -1) {
            throw new IOException("v degree not specified for surf, line " + lineno);
        }
        surface.udegree = degreeu;
        surface.vdegree = degreev;
    } else if (rtok.sval.equals("parm")) {
        if (curve != null || surface != null) {
            boolean closed = false;
            boolean isu = true;
            double[] knots = null;
            nextToken(rtok);
            if (rtok.ttype != ReaderTokenizer.TT_WORD || (!rtok.sval.equals("u") && !rtok.sval.equals("v"))) {
                if (curve != null) {
                    throw new IOException("u keyword expected, line " + lineno);
                } else {
                    throw new IOException("u or v keyword expected, line " + lineno);
                }
            }
            if (rtok.sval.equals("v")) {
                if (curve != null) {
                    throw new IOException("v keyword inappropriate for curve construct, line " + lineno);
                }
                isu = false;
            }
            nextToken(rtok);
            if (rtok.ttype == ReaderTokenizer.TT_WORD && rtok.sval.equals("closed")) {
                closed = true;
                nextToken(rtok);
            }
            rtok.pushBack();
            knots = scanDoubleList(rtok, "knot point");
            if (isu) {
                if (curve != null) {
                    curve.isClosed = closed;
                    curve.knots = knots;
                } else {
                    surface.uIsClosed = closed;
                    surface.uknots = knots;
                }
            } else {
                surface.vIsClosed = closed;
                surface.vknots = knots;
            }
        }
    } else if (rtok.sval.equals("end")) {
        if (surface != null) {
            myCurrentGroup.surfaceList.add(surface);
            surface = null;
        } else if (curve != null) {
            myCurrentGroup.curveList.add(curve);
            curve = null;
        }
    } else if (rtok.sval.equals("g") || rtok.sval.equals("sg") || rtok.sval.equals("mg") || rtok.sval.equals("o")) {
        rtok.parseNumbers(false);
        String groupName = scanName(rtok);
        if (rtok.ttype == ReaderTokenizer.TT_WORD) {
            setGroup(groupName);
        }
        while (rtok.ttype != ReaderTokenizer.TT_EOL) {
            // ignore secondary
            // groups?
            nextToken(rtok);
        }
        rtok.pushBack();
        rtok.parseNumbers(true);
    } else if (rtok.sval.equals("mtllib")) {
        String matFileName = scanFileName(rtok);
        try {
            parseMaterialFile(matFileName);
        } catch (Exception e) {
            if (verbose) {
                System.out.println("WavefrontReader warning: can't read mtllib '" + matFileName + "'; ignoring");
            }
        }
    } else if (rtok.sval.equals("usemtl")) {
        String matName = scanName(rtok);
        RenderProps props = myMaterialMap.get(matName);
        if (props != null) {
            // System.out.println ("usemtl " + matName);
            myCurrentGroup.props = props;
        } else {
            if (verbose) {
                System.out.println("WavefrontReader warning: material '" + matName + "' not found; ignoring");
            }
        }
    } else if (rtok.sval.equals("s")) {
        // process smoothing group
        // either a number, or "off"
        nextToken(rtok);
        if (rtok.ttype == ReaderTokenizer.TT_NUMBER) {
            if (verbose) {
                System.out.println("Wavefront smoothing group: " + (int) rtok.lval);
            }
        } else if ((rtok.ttype != ReaderTokenizer.TT_WORD) || !("off".equals(rtok.sval))) {
            System.out.println("Wavefront: unrecognized smoothing group" + rtok);
        }
        toEOL(rtok);
    } else {
        return false;
    }
    return true;
}
Also used : Vector4d(maspack.matrix.Vector4d) Vector3d(maspack.matrix.Vector3d) RenderProps(maspack.render.RenderProps) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

RenderProps (maspack.render.RenderProps)86 Color (java.awt.Color)17 Vector3d (maspack.matrix.Vector3d)17 RigidBody (artisynth.core.mechmodels.RigidBody)11 RigidTransform3d (maspack.matrix.RigidTransform3d)11 Shading (maspack.render.Renderer.Shading)10 PolygonalMesh (maspack.geometry.PolygonalMesh)8 Point3d (maspack.matrix.Point3d)8 RenderObject (maspack.render.RenderObject)7 FrameMarker (artisynth.core.mechmodels.FrameMarker)6 File (java.io.File)6 IOException (java.io.IOException)6 Vertex3d (maspack.geometry.Vertex3d)6 AffineTransform3d (maspack.matrix.AffineTransform3d)4 ColorMapProps (maspack.render.ColorMapProps)4 HasRenderProps (maspack.render.HasRenderProps)4 LineRenderProps (maspack.render.LineRenderProps)4 AxialSpring (artisynth.core.mechmodels.AxialSpring)3 Muscle (artisynth.core.mechmodels.Muscle)3 LinkedList (java.util.LinkedList)3