Skip to content

Commit

Permalink
add gltf convert support
Browse files Browse the repository at this point in the history
  • Loading branch information
fanvanzh committed Sep 22, 2018
1 parent 54a80aa commit e46962c
Show file tree
Hide file tree
Showing 11 changed files with 1,198 additions and 540 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ fbx => 3dtile, convert fbx file to 3dtile, include auto_lod\texture convert etc.
### 世界上最快的 3dtiles 转换工具,极度节省你的处理时间。

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

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

3dtile.exe -f osgb -i E:\Data\倾斜摄影\dayanta -o E:\Data\倾斜摄影\dayanta_test -c "{\"offset\": 0}"
3dtile.exe -f osgb -i E:\Data\dayanta -o E:\Data\dayanta_test -c "{\"offset\": 0}"

3dtile.exe -f gltf -i E:\Data\TT\001.osgb -o E:\Data\TT\001.glb

3dtile.exe -f gltf -i E:\Data\TT\001.obj -o E:\Data\TT\001.glb

### 参数说明:

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

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



**3、通用模型转gltf:**

支持 osg、osgb、obj、fbx、3ds 等单一通用模型数据转为 gltf、glb 格式。

转出格式为 2.0 的gltf,可在以下网址验证查看: https://pissang.github.io/clay-viewer/editor/



# Who use / Who star

. AnalyticalGraphicsInc
Expand Down
7 changes: 6 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ fn build_win_msvc() {
.file("./src/tileset.cpp")
.file("./src/shp23dtile.cpp")
.file("./src/osgb23dtile.cpp")
.file("./src/decompress.cpp")
.file("./src/dxt_img.cpp")
.file("./src/make_gltf.cpp")
.compile("3dtile");
// -------------
println!("cargo:rustc-link-search=native=./lib");
Expand Down Expand Up @@ -48,6 +49,8 @@ fn build_win_gun() {
.file("./src/tileset.cpp")
.file("./src/shp23dtile.cpp")
.file("./src/osgb23dtile.cpp")
.file("./src/dxt_img.cpp")
.file("./src/make_gltf.cpp")
.compile("3dtile");
// -------------
println!("cargo:rustc-link-search=native=./lib");
Expand Down Expand Up @@ -77,6 +80,8 @@ fn build_linux_unkonw() {
.file("./src/tileset.cpp")
.file("./src/shp23dtile.cpp")
.file("./src/osgb23dtile.cpp")
.file("./src/dxt_img.cpp")
.file("./src/make_gltf.cpp")
.compile("3dtile");
// -------------
println!("cargo:rustc-link-search=native=./lib");
Expand Down
142 changes: 142 additions & 0 deletions src/dxt_img.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
#include <vector>
#include <osg/Image>
using namespace std;

struct Color {
int r;
int g;
int b;
};

Color RGB565_RGB(unsigned short color0) {
unsigned long temp;
temp = (color0 >> 11) * 255 + 16;
unsigned char r0 = (unsigned char)((temp / 32 + temp) / 32);
temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
unsigned char g0 = (unsigned char)((temp / 64 + temp) / 64);
temp = (color0 & 0x001F) * 255 + 16;
unsigned char b0 = (unsigned char)((temp / 32 + temp) / 32);
return Color{ r0,g0,b0 };
}

Color Mix_Color(
unsigned short color0, unsigned short color1,
Color c0, Color c1, int idx) {
Color finalColor;
if (color0 > color1)
{
switch (idx)
{
case 0:
finalColor = Color{ c0.r, c0.g, c0.b };
break;
case 1:
finalColor = Color{ c1.r, c1.g, c1.b };
break;
case 2:
finalColor = Color{
(2 * c0.r + c1.r) / 3,
(2 * c0.g + c1.g) / 3,
(2 * c0.b + c1.b) / 3};
break;
case 3:
finalColor = Color{
(c0.r + 2 * c1.r) / 3,
(c0.g + 2 * c1.g) / 3,
(c0.b + 2 * c1.b) / 3 };
break;
}
}
else
{
switch (idx)
{
case 0:
finalColor = Color{ c0.r, c0.g, c0.b };
break;
case 1:
finalColor = Color{ c1.r, c1.g, c1.b };
break;
case 2:
finalColor = Color{ (c0.r + c1.r) / 2, (c0.g + c1.g) / 2, (c0.b + c1.b) / 2 };
break;
case 3:
finalColor = Color{ 0, 0, 0 };
break;
}
}
return finalColor;
}

void resize_Image(vector<unsigned char>& jpeg_buf, int width, int height, int new_w, int new_h) {
vector<unsigned char> new_buf(new_w * new_h * 3);
int scale = width / new_w;
for (int row = 0; row < new_h ; row++)
{
for(int col = 0; col < new_w; col++) {
int pos = row * new_w + col;
int old_pos = (row * width + col) * scale;
for (int i = 0; i < 3 ; i++)
{
new_buf[3 * pos + i] = jpeg_buf[3 * old_pos + i];
}
}
}
jpeg_buf = new_buf;
}

void fill_4BitImage(vector<unsigned char>& jpeg_buf, osg::Image* img, int& width, int& height) {
jpeg_buf.resize(width * height * 3);
unsigned char* pData = img->data();
int imgSize = img->getImageSizeInBytes();
int x_pos = 0;
int y_pos = 0;
for (size_t i = 0; i < imgSize; i += 8)
{
// 64 bit matrix
unsigned short color0, color1;
memcpy(&color0, pData, 2);
pData += 2;
memcpy(&color1, pData, 2);
pData += 2;
Color c0 = RGB565_RGB(color0);
Color c1 = RGB565_RGB(color1);
for (size_t i = 0; i < 4; i++)
{
unsigned char idx[4];
idx[0] = (*pData >> 6) & 0x03;
idx[1] = (*pData >> 4) & 0x03;
idx[2] = (*pData >> 2) & 0x03;
idx[3] = (*pData) & 0x03;
// 4 pixel color
for (size_t pixel_idx = 0; pixel_idx < 4; pixel_idx++)
{
Color cf = Mix_Color(color0, color1, c0, c1, idx[pixel_idx]);
int cell_x_pos = x_pos + pixel_idx;
int cell_y_pos = y_pos + i;
int byte_pos = (cell_x_pos + cell_y_pos * width) * 3;
jpeg_buf[byte_pos] = cf.r;
jpeg_buf[byte_pos + 1] = cf.g;
jpeg_buf[byte_pos + 2] = cf.b;
}
pData++;
}
x_pos += 4;
if (x_pos >= width) {
x_pos = 0;
y_pos += 4;
}
}
int max_size = 512;
if (width > max_size || height > max_size) {
int new_w = width, new_h = height;
while (new_w > max_size || new_h > max_size)
{
new_w /= 2;
new_h /= 2;
}
resize_Image(jpeg_buf, width, height, new_w, new_h);
width = new_w;
height = new_h;
}
}
6 changes: 6 additions & 0 deletions src/dxt_img.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef DXT_IMG_H
#define DXT_IMG_H

void fill_4BitImage(std::vector<unsigned char>& jpeg_buf, osg::Image* img, int& width, int& height);

#endif
14 changes: 11 additions & 3 deletions src/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ extern "C" bool mkdirs(const char* path);
extern "C" bool write_file(const char* filename, const char* buf, unsigned long buf_len);
extern "C" void log_error(const char* msg);


#ifdef WIN32
#define LOG_E(fmt,...) \
char buf[512];\
sprintf(buf,fmt,__VA_ARGS__);\
log_error(buf);
#else
#define LOG_E(fmt,...) \
char buf[512];\
sprintf(buf,fmt,__VA_ARGS__);\
log_error(buf);
char buf[512];\
sprintf(buf,fmt,##__VA_ARGS__);\
log_error(buf);
#endif

//// -- others
struct Transform
Expand Down
31 changes: 30 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn main() {
Arg::with_name("format")
.short("f")
.long("format")
.value_name("osgb,shape")
.value_name("osgb,shape,gltf")
.help("Set input format")
.required(true)
.takes_value(true),
Expand Down Expand Up @@ -128,12 +128,41 @@ fn main() {
"shape" => {
convert_shapefile(input, output, height_field);
}
"gltf" => {
convert_gltf(input, output);
}
_ => {
error!("not support now.");
}
}
}

// convert any thing to gltf
fn convert_gltf(src: &str, dest: &str) {
use std::ffi::CString;
if !dest.ends_with(".gltf") && !dest.ends_with(".glb") {
error!("output format not support now: {}", dest);
return
}
if !src.ends_with(".osgb") &&
!src.ends_with(".osg") &&
!src.ends_with(".obj") &&
!src.ends_with(".fbx") &&
!src.ends_with(".3ds") {
error!("input format not support now: {}", src);
return
}
unsafe {
let c_str = CString::new(dest).unwrap();
let ret = osgb::make_gltf(src.as_ptr(), c_str.as_ptr() as *const u8);
if !ret {
error!("convert failed");
} else {
info!("task over");
}
}
}

#[derive(Debug, Deserialize)]
struct ModelMetadata {
pub version: String,
Expand Down
Loading

0 comments on commit e46962c

Please sign in to comment.