Example #1
0
//----------------------------------------------------------------------------
void BouncingBall::CreateFloor ()
{
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);

    const float xExtent = 8.0f;
    const float yExtent = 16.0f;
    const float zValue = 0.0f;
    vba.Position<Float3>(0) = Float3(-xExtent, -yExtent, zValue);
    vba.Position<Float3>(1) = Float3(+xExtent, -yExtent, zValue);
    vba.Position<Float3>(2) = Float3(+xExtent, +yExtent, zValue);
    vba.Position<Float3>(3) = Float3(-xExtent, +yExtent, zValue);
    vba.TCoord<Float2>(0, 0) = Float2(0.0f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(1.0f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(1.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(0.0f, 1.0f);

    IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0;  indices[1] = 1;  indices[2] = 2;
    indices[3] = 0;  indices[4] = 2;  indices[5] = 3;

    mFloor = new0 TriMesh(vformat, vbuffer, ibuffer);

    std::string path = Environment::GetPathR("Floor.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(path);
    mFloor->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture,
        Shader::SF_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT));
}
Example #2
0
//----------------------------------------------------------------------------
void BouncingSpheres::CreateBackWall ()
{
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    int vstride = vformat->GetStride();

    Float3 backWallColor(209.0f/255.0f, 204.0f/255.0f, 180.0f/255.0f);

    VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);
    vba.Position<Float3>(0) = Float3(1.0f,  1.0f,  1.0f);
    vba.Position<Float3>(1) = Float3(1.0f, 20.0f,  1.0f);
    vba.Position<Float3>(2) = Float3(1.0f, 20.0f, 17.0f);
    vba.Position<Float3>(3) = Float3(1.0f,  1.0f, 17.0f);
    vba.Color<Float3>(0, 0) = backWallColor;
    vba.Color<Float3>(0, 1) = backWallColor;
    vba.Color<Float3>(0, 2) = backWallColor;
    vba.Color<Float3>(0, 3) = backWallColor;

    IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0;  indices[1] = 1;  indices[2] = 2;
    indices[3] = 0;  indices[4] = 2;  indices[5] = 3;

    mBackWall = new0 TriMesh(vformat, vbuffer, ibuffer);
    mBackWall->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());
}
Example #3
0
		void Renderer::Draw(const VertexArray& va, const IndexBuffer& ib, const Shader& shader) const
		{
			shader.Bind();
			ib.Bind();
			va.Bind();
			GLCall(glDrawElements(GL_TRIANGLES, ib.GetCount(), GL_UNSIGNED_INT, 0));
		}
//----------------------------------------------------------------------------
TriMesh *ScreenTarget::CreateRectangle (VertexFormat* vformat, 
	float xMin,	float xMax, float yMin, float yMax, float zValue)
{
	if (!ValidFormat(vformat))
		return 0;

	Float2 tc0, tc1, tc2, tc3;
	tc0 = Float2(0.0f, 0.0f);
	tc1 = Float2(1.0f, 0.0f);
	tc2 = Float2(1.0f, 1.0f);
	tc3 = Float2(0.0f, 1.0f);

	int vstride = vformat->GetStride();
	VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
	VertexBufferAccessor vba(vformat, vbuffer);
	vba.Position<Float3>(0) = Float3(xMin, yMin, zValue);
	vba.Position<Float3>(1) = Float3(xMax, yMin, zValue);
	vba.Position<Float3>(2) = Float3(xMax, yMax, zValue);
	vba.Position<Float3>(3) = Float3(xMin, yMax, zValue);
	vba.TCoord<Float2>(0, 0) = tc0;
	vba.TCoord<Float2>(0, 1) = tc1;
	vba.TCoord<Float2>(0, 2) = tc2;
	vba.TCoord<Float2>(0, 3) = tc3;

	// 为square创建IndexBuffer
	IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
	int* indices = (int*)ibuffer->GetData();
	indices[0] = 0;  indices[1] = 1;  indices[2] = 2;
	indices[3] = 0;  indices[4] = 2;  indices[5] = 3;

	return new0 TriMesh(vformat, vbuffer, ibuffer);
}
//----------------------------------------------------------------------------
TriMesh* PolyhedronDistance::CreatePlane ()
{
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);

    float size = 16.0f;
    vba.Position<Float3>(0) = Float3(-size, -size, -0.1f);
    vba.Position<Float3>(1) = Float3(+size, -size, -0.1f);
    vba.Position<Float3>(2) = Float3(+size, +size, -0.1f);
    vba.Position<Float3>(3) = Float3(-size, +size, -0.1f);
    vba.Color<Float3>(0, 0) = Float3(0.0f, 0.50f, 0.00f);
    vba.Color<Float3>(0, 1) = Float3(0.0f, 0.25f, 0.00f);
    vba.Color<Float3>(0, 2) = Float3(0.0f, 0.75f, 0.00f);
    vba.Color<Float3>(0, 3) = Float3(0.0f, 1.00f, 0.00f);

    IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0; indices[1] = 1; indices[2] = 2;
    indices[3] = 0; indices[4] = 2; indices[5] = 3;

    TriMesh* mesh = new0 TriMesh(vformat, vbuffer, ibuffer);
    mesh->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());

    return mesh;
}
IndexBuffer TerrainTessellator::createPatchIndexBuffer(const GraphicsDevice& device, unsigned levelOfDetail)
{
	// distance between two neighbor vertices
	int delta = MathHelper::pow2(levelOfDetail);

	// size of grid in current lod (e.g. lod 0: 64, lod 1: 32)
	int lodSize = size / delta;

	// main indices + skirt indices
	int numIndices = lodSize * lodSize * 6;

	IndexCollection indices(numIndices);

	int count = 0;

	unsigned interval = min(stripeSize * delta, size);

	// init main indices
	for (unsigned i = 0; i < size / interval; ++i)
		for (unsigned row = 0; row < size; row += delta)
			for (unsigned col = i * interval; col < (i+1) * interval; col += delta)
				initQuadIndices(row, col, delta, delta, &indices, &count);

	IndexBuffer indexBuffer = device.createIndexBuffer(sizeof(TerrainIndex) * numIndices, indexFormat, D3DUSAGE_WRITEONLY);

	indexBuffer.setData(indices);

	return indexBuffer;
}
Example #7
0
void BasicRenderer::DrawIndexedPrimitive(PrimitiveTypeEnum pt,                                                             
                        ObjectGUID vbId, 
                        ObjectGUID ibId,
                        uint32_t startIndex,
                        uint32_t indexCount,
                        uint32_t startVertex,                        
                        float* color,
                        float* xform)                        
 {
    if(!m_context) return; 
    UpdateCbPerDraw(xform,color);
	
	// Set primitive topology
    m_context->IASetPrimitiveTopology( (D3D11_PRIMITIVE_TOPOLOGY)pt );	

    // set vertex buffer
    VertexBuffer* vb = reinterpret_cast<VertexBuffer*>(vbId);
    UINT stride = vb->GetStride();    
    UINT Offset = 0;
    ID3D11Buffer* buffer = vb->GetBuffer();
    m_context->IASetVertexBuffers( 0, 1, &buffer, &stride, &Offset);

    // set index buffer
    IndexBuffer* ib = reinterpret_cast<IndexBuffer*>(ibId);
    m_context->IASetIndexBuffer(ib->GetBuffer(),(DXGI_FORMAT)ib->GetFormat(),0);

    m_context->DrawIndexed(indexCount,startIndex,startVertex);	
   
 }
Example #8
0
//----------------------------------------------------------------------------
Billboard::Billboard () :
mIsDynamic(true),
mIsUseTexAsSize(false),
mIsDoAlphaDisAfterStop(true),
mDoAlphaDisAfterStopSpeed(0.5f),
mIsUseTrim(false)
{
	SetTex("Data/engine/default.png");

	mAnchorPoint = Float2(0.5f, 0.5f);

	SetLocal(true);
	SetDynamic(mIsDynamic);

	IndexBuffer *iBuffer = new0 IndexBuffer(6, 2, Buffer::BU_STATIC);
	unsigned short *indices = (unsigned short*)iBuffer->GetData();
	unsigned short v0 = 0;
	unsigned short v1 = 1;
	unsigned short v2 = 2;
	unsigned short v3 = 3;
	*indices++ = v0;
	*indices++ = v1;
	*indices++ = v2;
	*indices++ = v1;
	*indices++ = v3;
	*indices++ = v2;
	SetIndexBuffer(iBuffer);

	mEffectableCtrl = new0 BillboardController();
	mEffectableCtrl->SetName("BillboardController");
	AttachController(mEffectableCtrl);
}
Example #9
0
bool HasDynamicBuffers(Model* model, unsigned lodLevel)
{
    unsigned numGeometries = model->GetNumGeometries();

    for (unsigned i = 0; i < numGeometries; ++i)
    {
        Geometry* geometry = model->GetGeometry(i, lodLevel);
        if (!geometry)
            continue;
        unsigned numVertexBuffers = geometry->GetNumVertexBuffers();
        for (unsigned j = 0; j < numVertexBuffers; ++j)
        {
            VertexBuffer* buffer = geometry->GetVertexBuffer(j);
            if (!buffer)
                continue;
            if (buffer->IsDynamic())
                return true;
        }
        IndexBuffer* buffer = geometry->GetIndexBuffer();
        if (buffer && buffer->IsDynamic())
            return true;
    }

    return false;
}
Example #10
0
//--------------------------------------------------------------------------
Helper::Ptr HelperManager::CreateTangentSpace(	VertexBuffer3F& _pvbo,
        VertexBuffer3F& _nvbo,
        VertexBuffer4F& _tvbo,
        IndexBuffer& _ibo,
        int _startIndex,
        int _countIndex,
        float _vectorSize)
{
    Helper::Ptr ref = Helper::Create();
    ref->vbuffer.Allocate(6*_countIndex,GL_STATIC_DRAW);
    ref->cbuffer.Allocate(6*_countIndex,GL_STATIC_DRAW);
    ref->type  = GL_LINES;
    ref->transform = glm::mat4(1.f);

    glm::vec3* hvertices = ref->vbuffer.Lock();
    glm::vec3* hcolors   = ref->cbuffer.Lock();

    glm::vec3* vertices = _pvbo.Lock();
    glm::vec4* tangents = _tvbo.Lock();
    glm::vec3* normals  = _nvbo.Lock();
    unsigned int* indices= _ibo.Lock();
    int current = 0;
    for(int i=_startIndex; i<_startIndex+_countIndex; ++i)
    {
        int index = indices[i];

        // Tangent
        hvertices[current+0] = vertices[index];
        hvertices[current+1] = vertices[index] + glm::vec3(tangents[index]) * _vectorSize;

        // Bitangent
        glm::vec3 bitangent = glm::cross(glm::vec3(tangents[index]),normals[index]) * glm::sign(tangents[index].w);
        hvertices[current+2] = vertices[index];
        hvertices[current+3] = vertices[index] + bitangent * _vectorSize;

        // Normal
        hvertices[current+4] = vertices[index];
        hvertices[current+5] = vertices[index] + normals[index] * _vectorSize;

        hcolors[current+0] = glm::vec3(1.f,0.f,0.f);
        hcolors[current+1] = glm::vec3(1.f,0.f,0.f);
        hcolors[current+2] = glm::vec3(0.f,1.f,0.f);
        hcolors[current+3] = glm::vec3(0.f,1.f,0.f);
        hcolors[current+4] = glm::vec3(0.f,0.f,1.f);
        hcolors[current+5] = glm::vec3(0.f,0.f,1.f);

        current += 6;
    }
    _ibo.Unlock();
    _nvbo.Unlock();
    _tvbo.Unlock();
    _pvbo.Unlock();

    ref->cbuffer.Unlock();
    ref->vbuffer.Unlock();

    helpers.push_back(ref);
    return ref;
}
Example #11
0
//----------------------------------------------------------------------------
void Fluids3D::UpdateIndexBuffer ()
{
    VertexBufferAccessor vba(mCube);
    APoint camPos = mCamera->GetPosition();
    const int numTriangles = mNumIndices/3;
    int* currentIndex = mIndices;

    mTriangles.clear();
    for (int t = 0; t < numTriangles; ++t)
    {
        Triangle tri;
        tri.mIndex0 = *currentIndex++;
        tri.mIndex1 = *currentIndex++;
        tri.mIndex2 = *currentIndex++;

#ifdef USE_PARTICLES
        float alpha = vba.Color<Float4>(0, tri.mIndex0)[3];
        if (alpha == 0.0f)
        {
            continue;
        }
#else
        float alpha0 = vba.Color<Float4>(0, tri.mIndex0)[3];
        float alpha1 = vba.Color<Float4>(0, tri.mIndex1)[3];
        float alpha2 = vba.Color<Float4>(0, tri.mIndex2)[3];
        if (alpha0 == 0.0f && alpha1 == 0.0f && alpha2 == 0.0f)
        {
            continue;
        }
#endif

        Vector3f scaledCenter =
            vba.Position<Vector3f>(tri.mIndex0) +
            vba.Position<Vector3f>(tri.mIndex1) +
            vba.Position<Vector3f>(tri.mIndex2);

        APoint output = mCube->WorldTransform*APoint(scaledCenter);
        AVector diff = output - camPos;
        tri.mNegSqrDistance = -diff.SquaredLength();

        mTriangles.insert(tri);
    }

    IndexBuffer* ibuffer = mCube->GetIndexBuffer();
    int* indices = (int*)ibuffer->GetData();
    ibuffer->SetNumElements(3*(int)mTriangles.size());

    std::multiset<Triangle>::iterator iter = mTriangles.begin();
    std::multiset<Triangle>::iterator end = mTriangles.end();
    for (/**/; iter != end; ++iter)
    {
        *indices++ = iter->mIndex0;
        *indices++ = iter->mIndex1;
        *indices++ = iter->mIndex2;
    }

    mRenderer->Update(ibuffer);
}
Example #12
0
//----------------------------------------------------------------------------
void ClipMesh::Update ()
{
    // Transform the model-space vertices to world space.
    int numVertices = (int)mTorusVerticesMS.size();
    int i;
    for (i = 0; i < numVertices; ++i)
    {
        mTorusVerticesWS[i] = mTorus->LocalTransform*mTorusVerticesMS[i];
    }

    // Partition the torus mesh.
    std::vector<APoint> clipVertices;
    std::vector<int> negIndices, posIndices;
    PartitionMesh(mTorusVerticesWS, mTorusIndices, mPlane, clipVertices,
        negIndices, posIndices);

    // Replace the torus vertex buffer.
    numVertices = (int)clipVertices.size();
    int stride = mTorus->GetVertexFormat()->GetStride();
    VertexBuffer* vbuffer = new0 VertexBuffer(numVertices, stride,
        Buffer::BU_STATIC);
    mTorus->SetVertexBuffer(vbuffer);
    VertexBufferAccessor vba(mTorus);
    Float3 black(0.0f, 0.0f, 0.0f);
    for (i = 0; i < numVertices; ++i)
    {
        // Transform the world-space vertex to model space.
        vba.Position<Float3>(i) =
            mTorus->LocalTransform.Inverse()*clipVertices[i];

        vba.Color<Float3>(0, i) = black;
    }

    // Modify the vertex color based on which mesh the vertices lie.
    int negQuantity = (int)negIndices.size();
    for (i = 0; i < negQuantity; ++i)
    {
        vba.Color<Float3>(0, negIndices[i])[2] = 1.0f;
    }
    int posQuantity = (int)posIndices.size();
    for (i = 0; i < posQuantity; ++i)
    {
        vba.Color<Float3>(0, posIndices[i])[0] = 1.0f;
    }

    // To display the triangles generated by the split.
    int numIndices = negQuantity + posQuantity;
    IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, sizeof(int));
    mTorus->SetIndexBuffer(ibuffer);
    int* indices = (int*)ibuffer->GetData();
    memcpy(indices, &negIndices[0], negQuantity*sizeof(int));
    memcpy(indices + negQuantity, &posIndices[0], posQuantity*sizeof(int));
}
Example #13
0
//----------------------------------------------------------------------------
void ClipMesh::CreateScene ()
{
    mScene = new0 Node();

    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);

    // The plane is fixed at z = 0.
    mPlane.SetNormal(AVector::UNIT_Z);
    mPlane.SetConstant(0.0f);
    mMeshPlane = StandardMesh(vformat).Rectangle(32, 32, 16.0f, 16.0f);
    VisualEffectInstance* instance =
        VertexColor3Effect::CreateUniqueInstance();
    instance->GetEffect()->GetWireState(0,0)->Enabled = true;
    mMeshPlane->SetEffectInstance(instance);
    mScene->AttachChild(mMeshPlane);

    VertexBufferAccessor vba(mMeshPlane);
    Float3 green(0.0f, 1.0f, 0.0f);
    int i;
    for (i = 0; i < vba.GetNumVertices(); ++i)
    {
        vba.Color<Float3>(0, i) = green;
    }

    // Get the positions and indices for a torus.
    mTorus = StandardMesh(vformat).Torus(64, 64, 4.0f, 1.0f);
    instance = VertexColor3Effect::CreateUniqueInstance();
    mTorusWireState = instance->GetEffect()->GetWireState(0, 0);
    mTorus->SetEffectInstance(instance);
    mScene->AttachChild(mTorus);

    vba.ApplyTo(mTorus);
    mTorusVerticesMS.resize(vba.GetNumVertices());
    mTorusVerticesWS.resize(vba.GetNumVertices());
    Float3 black(0.0f, 0.0f, 0.0f);
    for (i = 0; i < vba.GetNumVertices(); ++i)
    {
        mTorusVerticesMS[i] = vba.Position<Float3>(i);
        mTorusVerticesWS[i] = mTorusVerticesMS[i];
        vba.Color<Float3>(0, i) = black;
    }

    IndexBuffer* ibuffer = mTorus->GetIndexBuffer();
    int numIndices = ibuffer->GetNumElements();
    int* indices = (int*)ibuffer->GetData();
    mTorusIndices.resize(numIndices);
    memcpy(&mTorusIndices[0], indices, numIndices*sizeof(int));

    Update();
}
//----------------------------------------------------------------------------
TriMesh* ScreenTarget::CreateRectangle (VertexFormat* vformat, int rtWidth,
										int rtHeight, float xmin, float xmax, float ymin, float ymax,
										float zValue)
{
	if (ValidFormat(vformat) && ValidSizes(rtWidth, rtHeight))
	{
		Float2 tc0, tc1, tc2, tc3;
		if (VertexShader::GetProfile() == VertexShader::VP_ARBVP1)
		{
			tc0 = Float2(0.0f, 0.0f);
			tc1 = Float2(1.0f, 0.0f);
			tc2 = Float2(1.0f, 1.0f);
			tc3 = Float2(0.0f, 1.0f);
		}
		else
		{
			float dx = 0.5f*(xmax - xmin)/(float)(rtWidth - 1);
			float dy = 0.5f*(ymax - ymin)/(float)(rtHeight - 1);
			xmin -= dx;
			xmax -= dx;
			ymin += dy;
			ymax += dy;
			tc0 = Float2(0.0f, 1.0f);
			tc1 = Float2(1.0f, 1.0f);
			tc2 = Float2(1.0f, 0.0f);
			tc3 = Float2(0.0f, 0.0f);
		}

		int vstride = vformat->GetStride();
		VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
		VertexBufferAccessor vba(vformat, vbuffer);
		vba.Position<Float3>(0) = Float3(xmin, ymin, zValue);
		vba.Position<Float3>(1) = Float3(xmax, ymin, zValue);
		vba.Position<Float3>(2) = Float3(xmax, ymax, zValue);
		vba.Position<Float3>(3) = Float3(xmin, ymax, zValue);
		vba.TCoord<Float2>(0, 0) = tc0;
		vba.TCoord<Float2>(0, 1) = tc1;
		vba.TCoord<Float2>(0, 2) = tc2;
		vba.TCoord<Float2>(0, 3) = tc3;

		// 为square创建IndexBuffer
		IndexBuffer* ibuffer = new0 IndexBuffer(6, sizeof(int));
		int* indices = (int*)ibuffer->GetData();
		indices[0] = 0;  indices[1] = 1;  indices[2] = 2;
		indices[3] = 0;  indices[4] = 2;  indices[5] = 3;

		return new0 TriMesh(vformat, vbuffer, ibuffer);
	}

	return 0;
}
void InitIndexBuffer(IndexBuffer & pIB, LPVOID indices, UINT idxCnt, UINT stride)
{
	D3DFORMAT format;
	if (stride == sizeof(WORD)) 
		format = D3DFMT_INDEX16;
	else if (stride == sizeof(DWORD))
		format = D3DFMT_INDEX32;

	GetD3D9Device()->CreateIndexBuffer(idxCnt * stride, NULL, format, D3DPOOL_MANAGED, &pIB, NULL);
	LPVOID pIndex;
	pIB->Lock(0, 0, (LPVOID*)&pIndex, 0);
	memcpy(pIndex, indices, idxCnt * stride);
	pIB->Unlock();
}
Example #16
0
//----------------------------------------------------------------------------
TriMesh* StandardMesh::Hexahedron ()
{
    float fSqrtThird = Mathf::Sqrt(1.0f/3.0f);

    int numVertices = 8;
    int numTriangles = 12;
    int numIndices = 3*numTriangles;
    int stride = mVFormat->GetStride();

    // Create a vertex buffer.
    VertexBuffer* vbuffer = new0 VertexBuffer(numVertices, stride, mUsage);

    VertexBufferAccessor vba(mVFormat, vbuffer);

    // Generate geometry.
    vba.Position<Float3>(0) = Float3(-fSqrtThird, -fSqrtThird, -fSqrtThird);
    vba.Position<Float3>(1) = Float3( fSqrtThird, -fSqrtThird, -fSqrtThird);
    vba.Position<Float3>(2) = Float3( fSqrtThird,  fSqrtThird, -fSqrtThird);
    vba.Position<Float3>(3) = Float3(-fSqrtThird,  fSqrtThird, -fSqrtThird);
    vba.Position<Float3>(4) = Float3(-fSqrtThird, -fSqrtThird,  fSqrtThird);
    vba.Position<Float3>(5) = Float3( fSqrtThird, -fSqrtThird,  fSqrtThird);
    vba.Position<Float3>(6) = Float3( fSqrtThird,  fSqrtThird,  fSqrtThird);
    vba.Position<Float3>(7) = Float3(-fSqrtThird,  fSqrtThird,  fSqrtThird);
    CreatePlatonicNormals(vba);
    CreatePlatonicUVs(vba);
    TransformData(vba);

    // Generate indices.
    IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, 4, mUsage);
    int* indices = (int*)ibuffer->GetData();
    indices[ 0] = 0;  indices[ 1] = 3;  indices[ 2] = 2;
    indices[ 3] = 0;  indices[ 4] = 2;  indices[ 5] = 1;
    indices[ 6] = 0;  indices[ 7] = 1;  indices[ 8] = 5;
    indices[ 9] = 0;  indices[10] = 5;  indices[11] = 4;
    indices[12] = 0;  indices[13] = 4;  indices[14] = 7;
    indices[15] = 0;  indices[16] = 7;  indices[17] = 3;
    indices[18] = 6;  indices[19] = 5;  indices[20] = 1;
    indices[21] = 6;  indices[22] = 1;  indices[23] = 2;
    indices[24] = 6;  indices[25] = 2;  indices[26] = 3;
    indices[27] = 6;  indices[28] = 3;  indices[29] = 7;
    indices[30] = 6;  indices[31] = 7;  indices[32] = 4;
    indices[33] = 6;  indices[34] = 4;  indices[35] = 5;

    if (mInside)
    {
        ReverseTriangleOrder(numTriangles,indices);
    }

    return new0 TriMesh(mVFormat, vbuffer, ibuffer);
}
Example #17
0
//----------------------------------------------------------------------------
Node* ExtremalQuery::CreateVisualConvexPolyhedron ()
{
    const Vector3f* vertices = mConvexPolyhedron->GetVertices();
    int numTriangles = mConvexPolyhedron->GetNumTriangles();
    int numIndices = 3*numTriangles;
    const int* polyIndices = mConvexPolyhedron->GetIndices();

    // Visualize the convex polyhedron as a collection of face-colored
    // triangles.
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(numIndices, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);

    IndexBuffer* ibuffer = new0 IndexBuffer(numIndices, sizeof(int));
    int* indices = (int*)ibuffer->GetData();

    int i;
    for (i = 0; i < numIndices; ++i)
    {
        vba.Position<Vector3f>(i) = vertices[polyIndices[i]];
        indices[i] = i;
    }

    TriMesh* mesh = new0 TriMesh(vformat, vbuffer, ibuffer);

    // Use randomly generated vertex colors.
    for (i = 0; i < numTriangles; ++i)
    {
        Float3 color;
        for (int j = 0; j < 3; ++j)
        {
            color[j] = Mathf::UnitRandom();
        }

        vba.Color<Float3>(0, 3*i  ) = color;
        vba.Color<Float3>(0, 3*i+1) = color;
        vba.Color<Float3>(0, 3*i+2) = color;
    }

    mesh->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());

    Node* root = new0 Node();
    root->AttachChild(mesh);
    return root;
}
Example #18
0
File: Mesh.cpp Project: casfire/F3
Mesh::Mesh(
	const Attribute& position,
	const IndexBuffer& indices,
	MeshType type
)
: position(position)
, indices(&indices)
, start(0)
, count(indices.getCount())
, type(type)
{
	if (count + start > indices.getCount()) {
		throw Error("Invalid mesh range.", __FILE__, __LINE__);
	}
}
Example #19
0
	IndexBuffer* BufferFactory::createIndexBuffer( unsigned int Length, unsigned long Usage, D3DFORMAT Format, D3DPOOL Pool )
	{
		IndexBuffer* ib = new IndexBuffer;
		if (ib)
		{
			if (!ib->create(Length, Usage, Format, Pool))
			{
				delete ib;
				ib = NULL;
			}
		}

		//
		return ib;
	}
    void configure() {
        s_textureUnit = -1;
        s_validGeneration++;
        VertexLayout::clearCache();

        blending.init(GL_FALSE);
        blendingFunc.init(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        culling.init(GL_TRUE);
        cullFace.init(GL_BACK);
        frontFace.init(GL_CCW);
        depthTest.init(GL_TRUE);
        depthWrite.init(GL_TRUE);

        glDisable(GL_STENCIL_TEST);
        glDepthFunc(GL_LEQUAL);
        glClearDepthf(1.0);
        glDepthRangef(0.0, 1.0);

        static size_t max = std::numeric_limits<size_t>::max();

        clearColor.init(0.0, 0.0, 0.0, 0.0);
        shaderProgram.init(max, false);
        vertexBuffer.init(max, false);
        indexBuffer.init(max, false);
        texture.init(GL_TEXTURE_2D, max, false);
        texture.init(GL_TEXTURE_CUBE_MAP, max, false);
        textureUnit.init(max, false);
    }
Example #21
0
 void DeviceContext::Bind(const IndexBuffer& ib, NativeFormat::Enum indexFormat, unsigned offset)
 {
         // (it seems that index formats are always 16 bit for OpenGLES?)
     assert(indexFormat == NativeFormat::R16_UINT);
     assert(offset == 0);    // (not supported currently... But we could safe it up for the draw call)
     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (GLuint)ib.GetUnderlying());
 }
