use of com.jme3.shader.Attribute in project jmonkeyengine by jMonkeyEngine.
the class ShaderGenerationInfo method clone.
@Override
protected ShaderGenerationInfo clone() throws CloneNotSupportedException {
ShaderGenerationInfo clone = (ShaderGenerationInfo) super.clone();
for (ShaderNodeVariable attribute : attributes) {
clone.attributes.add(attribute.clone());
}
for (ShaderNodeVariable uniform : vertexUniforms) {
clone.vertexUniforms.add(uniform.clone());
}
clone.vertexGlobal = vertexGlobal.clone();
for (ShaderNodeVariable varying : varyings) {
clone.varyings.add(varying.clone());
}
for (ShaderNodeVariable uniform : fragmentUniforms) {
clone.fragmentUniforms.add(uniform.clone());
}
for (ShaderNodeVariable globals : fragmentGlobals) {
clone.fragmentGlobals.add(globals.clone());
}
clone.unusedNodes.addAll(unusedNodes);
return clone;
}
use of com.jme3.shader.Attribute in project jmonkeyengine by jMonkeyEngine.
the class GLRenderer method setVertexAttrib.
public void setVertexAttrib(VertexBuffer vb, VertexBuffer idb) {
if (vb.getBufferType() == VertexBuffer.Type.Index) {
throw new IllegalArgumentException("Index buffers not allowed to be set to vertex attrib");
}
if (context.boundShaderProgram <= 0) {
throw new IllegalStateException("Cannot render mesh without shader bound");
}
Attribute attrib = context.boundShader.getAttribute(vb.getBufferType());
int loc = attrib.getLocation();
if (loc == -1) {
// not defined
return;
}
if (loc == -2) {
loc = gl.glGetAttribLocation(context.boundShaderProgram, "in" + vb.getBufferType().name());
// the internal name of the enum (Position).
if (loc < 0) {
attrib.setLocation(-1);
// not available in shader.
return;
} else {
attrib.setLocation(loc);
}
}
if (vb.isInstanced()) {
if (!caps.contains(Caps.MeshInstancing)) {
throw new RendererException("Instancing is required, " + "but not supported by the " + "graphics hardware");
}
}
int slotsRequired = 1;
if (vb.getNumComponents() > 4) {
if (vb.getNumComponents() % 4 != 0) {
throw new RendererException("Number of components in multi-slot " + "buffers must be divisible by 4");
}
slotsRequired = vb.getNumComponents() / 4;
}
if (vb.isUpdateNeeded() && idb == null) {
updateBufferData(vb);
}
VertexBuffer[] attribs = context.boundAttribs;
for (int i = 0; i < slotsRequired; i++) {
if (!context.attribIndexList.moveToNew(loc + i)) {
gl.glEnableVertexAttribArray(loc + i);
}
}
if (attribs[loc] != vb) {
// NOTE: Use id from interleaved buffer if specified
int bufId = idb != null ? idb.getId() : vb.getId();
assert bufId != -1;
if (context.boundArrayVBO != bufId) {
gl.glBindBuffer(GL.GL_ARRAY_BUFFER, bufId);
context.boundArrayVBO = bufId;
//statistics.onVertexBufferUse(vb, true);
} else {
//statistics.onVertexBufferUse(vb, false);
}
if (slotsRequired == 1) {
gl.glVertexAttribPointer(loc, vb.getNumComponents(), convertFormat(vb.getFormat()), vb.isNormalized(), vb.getStride(), vb.getOffset());
} else {
for (int i = 0; i < slotsRequired; i++) {
// The pointer maps the next 4 floats in the slot.
// E.g.
// P1: XXXX____________XXXX____________
// P2: ____XXXX____________XXXX________
// P3: ________XXXX____________XXXX____
// P4: ____________XXXX____________XXXX
// stride = 4 bytes in float * 4 floats in slot * num slots
// offset = 4 bytes in float * 4 floats in slot * slot index
gl.glVertexAttribPointer(loc + i, 4, convertFormat(vb.getFormat()), vb.isNormalized(), 4 * 4 * slotsRequired, 4 * 4 * i);
}
}
for (int i = 0; i < slotsRequired; i++) {
int slot = loc + i;
if (vb.isInstanced() && (attribs[slot] == null || !attribs[slot].isInstanced())) {
// non-instanced -> instanced
glext.glVertexAttribDivisorARB(slot, vb.getInstanceSpan());
} else if (!vb.isInstanced() && attribs[slot] != null && attribs[slot].isInstanced()) {
// instanced -> non-instanced
glext.glVertexAttribDivisorARB(slot, 0);
}
attribs[slot] = vb;
}
}
}
use of com.jme3.shader.Attribute in project jmonkeyengine by jMonkeyEngine.
the class ShaderNodeLoaderDelegate method readInputMapping.
/**
* reads an input mapping
*
* @param statement1 the statement being read
* @return the mapping
* @throws IOException
*/
public VariableMapping readInputMapping(Statement statement1) throws IOException {
VariableMapping mapping = null;
try {
mapping = parseMapping(statement1, new boolean[] { false, true });
} catch (Exception e) {
throw new MatParseException("Unexpected mapping format", statement1, e);
}
ShaderNodeVariable left = mapping.getLeftVariable();
ShaderNodeVariable right = mapping.getRightVariable();
if (!updateVariableFromList(left, shaderNode.getDefinition().getInputs())) {
throw new MatParseException(left.getName() + " is not an input variable of " + shaderNode.getDefinition().getName(), statement1);
}
if (left.getType().startsWith("sampler") && !right.getNameSpace().equals("MatParam")) {
throw new MatParseException("Samplers can only be assigned to MatParams", statement1);
}
if (right.getNameSpace().equals("Global")) {
//Globals are all vec4 for now (maybe forever...)
right.setType("vec4");
// updateCondition(right, mapping);
storeGlobal(right, statement1);
} else if (right.getNameSpace().equals("Attr")) {
if (shaderNode.getDefinition().getType() == Shader.ShaderType.Fragment) {
throw new MatParseException("Cannot have an attribute as input in a fragment shader" + right.getName(), statement1);
}
updateVarFromAttributes(mapping.getRightVariable(), mapping);
// updateCondition(mapping.getRightVariable(), mapping);
storeAttribute(mapping.getRightVariable());
} else if (right.getNameSpace().equals("MatParam")) {
MatParam param = findMatParam(right.getName());
if (param == null) {
throw new MatParseException("Could not find a Material Parameter named " + right.getName(), statement1);
}
if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
if (updateRightFromUniforms(param, mapping, vertexDeclaredUniforms, statement1)) {
storeVertexUniform(mapping.getRightVariable());
}
} else {
if (updateRightFromUniforms(param, mapping, fragmentDeclaredUniforms, statement1)) {
if (mapping.getRightVariable().getType().contains("|")) {
String type = fixSamplerType(left.getType(), mapping.getRightVariable().getType());
if (type != null) {
mapping.getRightVariable().setType(type);
} else {
throw new MatParseException(param.getVarType().toString() + " can only be matched to one of " + param.getVarType().getGlslType().replaceAll("\\|", ",") + " found " + left.getType(), statement1);
}
}
storeFragmentUniform(mapping.getRightVariable());
}
}
} else if (right.getNameSpace().equals("WorldParam")) {
UniformBinding worldParam = findWorldParam(right.getName());
if (worldParam == null) {
throw new MatParseException("Could not find a World Parameter named " + right.getName(), statement1);
}
if (shaderNode.getDefinition().getType() == Shader.ShaderType.Vertex) {
if (updateRightFromUniforms(worldParam, mapping, vertexDeclaredUniforms)) {
storeVertexUniform(mapping.getRightVariable());
}
} else {
if (updateRightFromUniforms(worldParam, mapping, fragmentDeclaredUniforms)) {
storeFragmentUniform(mapping.getRightVariable());
}
}
} else {
ShaderNode node = nodes.get(right.getNameSpace());
if (node == null) {
throw new MatParseException("Undeclared node" + right.getNameSpace() + ". Make sure this node is declared before the current node", statement1);
}
ShaderNodeVariable var = findNodeOutput(node.getDefinition().getOutputs(), right.getName());
if (var == null) {
throw new MatParseException("Cannot find output variable" + right.getName() + " form ShaderNode " + node.getName(), statement1);
}
right.setNameSpace(node.getName());
right.setType(var.getType());
right.setMultiplicity(var.getMultiplicity());
mapping.setRightVariable(right);
storeVaryings(node, mapping.getRightVariable());
}
checkTypes(mapping, statement1);
return mapping;
}
use of com.jme3.shader.Attribute in project jmonkeyengine by jMonkeyEngine.
the class FbxNode method toJmeObject.
@Override
public Spatial toJmeObject() {
Spatial spatial;
if (nodeAttribute instanceof FbxMesh) {
FbxMesh fbxMesh = (FbxMesh) nodeAttribute;
IntMap<Mesh> jmeMeshes = fbxMesh.getJmeObject();
if (jmeMeshes == null || jmeMeshes.size() == 0) {
// No meshes found on FBXMesh (??)
logger.log(Level.WARNING, "No meshes could be loaded. Creating empty node.");
spatial = new Node(getName() + "-node");
} else {
// Multiple jME3 geometries required for a single FBXMesh.
String nodeName;
if (children.isEmpty()) {
nodeName = getName() + "-mesh";
} else {
nodeName = getName() + "-node";
}
Node node = new Node(nodeName);
boolean singleMesh = jmeMeshes.size() == 1;
for (IntMap.Entry<Mesh> meshInfo : jmeMeshes) {
node.attachChild(tryCreateGeometry(meshInfo.getKey(), meshInfo.getValue(), singleMesh));
}
spatial = node;
}
} else {
if (nodeAttribute != null) {
// Just specifies that this is a "null" node.
nodeAttribute.getJmeObject();
}
// TODO: handle other node attribute types.
// right now everything we don't know about gets converted
// to jME3 Node.
spatial = new Node(getName() + "-node");
}
if (!children.isEmpty()) {
// Check uniform scale.
// Although, if inheritType is 0 (eInheritRrSs)
// it might not be a problem.
Vector3f localScale = jmeLocalNodeTransform.getScale();
if (!FastMath.approximateEquals(localScale.x, localScale.y) || !FastMath.approximateEquals(localScale.x, localScale.z)) {
logger.log(Level.WARNING, "Non-uniform scale detected on parent node. " + "The model may appear distorted.");
}
}
spatial.setLocalTransform(jmeLocalNodeTransform);
if (visibility == 0.0) {
spatial.setCullHint(CullHint.Always);
}
for (Map.Entry<String, Object> userDataEntry : userData.entrySet()) {
spatial.setUserData(userDataEntry.getKey(), userDataEntry.getValue());
}
return spatial;
}
use of com.jme3.shader.Attribute in project jmonkeyengine by jMonkeyEngine.
the class SceneLoader method parseEntity.
private void parseEntity(Attributes attribs) throws SAXException {
String name = attribs.getValue("name");
if (name == null) {
name = "OgreEntity-" + (++nodeIdx);
} else {
name += "-entity";
}
String meshFile = attribs.getValue("meshFile");
if (meshFile == null) {
throw new SAXException("Required attribute 'meshFile' missing for 'entity' node");
}
// TODO: Not currently used
String materialName = attribs.getValue("materialName");
if (folderName != null) {
meshFile = folderName + meshFile;
}
// NOTE: append "xml" since its assumed mesh files are binary in dotScene
meshFile += ".xml";
entityNode = new com.jme3.scene.Node(name);
OgreMeshKey meshKey = new OgreMeshKey(meshFile, materialList);
try {
try {
Spatial ogreMesh = (Spatial) meshLoader.load(assetManager.locateAsset(meshKey));
entityNode.attachChild(ogreMesh);
} catch (IOException e) {
throw new AssetNotFoundException(meshKey.toString());
}
} catch (AssetNotFoundException ex) {
if (ex.getMessage().equals(meshFile)) {
logger.log(Level.WARNING, "Cannot locate {0} for scene {1}", new Object[] { meshKey, key });
// Attach placeholder asset.
Spatial model = PlaceholderAssets.getPlaceholderModel(assetManager);
model.setKey(key);
entityNode.attachChild(model);
} else {
throw ex;
}
}
node.attachChild(entityNode);
node = null;
}
Aggregations