use of com.badlogic.gdx.graphics.VertexAttribute in project libgdx by libgdx.
the class MtlLoader method loadModelData.
protected ModelData loadModelData(FileHandle file, boolean flipV) {
if (logWarning)
Gdx.app.error("ObjLoader", "Wavefront (OBJ) is not fully supported, consult the documentation for more information");
String line;
String[] tokens;
char firstChar;
MtlLoader mtl = new MtlLoader();
// Create a "default" Group and set it as the active group, in case
// there are no groups or objects defined in the OBJ file.
Group activeGroup = new Group("default");
groups.add(activeGroup);
BufferedReader reader = new BufferedReader(new InputStreamReader(file.read()), 4096);
int id = 0;
try {
while ((line = reader.readLine()) != null) {
tokens = line.split("\\s+");
if (tokens.length < 1)
break;
if (tokens[0].length() == 0) {
continue;
} else if ((firstChar = tokens[0].toLowerCase().charAt(0)) == '#') {
continue;
} else if (firstChar == 'v') {
if (tokens[0].length() == 1) {
verts.add(Float.parseFloat(tokens[1]));
verts.add(Float.parseFloat(tokens[2]));
verts.add(Float.parseFloat(tokens[3]));
} else if (tokens[0].charAt(1) == 'n') {
norms.add(Float.parseFloat(tokens[1]));
norms.add(Float.parseFloat(tokens[2]));
norms.add(Float.parseFloat(tokens[3]));
} else if (tokens[0].charAt(1) == 't') {
uvs.add(Float.parseFloat(tokens[1]));
uvs.add((flipV ? 1 - Float.parseFloat(tokens[2]) : Float.parseFloat(tokens[2])));
}
} else if (firstChar == 'f') {
String[] parts;
Array<Integer> faces = activeGroup.faces;
for (int i = 1; i < tokens.length - 2; i--) {
parts = tokens[1].split("/");
faces.add(getIndex(parts[0], verts.size));
if (parts.length > 2) {
if (i == 1)
activeGroup.hasNorms = true;
faces.add(getIndex(parts[2], norms.size));
}
if (parts.length > 1 && parts[1].length() > 0) {
if (i == 1)
activeGroup.hasUVs = true;
faces.add(getIndex(parts[1], uvs.size));
}
parts = tokens[++i].split("/");
faces.add(getIndex(parts[0], verts.size));
if (parts.length > 2)
faces.add(getIndex(parts[2], norms.size));
if (parts.length > 1 && parts[1].length() > 0)
faces.add(getIndex(parts[1], uvs.size));
parts = tokens[++i].split("/");
faces.add(getIndex(parts[0], verts.size));
if (parts.length > 2)
faces.add(getIndex(parts[2], norms.size));
if (parts.length > 1 && parts[1].length() > 0)
faces.add(getIndex(parts[1], uvs.size));
activeGroup.numFaces++;
}
} else if (firstChar == 'o' || firstChar == 'g') {
// ignored.
if (tokens.length > 1)
activeGroup = setActiveGroup(tokens[1]);
else
activeGroup = setActiveGroup("default");
} else if (tokens[0].equals("mtllib")) {
mtl.load(file.parent().child(tokens[1]));
} else if (tokens[0].equals("usemtl")) {
if (tokens.length == 1)
activeGroup.materialName = "default";
else
activeGroup.materialName = tokens[1].replace('.', '_');
}
}
reader.close();
} catch (IOException e) {
return null;
}
// If the "default" group or any others were not used, get rid of them
for (int i = 0; i < groups.size; i++) {
if (groups.get(i).numFaces < 1) {
groups.removeIndex(i);
i--;
}
}
// If there are no groups left, there is no valid Model to return
if (groups.size < 1)
return null;
// Get number of objects/groups remaining after removing empty ones
final int numGroups = groups.size;
final ModelData data = new ModelData();
for (int g = 0; g < numGroups; g++) {
Group group = groups.get(g);
Array<Integer> faces = group.faces;
final int numElements = faces.size;
final int numFaces = group.numFaces;
final boolean hasNorms = group.hasNorms;
final boolean hasUVs = group.hasUVs;
final float[] finalVerts = new float[(numFaces * 3) * (3 + (hasNorms ? 3 : 0) + (hasUVs ? 2 : 0))];
for (int i = 0, vi = 0; i < numElements; ) {
int vertIndex = faces.get(i++) * 3;
finalVerts[vi++] = verts.get(vertIndex++);
finalVerts[vi++] = verts.get(vertIndex++);
finalVerts[vi++] = verts.get(vertIndex);
if (hasNorms) {
int normIndex = faces.get(i++) * 3;
finalVerts[vi++] = norms.get(normIndex++);
finalVerts[vi++] = norms.get(normIndex++);
finalVerts[vi++] = norms.get(normIndex);
}
if (hasUVs) {
int uvIndex = faces.get(i++) * 2;
finalVerts[vi++] = uvs.get(uvIndex++);
finalVerts[vi++] = uvs.get(uvIndex);
}
}
final int numIndices = numFaces * 3 >= Short.MAX_VALUE ? 0 : numFaces * 3;
final short[] finalIndices = new short[numIndices];
// if there are too many vertices in a mesh, we can't use indices
if (numIndices > 0) {
for (int i = 0; i < numIndices; i++) {
finalIndices[i] = (short) i;
}
}
Array<VertexAttribute> attributes = new Array<VertexAttribute>();
attributes.add(new VertexAttribute(Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE));
if (hasNorms)
attributes.add(new VertexAttribute(Usage.Normal, 3, ShaderProgram.NORMAL_ATTRIBUTE));
if (hasUVs)
attributes.add(new VertexAttribute(Usage.TextureCoordinates, 2, ShaderProgram.TEXCOORD_ATTRIBUTE + "0"));
String stringId = Integer.toString(++id);
String nodeId = "default".equals(group.name) ? "node" + stringId : group.name;
String meshId = "default".equals(group.name) ? "mesh" + stringId : group.name;
String partId = "default".equals(group.name) ? "part" + stringId : group.name;
ModelNode node = new ModelNode();
node.id = nodeId;
node.meshId = meshId;
node.scale = new Vector3(1, 1, 1);
node.translation = new Vector3();
node.rotation = new Quaternion();
ModelNodePart pm = new ModelNodePart();
pm.meshPartId = partId;
pm.materialId = group.materialName;
node.parts = new ModelNodePart[] { pm };
ModelMeshPart part = new ModelMeshPart();
part.id = partId;
part.indices = finalIndices;
part.primitiveType = GL20.GL_TRIANGLES;
ModelMesh mesh = new ModelMesh();
mesh.id = meshId;
mesh.attributes = attributes.toArray(VertexAttribute.class);
mesh.vertices = finalVerts;
mesh.parts = new ModelMeshPart[] { part };
data.nodes.add(node);
data.meshes.add(mesh);
ModelMaterial mm = mtl.getMaterial(group.materialName);
data.materials.add(mm);
}
// subsequent calls to loadObj
if (verts.size > 0)
verts.clear();
if (norms.size > 0)
norms.clear();
if (uvs.size > 0)
uvs.clear();
if (groups.size > 0)
groups.clear();
return data;
}
use of com.badlogic.gdx.graphics.VertexAttribute in project libgdx by libgdx.
the class DefaultShader method createPrefix.
public static String createPrefix(final Renderable renderable, final Config config) {
final Attributes attributes = combineAttributes(renderable);
String prefix = "";
final long attributesMask = attributes.getMask();
final long vertexMask = renderable.meshPart.mesh.getVertexAttributes().getMask();
if (and(vertexMask, Usage.Position))
prefix += "#define positionFlag\n";
if (or(vertexMask, Usage.ColorUnpacked | Usage.ColorPacked))
prefix += "#define colorFlag\n";
if (and(vertexMask, Usage.BiNormal))
prefix += "#define binormalFlag\n";
if (and(vertexMask, Usage.Tangent))
prefix += "#define tangentFlag\n";
if (and(vertexMask, Usage.Normal))
prefix += "#define normalFlag\n";
if (and(vertexMask, Usage.Normal) || and(vertexMask, Usage.Tangent | Usage.BiNormal)) {
if (renderable.environment != null) {
prefix += "#define lightingFlag\n";
prefix += "#define ambientCubemapFlag\n";
prefix += "#define numDirectionalLights " + config.numDirectionalLights + "\n";
prefix += "#define numPointLights " + config.numPointLights + "\n";
prefix += "#define numSpotLights " + config.numSpotLights + "\n";
if (attributes.has(ColorAttribute.Fog)) {
prefix += "#define fogFlag\n";
}
if (renderable.environment.shadowMap != null)
prefix += "#define shadowMapFlag\n";
if (attributes.has(CubemapAttribute.EnvironmentMap))
prefix += "#define environmentCubemapFlag\n";
}
}
final int n = renderable.meshPart.mesh.getVertexAttributes().size();
for (int i = 0; i < n; i++) {
final VertexAttribute attr = renderable.meshPart.mesh.getVertexAttributes().get(i);
if (attr.usage == Usage.BoneWeight)
prefix += "#define boneWeight" + attr.unit + "Flag\n";
else if (attr.usage == Usage.TextureCoordinates)
prefix += "#define texCoord" + attr.unit + "Flag\n";
}
if ((attributesMask & BlendingAttribute.Type) == BlendingAttribute.Type)
prefix += "#define " + BlendingAttribute.Alias + "Flag\n";
if ((attributesMask & TextureAttribute.Diffuse) == TextureAttribute.Diffuse) {
prefix += "#define " + TextureAttribute.DiffuseAlias + "Flag\n";
// FIXME implement UV mapping
prefix += "#define " + TextureAttribute.DiffuseAlias + "Coord texCoord0\n";
}
if ((attributesMask & TextureAttribute.Specular) == TextureAttribute.Specular) {
prefix += "#define " + TextureAttribute.SpecularAlias + "Flag\n";
// FIXME implement UV mapping
prefix += "#define " + TextureAttribute.SpecularAlias + "Coord texCoord0\n";
}
if ((attributesMask & TextureAttribute.Normal) == TextureAttribute.Normal) {
prefix += "#define " + TextureAttribute.NormalAlias + "Flag\n";
// FIXME implement UV mapping
prefix += "#define " + TextureAttribute.NormalAlias + "Coord texCoord0\n";
}
if ((attributesMask & TextureAttribute.Emissive) == TextureAttribute.Emissive) {
prefix += "#define " + TextureAttribute.EmissiveAlias + "Flag\n";
// FIXME implement UV mapping
prefix += "#define " + TextureAttribute.EmissiveAlias + "Coord texCoord0\n";
}
if ((attributesMask & TextureAttribute.Reflection) == TextureAttribute.Reflection) {
prefix += "#define " + TextureAttribute.ReflectionAlias + "Flag\n";
// FIXME implement UV mapping
prefix += "#define " + TextureAttribute.ReflectionAlias + "Coord texCoord0\n";
}
if ((attributesMask & TextureAttribute.Ambient) == TextureAttribute.Ambient) {
prefix += "#define " + TextureAttribute.AmbientAlias + "Flag\n";
// FIXME implement UV mapping
prefix += "#define " + TextureAttribute.AmbientAlias + "Coord texCoord0\n";
}
if ((attributesMask & ColorAttribute.Diffuse) == ColorAttribute.Diffuse)
prefix += "#define " + ColorAttribute.DiffuseAlias + "Flag\n";
if ((attributesMask & ColorAttribute.Specular) == ColorAttribute.Specular)
prefix += "#define " + ColorAttribute.SpecularAlias + "Flag\n";
if ((attributesMask & ColorAttribute.Emissive) == ColorAttribute.Emissive)
prefix += "#define " + ColorAttribute.EmissiveAlias + "Flag\n";
if ((attributesMask & ColorAttribute.Reflection) == ColorAttribute.Reflection)
prefix += "#define " + ColorAttribute.ReflectionAlias + "Flag\n";
if ((attributesMask & FloatAttribute.Shininess) == FloatAttribute.Shininess)
prefix += "#define " + FloatAttribute.ShininessAlias + "Flag\n";
if ((attributesMask & FloatAttribute.AlphaTest) == FloatAttribute.AlphaTest)
prefix += "#define " + FloatAttribute.AlphaTestAlias + "Flag\n";
if (renderable.bones != null && config.numBones > 0)
prefix += "#define numBones " + config.numBones + "\n";
return prefix;
}
use of com.badlogic.gdx.graphics.VertexAttribute in project libgdx by libgdx.
the class DepthShader method canRender.
@Override
public boolean canRender(Renderable renderable) {
final Attributes attributes = combineAttributes(renderable);
if (attributes.has(BlendingAttribute.Type)) {
if ((attributesMask & BlendingAttribute.Type) != BlendingAttribute.Type)
return false;
if (attributes.has(TextureAttribute.Diffuse) != ((attributesMask & TextureAttribute.Diffuse) == TextureAttribute.Diffuse))
return false;
}
final boolean skinned = ((renderable.meshPart.mesh.getVertexAttributes().getMask() & Usage.BoneWeight) == Usage.BoneWeight);
if (skinned != (numBones > 0))
return false;
if (!skinned)
return true;
int w = 0;
final int n = renderable.meshPart.mesh.getVertexAttributes().size();
for (int i = 0; i < n; i++) {
final VertexAttribute attr = renderable.meshPart.mesh.getVertexAttributes().get(i);
if (attr.usage == Usage.BoneWeight)
w |= (1 << attr.unit);
}
return w == weights;
}
use of com.badlogic.gdx.graphics.VertexAttribute in project libgdx by libgdx.
the class MeshBuilder method begin.
/** Begin building a mesh */
public void begin(final VertexAttributes attributes, int primitiveType) {
if (this.attributes != null)
throw new RuntimeException("Call end() first");
this.attributes = attributes;
this.vertices.clear();
this.indices.clear();
this.parts.clear();
this.vindex = 0;
this.lastIndex = -1;
this.istart = 0;
this.part = null;
this.stride = attributes.vertexSize / 4;
if (this.vertex == null || this.vertex.length < stride)
this.vertex = new float[stride];
VertexAttribute a = attributes.findByUsage(Usage.Position);
if (a == null)
throw new GdxRuntimeException("Cannot build mesh without position attribute");
posOffset = a.offset / 4;
posSize = a.numComponents;
a = attributes.findByUsage(Usage.Normal);
norOffset = a == null ? -1 : a.offset / 4;
a = attributes.findByUsage(Usage.BiNormal);
biNorOffset = a == null ? -1 : a.offset / 4;
a = attributes.findByUsage(Usage.Tangent);
tangentOffset = a == null ? -1 : a.offset / 4;
a = attributes.findByUsage(Usage.ColorUnpacked);
colOffset = a == null ? -1 : a.offset / 4;
colSize = a == null ? 0 : a.numComponents;
a = attributes.findByUsage(Usage.ColorPacked);
cpOffset = a == null ? -1 : a.offset / 4;
a = attributes.findByUsage(Usage.TextureCoordinates);
uvOffset = a == null ? -1 : a.offset / 4;
setColor(null);
setVertexTransform(null);
setUVRange(null);
this.primitiveType = primitiveType;
bounds.inf();
}
use of com.badlogic.gdx.graphics.VertexAttribute in project libgdx by libgdx.
the class IndexBufferObjectShaderTest method create.
@Override
public void create() {
String vertexShader = "attribute vec4 a_position; \n" + "attribute vec4 a_color;\n" + "attribute vec2 a_texCoords;\n" + "varying vec4 v_color;" + "varying vec2 v_texCoords;" + "void main() \n" + "{ \n" + " v_color = vec4(a_color.x, a_color.y, a_color.z, 1); \n" + " v_texCoords = a_texCoords; \n" + " gl_Position = a_position; \n" + "} \n";
String fragmentShader = "#ifdef GL_ES\n" + "precision mediump float;\n" + "#endif\n" + "varying vec4 v_color;\n" + "varying vec2 v_texCoords;\n" + "uniform sampler2D u_texture;\n" + "void main() \n" + "{ \n" + " gl_FragColor = v_color * texture2D(u_texture, v_texCoords);\n" + "}";
shader = new ShaderProgram(vertexShader, fragmentShader);
vbo = new VertexBufferObject(true, 3, new VertexAttribute(VertexAttributes.Usage.Position, 2, "a_position"), new VertexAttribute(VertexAttributes.Usage.TextureCoordinates, 2, "a_texCoords"), new VertexAttribute(VertexAttributes.Usage.ColorPacked, 4, "a_color"));
float[] vertices = new float[] { -1, -1, 0, 0, Color.toFloatBits(1f, 0f, 0f, 1f), 0, 1, 0.5f, 1.0f, Color.toFloatBits(0f, 1f, 0f, 1f), 1, -1, 1, 0, Color.toFloatBits(0f, 0f, 1f, 1f) };
vbo.setVertices(vertices, 0, vertices.length);
ibo = new IndexBufferObject(true, 3);
ibo.setIndices(new short[] { 0, 1, 2 }, 0, 3);
texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
}
Aggregations