Added an experimental MikkTspace generator
This commit is contained in:
		
							parent
							
								
									963bbd42e5
								
							
						
					
					
						commit
						44601cf5ff
					
				| @ -0,0 +1,97 @@ | |||||||
|  | /* | ||||||
|  |  * To change this license header, choose License Headers in Project Properties. | ||||||
|  |  * To change this template file, choose Tools | Templates | ||||||
|  |  * and open the template in the editor. | ||||||
|  |  */ | ||||||
|  | package com.jme3.util.mikktspace; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @author Nehon | ||||||
|  |  */ | ||||||
|  | public interface MikkTSpaceContext { | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Returns the number of faces (triangles/quads) on the mesh to be | ||||||
|  |      * processed. | ||||||
|  |      * | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     public int getNumFaces(); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * Returns the number of vertices on face number iFace iFace is a number in | ||||||
|  |      * the range {0, 1, ..., getNumFaces()-1} | ||||||
|  |      * | ||||||
|  |      * @param face | ||||||
|  |      * @return | ||||||
|  |      */ | ||||||
|  |     public int getNumVerticesOfFace(int face); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * 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. | ||||||
|  |      * | ||||||
|  |      * @param posOut | ||||||
|  |      * @param face | ||||||
|  |      * @param vert | ||||||
|  |      */ | ||||||
|  |     public void getPosition(float posOut[], int face, int vert); | ||||||
|  | 
 | ||||||
|  |     public void getNormal(float normOut[], int face, int vert); | ||||||
|  | 
 | ||||||
|  |     public void getTexCoord(float texOut[], int face, int vert); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * The call-backsetTSpaceBasic() is sufficient for basic normal mapping. | ||||||
|  |      * This function is used to return the tangent and sign to the application. | ||||||
|  |      * tangent 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. | ||||||
|  |      * | ||||||
|  |      * @param tangent | ||||||
|  |      * @param sign | ||||||
|  |      * @param face | ||||||
|  |      * @param vert | ||||||
|  |      */ | ||||||
|  |     public void setTSpaceBasic(float tangent[], float sign, int face, int vert); | ||||||
|  | 
 | ||||||
|  |     /** | ||||||
|  |      * This function is used to return tangent space results to the application. | ||||||
|  |      * tangent and biTangent are unit length vectors and fMagS and fMagT are | ||||||
|  |      * their true magnitudes which can be used for relief mapping effects. | ||||||
|  |      * | ||||||
|  |      * biTangent is the "real" bitangent and thus may not be perpendicular to | ||||||
|  |      * tangent. 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. | ||||||
|  |      * | ||||||
|  |      * <pre> | ||||||
|  |      * fSign = bIsOrientationPreserving ? 1.0f : (-1.0f); | ||||||
|  |      * bitangent = fSign * cross(vN, tangent); | ||||||
|  |      * </pre> | ||||||
|  |      * | ||||||
|  |      * 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. | ||||||
|  |      * | ||||||
|  |      * @param tangent | ||||||
|  |      * @param biTangent | ||||||
|  |      * @param magS | ||||||
|  |      * @param magT | ||||||
|  |      * @param isOrientationPreserving | ||||||
|  |      * @param face | ||||||
|  |      * @param vert | ||||||
|  |      */ | ||||||
|  |     void setTSpace(float tangent[], float biTangent[], float magS, float magT, | ||||||
|  |             boolean isOrientationPreserving, int face, int vert); | ||||||
|  | } | ||||||
| @ -0,0 +1,100 @@ | |||||||
|  | /* | ||||||
|  |  * To change this license header, choose License Headers in Project Properties. | ||||||
|  |  * To change this template file, choose Tools | Templates | ||||||
|  |  * and open the template in the editor. | ||||||
|  |  */ | ||||||
|  | package com.jme3.util.mikktspace; | ||||||
|  | 
 | ||||||