Example #22
0
bool Model::EndLoad()
{
    // Upload vertex buffer data
    for (unsigned i = 0; i < vertexBuffers_.Size(); ++i)
    {
        VertexBuffer* buffer = vertexBuffers_[i];
        VertexBufferDesc& desc = loadVBData_[i];
        if (desc.data_)
        {
            buffer->SetShadowed(true);
            buffer->SetSize(desc.vertexCount_, desc.vertexElements_);
            buffer->SetData(desc.data_.Get());
        }
    }

    // Upload index buffer data
    for (unsigned i = 0; i < indexBuffers_.Size(); ++i)
    {
        IndexBuffer* buffer = indexBuffers_[i];
        IndexBufferDesc& desc = loadIBData_[i];
        if (desc.data_)
        {
            buffer->SetShadowed(true);
            buffer->SetSize(desc.indexCount_, desc.indexSize_ > sizeof(unsigned short));
            buffer->SetData(desc.data_.Get());
        }
    }

    // Set up geometries
    for (unsigned i = 0; i < geometries_.Size(); ++i)
    {
        for (unsigned j = 0; j < geometries_[i].Size(); ++j)
        {
            Geometry* geometry = geometries_[i][j];
            GeometryDesc& desc = loadGeometries_[i][j];
            geometry->SetVertexBuffer(0, vertexBuffers_[desc.vbRef_]);
            geometry->SetIndexBuffer(indexBuffers_[desc.ibRef_]);
            geometry->SetDrawRange(desc.type_, desc.indexStart_, desc.indexCount_);
        }
    }

    loadVBData_.Clear();
    loadIBData_.Clear();
    loadGeometries_.Clear();
    return true;
}
Example #23
0
//----------------------------------------------------------------------------
void IntersectTriangleCylinder::CreateScene ()
{
    mScene = new0 Node();
    mCullState = new0 CullState();
    mCullState->Enabled = false;
    mRenderer->SetOverrideCullState(mCullState);
    mWireState = new0 WireState();
    mRenderer->SetOverrideWireState(mWireState);

    VertexFormat* vformat = VertexFormat::Create(2,
                            VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
                            VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(3, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);
    vba.Position<Vector3f>(0) = (const Vector3f&)mTriangleMVertex0;
    vba.Color<Float3>(0, 0) = Float3(0.0f, 0.0f, 1.0f);
    vba.Position<Vector3f>(1) = (const Vector3f&)mTriangleMVertex1;
    vba.Color<Float3>(0, 1) = Float3(0.0f, 0.0f, 1.0f);
    vba.Position<Vector3f>(2) = (const Vector3f&)mTriangleMVertex2;
    vba.Color<Float3>(0, 2) = Float3(0.0f, 0.0f, 1.0f);

    IndexBuffer* ibuffer = new0 IndexBuffer(3, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[0] = 0;
    indices[1] = 1;
    indices[2] = 2;
    mTMesh = new0 TriMesh(vformat, vbuffer, ibuffer);
    mTMesh->SetEffectInstance(
        VertexColor3Effect::CreateUniqueInstance());
    mTMesh->LocalTransform.SetTranslate(APoint(0.0f, 1.125f, 0.0f));

    mCMesh = StandardMesh(vformat).Cylinder(8, 16, mCylinderRadius,
                                            mCylinderHeight, false);
    vba.ApplyTo(mCMesh);
    for (int i = 0; i < vba.GetNumVertices(); ++i)
    {
        vba.Color<Float3>(0, i) = Float3(1.0f, 0.0f, 0.0f);
    }
    mCMesh->SetEffectInstance(
        VertexColor3Effect::CreateUniqueInstance());

    mScene->AttachChild(mTMesh);
    mScene->AttachChild(mCMesh);
}
//----------------------------------------------------------------------------
TriMesh* PolyhedronDistance::CreateTetra (float size, bool isBlack)
{
    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(4, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);

    vba.Position<Vector3f>(0) = -(size/3.0f)*Vector3f(1.0f, 1.0f, 1.0f);
    vba.Position<Vector3f>(1) = Vector3f(size, 0.0f, 0.0f);
    vba.Position<Vector3f>(2) = Vector3f(0.0f, size, 0.0f);
    vba.Position<Vector3f>(3) = Vector3f(0.0f, 0.0f, size);

    if (isBlack)
    {
        // Black tetrahedra for the small ones used as points.
        Float3 black(0.0f, 0.0f, 0.0f);
        vba.Color<Float3>(0, 0) = black;
        vba.Color<Float3>(0, 1) = black;
        vba.Color<Float3>(0, 2) = black;
        vba.Color<Float3>(0, 3) = black;
    }
    else
    {
        // Colorful colors for the tetrahedra under study.
        vba.Color<Float3>(0, 0) = Float3(0.0f, 0.0f, 1.0f);
        vba.Color<Float3>(0, 1) = Float3(0.0f, 1.0f, 0.0f);
        vba.Color<Float3>(0, 2) = Float3(1.0f, 0.0f, 0.0f);
        vba.Color<Float3>(0, 3) = Float3(1.0f, 1.0f, 1.0f);
    }

    IndexBuffer* ibuffer = new0 IndexBuffer(12, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[ 0] = 0; indices[ 1] = 2; indices[ 2] = 1;
    indices[ 3] = 0; indices[ 4] = 3; indices[ 5] = 2;
    indices[ 6] = 0; indices[ 7] = 1; indices[ 8] = 3;
    indices[ 9] = 1; indices[10] = 2; indices[11] = 3;

    TriMesh* mesh = new0 TriMesh(vformat, vbuffer, ibuffer);
    mesh->SetEffectInstance(VertexColor3Effect::CreateUniqueInstance());

    return mesh;
}
//----------------------------------------------------------------------------
TriMesh* RoughPlaneSolidBox::CreateRamp ()
{
    float x = 8.0f;
    float y = 8.0f;
    float z = y*Mathf::Tan((float)mModule.Angle);

    VertexFormat* vformat = VertexFormat::Create(2,
        VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0,
        VertexFormat::AU_TEXCOORD, VertexFormat::AT_FLOAT2, 0);
    int vstride = vformat->GetStride();

    VertexBuffer* vbuffer = new0 VertexBuffer(6, vstride);
    VertexBufferAccessor vba(vformat, vbuffer);

    vba.Position<Float3>(0) = Float3(-x, 0.0f, 0.0f);
    vba.Position<Float3>(1) = Float3(+x, 0.0f, 0.0f);
    vba.Position<Float3>(2) = Float3(-x, y, 0.0f);
    vba.Position<Float3>(3) = Float3(+x, y, 0.0f);
    vba.Position<Float3>(4) = Float3(-x, y, z);
    vba.Position<Float3>(5) = Float3(+x, y, z);
    vba.TCoord<Float2>(0, 0) = Float2(0.25f, 0.0f);
    vba.TCoord<Float2>(0, 1) = Float2(0.75f, 0.0f);
    vba.TCoord<Float2>(0, 2) = Float2(0.0f, 1.0f);
    vba.TCoord<Float2>(0, 3) = Float2(1.0f, 1.0f);
    vba.TCoord<Float2>(0, 4) = Float2(0.25f, 1.0f);
    vba.TCoord<Float2>(0, 5) = Float2(0.75f, 1.0f);

    IndexBuffer* ibuffer = new0 IndexBuffer(18, sizeof(int));
    int* indices = (int*)ibuffer->GetData();
    indices[ 0] = 0;  indices[ 1] = 1;  indices[ 2] = 4;
    indices[ 3] = 1;  indices[ 4] = 5;  indices[ 5] = 4;
    indices[ 6] = 0;  indices[ 7] = 4;  indices[ 8] = 2;
    indices[ 9] = 1;  indices[10] = 3;  indices[11] = 5;
    indices[12] = 3;  indices[13] = 2;  indices[14] = 4;
    indices[15] = 3;  indices[16] = 4;  indices[17] = 5;

    TriMesh* ramp = new0 TriMesh(vformat, vbuffer, ibuffer);

    std::string path = Environment::GetPathR("Metal.wmtf");
    Texture2D* texture = Texture2D::LoadWMTF(path);
    ramp->SetEffectInstance(Texture2DEffect::CreateUniqueInstance(texture,
        Shader::SF_LINEAR, Shader::SC_REPEAT, Shader::SC_REPEAT));

    return ramp;
}
Example #26
0
namespace RenderState {

    Blending blending;
    DepthTest depthTest;
    StencilTest stencilTest;
    Culling culling;
    DepthWrite depthWrite;
    BlendingFunc blendingFunc;
    StencilWrite stencilWrite;
    StencilFunc stencilFunc;
    StencilOp stencilOp;
    ColorWrite colorWrite;
    FrontFace frontFace;
    CullFace cullFace;

    VertexBuffer vertexBuffer;
    IndexBuffer indexBuffer;

    TextureUnit textureUnit;
    Texture texture;

    GLuint getTextureUnit(GLuint _unit) {
        if (_unit >= TANGRAM_MAX_TEXTURE_UNIT) {
            logMsg("Warning: trying to access unavailable texture unit");
        }

        return GL_TEXTURE0 + _unit;
    }

    void bindVertexBuffer(GLuint _id) { glBindBuffer(GL_ARRAY_BUFFER, _id); }
    void bindIndexBuffer(GLuint _id) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _id); }
    void activeTextureUnit(GLuint _unit) { glActiveTexture(getTextureUnit(_unit)); }
    void bindTexture(GLenum _target, GLuint _textureId) { glBindTexture(_target, _textureId); }

    void configure() {
        blending.init(GL_FALSE);
        blendingFunc.init(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        culling.init(GL_TRUE);
        cullFace.init(GL_BACK);
        frontFace.init(GL_CCW);
        depthTest.init(GL_TRUE);
        depthWrite.init(GL_TRUE);

        glDisable(GL_STENCIL_TEST);
        glDepthFunc(GL_LEQUAL);
        glClearDepthf(1.0);
        glDepthRangef(0.0, 1.0);
        glClearColor(0.3, 0.3, 0.3, 1.0);

        vertexBuffer.init(std::numeric_limits<unsigned int>::max(), false);
        indexBuffer.init(std::numeric_limits<unsigned int>::max(), false);
        texture.init(GL_TEXTURE_2D, std::numeric_limits<unsigned int>::max(), false);
        texture.init(GL_TEXTURE_CUBE_MAP, std::numeric_limits<unsigned int>::max(), false);
        textureUnit.init(std::numeric_limits<unsigned int>::max(), false);
    }

}
void MeshRenderer::Update(GameTime* gameTime,GraphicsDevice* graphicsDevice)
{
	int drawCalls = 0;
	int triangles = 0;
	if(_mesh != NULL)
	{
		VertexBuffer* vertexBuffer = _mesh->GetVertexBuffer();
		IndexBuffer* indexBuffer = _mesh->GetIndexBuffer();
		
		//Set the per object uniforms of the game object(for example - the world matrix)
		_material->SetObjectUniforms(graphicsDevice, GetGameObject());

		vertexBuffer->BindBuffer();
		//Draw the mesh
		unsigned int numberOfAttributeInformations = vertexBuffer->GetNumberOfAttributeInfos();
		for(unsigned int i = 0; i < numberOfAttributeInformations; i++)
		{
			const VertexAttributeInformation& thisInfo = vertexBuffer->GetVertexAttributeInformation(i);
			graphicsDevice->EnableVertexAttribute(thisInfo.GetIndex());
			graphicsDevice->SetVertexAttribute(thisInfo.GetIndex(), 
							  thisInfo.GetSize(), 
							  thisInfo.GetType(),
							  thisInfo.GetIsNormalized(), 
							  thisInfo.GetStride(), 
							  thisInfo.GetOffset());
		}

		//Bind the index buffer
		indexBuffer->BindBuffer();
		//DRAW
		graphicsDevice->DrawElements(GraphicsPrimitiveType::Triangles(), indexBuffer->GetNumberOfElements(), indexBuffer->GetIndexDataType(), (void*)0);
		drawCalls++;
		triangles += indexBuffer->GetNumberOfElements() / 3;

		for(unsigned int i = 0; i < numberOfAttributeInformations; i++)
		{
			graphicsDevice->DisableVertexAttribute(vertexBuffer->GetVertexAttributeInformation(i).GetIndex());
		}
		indexBuffer->UnbindBuffer();
		vertexBuffer->UnbindBuffer();
	}
	SetNumberOfDrawCalls(drawCalls);
	SetNumberOfTriangles(triangles);
}
Example #28
0
void VertexArray::draw(const VertexBuffer& vb, const IndexBuffer& ib, PrimitiveType mode, GLenum type, GLsizei count)
{
    glCheck(gl::BindVertexArray(id()));
    glCheck(gl::BindBuffer(gl::ARRAY_BUFFER, vb.id()));
    glCheck(gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, ib.id()));
    glCheck(gl::DrawElements(static_cast<GLenum>(mode), count, type, (const GLvoid*)0));
    glCheck(gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, 0));
    glCheck(gl::BindBuffer(gl::ARRAY_BUFFER, 0));
    glCheck(gl::BindVertexArray(0));
}
Example #29
0
void TetrahedronEnvironment::init()
{
    float vertexArray[] =
    {
         1.0,  0.0,     -0.25 * 1.41421,
        -0.5,  0.86603, -0.25 * 1.41421,
        -0.5, -0.86603, -0.25 * 1.41421,
         0.0,  0.0,      0.75 * 1.41421
    };

    int indexArray[] =
    {
        0,2,1,
        0,1,3,
        1,2,3,
        2,0,3
    };

    buffer.set(vertexArray, sizeof(vertexArray) / sizeof(float));
    indexBuffer.set(indexArray, sizeof(indexArray) / sizeof(int));

    geometry["position"] = Field(&buffer, 3, 3, 0);
    geometry.indices = &indexBuffer;

    effect.vertexCode = 
        "\n"
        "uniform mat4 modelView;\n"
        "uniform mat4 projection;\n"
        "\n"
        "attribute vec3 position;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    gl_Position = projection * modelView * vec4(position, 1.0);\n"
        "}\n";

    effect.fragmentCode =
        "\n"
        "uniform vec4 color;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    gl_FragColor = color;\n"
        "}\n";
    
    effect.compile();

    material.effect = &effect;

    shape.geometry = &geometry;
    shape.assumptions.push_back(&camera);
    shape.assumptions.push_back(&material);

    material["color"] = Vec4(1.0, 0.0, 0.0, 1.0);
}
Example #30
0
//----------------------------------------------------------------------------
ClodMesh::ClodMesh (TriMesh* mesh, CollapseRecordArray* recordArray)
:
TriMesh(mesh->GetVertexFormat(), mesh->GetVertexBuffer(), 0),
mCurrentRecord(0),
mTargetRecord(0),
mRecordArray(recordArray)
{
	assertion(recordArray != 0, "Record array is needed for construction.\n");

	// 创建一份顶点索引的拷贝
	IndexBuffer* ibuffer = mesh->GetIndexBuffer();
	int numIndices = ibuffer->GetNumElements();
	int elementSize = ibuffer->GetElementSize();
	assertion(elementSize == 4, "Invalid indices.\n");

	char* srcIndices = ibuffer->GetData();
	mIBuffer = new0 IndexBuffer(numIndices, elementSize);
	char* trgIndices = mIBuffer->GetData();
	memcpy(trgIndices, srcIndices, numIndices*elementSize);
}