当前位置: 首页 > 面试题库 >

java3d中的面部着色

谭煜
2023-03-14
问题内容

使用java3d,如何不按每个顶点着色,而是按每个面着色?

我尝试学习java3d,但是我制作的Shape3d看起来并不像预期的那样。我想用不同的颜色给不同的三角形上色,但是我不知道该怎么做。

纹理看起来像是一种矫kill过正,而且,我还没有完全掌握这一部分。据我所见,Materials可以在完整的GeometryArray上工作,所以这里不是很好。每个顶点的颜色不会解决问题。


问题答案:

我喜欢这个解决方案。“每个顶点的颜色”实际上是每个顶点的参考,因此每个顶点的每个面都可以具有不同的颜色。

这是一个有效的示例(非常小)。

    package examples;

    import javax.media.j3d.Alpha;
    import javax.media.j3d.AmbientLight;
    import javax.media.j3d.Appearance;
    import javax.media.j3d.BoundingSphere;
    import javax.media.j3d.Bounds;
    import javax.media.j3d.BranchGroup;
    import javax.media.j3d.DirectionalLight;
    import javax.media.j3d.GeometryArray;
    import javax.media.j3d.IndexedTriangleArray;
    import javax.media.j3d.Node;
    import javax.media.j3d.PolygonAttributes;
    import javax.media.j3d.RotationInterpolator;
    import javax.media.j3d.Shape3D;
    import javax.media.j3d.Transform3D;
    import javax.media.j3d.TransformGroup;
    import javax.media.j3d.View;
    import javax.media.j3d.ViewPlatform;
    import javax.vecmath.Color3f;
    import javax.vecmath.Point3d;
    import javax.vecmath.Vector3f;

    import com.sun.j3d.utils.geometry.GeometryInfo;
    import com.sun.j3d.utils.geometry.NormalGenerator;
    import com.sun.j3d.utils.universe.MultiTransformGroup;
    import com.sun.j3d.utils.universe.SimpleUniverse;
    import com.sun.j3d.utils.universe.ViewingPlatform;

    public class Example {

      public static void main(final String[] args) {
        new Example();
      }

      public Example() {
        final SimpleUniverse universe = new SimpleUniverse();

        final BranchGroup group = new BranchGroup();
        final TransformGroup tg = new TransformGroup();
        final BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
        final Node blob = createMonster();
        { //to enable rotation
          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
          tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        }
        // Create rotation to see all sides of the object
        final Transform3D btrans = new Transform3D();
        btrans.rotX(1);
        final Transform3D ztrans = new Transform3D();
        ztrans.rotZ(-1);
        btrans.mul(ztrans);
        final RotationInterpolator ri = createRotation(tg, bounds, btrans, 7000);
        tg.addChild(ri);
        tg.addChild(blob);
        //
        group.addChild(tg);
        //
        addLight(group, new Color3f(1f, 1f, 0f), new Vector3f(4.0f, +7.0f, -12.0f));
        addLight(group, new Color3f(1f, 1f, 1f));
        //
        final ViewingPlatform viewingPlatform = universe.getViewingPlatform();
        final ViewPlatform view = viewingPlatform.getViewPlatform();
        view.setViewAttachPolicy(View.NOMINAL_SCREEN);
        viewingPlatform.setNominalViewingTransform();

        final MultiTransformGroup mtg = viewingPlatform.getMultiTransformGroup();
        final TransformGroup vtg = mtg.getTransformGroup(0);
        final Transform3D t1 = btrans;
        vtg.getTransform(t1);
        t1.setScale(2);
        vtg.setTransform(t1);
        group.compile();
        // add the group of objects to the Universe
        universe.addBranchGraph(group);

      }

      private Node createMonster() {
        final Shape3D child = createShape();
        final Appearance fillAppNode = new Appearance();
        final PolygonAttributes pAtt = new PolygonAttributes();
        // avoid trouble with clockwise faces
        pAtt.setCullFace(PolygonAttributes.CULL_NONE);
        pAtt.setBackFaceNormalFlip(true);
        fillAppNode.setPolygonAttributes(pAtt);
        child.setAppearance(fillAppNode);

        return child;
      }

      private Shape3D createShape() {
        final Point3d[] points = new Point3d[] {//
        new Point3d(-1, 1, 0), //
            new Point3d(1, 1, 0), //
            new Point3d(1, -1, 0), //
            new Point3d(-1, -1, 0), //
            new Point3d(0, 0, 1), //
        };
        final int[] idxs = new int[] {// the triangle faces
        0, 1, 2,//
            2, 3, 0,//
            1, 0, 4,//
            1, 2, 4,//
            2, 3, 4,//
            3, 0, 4 };
        final IndexedTriangleArray ita = new IndexedTriangleArray(points.length, GeometryArray.COORDINATES
            | GeometryArray.COLOR_3, idxs.length);
        ita.setCoordinates(0, points);
        ita.setCoordinateIndices(0, idxs);
        //per vertex colors really are per vertex reference
        //so each vertex can have a different color for each of its faces
        final Color3f[] colors = new Color3f[] {//
        new Color3f(1, 0, 1),//
            new Color3f(0, 1, 0),//
            new Color3f(0, 0, 1),//
            new Color3f(1, 1, 0),//
            new Color3f(1, 1, 1),//
        };
        ita.setColors(0, colors);
        ita.setColorIndices(0, idxs);
        ita.setColorIndices(0, new int[] { 0, 0, 0, 1, 1, 1 });
        //
        final GeometryInfo gi = new GeometryInfo(ita);
        //also generate normal vectors so that the surface can be light
        final NormalGenerator normalGenerator = new NormalGenerator();
        normalGenerator.generateNormals(gi);

        final GeometryArray geometryArray = gi.getGeometryArray();
        return new Shape3D(geometryArray);
      }

      private RotationInterpolator createRotation(final TransformGroup objScale, final Bounds bounds,
          final Transform3D btrans, final long milis) {
        final Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, milis, 0, 0, 0, 0, 0);

        final RotationInterpolator ri = new RotationInterpolator(alpha, objScale, btrans, 0.0f, (float) Math.PI * 2.0f);
        ri.setSchedulingBounds(bounds);
        return ri;
      }

      private void addLight(final BranchGroup group, final Color3f light1Color, final Vector3f ligthDirection) {
        {
          final BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
          final DirectionalLight light1 = new DirectionalLight(light1Color, ligthDirection);
          light1.setInfluencingBounds(bounds);
          group.addChild(light1);
        }
      }

      private void addLight(final BranchGroup group, final Color3f light1Color) {
        {
          final BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
          final AmbientLight light1 = new AmbientLight(light1Color);
          light1.setInfluencingBounds(bounds);
          group.addChild(light1);
        }
      }
    }


 类似资料:
  • Java3D 开源项目包含一组 3D 图形 API。该 Java3D API 提供的一组面向对象接口支持简单、高级编程模型,你可以用于构建,展示和控制 3D 对象的行为与可视化环境。Java3D 项目由多个子项目组成主要包括: j3d-core(Java3D API 的核心), vecmath(3D 矢量数学包)与 j3d-core-utils(Java3D 核心 utilitie),Java3D VRML97 Loader 等。

  • 我试图让J3D在Eclipse中工作,并且阅读了一个又一个论坛,但似乎无法解决我的问题。我用的是Ubuntu11.10 我做到了: 0)解压home/j3d目录中的j3d-1_5_2-linux-i586,然后将i386文件夹添加到usr/lib/jvm/java-6-openjdkjre/lib/i386 线程“main”java.lang.unsatisfiedlinkerror:/usr/l

  • 有一个运行Libgdx的Android应用程序,其中有很多精灵。目前,我根据它们所属的类别给它们涂上不同的颜色,例如“红色”、“绿色”、“蓝色”等。然而,我认为我更喜欢只给雪碧的一部分涂颜色,而不是整个雪碧。 目前我使用: 用于给批次上色,并将其应用于整个雪碧。当那一组完成后,我改变颜色,转到下一组。当完成分组时,我重置批次颜色。 有没有着色器/搅拌器的方式只着色精灵的一部分?例如,任何在我的纹理

  • 毕业没什么社会经验,找了好久的工作任然一无所获,不知道该怎么办,也不知道自己错在哪,成绩是那么优秀,以下是出国留学网小编为大家整理的关于面试的着装技巧,欢迎大家阅读,更多精彩内容请关注出国留学网. 男生面试着装上的技巧 1西服是不可缺的,并且西服要干净挺拔,千万不要穿着老爸以前皱巴巴的西服去面试,领带佩戴与西服相比较不要太夸张,皮鞋最好选择经典的黑色或者深的棕色也可以,给人以稳重大方的形象。 2面

  • 我已经实际搜索但没有找到解决方案在任何网站。我试图在MacOS中使用Java3D用Java编写一个程序,我试图在Eclipse中使用Java3D。我正在尝试运行一个简单的HelloUniverse代码,但我得到了以下异常: 我已经在构建路径中添加了j3dcore.jar、vecmath.jar和j3dutils.jar以及一系列其他库,但似乎都不起作用。 有谁能帮助我在Mac中使用eclipse中

  • 我似乎无法理解从顶点到像素的OpenGL管道过程。 有人能告诉我顶点法线在这两种着色技术中有多重要吗?据我所知,在gouraud中,在每个顶点计算照明,然后在顶点之间的多边形上插值结果颜色(在光栅化之前,这是在片段操作中完成的吗?),phong着色包括首先插值顶点法线,然后计算每个法线上的照明。 另一件事是,当凹凸贴图应用于一个平面(2个三角形)和一个砖纹理作为漫反射时,使用其相应的凹凸贴图,所有