|  | import com.jme3.scene.Mesh; | ||||||
|  | import com.jme3.scene.VertexBuffer; | ||||||
|  | import com.jme3.scene.mesh.IndexBuffer; | ||||||
|  | import com.jme3.util.BufferUtils; | ||||||
|  | import java.nio.FloatBuffer; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * | ||||||
|  |  * @author Nehon | ||||||
|  |  */ | ||||||
|  | public class MikkTSpaceImpl implements MikkTSpaceContext { | ||||||
|  | 
 | ||||||
|  |     Mesh mesh; | ||||||
|  | 
 | ||||||
|  |     public MikkTSpaceImpl(Mesh mesh) { | ||||||
|  |         this.mesh = mesh; | ||||||
|  |         VertexBuffer tangentBuffer = mesh.getBuffer(VertexBuffer.Type.Tangent); | ||||||
|  |         if(tangentBuffer == null){ | ||||||
|  |             FloatBuffer fb = BufferUtils.createFloatBuffer(mesh.getVertexCount() * 4); | ||||||
|  |             mesh.setBuffer(VertexBuffer.Type.Tangent, 4, fb);             | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         //TODO ensure the Tangent buffer exists, else create one. | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public int getNumFaces() { | ||||||
|  |         return mesh.getTriangleCount();         | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public int getNumVerticesOfFace(int face) { | ||||||
|  |         return 3; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void getPosition(float[] posOut, int face, int vert) { | ||||||
|  |         int vertIndex = getIndex(face, vert); | ||||||
|  |         VertexBuffer position = mesh.getBuffer(VertexBuffer.Type.Position); | ||||||
|  |         FloatBuffer pos = (FloatBuffer) position.getData(); | ||||||
|  |         pos.position(vertIndex * 3); | ||||||
|  |         posOut[0] = pos.get(); | ||||||
|  |         posOut[1] = pos.get(); | ||||||
|  |         posOut[2] = pos.get(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void getNormal(float[] normOut, int face, int vert) { | ||||||
|  |         int vertIndex = getIndex(face, vert); | ||||||
|  |         VertexBuffer normal = mesh.getBuffer(VertexBuffer.Type.Normal); | ||||||
|  |         FloatBuffer norm = (FloatBuffer) normal.getData(); | ||||||
|  |         norm.position(vertIndex * 3); | ||||||
|  |         normOut[0] = norm.get(); | ||||||
|  |         normOut[1] = norm.get(); | ||||||
|  |         normOut[2] = norm.get(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void getTexCoord(float[] texOut, int face, int vert) { | ||||||
|  |         int vertIndex = getIndex(face, vert); | ||||||
|  |         VertexBuffer texCoord = mesh.getBuffer(VertexBuffer.Type.TexCoord); | ||||||
|  |         FloatBuffer tex = (FloatBuffer) texCoord.getData(); | ||||||
|  |         tex.position(vertIndex * 2); | ||||||
|  |         texOut[0] = tex.get(); | ||||||
|  |         texOut[1] = tex.get();         | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void setTSpaceBasic(float[] tangent, float sign, int face, int vert) { | ||||||
|  |         int vertIndex = getIndex(face, vert); | ||||||
|  |         VertexBuffer tangentBuffer = mesh.getBuffer(VertexBuffer.Type.Tangent); | ||||||
|  |         FloatBuffer tan = (FloatBuffer) tangentBuffer.getData(); | ||||||
|  |          | ||||||
|  |         tan.position(vertIndex * 4); | ||||||
|  |         tan.put(tangent); | ||||||
|  |         tan.put(sign); | ||||||
|  |          | ||||||
|  |         tan.rewind(); | ||||||
|  |         tangentBuffer.setUpdateNeeded(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public void setTSpace(float[] tangent, float[] biTangent, float magS, float magT, boolean isOrientationPreserving, int face, int vert) { | ||||||
|  |         //Do nothing | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     private int getIndex(int face, int vert) { | ||||||
|  |         IndexBuffer index = mesh.getIndexBuffer(); | ||||||
|  |         int vertIndex = index.get(face * 3 + vert); | ||||||
|  |         return vertIndex; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  | } | ||||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user