LWJGL Assimp:加载纹理

时间:2017-04-29 00:50:45

标签: java lwjgl assimp

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

1 个答案:

答案 0 :(得分: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);