`
kennyluo
  • 浏览: 81855 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类
最新评论

apply a Texture Image to a Cube

阅读更多
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;

import javax.media.j3d.Alpha;
import javax.media.j3d.Appearance;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Texture;
import javax.media.j3d.TextureAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3d;

import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.image.TextureLoader;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class TextureImage extends Applet {

  private java.net.URL texImage = null;

  private SimpleUniverse u = null;

  public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    // Create the transform group node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at runtime. Add it to the
    // root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    // Create appearance object for textured cube
    Appearance app = new Appearance();
    Texture tex = new TextureLoader(texImage, this).getTexture();
    app.setTexture(tex);
    TextureAttributes texAttr = new TextureAttributes();
    texAttr.setTextureMode(TextureAttributes.MODULATE);
    app.setTextureAttributes(texAttr);

    // Create textured cube and add it to the scene graph.
    Box textureCube = new Box(0.4f, 0.4f, 0.4f,
        Box.GENERATE_TEXTURE_COORDS, app);
    objTrans.addChild(textureCube);

    // Create a new Behavior object that will perform the desired
    // operation on the specified transform object and add it into
    // the scene graph.
    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
        4000, 0, 0, 0, 0, 0);

    RotationInterpolator rotator = new RotationInterpolator(rotationAlpha,
        objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
        100.0);
    rotator.setSchedulingBounds(bounds);
    objTrans.addChild(rotator);

    // Have Java 3D perform optimizations on this scene graph.
    objRoot.compile();

    return objRoot;
  }

  public TextureImage() {
  }

  public TextureImage(java.net.URL url) {
    texImage = url;
  }

  public void init() {
    if (texImage == null) {
      // the path to the image for an applet
      try {
        texImage = new java.net.URL(getCodeBase().toString()
            + "/stone.jpg");
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    }
    setLayout(new BorderLayout());
    GraphicsConfiguration config = SimpleUniverse
        .getPreferredConfiguration();

    Canvas3D c = new Canvas3D(config);
    add("Center", c);

    // Create a simple scene and attach it to the virtual universe
    BranchGroup scene = createSceneGraph();
    u = new SimpleUniverse(c);

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    u.getViewingPlatform().setNominalViewingTransform();

    u.addBranchGraph(scene);
  }

  public void destroy() {
    u.cleanup();
  }

  //
  // The following allows TextureImage to be run as an application
  // as well as an applet
  //
  public static void main(String[] args) {
    java.net.URL url = null;
    if (args.length > 0) {
      try {
        url = new java.net.URL("file:" + args[0]);
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    } else {
      // the path to the image for an application
      try {
        url = new java.net.URL("file:stone.jpg");
      } catch (java.net.MalformedURLException ex) {
        System.out.println(ex.getMessage());
        System.exit(1);
      }
    }
    new MainFrame(new TextureImage(url), 256, 256);
  }

}

 

分享到:
评论
2 楼 kennyluo 2011-07-16  
网页上发布的代码上缺少了"//",注意"file://"路径
1 楼 kyng 2011-05-16  
图片放到目录下。。。修改对应名称 。。怎么还出错!!!

Exception in thread "Thread-3" com.sun.j3d.utils.image.ImageException: javax.imageio.IIOException: Can't get input stream from URL!
at com.sun.j3d.utils.image.TextureLoader$2.run(TextureLoader.java:432)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.j3d.utils.image.TextureLoader.<init>(TextureLoader.java:425)
at com.sun.j3d.utils.image.TextureLoader.<init>(TextureLoader.java:380)
at firstDemo.TextureImage.createSceneGraph(TextureImage.java:44)
at firstDemo.TextureImage.init(TextureImage.java:101)
at com.sun.j3d.utils.applet.MainFrame.run(MainFrame.java:267)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.imageio.IIOException: Can't get input stream from URL!
at javax.imageio.ImageIO.read(ImageIO.java:1369)
at com.sun.j3d.utils.image.TextureLoader$2.run(TextureLoader.java:430)
... 7 more
Caused by: java.io.FileNotFoundException: toe.jpg (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at java.net.URL.openStream(URL.java:1009)
at javax.imageio.ImageIO.read(ImageIO.java:1367)
... 8 more

相关推荐

    image processing about texture

    image processing texture analysisimage processing texture analysisimage processing texture analysisimage processing texture analysis

    cube with a texture_opengl_visualstudio_

    标题“cube with a texture_opengl_visualstudio_”和描述“rotating cube with a texture”指向一个使用OpenGL在Visual Studio环境中实现的3D图形项目。在这个项目中,开发者创建了一个带有纹理贴图的旋转立方体。...

    a texture aware salient edge model for image retaraeting

    Image retargeting aims at adapting a given image to fit the size of arbitrary displays without severe visual distortions.

    OpenGL ES2.0 Cubemap texture

    在本文中,我们将深入探讨如何在Xcode 4.5环境下使用OpenGL ES 2.0来实现立方体贴图纹理(Cubemap texture)。 立方体贴图是一种特殊的纹理类型,用于表示环境映射,它可以将六个正交的2D纹理面组合成一个3D对象的...

    Color and texture fusion application to aerial image segmentation

    color and texture information for the segmentation of color images. The algorithm uses maximum likelihood classication combined with a certainty based fusion criterion. The algorithm was validated ...

    ImageMagick图片批量处理

    -enhance apply a digital filter to enhance a noisy image -equalize perform histogram equalization to an image -evaluate operator value evaluate an arithmetic, relational, or logical expression -...

    a example of adding backfround and texture for image

    综上所述,"a example of adding backfround and texture for image"这个项目涵盖了OpenCV中的多个核心概念和技术,包括图像合成、图像融合、色彩空间转换、图像分割以及纹理应用。通过学习和实践这个项目,可以深入...

    Render to Texture

    "Render to Texture",在OpenGL编程中,是一个高级特性,允许开发者将渲染的结果保存到一个纹理对象中,而不是直接显示到屏幕上。这个过程通常被称为离屏渲染,它为许多复杂的技术提供了基础,如镜像效果、环境映射...

    Unity插件:Easy Movie Texture (Video Texture) v3.24

    [ And it uses a dynamic link, there is no obligation to disclose the source. ] [Source code is included in the package. If necessary, it may be used to modify. ] Supported resolutions: Android: ...

    Textures5-2024322

    You could place a wood texture on a cube to make the cube look like it is actually made of wood. The Texture sample project adds a banana peel texture to the cylinder created in tutorial 4. This ...

    Rotation invariant texture classification using LBP variance (LBPV)

    distribution, we first estimate the principal orientations of the texture image and then use them to align LBP histograms. The aligned histograms are then in turn used to measure the dissimilarity ...

    Image Texture Analysis Foundations Models and Algorithms

    新书《Image Texture Analysis Foundations Models and Algorithms》(图像纹理分析:基础、模型与算法)由Chih-Cheng Hung、Enmin Song和Yihua Lan共同撰写,其中Chih-Cheng Hung来自美国肯尼索州立大学,Enmin ...

    Image Processing:Dealing With Texture

    《图像处理:处理纹理》是图像处理领域内一部权威性的著作,由Maria Petrou和Pedro Garcia Sevilla两位学者共同编写,他们分别来自英国伦敦帝国理工学院和西班牙卡斯特利翁的Jaume I大学。该书由全球知名的学术出版...

    fast texture synthesis on arbitrary meshs

    While texture synthesis on surfaces has received much attention in computer graphics, the ideal solution ...preprocessed texture sample can be used to synthesize a texture map on any number of meshes.

    Image Quilting for Texture Synthesis and Transfer

    标题:“Image Quilting for Texture Synthesis and Transfer”(图像拼接在纹理合成与迁移中的应用) 描述:本文提出了一种基于图像的方法,通过拼接现有图像的小块来生成新的视觉效果,这一过程称为图像拼接。首先...

    使用fbo来实现render to texture演示

    在计算机图形学中,Framebuffer Object(FBO)是OpenGL中的一种技术,用于实现"Render to Texture"(RTT)。RTT允许我们不在屏幕上渲染,而是将渲染结果存储到纹理中,这种纹理可以作为后续渲染步骤的输入。FBO是...

    Unity3D动态加载texture2D图片1

    尤其是在处理大量图像资源时,如Texture2D,动态加载可以避免在游戏启动时一次性加载所有资源导致的加载时间过长。本文将深入讲解如何在Unity3D中实现Texture2D图片的动态加载,以及解决可能遇到的纹理失真问题。 ...

    Easy Movie Texture

    [ And it uses a dynamic link, there is no obligation to disclose the source. ] [Source code is included in the package. If necessary, it may be used to modify. ] Download link is below. EasyMovie...

    Halcon texture_laws 文献参考资料

    纹理定律(Texture Laws)是计算机视觉和图像处理领域中用于分析和识别图像纹理特征的一种方法。在Halcon这种强大的机器视觉软件中,`texture_laws`算子是专门用于提取和分析图像纹理信息的工具。它基于纹理定律理论...

    Unity 重新设置Texture2D的大小

    ### Unity 重新设置Texture2D的大小 在Unity游戏开发中,经常需要处理各种纹理(Textures),特别是当涉及到动态创建或调整纹理大小时。本文将详细介绍如何使用Unity中的`Texture2D`类来重新设置其大小,并提供一个...

Global site tag (gtag.js) - Google Analytics