Skip to content

Commit e46962c

Browse files
committed
add gltf convert support
1 parent 54a80aa commit e46962c

File tree

11 files changed

+1198
-540
lines changed

11 files changed

+1198
-540
lines changed

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ fbx => 3dtile, convert fbx file to 3dtile, include auto_lod\texture convert etc.
2222
### 世界上最快的 3dtiles 转换工具,极度节省你的处理时间。
2323

2424
### 命令行:
25-
3dtile.exe [FLAGS] [OPTIONS] --format <osgb,shape> --input <FILE> --output <FILE>
25+
3dtile.exe [FLAGS] [OPTIONS] --format <osgb,shape,gltf> --input <FILE> --output <FILE>
2626

2727
### 示例:
28-
3dtile.exe -f osgb -i E:\Data\倾斜摄影\hgc -o E:\Data\倾斜摄影\hgc_test
28+
3dtile.exe -f osgb -i E:\Data\hgc -o E:\Data\hgc_test
2929

30-
3dtile.exe -f osgb -i E:\Data\倾斜摄影\dayanta -o E:\Data\倾斜摄影\dayanta_test -c "{\"offset\": 0}"
30+
3dtile.exe -f osgb -i E:\Data\dayanta -o E:\Data\dayanta_test -c "{\"offset\": 0}"
31+
32+
3dtile.exe -f gltf -i E:\Data\TT\001.osgb -o E:\Data\TT\001.glb
33+
34+
3dtile.exe -f gltf -i E:\Data\TT\001.obj -o E:\Data\TT\001.glb
3135

3236
### 参数说明:
3337

