[ create a new paste ] login | about

Link: http://codepad.org/x5csLmkL    [ raw code | fork ]

C++, pasted on Dec 7:
Model* box = cache->GetResource<Model>("Models/cube.mdl");

IndexBuffer* indexBuffer = box->GetGeometry(0, 0)->GetIndexBuffer();
unsigned indicesCount = indexBuffer->GetIndexCount();
unsigned indicesSize = indexBuffer->GetIndexSize();

const unsigned short* indicesData = (const unsigned short*)indexBuffer->Lock(0, indicesCount);
int indicesArray[indicesCount];

for (unsigned i = 0; i < indicesCount ; ++i)
{
    unsigned short index = indicesData[i];
    indicesArray[i] = indicesData[i];
}

//Vertices data
VertexBuffer* vertexBuffer = box->GetGeometry(0, 0)->GetVertexBuffer(0);
unsigned short vertexCount = vertexBuffer->GetVertexCount();
unsigned short vertexSize = vertexBuffer->GetVertexSize();

const unsigned char* vertexData2 = (const unsigned char*)vertexBuffer->Lock(0, vertexCount);
float vertexArray[(vertexCount * 3)*2];

int size = 3*2;
int offset;
for (unsigned i = 0; i < vertexCount; ++i)
{
    const Urho3D::Vector3& src = *reinterpret_cast<const Urho3D::Vector3*>(vertexData2 + i * vertexSize);

    offset = i * size;
    vertexArray[offset] = src.x_;
    vertexArray[offset + 1] = src.y_;
    vertexArray[offset + 2] = src.z_;
    vertexArray[offset + 3] = 0.0;
    vertexArray[offset + 4] = 0.0;
    vertexArray[offset + 5] = 0.0;
}

// Calculate face normals now
for (unsigned i = 0; i < vertexCount; i += 3)
{
    Urho3D::Vector3& v1 = *(reinterpret_cast<Urho3D::Vector3*>(&vertexData[6 * i]));
    Urho3D::Vector3& v2 = *(reinterpret_cast<Urho3D::Vector3*>(&vertexData[6 * (i + 1)]));
    Urho3D::Vector3& v3 = *(reinterpret_cast<Urho3D::Vector3*>(&vertexData[6 * (i + 2)]));
    Urho3D::Vector3& n1 = *(reinterpret_cast<Urho3D::Vector3*>(&vertexData[6 * i + 3]));
    Urho3D::Vector3& n2 = *(reinterpret_cast<Urho3D::Vector3*>(&vertexData[6 * (i + 1) + 3]));
    Urho3D::Vector3& n3 = *(reinterpret_cast<Urho3D::Vector3*>(&vertexData[6 * (i + 2) + 3]));
    
    Urho3D::Vector3 edge1 = v1 - v2;
    Urho3D::Vector3 edge2 = v1 - v3;
    n1 = n2 = n3 = edge1.CrossProduct(edge2).Normalized();
}

SharedPtr<Model> fromScratchModel(new Model(context_));
SharedPtr<VertexBuffer> vb(new VertexBuffer(context_));
SharedPtr<IndexBuffer> ib(new IndexBuffer(context_));
SharedPtr<Geometry> geom(new Geometry(context_));

// Shadowed buffer needed for raycasts to work, and so that data can be automatically restored on device loss
vb->SetShadowed(true);
vb->SetSize(vertexCount,  MASK_POSITION|MASK_NORMAL);
vb->SetData(vertexArray);

ib->SetShadowed(true);
ib->SetSize(indicesCount, false);
ib->SetData(indicesArray);

geom->SetVertexBuffer(0, vb);
geom->SetIndexBuffer(ib);
geom->SetDrawRange(TRIANGLE_LIST, 0, vertexCount);

fromScratchModel->SetNumGeometries(1);
fromScratchModel->SetGeometry(0, 0, geom);
fromScratchModel->SetBoundingBox(BoundingBox(Urho3D::Vector3(-0.5f, -0.5f, -0.5f), Urho3D::Vector3(0.5f, 0.5f, 0.5f)));

Node* node = scene_->CreateChild("FromScratchObject");
node->SetPosition(Urho3D::Vector3(0.0f, 3.0f, 0.0f));
StaticModel* object = node->CreateComponent<StaticModel>();
object->SetModel(fromScratchModel);


Create a new paste based on this one


Comments: