Add generation example
Former-commit-id: fb3b6b7f4a340a1e1afe156697cc60d7d7b34117
This commit is contained in:
parent
dc8fe97715
commit
4f7c63a945
@ -1,5 +1,5 @@
|
||||
[package]
|
||||
name = "mikktspace-sys"
|
||||
name = "mikktspace"
|
||||
version = "0.1.0"
|
||||
authors = ["alteous <alteous@outlook.com>"]
|
||||
build = "build.rs"
|
||||
@ -8,7 +8,7 @@ build = "build.rs"
|
||||
cmake = "0.1"
|
||||
|
||||
[dependencies]
|
||||
gltf = "0.7"
|
||||
cgmath = "0.15"
|
||||
|
||||
[[example]]
|
||||
name = "generate"
|
||||
@ -1,45 +1,172 @@
|
||||
extern crate cgmath;
|
||||
extern crate mikktspace;
|
||||
|
||||
extern crate gltf;
|
||||
use cgmath::prelude::*;
|
||||
|
||||
use std::io::Write;
|
||||
pub type Face = [u32; 3];
|
||||
pub type Vec2 = [f32; 2];
|
||||
pub type Vec3 = [f32; 3];
|
||||
pub type Vec4 = [f32; 4];
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Vertex {
|
||||
position: Vec3,
|
||||
normal: Vec3,
|
||||
tex_coord: Vec2,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct NewVertex {
|
||||
position: Vec3,
|
||||
normal: Vec3,
|
||||
tex_coord: Vec2,
|
||||
tangent: Vec4,
|
||||
}
|
||||
|
||||
fn make_cube() -> (Vec<Face>, Vec<Vertex>) {
|
||||
struct ControlPoint {
|
||||
uv: Vec2,
|
||||
dir: Vec3,
|
||||
}
|
||||
let mut faces = Vec::new();
|
||||
let mut ctl_pts = Vec::new();
|
||||
let mut vertices = Vec::new();
|
||||
|
||||
// +x plane
|
||||
{
|
||||
let base = ctl_pts.len() as u32;
|
||||
faces.push([base, base + 1, base + 4]);
|
||||
faces.push([base + 1, base + 2, base + 4]);
|
||||
faces.push([base + 2, base + 3, base + 4]);
|
||||
faces.push([base + 3, base, base + 4]);
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [1.0, -1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [1.0, -1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 1.0], dir: [1.0, 1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 0.0], dir: [1.0, 1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.5, 0.5], dir: [1.0, 0.0, 0.0] });
|
||||
}
|
||||
|
||||
// -x plane
|
||||
{
|
||||
let base = ctl_pts.len() as u32;
|
||||
faces.push([base, base + 1, base + 4]);
|
||||
faces.push([base + 1, base + 2, base + 4]);
|
||||
faces.push([base + 2, base + 3, base + 4]);
|
||||
faces.push([base + 3, base, base + 4]);
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 0.0], dir: [-1.0, 1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 1.0], dir: [-1.0, 1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [-1.0, -1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [-1.0, -1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.5, 0.5], dir: [-1.0, 0.0, 0.0] });
|
||||
}
|
||||
|
||||
// +y plane
|
||||
{
|
||||
let base = ctl_pts.len() as u32;
|
||||
faces.push([base, base + 1, base + 4]);
|
||||
faces.push([base + 1, base + 2, base + 4]);
|
||||
faces.push([base + 2, base + 3, base + 4]);
|
||||
faces.push([base + 3, base, base + 4]);
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [1.0, 1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [1.0, 1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [-1.0, 1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [-1.0, 1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.5], dir: [0.0, 1.0, 0.0] });
|
||||
}
|
||||
|
||||
// -y plane
|
||||
{
|
||||
let base = ctl_pts.len() as u32;
|
||||
faces.push([base, base + 1, base + 4]);
|
||||
faces.push([base + 1, base + 2, base + 4]);
|
||||
faces.push([base + 2, base + 3, base + 4]);
|
||||
faces.push([base + 3, base, base + 4]);
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [-1.0, -1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [-1.0, -1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [1.0, -1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [1.0, -1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.5], dir: [0.0, -1.0, 0.0] });
|
||||
}
|
||||
|
||||
// +z plane
|
||||
{
|
||||
let base = ctl_pts.len() as u32;
|
||||
faces.push([base, base + 1, base + 4]);
|
||||
faces.push([base + 1, base + 2, base + 4]);
|
||||
faces.push([base + 2, base + 3, base + 4]);
|
||||
faces.push([base + 3, base, base + 4]);
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [-1.0, 1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [-1.0, -1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 1.0], dir: [1.0, -1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 0.0], dir: [1.0, 1.0, 1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.5, 0.5], dir: [0.0, 0.0, 1.0] });
|
||||
}
|
||||
|
||||
// -z plane
|
||||
{
|
||||
let base = ctl_pts.len() as u32;
|
||||
faces.push([base, base + 1, base + 4]);
|
||||
faces.push([base + 1, base + 2, base + 4]);
|
||||
faces.push([base + 2, base + 3, base + 4]);
|
||||
faces.push([base + 3, base, base + 4]);
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 0.0], dir: [1.0, 1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [1.0, 1.0], dir: [1.0, -1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 1.0], dir: [-1.0, -1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.0, 0.0], dir: [-1.0, 1.0, -1.0] });
|
||||
ctl_pts.push(ControlPoint { uv: [0.5, 0.5], dir: [0.0, 0.0, -1.0] });
|
||||
}
|
||||
|
||||
for pt in ctl_pts {
|
||||
let p: cgmath::Vector3<f32> = pt.dir.into();
|
||||
let n: cgmath::Vector3<f32> = p.normalize();
|
||||
let t: cgmath::Vector2<f32> = pt.uv.into();
|
||||
vertices.push(Vertex {
|
||||
position: (p / 2.0).into(),
|
||||
normal: n.into(),
|
||||
tex_coord: t.into(),
|
||||
});
|
||||
}
|
||||
|
||||
(faces, vertices)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let path = "test-data/Avocado.gltf";
|
||||
let gltf = gltf::Import::from_path(path).sync().unwrap();
|
||||
let mesh = gltf.meshes().nth(0).unwrap();
|
||||
let primitive = mesh.primitives().nth(0).unwrap();
|
||||
let positions: Vec<[f32; 3]> = primitive.positions().unwrap().collect();
|
||||
let normals: Vec<[f32; 3]> = primitive.normals().unwrap().collect();
|
||||
let mut tex_coords: Vec<[f32; 2]> = vec![];
|
||||
let mut indices: Vec<u16> = vec![];
|
||||
match primitive.tex_coords(0).unwrap() {
|
||||
gltf::mesh::TexCoords::F32(iter) => tex_coords.extend(iter),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
match primitive.indices().unwrap() {
|
||||
gltf::mesh::Indices::U16(iter) => indices.extend(iter),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
let (faces, vertices) = make_cube();
|
||||
//println!("{:#?}", faces);
|
||||
//println!("{:#?}", vertices);
|
||||
|
||||
let file = std::fs::File::create("Avocado.obj").unwrap();
|
||||
let mut writer = std::io::BufWriter::new(file);
|
||||
for position in &positions {
|
||||
writeln!(writer, "v {} {} {}", position[0], position[1], position[2]);
|
||||
}
|
||||
for normal in &normals {
|
||||
writeln!(writer, "vn {} {} {}", normal[0], normal[1], normal[2]);
|
||||
}
|
||||
for tex_coord in &tex_coords {
|
||||
writeln!(writer, "vt {} {}", tex_coord[0], tex_coord[1]);
|
||||
}
|
||||
let mut i = indices.iter();
|
||||
while let (Some(v0), Some(v1), Some(v2)) = (i.next(), i.next(), i.next()) {
|
||||
writeln!(
|
||||
writer,
|
||||
"f {}/{}/{} {}/{}/{} {}/{}/{}",
|
||||
1 + v0, 1 + v0, 1 + v0,
|
||||
1 + v1, 1 + v1, 1 + v1,
|
||||
1 + v2, 1 + v2, 1 + v2,
|
||||
let vertex = |face, vert| {
|
||||
let vs: &[u32; 3] = &faces[face % faces.len()];
|
||||
println!("reading {}, {}", face, vert);
|
||||
&vertices[vs[vert] as usize % vertices.len()]
|
||||
};
|
||||
let vertices_per_face = || 3;
|
||||
let face_count = || faces.len();
|
||||
let position = |face, vert| &vertex(face, vert).position;
|
||||
let normal = |face, vert| &vertex(face, vert).normal;
|
||||
let tex_coord = |face, vert| &vertex(face, vert).tex_coord;
|
||||
|
||||
let mut new_vertices = Vec::new();
|
||||
|
||||
{
|
||||
let mut set_tangent = |face, vert, tangent| {
|
||||
println!("setting {}, {}", face, vert);
|
||||
new_vertices.push(NewVertex {
|
||||
position: *position(face, vert),
|
||||
normal: *normal(face, vert),
|
||||
tex_coord: *tex_coord(face, vert),
|
||||
tangent: tangent,
|
||||
});
|
||||
};
|
||||
let ret = mikktspace::generate(
|
||||
&vertices_per_face,
|
||||
&face_count,
|
||||
&position,
|
||||
&normal,
|
||||
&tex_coord,
|
||||
&mut set_tangent,
|
||||
);
|
||||
assert_eq!(true, ret);
|
||||
}
|
||||
println!("{:#?}", new_vertices);
|
||||
}
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
[package]
|
||||
name = "mikktspace-sys"
|
||||
version = "0.1.0"
|
||||
authors = ["alteous <alteous@outlook.com>"]
|
||||
build = "build.rs"
|
||||
|
||||
[build-dependencies]
|
||||
cmake = "0.1"
|
||||
|
||||
[dependencies]
|
||||
@ -1,9 +0,0 @@
|
||||
|
||||
extern crate cmake;
|
||||
|
||||
fn main() {
|
||||
let dst = cmake::build("libmikktspace");
|
||||
println!("cargo:rustc-link-search=native={}", dst.display());
|
||||
println!("cargo:rustc-link-lib=static=mikktspace");
|
||||
}
|
||||
|
||||
@ -1,11 +0,0 @@
|
||||
cmake_minimum_required(VERSION 2.8)
|
||||
project(mikktspace)
|
||||
set(PROJECT_VERSION_MAJOR "1")
|
||||
set(PROJECT_VERSION_MINOR "0")
|
||||
set(PROJECT_VERSION_PATCH "0")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -std=c11")
|
||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_DEBUG} -ggdb -DDEBUG")
|
||||
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELEASE} -O2")
|
||||
set(SOURCES mikktspace.h mikktspace.c)
|
||||
add_library(mikktspace STATIC ${SOURCES})
|
||||
install(TARGETS mikktspace ARCHIVE DESTINATION ".")
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,145 +0,0 @@
|
||||
/** \file mikktspace/mikktspace.h
|
||||
* \ingroup mikktspace
|
||||
*/
|
||||
/**
|
||||
* Copyright (C) 2011 by Morten S. Mikkelsen
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef __MIKKTSPACE_H__
|
||||
#define __MIKKTSPACE_H__
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Author: Morten S. Mikkelsen
|
||||
* Version: 1.0
|
||||
*
|
||||
* The files mikktspace.h and mikktspace.c are designed to be
|
||||
* stand-alone files and it is important that they are kept this way.
|
||||
* Not having dependencies on structures/classes/libraries specific
|
||||
* to the program, in which they are used, allows them to be copied
|
||||
* and used as is into any tool, program or plugin.
|
||||
* The code is designed to consistently generate the same
|
||||
* tangent spaces, for a given mesh, in any tool in which it is used.
|
||||
* This is done by performing an internal welding step and subsequently an order-independent evaluation
|
||||
* of tangent space for meshes consisting of triangles and quads.
|
||||
* This means faces can be received in any order and the same is true for
|
||||
* the order of vertices of each face. The generated result will not be affected
|
||||
* by such reordering. Additionally, whether degenerate (vertices or texture coordinates)
|
||||
* primitives are present or not will not affect the generated results either.
|
||||
* Once tangent space calculation is done the vertices of degenerate primitives will simply
|
||||
* inherit tangent space from neighboring non degenerate primitives.
|
||||
* The analysis behind this implementation can be found in my master's thesis
|
||||
* which is available for download --> http://image.diku.dk/projects/media/morten.mikkelsen.08.pdf
|
||||
* Note that though the tangent spaces at the vertices are generated in an order-independent way,
|
||||
* by this implementation, the interpolated tangent space is still affected by which diagonal is
|
||||
* chosen to split each quad. A sensible solution is to have your tools pipeline always
|
||||
* split quads by the shortest diagonal. This choice is order-independent and works with mirroring.
|
||||
* If these have the same length then compare the diagonals defined by the texture coordinates.
|
||||
* XNormal which is a tool for baking normal maps allows you to write your own tangent space plugin
|
||||
* and also quad triangulator plugin.
|
||||
*/
|
||||
|
||||
|
||||
typedef int tbool;
|
||||
typedef struct SMikkTSpaceContext SMikkTSpaceContext;
|
||||
|
||||
typedef struct {
|
||||
// Returns the number of faces (triangles/quads) on the mesh to be processed.
|
||||
int (*m_getNumFaces)(const SMikkTSpaceContext * pContext);
|
||||
|
||||
// Returns the number of vertices on face number iFace
|
||||
// iFace is a number in the range {0, 1, ..., getNumFaces()-1}
|
||||
int (*m_getNumVerticesOfFace)(const SMikkTSpaceContext * pContext, const int iFace);
|
||||
|
||||
// returns the position/normal/texcoord of the referenced face of vertex number iVert.
|
||||
// iVert is in the range {0,1,2} for triangles and {0,1,2,3} for quads.
|
||||
void (*m_getPosition)(const SMikkTSpaceContext * pContext, float fvPosOut[], const int iFace, const int iVert);
|
||||
void (*m_getNormal)(const SMikkTSpaceContext * pContext, float fvNormOut[], const int iFace, const int iVert);
|
||||
void (*m_getTexCoord)(const SMikkTSpaceContext * pContext, float fvTexcOut[], const int iFace, const int iVert);
|
||||
|
||||
// either (or both) of the two setTSpace callbacks can be set.
|
||||
// The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
|
||||
|
||||
// This function is used to return the tangent and fSign to the application.
|
||||
// fvTangent is a unit length vector.
|
||||
// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
|
||||
// bitangent = fSign * cross(vN, tangent);
|
||||
// Note that the results are returned unindexed. It is possible to generate a new index list
|
||||
// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
|
||||
// DO NOT! use an already existing index list.
|
||||
void (*m_setTSpaceBasic)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fSign, const int iFace, const int iVert);
|
||||
|
||||
// This function is used to return tangent space results to the application.
|
||||
// fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
|
||||
// true magnitudes which can be used for relief mapping effects.
|
||||
// fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
|
||||
// However, both are perpendicular to the vertex normal.
|
||||
// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
|
||||
// fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
|
||||
// bitangent = fSign * cross(vN, tangent);
|
||||
// Note that the results are returned unindexed. It is possible to generate a new index list
|
||||
// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
|
||||
// DO NOT! use an already existing index list.
|
||||
void (*m_setTSpace)(const SMikkTSpaceContext * pContext, const float fvTangent[], const float fvBiTangent[], const float fMagS, const float fMagT,
|
||||
const tbool bIsOrientationPreserving, const int iFace, const int iVert);
|
||||
} SMikkTSpaceInterface;
|
||||
|
||||
struct SMikkTSpaceContext
|
||||
{
|
||||
SMikkTSpaceInterface * m_pInterface; // initialized with callback functions
|
||||
void * m_pUserData; // pointer to client side mesh data etc. (passed as the first parameter with every interface call)
|
||||
};
|
||||
|
||||
// these are both thread safe!
|
||||
tbool genTangSpaceDefault(const SMikkTSpaceContext * pContext); // Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
|
||||
tbool genTangSpace(const SMikkTSpaceContext * pContext, const float fAngularThreshold);
|
||||
|
||||
|
||||
// To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal maps, the
|
||||
// normal map sampler must use the exact inverse of the pixel shader transformation.
|
||||
// The most efficient transformation we can possibly do in the pixel shader is
|
||||
// achieved by using, directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
|
||||
// pixel shader (fast transform out)
|
||||
// vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
|
||||
// where vNt is the tangent space normal. The normal map sampler must likewise use the
|
||||
// interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the pixel shader.
|
||||
// sampler does (exact inverse of pixel shader):
|
||||
// float3 row0 = cross(vB, vN);
|
||||
// float3 row1 = cross(vN, vT);
|
||||
// float3 row2 = cross(vT, vB);
|
||||
// float fSign = dot(vT, row0)<0 ? -1 : 1;
|
||||
// vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
|
||||
// where vNout is the sampled normal in some chosen 3D space.
|
||||
//
|
||||
// Should you choose to reconstruct the bitangent in the pixel shader instead
|
||||
// of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler also.
|
||||
// Finally, beware of quad triangulations. If the normal map sampler doesn't use the same triangulation of
|
||||
// quads as your renderer then problems will occur since the interpolated tangent spaces will differ
|
||||
// eventhough the vertex level tangent spaces match. This can be solved either by triangulating before
|
||||
// sampling/exporting or by using the order-independent choice of diagonal for splitting quads suggested earlier.
|
||||
// However, this must be used both by the sampler and your tools/rendering pipeline.
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@ -1,136 +0,0 @@
|
||||
|
||||
#![allow(bad_style)]
|
||||
|
||||
use std::os::raw::*;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub type tbool = c_int;
|
||||
|
||||
#[allow(dead_code)]
|
||||
const TFALSE: tbool = 0;
|
||||
|
||||
#[allow(dead_code)]
|
||||
const TTRUE: tbool = 1;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct SMikkTSpaceInterface {
|
||||
/// Returns the number of faces (triangles/quads) on the mesh to be processed.
|
||||
pub m_getNumFaces: extern "C" fn(pContext: *const SMikkTSpaceContext) -> c_int,
|
||||
|
||||
/// Returns the number of vertices on face number iFace
|
||||
/// iFace is a number in the range {0, 1, ..., getNumFaces()-1}
|
||||
pub m_getNumVerticesOfFace: extern "C" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
iFace: c_int,
|
||||
) -> c_int,
|
||||
|
||||
/// Returns the position of the referenced face of vertex number
|
||||
/// iVert, in the range {0,1,2} for triangles, and {0,1,2,3} for quads.
|
||||
pub m_getPosition: extern "C" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
fvPosOut: *mut c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
),
|
||||
|
||||
/// Returns the normal of the referenced face of vertex number
|
||||
/// iVert, in the range {0,1,2} for triangles, and {0,1,2,3} for quads.
|
||||
pub m_getNormal: extern "C" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
fvNormOut: *mut c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
),
|
||||
|
||||
/// Returns the texcoord of the referenced face of vertex number
|
||||
/// iVert, in the range {0,1,2} for triangles, and {0,1,2,3} for quads.
|
||||
pub m_getTexCoord: extern "C" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
fvTexcOut: *mut c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
),
|
||||
|
||||
/// either (or both) of the two setTSpace callbacks can be set.
|
||||
/// The call-back m_setTSpaceBasic() is sufficient for basic normal mapping.
|
||||
|
||||
/// This function is used to return the tangent and fSign to the application.
|
||||
/// fvTangent is a unit length vector.
|
||||
/// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
|
||||
/// bitangent = fSign * cross(vN, tangent);
|
||||
/// Note that the results are returned unindexed. It is possible to generate a new index list
|
||||
/// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
|
||||
/// DO NOT! use an already existing index list.
|
||||
pub m_setTSpaceBasic: extern "C" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
fvTangent: *const c_float,
|
||||
fSign: *const c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
),
|
||||
|
||||
/// This function is used to return tangent space results to the application.
|
||||
/// fvTangent and fvBiTangent are unit length vectors and fMagS and fMagT are their
|
||||
/// true magnitudes which can be used for relief mapping effects.
|
||||
/// fvBiTangent is the "real" bitangent and thus may not be perpendicular to fvTangent.
|
||||
/// However, both are perpendicular to the vertex normal.
|
||||
/// For normal maps it is sufficient to use the following simplified version of the bitangent which is generated at pixel/vertex level.
|
||||
/// fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
|
||||
/// bitangent = fSign * cross(vN, tangent);
|
||||
/// Note that the results are returned unindexed. It is possible to generate a new index list
|
||||
/// But averaging/overwriting tangent spaces by using an already existing index list WILL produce INCRORRECT results.
|
||||
/// DO NOT! use an already existing index list.
|
||||
pub m_setTSpace: extern "C" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
fvTangent: *const c_float,
|
||||
fvBiTangent: *const c_float,
|
||||
fMagS: *const c_float,
|
||||
fMagT: *const c_float,
|
||||
bIsOrientationPreserving: tbool,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
),
|
||||
}
|
||||
|
||||
/// these are both thread safe!
|
||||
/// Default (recommended) fAngularThreshold is 180 degrees (which means threshold disabled)
|
||||
pub type genTangSpaceDefault = extern "system" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
) -> tbool;
|
||||
|
||||
pub type genTangSpace = extern "system" fn(
|
||||
pContext: *const SMikkTSpaceContext,
|
||||
fAngularThreshold: c_float,
|
||||
) -> tbool;
|
||||
|
||||
/// To avoid visual errors (distortions/unwanted hard edges in lighting), when using sampled normal maps, the
|
||||
/// normal map sampler must use the exact inverse of the pixel shader transformation.
|
||||
/// The most efficient transformation we can possibly do in the pixel shader is
|
||||
/// achieved by using, directly, the "unnormalized" interpolated tangent, bitangent and vertex normal: vT, vB and vN.
|
||||
/// pixel shader (fast transform out)
|
||||
/// vNout = normalize( vNt.x * vT + vNt.y * vB + vNt.z * vN );
|
||||
/// where vNt is the tangent space normal. The normal map sampler must likewise use the
|
||||
/// interpolated and "unnormalized" tangent, bitangent and vertex normal to be compliant with the pixel shader.
|
||||
/// sampler does (exact inverse of pixel shader):
|
||||
/// float3 row0 = cross(vB, vN);
|
||||
/// float3 row1 = cross(vN, vT);
|
||||
/// float3 row2 = cross(vT, vB);
|
||||
/// float fSign = dot(vT, row0)<0 ? -1 : 1;
|
||||
/// vNt = normalize( fSign * float3(dot(vNout,row0), dot(vNout,row1), dot(vNout,row2)) );
|
||||
/// where vNout is the sampled normal in some chosen 3D space.
|
||||
///
|
||||
/// Should you choose to reconstruct the bitangent in the pixel shader instead
|
||||
/// of the vertex shader, as explained earlier, then be sure to do this in the normal map sampler also.
|
||||
/// Finally, beware of quad triangulations. If the normal map sampler doesn't use the same triangulation of
|
||||
/// quads as your renderer then problems will occur since the interpolated tangent spaces will differ
|
||||
/// eventhough the vertex level tangent spaces match. This can be solved either by triangulating before
|
||||
/// sampling/exporting or by using the order-independent choice of diagonal for splitting quads suggested earlier.
|
||||
/// However, this must be used both by the sampler and your tools/rendering pipeline.
|
||||
|
||||
#[repr(C)]
|
||||
pub struct SMikkTSpaceContext {
|
||||
/// initialized with callback functions
|
||||
pub m_pInterface: *const SMikkTSpaceInterface,
|
||||
/// pointer to client side mesh data etc. (passed as the first parameter with every interface call)
|
||||
pub m_pUserData: *const c_void,
|
||||
}
|
||||
@ -1,129 +0,0 @@
|
||||
|
||||
mod ffi;
|
||||
|
||||
use std::os::raw::*;
|
||||
use std::mem;
|
||||
|
||||
const INTERFACE: ffi::SMikkTSpaceInterface = ffi::SMikkTSpaceInterface {
|
||||
m_getNumFaces: faces,
|
||||
m_getNumVerticesOfFace: vertices,
|
||||
m_getPosition: position,
|
||||
m_getNormal: normal,
|
||||
m_getTexCoord: tex_coord,
|
||||
m_setTSpaceBasic: set_tspace_basic,
|
||||
m_setTSpace: set_tspace,
|
||||
};
|
||||
|
||||
pub struct Context {
|
||||
faces: i32,
|
||||
}
|
||||
|
||||
/// Returns the number of faces (triangles/quads) on the mesh to be processed.
|
||||
#[no_mangle]
|
||||
extern "C" fn faces(pContext: *const ffi::SMikkTSpaceContext) -> c_int {
|
||||
unsafe {
|
||||
let m: *const Context = mem::transmute(pContext);
|
||||
(*m).faces as c_int
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of vertices on face number iFace
|
||||
/// iFace is a number in the range {0, 1, ..., getNumFaces()-1}
|
||||
#[no_mangle]
|
||||
extern "C" fn vertices(
|
||||
pContext: *const ffi::SMikkTSpaceContext,
|
||||
iFace: c_int,
|
||||
) -> c_int {
|
||||
unsafe {
|
||||
let _: *const Context = mem::transmute(pContext);
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the position of the referenced face of vertex number
|
||||
/// iVert, in the range {0,1,2} for triangles, and {0,1,2,3} for quads.
|
||||
#[no_mangle]
|
||||
extern "C" fn position(
|
||||
pContext: *const ffi::SMikkTSpaceContext,
|
||||
fvPosOut: *mut c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
) {
|
||||
unsafe {
|
||||
let _: *const Context = mem::transmute(pContext);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the normal of the referenced face of vertex number
|
||||
/// iVert, in the range {0,1,2} for triangles, and {0,1,2,3} for quads.
|
||||
#[no_mangle]
|
||||
extern "C" fn normal(
|
||||
pContext: *const ffi::SMikkTSpaceContext,
|
||||
fvPosOut: *mut c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
) {
|
||||
unsafe {
|
||||
let _: *const Context = mem::transmute(pContext);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the texcoord of the referenced face of vertex number
|
||||
/// iVert, in the range {0,1,2} for triangles, and {0,1,2,3} for quads.
|
||||
#[no_mangle]
|
||||
extern "C" fn tex_coord(
|
||||
pContext: *const ffi::SMikkTSpaceContext,
|
||||
fvTexcOut: *mut c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
) {
|
||||
unsafe {
|
||||
let _: *const Context = mem::transmute(pContext);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the tangent and its sign to the application.
|
||||
#[no_mangle]
|
||||
extern "C" fn set_tspace_basic(
|
||||
pContext: *const ffi::SMikkTSpaceContext,
|
||||
fvTangent: *const c_float,
|
||||
fSign: *const c_float,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
) {
|
||||
unsafe {
|
||||
let _: *const Context = mem::transmute(pContext);
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns tangent space results to the application.
|
||||
#[no_mangle]
|
||||
extern "C" fn set_tspace(
|
||||
pContext: *const ffi::SMikkTSpaceContext,
|
||||
fvTangent: *const c_float,
|
||||
fvBiTangent: *const c_float,
|
||||
fMagS: *const c_float,
|
||||
fMagT: *const c_float,
|
||||
bIsOrientationPreserving: ffi::tbool,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
) {
|
||||
unsafe {
|
||||
let _: *const Context = mem::transmute(pContext);
|
||||
}
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn new() -> Self {
|
||||
Context {
|
||||
faces: 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[test]
|
||||
fn it_works() {
|
||||
}
|
||||
}
|
||||
19
src/lib.rs
19
src/lib.rs
@ -17,7 +17,9 @@ const INTERFACE: ffi::SMikkTSpaceInterface = ffi::SMikkTSpaceInterface {
|
||||
m_setTSpace: set_tspace,
|
||||
};
|
||||
|
||||
/// Rust front-end API for tangent generation.
|
||||
struct Closures<'a> {
|
||||
/// Returns the number of vertices per face.
|
||||
pub vertices_per_face: &'a Fn() -> usize,
|
||||
|
||||
/// Returns the number of faces.
|
||||
@ -124,19 +126,23 @@ extern "C" fn set_tspace_basic(
|
||||
|
||||
/// Returns tangent space results to the application.
|
||||
extern "C" fn set_tspace(
|
||||
_pContext: *mut ffi::SMikkTSpaceContext,
|
||||
_fvTangent: *const c_float,
|
||||
pContext: *mut ffi::SMikkTSpaceContext,
|
||||
fvTangent: *const c_float,
|
||||
_fvBiTangent: *const c_float,
|
||||
_fMagS: *const c_float,
|
||||
_fMagT: *const c_float,
|
||||
_bIsOrientationPreserving: ffi::tbool,
|
||||
_iFace: c_int,
|
||||
_iVert: c_int,
|
||||
bIsOrientationPreserving: ffi::tbool,
|
||||
iFace: c_int,
|
||||
iVert: c_int,
|
||||
) {
|
||||
unimplemented!()
|
||||
const POSITIVE: f32 = 1.0;
|
||||
const NEGATIVE: f32 = -1.0;
|
||||
let fSign = if bIsOrientationPreserving != 0 { &POSITIVE } else { &NEGATIVE };
|
||||
set_tspace_basic(pContext, fvTangent, fSign, iFace, iVert);
|
||||
}
|
||||
|
||||
impl<'a> Closures<'a> {
|
||||
/// Generates tangents.
|
||||
pub fn generate(mut self) -> bool {
|
||||
let ctx = ffi::SMikkTSpaceContext {
|
||||
m_pInterface: &INTERFACE,
|
||||
@ -148,6 +154,7 @@ impl<'a> Closures<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Generates tangents.
|
||||
pub fn generate<'a>(
|
||||
vertices_per_face: &'a Fn() -> usize,
|
||||
face_count: &'a Fn() -> usize,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user