]> git.tdb.fi Git - libs/gl.git/blob - blender/io_mspgl/export_texture.py
3e78a4cd6ee33af497f4cde7d6a27f281490982c
[libs/gl.git] / blender / io_mspgl / export_texture.py
1 import os
2
3 class TextureExporter:
4         def __init__(self):
5                 self.inline_data = True
6
7         def export_texture(self, tex_node, usage='RGB'):
8                 image = tex_node.image
9                 from .datafile import Resource, Statement, Token
10                 tex_res = Resource(image.name+".tex2d", "texture2d")
11
12                 use_interpolation = tex_node.interpolation!='Closest'
13                 if use_interpolation:
14                         if tex_node.use_mipmap:
15                                 tex_res.statements.append(Statement("filter", Token('LINEAR_MIPMAP_LINEAR')))
16                                 tex_res.statements.append(Statement("generate_mipmap", True))
17                         else:
18                                 tex_res.statements.append(Statement("filter", Token('LINEAR')))
19                         tex_res.statements.append(Statement("max_anisotropy", tex_node.max_anisotropy))
20                 else:
21                         if tex_node.use_mipmap:
22                                 tex_res.statements.append(Statement("filter", Token('NEAREST_MIPMAP_NEAREST')))
23                                 tex_res.statements.append(Statement("generate_mipmap", True))
24                         else:
25                                 tex_res.statements.append(Statement("filter", Token('NEAREST')))
26
27                 colorspace = image.colorspace_settings.name
28                 if usage=='GRAY' and colorspace=='sRGB':
29                                 raise Exception("Grayscale textures with sRGB colorspace are not supported")
30
31                 fn = os.path.basename(image.filepath)
32                 if not self.inline_data and fn:
33                         srgb = "_srgb" if colorspace=='sRGB' else ""
34                         tex_res.statements.append(Statement("external_image"+srgb, fn))
35                 else:
36                         if usage=='RGBA':
37                                 fmt = 'SRGB8_ALPHA8' if colorspace=='sRGB' else 'RGBA8'
38                         elif usage=='GRAY':
39                                 fmt = 'LUMINANCE8'
40                         else:
41                                 fmt = 'SRGB8' if colorspace=='sRGB' else 'RGB8'
42
43                         tex_res.statements.append(Statement("storage", Token(fmt), image.size[0], image.size[1]))
44
45                         texdata = ""
46                         if usage=='RGBA':
47                                 for p in image.pixels:
48                                         texdata += "\\x{:02X}".format(int(p*255))
49                         elif usage=='GRAY':
50                                 for i in range(0, len(image.pixels), 4):
51                                         texdata += "\\x{:02X}".format(image.pixels[i])
52                         else:
53                                 for i in range(0, len(image.pixels), 4):
54                                         for j in range(3):
55                                                 texdata += "\\x{:02X}".format(int(image.pixels[i+j]*255))
56                         tex_res.statements.append(Statement("raw_data", texdata))
57
58                 return tex_res