@@ -46,7 +50,7 @@ fbx => 3dtile, convert fbx file to 3dtile, include auto_lod\texture convert etc.
4650
}
4751
```
4852
```
49-
-f, --format <osgb,shape>
53+
-f, --format <osgb,shape,gltf>
5054
5155
-i, --input <FILE>
5256
@@ -58,9 +62,9 @@ fbx => 3dtile, convert fbx file to 3dtile, include auto_lod\texture convert etc.
5862
```
5963
-c 在命令行传入 json 配置的字符串, json 内容为选配,可部分实现。
6064
61-
-f 输入数据格式: osgb 为倾斜摄影格式数据。
65+
-f 输入数据格式: osgb 为倾斜摄影格式数据, shape 为shapefile面数据, gltf 为单一通用模型转gltf
6266
63-
-i 输入数据的目录,截止到 "\Data" 目录的上一级。
67+
-i 输入数据的目录,osgb数据截止到 "\Data" 目录的上一级,其他格式具体到文件名
6468
6569
-o 输出目录。最终结果位于输出目录的 "\Data" 目录。
6670
@@ -91,6 +95,16 @@ fbx => 3dtile, convert fbx file to 3dtile, include auto_lod\texture convert etc.
9195

9296
shapefile 中需要有字段来表示高度信息。
9397

98+
99+
100+
**3、通用模型转gltf:**
101+
102+
支持 osg、osgb、obj、fbx、3ds 等单一通用模型数据转为 gltf、glb 格式。
103+
104+
转出格式为 2.0 的gltf,可在以下网址验证查看: https://pissang.github.io/clay-viewer/editor/
105+
106+
107+
94108
# Who use / Who star
95109

96110
. AnalyticalGraphicsInc

build.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ fn build_win_msvc() {
1717
.file("./src/tileset.cpp")
1818
.file("./src/shp23dtile.cpp")
1919
.file("./src/osgb23dtile.cpp")
20-
.file("./src/decompress.cpp")
20+
.file("./src/dxt_img.cpp")
21+
.file("./src/make_gltf.cpp")
2122
.compile("3dtile");
2223
// -------------
2324
println!("cargo:rustc-link-search=native=./lib");
@@ -48,6 +49,8 @@ fn build_win_gun() {
4849
.file("./src/tileset.cpp")
4950
.file("./src/shp23dtile.cpp")
5051
.file("./src/osgb23dtile.cpp")
52+
.file("./src/dxt_img.cpp")
53+
.file("./src/make_gltf.cpp")
5154
.compile("3dtile");
5255
// -------------
5356
println!("cargo:rustc-link-search=native=./lib");
@@ -77,6 +80,8 @@ fn build_linux_unkonw() {
7780
.file("./src/tileset.cpp")
7881
.file("./src/shp23dtile.cpp")
7982
.file("./src/osgb23dtile.cpp")
83+
.file("./src/dxt_img.cpp")
84+
.file("./src/make_gltf.cpp")
8085
.compile("3dtile");
8186
// -------------
8287
println!("cargo:rustc-link-search=native=./lib");

src/dxt_img.cpp

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <vector>
2+
#include <osg/Image>
3+
using namespace std;
4+
5+
struct Color {
6+
int r;
7+
int g;
8+
int b;
9+
};
10+
11+
Color RGB565_RGB(unsigned short color0) {
12+
unsigned long temp;
13+
temp = (color0 >> 11) * 255 + 16;
14+
unsigned char r0 = (unsigned char)((temp / 32 + temp) / 32);
15+
temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
16+
unsigned char g0 = (unsigned char)((temp / 64 + temp) / 64);
17+
temp = (color0 & 0x001F) * 255 + 16;
18+
unsigned char b0 = (unsigned char)((temp / 32 + temp) / 32);
19+
return Color{ r0,g0,b0 };
20+
}
21+
22+
Color Mix_Color(
23+
unsigned short color0, unsigned short color1,
24+
Color c0, Color c1, int idx) {
25+
Color finalColor;
26+
if (color0 > color1)
27+
{
28+
switch (idx)
29+
{
30+
case 0:
31+
finalColor = Color{ c0.r, c0.g, c0.b };
32+
break;
33+
case 1:
34+
finalColor = Color{ c1.r, c1.g, c1.b };
35+
break;
36+
case 2:
37+
finalColor = Color{
38+
(2 * c0.r + c1.r) / 3,
39+
(2 * c0.g + c1.g) / 3,
40+
(2 * c0.b + c1.b) / 3};
41+
break;
42+
case 3:
43+
finalColor = Color{
44+
(c0.r + 2 * c1.r) / 3,
45+
(c0.g + 2 * c1.g) / 3,
46+
(c0.b + 2 * c1.b) / 3 };
47+
break;
48+
}
49+
}
50+
else
51+
{
52+
switch (idx)
53+
{
54+
case 0:
55+
finalColor = Color{ c0.r, c0.g, c0.b };
56+
break;
57+
case 1:
58+
finalColor = Color{ c1.r, c1.g, c1.b };
59+
break;
60+
case 2:
61+
finalColor = Color{ (c0.r + c1.r) / 2, (c0.g + c1.g) / 2, (c0.b + c1.b) / 2 };
62+
break;
63+
case 3:
64+
finalColor = Color{ 0, 0, 0 };
65+
break;
66+
}
67+
}
68+
return finalColor;
69+
}
70+
71+
void resize_Image(vector<unsigned char>& jpeg_buf, int width, int height, int new_w, int new_h) {
72+
vector<unsigned char> new_buf(new_w * new_h * 3);
73+
int scale = width / new_w;
74+
for (int row = 0; row < new_h ; row++)
75+
{
76+
for(int col = 0; col < new_w; col++) {
77+
int pos = row * new_w + col;
78+
int old_pos = (row * width + col) * scale;
79+
for (int i = 0; i < 3 ; i++)
80+
{
81+
new_buf[3 * pos + i] = jpeg_buf[3 * old_pos + i];
82+
}
83+
}
84+
}
85+
jpeg_buf = new_buf;
86+
}
87+
88+
void fill_4BitImage(vector<unsigned char>& jpeg_buf, osg::Image* img, int& width, int& height) {
89+
jpeg_buf.resize(width * height * 3);
90+
unsigned char* pData = img->data();
91+
int imgSize = img->getImageSizeInBytes();
92+
int x_pos = 0;
93+
int y_pos = 0;
94+
for (size_t i = 0; i < imgSize; i += 8)
95+
{
96+
// 64 bit matrix
97+
unsigned short color0, color1;
98+
memcpy(&color0, pData, 2);
99+
pData += 2;
100+
memcpy(&color1, pData, 2);
101+
pData += 2;
102+
Color c0 = RGB565_RGB(color0);
103+
Color c1 = RGB565_RGB(color1);
104+
for (size_t i = 0; i < 4; i++)
105+
{
106+
unsigned char idx[4];
107+
idx[0] = (*pData >> 6) & 0x03;
108+
idx[1] = (*pData >> 4) & 0x03;
109+
idx[2] = (*pData >> 2) & 0x03;
110+
idx[3] = (*pData) & 0x03;
111+
// 4 pixel color
112+
for (size_t pixel_idx = 0; pixel_idx < 4; pixel_idx++)
113+
{
114+
Color cf = Mix_Color(color0, color1, c0, c1, idx[pixel_idx]);
115+
int cell_x_pos = x_pos + pixel_idx;
116+
int cell_y_pos = y_pos + i;
117+
int byte_pos = (cell_x_pos + cell_y_pos * width) * 3;
118+
jpeg_buf[byte_pos] = cf.r;
119+
jpeg_buf[byte_pos + 1] = cf.g;
120+
jpeg_buf[byte_pos + 2] = cf.b;
121+
}
122+
pData++;
123+
}
124+
x_pos += 4;
125+
if (x_pos >= width) {
126+
x_pos = 0;
127+
y_pos += 4;
128+
}
129+
}
130+
int max_size = 512;
131+
if (width > max_size || height > max_size) {
132+
int new_w = width, new_h = height;
133+
while (new_w > max_size || new_h > max_size)
134+
{
135+
new_w /= 2;
136+
new_h /= 2;
137+
}
138+
resize_Image(jpeg_buf, width, height, new_w, new_h);
139+
width = new_w;
140+
height = new_h;
141+
}
142+
}

src/dxt_img.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef DXT_IMG_H
2+
#define DXT_IMG_H
3+
4+
void fill_4BitImage(std::vector<unsigned char>& jpeg_buf, osg::Image* img, int& width, int& height);
5+
6+
#endif

src/extern.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@ extern "C" bool mkdirs(const char* path);
44
extern "C" bool write_file(const char* filename, const char* buf, unsigned long buf_len);
55
extern "C" void log_error(const char* msg);
66

7+
8+
#ifdef WIN32
9+
#define LOG_E(fmt,...) \
10+
char buf[512];\
11+
sprintf(buf,fmt,__VA_ARGS__);\
12+
log_error(buf);
13+
#else
714
#define LOG_E(fmt,...) \
8-
char buf[512];\
9-
sprintf(buf,fmt,__VA_ARGS__);\
10-
log_error(buf);
15+
char buf[512];\
16+
sprintf(buf,fmt,##__VA_ARGS__);\
17+
log_error(buf);
18+
#endif
1119

1220
//// -- others
1321
struct Transform

src/main.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn main() {
7272
Arg::with_name("format")
7373
.short("f")
7474
.long("format")
75-
.value_name("osgb,shape")
75+
.value_name("osgb,shape,gltf")
7676
.help("Set input format")
7777
.required(true)
7878
.takes_value(true),
@@ -128,12 +128,41 @@ fn main() {
128128
"shape" => {
129129
convert_shapefile(input, output, height_field);
130130
}
131+
"gltf" => {
132+
convert_gltf(input, output);
133+
}
131134
_ => {
132135
error!("not support now.");
133136
}
134137
}
135138
}
136139

140+
// convert any thing to gltf
141+
fn convert_gltf(src: &str, dest: &str) {
142+
use std::ffi::CString;
143+
if !dest.ends_with(".gltf") && !dest.ends_with(".glb") {
144+
error!("output format not support now: {}", dest);
145+
return
146+
}
147+
if !src.ends_with(".osgb") &&
148+
!src.ends_with(".osg") &&
149+
!src.ends_with(".obj") &&
150+
!src.ends_with(".fbx") &&
151+
!src.ends_with(".3ds") {
152+
error!("input format not support now: {}", src);
153+
return
154+
}
155+
unsafe {
156+
let c_str = CString::new(dest).unwrap();
157+
let ret = osgb::make_gltf(src.as_ptr(), c_str.as_ptr() as *const u8);
158+
if !ret {
159+
error!("convert failed");
160+
} else {
161+
info!("task over");
162+
}
163+
}
164+
}
165+
137166
#[derive(Debug, Deserialize)]
138167
struct ModelMetadata {
139168
pub version: String,

0 commit comments

Comments
 (0)