unsigned int Model::draw(bool wireframe) { GP_ASSERT(_mesh); unsigned int partCount = _mesh->getPartCount(); if (partCount == 0) { // No mesh parts (index buffers). if (_material) { Technique* technique = _material->getTechnique(); GP_ASSERT(technique); unsigned int passCount = technique->getPassCount(); for (unsigned int i = 0; i < passCount; ++i) { Pass* pass = technique->getPassByIndex(i); GP_ASSERT(pass); pass->bind(); GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); if (!wireframe || !drawWireframe(_mesh)) { GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) ); } pass->unbind(); } } } else { for (unsigned int i = 0; i < partCount; ++i) { MeshPart* part = _mesh->getPart(i); GP_ASSERT(part); // Get the material for this mesh part. Material* material = getMaterial(i); if (material) { Technique* technique = material->getTechnique(); GP_ASSERT(technique); unsigned int passCount = technique->getPassCount(); for (unsigned int j = 0; j < passCount; ++j) { Pass* pass = technique->getPassByIndex(j); GP_ASSERT(pass); pass->bind(); GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) ); if (!wireframe || !drawWireframe(part)) { GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) ); } pass->unbind(); } } } } return partCount; }
void MeshBatch::draw() { if (_vertexCount == 0 || (_indexed && _indexCount == 0)) return; // nothing to draw // Not using VBOs, so unbind the element array buffer. // ARRAY_BUFFER will be unbound automatically during pass->bind(). GPRHI_ASSERT( GPRHI_BindBuffer(GP_RHI_BUFFER_INDEX, 0 ) ); GP_ASSERT(_material); if (_indexed) GP_ASSERT(_indices); // Bind the material. Technique* technique = _material->getTechnique(); GP_ASSERT(technique); unsigned int passCount = technique->getPassCount(); for (unsigned int i = 0; i < passCount; ++i) { Pass* pass = technique->getPassByIndex(i); GP_ASSERT(pass); pass->bind(); if (_indexed) { GPRHI_ASSERT( GPRHI_DrawElements(_primitiveType, _indexCount, GP_RHI_FORMAT_UNSIGNED_SHORT, (gp_void*)_indices) ); } else { GPRHI_ASSERT( GPRHI_DrawArrays(_primitiveType, 0, _vertexCount) ); } pass->unbind(); } }
void Model::setMaterialNodeBinding(Material *material) { GP_ASSERT(material); if (_node) { material->setNodeBinding(_node); unsigned int techniqueCount = material->getTechniqueCount(); for (unsigned int i = 0; i < techniqueCount; ++i) { Technique* technique = material->getTechniqueByIndex(i); GP_ASSERT(technique); technique->setNodeBinding(_node); unsigned int passCount = technique->getPassCount(); for (unsigned int j = 0; j < passCount; ++j) { Pass* pass = technique->getPassByIndex(j); GP_ASSERT(pass); pass->setNodeBinding(_node); } } } }
static int lua_Technique_getPassCount(lua_State* state) { // Get the number of parameters. int paramCount = lua_gettop(state); // Attempt to match the parameters to a valid binding. switch (paramCount) { case 1: { if ((lua_type(state, 1) == LUA_TUSERDATA)) { Technique* instance = getInstance(state); unsigned int result = instance->getPassCount(); // Push the return value onto the stack. lua_pushunsigned(state, result); return 1; } lua_pushstring(state, "lua_Technique_getPassCount - Failed to match the given parameters to a valid function signature."); lua_error(state); break; } default: { lua_pushstring(state, "Invalid number of parameters (expected 1)."); lua_error(state); break; } } return 0; }
bool hasParameter( Material* material, const char* name ) { unsigned int mc = material->getTechniqueCount(); for (unsigned int i = 0; i < mc; ++i) { Technique *tech = material->getTechniqueByIndex( i ); unsigned int pc = tech->getPassCount(); for (unsigned int j = 0; j < pc; ++j) { Pass *pass = tech->getPassByIndex(j); Effect *effect = pass->getEffect(); if (effect->getUniform( name ) != NULL) return true; } } return false; }
void MeshBatch::updateVertexAttributeBinding() { GP_ASSERT(_material); // Update our vertex attribute bindings. for (unsigned int i = 0, techniqueCount = _material->getTechniqueCount(); i < techniqueCount; ++i) { Technique* t = _material->getTechniqueByIndex(i); GP_ASSERT(t); for (unsigned int j = 0, passCount = t->getPassCount(); j < passCount; ++j) { Pass* p = t->getPassByIndex(j); GP_ASSERT(p); VertexAttributeBinding* b = VertexAttributeBinding::create(_vertexFormat, _vertices, p->getEffect()); p->setVertexAttributeBinding(b); SAFE_RELEASE(b); } } }
void Model::setMaterialNodeBinding(Material *material) { if (_node) { material->setNodeBinding(_node); unsigned int techniqueCount = material->getTechniqueCount(); for (unsigned int i = 0; i < techniqueCount; ++i) { Technique* technique = material->getTechnique(i); technique->setNodeBinding(_node); unsigned int passCount = technique->getPassCount(); for (unsigned int j = 0; j < passCount; ++j) { Pass* pass = technique->getPass(j); pass->setNodeBinding(_node); } } } }
void Model::setMaterial(Material* material, int partIndex) { assert(partIndex == -1 || (partIndex >= 0 && partIndex < (int)getMeshPartCount())); Material* oldMaterial = NULL; if (partIndex == -1) { oldMaterial = _material; // Set new shared material. if (material) { _material = material; _material->addRef(); } } else if (partIndex >= 0 && partIndex < (int)getMeshPartCount()) { // Ensure mesh part count is up-to-date. validatePartCount(); // Release existing part material and part binding. if (_partMaterials) { oldMaterial = _partMaterials[partIndex]; } else { // Allocate part arrays for the first time. if (_partMaterials == NULL) { _partMaterials = new Material*[_partCount]; memset(_partMaterials, 0, sizeof(Material*) * _partCount); } } // Set new part material. if (material) { _partMaterials[partIndex] = material; material->addRef(); } } // Release existing material and binding. if (oldMaterial) { for (unsigned int i = 0, tCount = material->getTechniqueCount(); i < tCount; ++i) { Technique* t = material->getTechnique(i); for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j) { t->getPass(j)->setVertexAttributeBinding(NULL); } } SAFE_RELEASE(oldMaterial); } if (material) { // Hookup vertex attribute bindings for all passes in the new material. for (unsigned int i = 0, tCount = material->getTechniqueCount(); i < tCount; ++i) { Technique* t = material->getTechnique(i); for (unsigned int j = 0, pCount = t->getPassCount(); j < pCount; ++j) { Pass* p = t->getPass(j); VertexAttributeBinding* b = VertexAttributeBinding::create(_mesh, p->getEffect()); p->setVertexAttributeBinding(b); SAFE_RELEASE(b); } } // Apply node binding for the new material. if (_node) { setMaterialNodeBinding(material); } } }
void Model::draw(bool wireframe) { unsigned int partCount = _mesh->getPartCount(); if (partCount == 0) { // No mesh parts (index buffers). if (_material) { Technique* technique = _material->getTechnique(); unsigned int passCount = technique->getPassCount(); for (unsigned int i = 0; i < passCount; ++i) { Pass* pass = technique->getPass(i); pass->bind(); GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0) ); if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP)) { unsigned int vertexCount = _mesh->getVertexCount(); for (unsigned int j = 0; j < vertexCount; j += 3) { GL_ASSERT( glDrawArrays(GL_LINE_LOOP, j, 3) ); } } else { GL_ASSERT( glDrawArrays(_mesh->getPrimitiveType(), 0, _mesh->getVertexCount()) ); } pass->unbind(); } } } else { for (unsigned int i = 0; i < partCount; ++i) { MeshPart* part = _mesh->getPart(i); // Get the material for this mesh part. Material* material = getMaterial(i); if (material) { Technique* technique = material->getTechnique(); unsigned int passCount = technique->getPassCount(); for (unsigned int j = 0; j < passCount; ++j) { Pass* pass = technique->getPass(j); pass->bind(); GL_ASSERT( glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, part->_indexBuffer) ); if (wireframe && (_mesh->getPrimitiveType() == Mesh::TRIANGLES || _mesh->getPrimitiveType() == Mesh::TRIANGLE_STRIP)) { unsigned int indexCount = part->getIndexCount(); unsigned int indexSize = 0; switch (part->getIndexFormat()) { case Mesh::INDEX8: indexSize = 1; break; case Mesh::INDEX16: indexSize = 2; break; case Mesh::INDEX32: indexSize = 4; break; } for (unsigned int k = 0; k < indexCount; k += 3) { GL_ASSERT( glDrawElements(GL_LINE_LOOP, 3, part->getIndexFormat(), ((const GLvoid*)(k*indexSize))) ); } } else { GL_ASSERT( glDrawElements(part->getPrimitiveType(), part->getIndexCount(), part->getIndexFormat(), 0) ); } pass->unbind(); } } } } }