【问题标题】:LWJGL Assimp: Loading TexturesLWJGL Assimp:加载纹理
【发布时间】:2017-09-27 04:39:01
【问题描述】:

我正在使用 LWJGL 3 版本的 assimp,并且在加载模型时遇到了困难。我遇到的问题是加载纹理的实际像素数据。使用 AIMaterial 对象加载这些纹理的过程是什么?

【问题讨论】:

    标签: java lwjgl assimp


    【解决方案1】:

    当我为快速演示测试执行此操作时,我只是使用了 Java 中的常规 Image IO。它没有那么花哨,但它很有效,可能会让你继续前进:

       public static ByteBuffer decodePng( BufferedImage image )
               throws IOException
       {
    
          int width = image.getWidth();
          int height = image.getHeight();
    
          // Load texture contents into a byte buffer
          ByteBuffer buf = ByteBuffer.allocateDirect(
                  4 * width * height );
    
          // decode image
          // ARGB format to -> RGBA
          for( int h = 0; h < height; h++ )
             for( int w = 0; w < width; w++ ) {
                int argb = image.getRGB( w, h );
                buf.put( (byte) ( 0xFF & ( argb >> 16 ) ) );
                buf.put( (byte) ( 0xFF & ( argb >> 8 ) ) );
                buf.put( (byte) ( 0xFF & ( argb ) ) );
                buf.put( (byte) ( 0xFF & ( argb >> 24 ) ) );
             }
          buf.flip();
          return buf;
       }
    

    图像作为另一个例程的一部分加载的位置:

        public Texture(InputStream is) throws Exception {
            try {
                // Load Texture file
    
                BufferedImage image = ImageIO.read(is);
    
                this.width = image.getWidth();
                this.height = image.getHeight();
    
                // Load texture contents into a byte buffer
    
                ByteBuffer buf = xogl.utils.TextureUtils.decodePng(image);
    
                // Create a new OpenGL texture 
                this.id = glGenTextures();
                // Bind the texture
                glBindTexture(GL_TEXTURE_2D, this.id);
    
                // Tell OpenGL how to unpack the RGBA bytes. Each component is 1 byte size
                glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
                // Upload the texture data
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, this.width, this.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buf);
    

    【讨论】:

    • 这是一个很好的答案,这是我在切换到 assimp 之前使用的解决方案。然而,assimo 可用于加载各种文件,并且很难判断特定模型的纹理存储在哪里。我想知道是否有办法通过 assimp 找出纹理文件的路径或以抽象方式加载它。
    • 如果您查看我的代码,我正在加载流,而不是文件。 Java 喜欢将资源打包为资源流,而不是文件。我很失望没有人扩展 assimp 来加载流。
    • 对不起,我的解释可能不清楚。我知道如何从输入流加载纹理,但问题是我不知道哪个图像文件与 assimp 材质相关联。我想要的是从 AIMaterial 对象中找出图像文件的路径或图像文件的输入流的某种方法。
    • 我仍然不确定您要做什么,但如果您正在制作某种资产管理系统,您记得“/tex/skin1.png”对应于一些数据image,我会把这两个放在一个新类型Asset 中,然后将其存储在 Map 或 Set 中,以便您以后可以找到它而无需再次加载。
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2012-01-13
    • 2021-02-04
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多