Skip to content

Commit 44601cf

Browse files
committed
Added an experimental MikkTspace generator
1 parent 963bbd4 commit 44601cf

File tree

3 files changed

+1914
-0
lines changed

3 files changed

+1914
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.jme3.util.mikktspace;
7+
8+
/**
9+
*
10+
* @author Nehon
11+
*/
12+
public interface MikkTSpaceContext {
13+
14+
/**
15+
* Returns the number of faces (triangles/quads) on the mesh to be
16+
* processed.
17+
*
18+
* @return
19+
*/
20+
public int getNumFaces();
21+
22+
/**
23+
* Returns the number of vertices on face number iFace iFace is a number in
24+
* the range {0, 1, ..., getNumFaces()-1}
25+
*
26+
* @param face
27+
* @return
28+
*/
29+
public int getNumVerticesOfFace(int face);
30+
31+
/**
32+
* returns the position/normal/texcoord of the referenced face of vertex
33+
* number iVert. iVert is in the range {0,1,2} for triangles and {0,1,2,3}
34+
* for quads.
35+
*
36+
* @param posOut
37+
* @param face
38+
* @param vert
39+
*/
40+
public void getPosition(float posOut[], int face, int vert);
41+
42+
public void getNormal(float normOut[], int face, int vert);
43+
44+
public void getTexCoord(float texOut[], int face, int vert);
45+
46+
/**
47+
* The call-backsetTSpaceBasic() is sufficient for basic normal mapping.
48+
* This function is used to return the tangent and sign to the application.
49+
* tangent is a unit length vector. For normal maps it is sufficient to use
50+
* the following simplified version of the bitangent which is generated at
51+
* pixel/vertex level.
52+
*
53+
* bitangent = fSign * cross(vN, tangent);
54+
*
55+
* Note that the results are returned unindexed. It is possible to generate
56+
* a new index list But averaging/overwriting tangent spaces by using an
57+
* already existing index list WILL produce INCRORRECT results. DO NOT! use
58+
* an already existing index list.
59+
*
60+
* @param tangent
61+
* @param sign
62+
* @param face
63+
* @param vert
64+
*/
65+
public void setTSpaceBasic(float tangent[], float sign, int face, int vert);
66+
67+
/**
68+
* This function is used to return tangent space results to the application.
69+
* tangent and biTangent are unit length vectors and fMagS and fMagT are
70+
* their true magnitudes which can be used for relief mapping effects.
71+
*
72+
* biTangent is the "real" bitangent and thus may not be perpendicular to
73+
* tangent. However, both are perpendicular to the vertex normal. For normal
74+
* maps it is sufficient to use the following simplified version of the
75+
* bitangent which is generated at pixel/vertex level.
76+
*
77+
* <pre>
78+
* fSign = bIsOrientationPreserving ? 1.0f : (-1.0f);
79+
* bitangent = fSign * cross(vN, tangent);
80+
* </pre>
81+
*
82+
* Note that the results are returned unindexed. It is possible to generate
83+
* a new index list. But averaging/overwriting tangent spaces by using an
84+
* already existing index list WILL produce INCRORRECT results. DO NOT! use
85+
* an already existing index list.
86+
*
87+
* @param tangent
88+
* @param biTangent
89+
* @param magS
90+
* @param magT
91+
* @param isOrientationPreserving
92+
* @param face
93+
* @param vert
94+
*/
95+
void setTSpace(float tangent[], float biTangent[], float magS, float magT,
96+
boolean isOrientationPreserving, int face, int vert);
97+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.jme3.util.mikktspace;
7+
8+
import com.jme3.scene.Mesh;
9+
import com.jme3.scene.VertexBuffer;
10+
import com.jme3.scene.mesh.IndexBuffer;
11+
import com.jme3.util.BufferUtils;
12+
import java.nio.FloatBuffer;
13+
14+
/**
15+
*
16+
* @author Nehon
17+
*/
18+
public class MikkTSpaceImpl implements MikkTSpaceContext {
19+
20+
Mesh mesh;
21+
22+
public MikkTSpaceImpl(Mesh mesh) {
23+
this.mesh = mesh;
24+
VertexBuffer tangentBuffer = mesh.getBuffer(VertexBuffer.Type.Tangent);
25+
if(tangentBuffer == null){
26+
FloatBuffer fb = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 4);
27+
mesh.setBuffer(VertexBuffer.Type.Tangent, 4, fb);
28+
}
29+
30+
//TODO ensure the Tangent buffer exists, else create one.
31+
}
32+
33+
@Override
34+
public int getNumFaces() {
35+
return mesh.getTriangleCount();
36+
}
37+
38+
@Override
39+
public int getNumVerticesOfFace(int face) {
40+
return 3;
41+
}
42+
43+
@Override
44+
public void getPosition(float[] posOut, int face, int vert) {
45+
int vertIndex = getIndex(face, vert);
46+
VertexBuffer position = mesh.getBuffer(VertexBuffer.Type.Position);
47+
FloatBuffer pos = (FloatBuffer) position.getData();
48+
pos.position(vertIndex * 3);
49+
posOut[0] = pos.get();
50+
posOut[1] = pos.get();
51+
posOut[2] = pos.get();
52+
}
53+
54+
@Override
55+
public void getNormal(float[] normOut, int face, int vert) {
56+
int vertIndex = getIndex(face, vert);
57+
VertexBuffer normal = mesh.getBuffer(VertexBuffer.Type.Normal);
58+
FloatBuffer norm = (FloatBuffer) normal.getData();
59+
norm.position(vertIndex * 3);
60+
normOut[0] = norm.get();
61+
normOut[1] = norm.get();
62+
normOut[2] = norm.get();
63+
}
64+
65+
@Override
66+
public void getTexCoord(float[] texOut, int face, int vert) {
67+
int vertIndex = getIndex(face, vert);
68+
VertexBuffer texCoord = mesh.getBuffer(VertexBuffer.Type.TexCoord);
69+
FloatBuffer tex = (FloatBuffer) texCoord.getData();
70+
tex.position(vertIndex * 2);
71+
texOut[0] = tex.get();
72+
texOut[1] = tex.get();
73+
}
74+
75+
@Override
76+
public void setTSpaceBasic(float[] tangent, float sign, int face, int vert) {
77+
int vertIndex = getIndex(face, vert);
78+
VertexBuffer tangentBuffer = mesh.getBuffer(VertexBuffer.Type.Tangent);
79+
FloatBuffer tan = (FloatBuffer) tangentBuffer.getData();
80+
81+
tan.position(vertIndex * 4);
82+
tan.put(tangent);
83+
tan.put(sign);
84+
85+
tan.rewind();
86+
tangentBuffer.setUpdateNeeded();
87+
}
88+
89+
@Override
90+
public void setTSpace(float[] tangent, float[] biTangent, float magS, float magT, boolean isOrientationPreserving, int face, int vert) {
91+
//Do nothing
92+
}
93+
94+
private int getIndex(int face, int vert) {
95+
IndexBuffer index = mesh.getIndexBuffer();
96+
int vertIndex = index.get(face * 3 + vert);
97+
return vertIndex;
98+
}
99+
100+
}

0 commit comments

Comments
 (0)