use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.
the class CubeMapWrapper method getPixel.
/**
* Reads a pixel from the cube map given the coordinate vector
* @param vector the direction vector to fetch the texel
* @param store the color in which to store the pixel color read.
* @return the color of the pixel read.
*/
public ColorRGBA getPixel(Vector3f vector, ColorRGBA store) {
if (store == null) {
store = new ColorRGBA();
}
int face = EnvMapUtils.getCubemapFaceTexCoordFromVector(vector, sizes[0], uvs, EnvMapUtils.FixSeamsMethod.Stretch);
raster.setSlice(face);
return raster.getPixel((int) uvs.x, (int) uvs.y, store);
}
use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.
the class BillboardControl method rotateCameraAligned.
/**
* Aligns this Billboard so that it points to the camera position.
*
* @param camera
* Camera
*/
private void rotateCameraAligned(Camera camera) {
look.set(camera.getLocation()).subtractLocal(spatial.getWorldTranslation());
// coopt left for our own purposes.
Vector3f xzp = left;
// The xzp vector is the projection of the look vector on the xz plane
xzp.set(look.x, 0, look.z);
// check for undefined rotation...
if (xzp.equals(Vector3f.ZERO)) {
return;
}
look.normalizeLocal();
xzp.normalizeLocal();
float cosp = look.dot(xzp);
// compute the local orientation matrix for the billboard
orient.set(0, 0, xzp.z);
orient.set(0, 1, xzp.x * -look.y);
orient.set(0, 2, xzp.x * cosp);
orient.set(1, 0, 0);
orient.set(1, 1, cosp);
orient.set(1, 2, look.y);
orient.set(2, 0, -xzp.x);
orient.set(2, 1, xzp.z * -look.y);
orient.set(2, 2, xzp.z * cosp);
// The billboard must be oriented to face the camera before it is
// transformed into the world.
spatial.setLocalRotation(orient);
fixRefreshFlags();
}
use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.
the class KTXLoader method load.
private Image load(InputStream stream) {
byte[] fileId = new byte[12];
DataInput in = new DataInputStream(stream);
try {
stream.read(fileId, 0, 12);
if (!checkFileIdentifier(fileId)) {
throw new IllegalArgumentException("Unrecognized ktx file identifier : " + new String(fileId) + " should be " + new String(fileIdentifier));
}
int endianness = in.readInt();
//opposite endianness
if (endianness == 0x01020304) {
in = new LittleEndien(stream);
}
int glType = in.readInt();
int glTypeSize = in.readInt();
int glFormat = in.readInt();
int glInternalFormat = in.readInt();
int glBaseInternalFormat = in.readInt();
int pixelWidth = in.readInt();
int pixelHeight = in.readInt();
int pixelDepth = in.readInt();
int numberOfArrayElements = in.readInt();
int numberOfFaces = in.readInt();
int numberOfMipmapLevels = in.readInt();
int bytesOfKeyValueData = in.readInt();
log.log(Level.FINE, "glType = {0}", glType);
log.log(Level.FINE, "glTypeSize = {0}", glTypeSize);
log.log(Level.FINE, "glFormat = {0}", glFormat);
log.log(Level.FINE, "glInternalFormat = {0}", glInternalFormat);
log.log(Level.FINE, "glBaseInternalFormat = {0}", glBaseInternalFormat);
log.log(Level.FINE, "pixelWidth = {0}", pixelWidth);
log.log(Level.FINE, "pixelHeight = {0}", pixelHeight);
log.log(Level.FINE, "pixelDepth = {0}", pixelDepth);
log.log(Level.FINE, "numberOfArrayElements = {0}", numberOfArrayElements);
log.log(Level.FINE, "numberOfFaces = {0}", numberOfFaces);
log.log(Level.FINE, "numberOfMipmapLevels = {0}", numberOfMipmapLevels);
log.log(Level.FINE, "bytesOfKeyValueData = {0}", bytesOfKeyValueData);
if ((numberOfFaces > 1 && pixelDepth > 1) || (numberOfFaces > 1 && numberOfArrayElements > 1) || (pixelDepth > 1 && numberOfArrayElements > 1)) {
throw new UnsupportedOperationException("jME doesn't support cube maps of 3D textures or arrays of 3D texture or arrays of cube map of 3d textures");
}
PixelReader pixelReader = parseMetaData(bytesOfKeyValueData, in);
if (pixelReader == null) {
pixelReader = new SrTuRoPixelReader();
}
//some of the values may be 0 we need them at least to be 1
pixelDepth = Math.max(1, pixelDepth);
numberOfArrayElements = Math.max(1, numberOfArrayElements);
numberOfFaces = Math.max(1, numberOfFaces);
numberOfMipmapLevels = Math.max(1, numberOfMipmapLevels);
int nbSlices = Math.max(numberOfFaces, numberOfArrayElements);
Image.Format imgFormat = getImageFormat(glFormat, glInternalFormat, glType);
log.log(Level.FINE, "img format {0}", imgFormat.toString());
int bytePerPixel = imgFormat.getBitsPerPixel() / 8;
int byteBuffersSize = computeBuffersSize(numberOfMipmapLevels, pixelWidth, pixelHeight, bytePerPixel, pixelDepth);
log.log(Level.FINE, "data size {0}", byteBuffersSize);
int[] mipMapSizes = new int[numberOfMipmapLevels];
Image image = createImage(nbSlices, byteBuffersSize, imgFormat, pixelWidth, pixelHeight, pixelDepth);
byte[] pixelData = new byte[bytePerPixel];
int offset = 0;
//iterate over data
for (int mipLevel = 0; mipLevel < numberOfMipmapLevels; mipLevel++) {
//size of the image in byte.
//this value is bogus in many example, when using mipmaps.
//instead we compute the theorical size and display a warning when it does not match.
int fileImageSize = in.readInt();
int width = Math.max(1, pixelWidth >> mipLevel);
int height = Math.max(1, pixelHeight >> mipLevel);
int imageSize = width * height * bytePerPixel;
mipMapSizes[mipLevel] = imageSize;
log.log(Level.FINE, "current mip size {0}", imageSize);
if (fileImageSize != imageSize) {
log.log(Level.WARNING, "Mip map size is wrong in the file for mip level {0} size is {1} should be {2}", new Object[] { mipLevel, fileImageSize, imageSize });
}
for (int arrayElem = 0; arrayElem < numberOfArrayElements; arrayElem++) {
for (int face = 0; face < numberOfFaces; face++) {
int nbPixelRead = 0;
for (int depth = 0; depth < pixelDepth; depth++) {
ByteBuffer byteBuffer = image.getData(getSlice(face, arrayElem));
log.log(Level.FINE, "position {0}", byteBuffer.position());
byteBuffer.position(offset);
nbPixelRead = pixelReader.readPixels(width, height, pixelData, byteBuffer, in);
}
//cube padding
if (numberOfFaces == 6 && numberOfArrayElements == 0) {
in.skipBytes(3 - ((nbPixelRead + 3) % 4));
}
}
}
//mip padding
log.log(Level.FINE, "skipping {0}", (3 - ((imageSize + 3) % 4)));
in.skipBytes(3 - ((imageSize + 3) % 4));
offset += imageSize;
}
//there are loaded mip maps we set the sizes
if (numberOfMipmapLevels > 1) {
image.setMipMapSizes(mipMapSizes);
}
//if 3D texture and slices' orientation is inside, we reverse the data array.
if (pixelDepth > 1 && slicesInside) {
Collections.reverse(image.getData());
}
return image;
} catch (IOException ex) {
Logger.getLogger(KTXLoader.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.
the class KTXWriter method write.
/**
* Writes an image with the given params
*
* textureType, allows one to write textureArrays, Texture3D, and TextureCubeMaps.
* Texture2D will write a 2D image.
* Note that the fileName should contain the extension (.ktx sounds like a wise choice)
* @param image the image to write
* @param textureType the texture type
* @param fileName the name of the file to write
*/
public void write(Image image, Class<? extends Texture> textureType, String fileName) {
FileOutputStream outs = null;
try {
File file = new File(filePath + "/" + fileName);
outs = new FileOutputStream(file);
DataOutput out = new DataOutputStream(outs);
//fileID
out.write(fileIdentifier);
//endianness
out.writeInt(0x04030201);
GLImageFormat format = getGlFormat(image.getFormat());
//glType
out.writeInt(format.dataType);
//glTypeSize
out.writeInt(1);
//glFormat
out.writeInt(format.format);
//glInernalFormat
out.writeInt(format.internalFormat);
//glBaseInternalFormat
out.writeInt(format.format);
//pixelWidth
out.writeInt(image.getWidth());
//pixelHeight
out.writeInt(image.getHeight());
int pixelDepth = 1;
int numberOfArrayElements = 1;
int numberOfFaces = 1;
if (image.getDepth() > 1) {
//pixelDepth
if (textureType == Texture3D.class) {
pixelDepth = image.getDepth();
}
}
if (image.getData().size() > 1) {
//numberOfArrayElements
if (textureType == TextureArray.class) {
numberOfArrayElements = image.getData().size();
}
//numberOfFaces
if (textureType == TextureCubeMap.class) {
numberOfFaces = image.getData().size();
}
}
out.writeInt(pixelDepth);
out.writeInt(numberOfArrayElements);
out.writeInt(numberOfFaces);
int numberOfMipmapLevels = 1;
//numberOfMipmapLevels
if (image.hasMipmaps()) {
numberOfMipmapLevels = image.getMipMapSizes().length;
}
out.writeInt(numberOfMipmapLevels);
//bytesOfKeyValueData
String keyValues = "KTXorientation\0S=r,T=u\0";
int bytesOfKeyValueData = keyValues.length() + 4;
int padding = 3 - ((bytesOfKeyValueData + 3) % 4);
bytesOfKeyValueData += padding;
out.writeInt(bytesOfKeyValueData);
//keyAndValueByteSize
out.writeInt(bytesOfKeyValueData - 4 - padding);
//values
out.writeBytes(keyValues);
pad(padding, out);
int offset = 0;
//iterate over data
for (int mipLevel = 0; mipLevel < numberOfMipmapLevels; mipLevel++) {
int width = Math.max(1, image.getWidth() >> mipLevel);
int height = Math.max(1, image.getHeight() >> mipLevel);
int imageSize;
if (image.hasMipmaps()) {
imageSize = image.getMipMapSizes()[mipLevel];
} else {
imageSize = width * height * image.getFormat().getBitsPerPixel() / 8;
}
out.writeInt(imageSize);
for (int arrayElem = 0; arrayElem < numberOfArrayElements; arrayElem++) {
for (int face = 0; face < numberOfFaces; face++) {
int nbPixelWritten = 0;
for (int depth = 0; depth < pixelDepth; depth++) {
ByteBuffer byteBuffer = image.getData(getSlice(face, arrayElem));
// BufferUtils.ensureLargeEnough(byteBuffer, imageSize);
log.log(Level.FINE, "position {0}", byteBuffer.position());
byteBuffer.position(offset);
byte[] b = getByteBufferArray(byteBuffer, imageSize);
out.write(b);
nbPixelWritten = b.length;
}
//cube padding
if (numberOfFaces == 6 && numberOfArrayElements == 0) {
padding = 3 - ((nbPixelWritten + 3) % 4);
pad(padding, out);
}
}
}
//mip padding
log.log(Level.FINE, "skipping {0}", (3 - ((imageSize + 3) % 4)));
padding = 3 - ((imageSize + 3) % 4);
pad(padding, out);
offset += imageSize;
}
} catch (FileNotFoundException ex) {
Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (outs != null) {
outs.close();
}
} catch (IOException ex) {
Logger.getLogger(KTXWriter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
use of com.jme3.scene.plugins.blender.meshes.Face in project jmonkeyengine by jMonkeyEngine.
the class OBJLoader method createGeometry.
protected Geometry createGeometry(ArrayList<Face> faceList, String matName) throws IOException {
if (faceList.isEmpty())
throw new IOException("No geometry data to generate mesh");
// Create mesh from the faces
Mesh mesh = constructMesh(faceList);
Geometry geom = new Geometry(objName + "-geom-" + (geomIndex++), mesh);
Material material = null;
if (matName != null && matList != null) {
// Get material from material list
material = matList.get(matName);
}
if (material == null) {
// create default material
material = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
material.setFloat("Shininess", 64);
}
geom.setMaterial(material);
if (material.isTransparent())
geom.setQueueBucket(Bucket.Transparent);
else
geom.setQueueBucket(Bucket.Opaque);
if (material.getMaterialDef().getName().contains("Lighting") && mesh.getFloatBuffer(Type.Normal) == null) {
logger.log(Level.WARNING, "OBJ mesh {0} doesn't contain normals! " + "It might not display correctly", geom.getName());
}
return geom;
}
Aggregations