void TerrainRenderablePlane::MakeIndexBuffer() { auto device = Globals::GetDevice()->GetDeviceD3D9(); HRESULT res = device->CreateIndexBuffer(GetIndexNum() * sizeof(WORD), D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_MANAGED, &m_pIB, 0); if (res != S_OK) ReportErr("Terrain CreateIB Failed"); WORD* g_Indices = 0; res = m_pIB->Lock(0, 0, (void**)&g_Indices, 0); m_IndexData.push_back(0); m_IndexData.push_back(1); m_IndexData.push_back(2); m_IndexData.push_back(0); m_IndexData.push_back(2); m_IndexData.push_back(3); WORD* first = &(m_IndexData[0]); memcpy_s(g_Indices, GetIndexNum() * sizeof(WORD), first, GetIndexNum() * sizeof(WORD)); res = m_pIB->Unlock(); }
bool Tutorial18::Init(char* pVSFileName, char* pFSFileName) { Vector3f Pos(0.0f, 0.0f, -3.0f); Vector3f Target(0.0f, 0.0f, 1.0f); Vector3f Up(0.0, 1.0f, 0.0f); m_pGameCamera = new Camera(WINDOW_WIDTH, WINDOW_HEIGHT, Pos, Target, Up); unsigned int Indices[] = { 0, 3, 1, 1, 3, 2, 2, 3, 0, 1, 2, 0 }; CreateIndexBuffer(Indices, sizeof(Indices)); CreateVertexBuffer(Indices, ARRAY_SIZE_IN_ELEMENTS(Indices)); m_pEffect = new LightingTechnique(); if (!m_pEffect->Init(pVSFileName, pFSFileName)) { printf("Error initializing the lighting technique\n"); return false; } m_pEffect->Enable(); m_pEffect->SetTextureUnit(0); m_pTexture = new Texture(GL_TEXTURE_2D, "/home/lparkin/Projects/S3/OpenGlDirsProject/LearnOpenGL-nonQt/Project/Content/test.png"); if (!m_pTexture->Load()) { return false; } return true; }
bool Init() { Vector3f Pos(0.0f, 0.0f, -3.0f); Vector3f Target(0.0f, 0.0f, 1.0f); Vector3f Up(0.0, 1.0f, 0.0f); m_pGameCamera = new Camera(WINDOW_WIDTH, WINDOW_HEIGHT, Pos, Target, Up); unsigned int Indices[] = { 0, 3, 1, 1, 3, 2, 2, 3, 0, 1, 2, 0 }; CreateIndexBuffer(Indices, sizeof(Indices)); CreateVertexBuffer(Indices, ARRAY_SIZE_IN_ELEMENTS(Indices)); m_pEffect = new LightingTechnique(); if (!m_pEffect->Init()) { printf("Error initializing the lighting technique\n"); return false; } m_pEffect->Enable(); m_pEffect->SetTextureUnit(0); m_pTexture = new Texture(GL_TEXTURE_2D, "test.png"); if (!m_pTexture->Load()) { return false; } return true; }
void DrawOutdent(const float size[2], Graphics::RenderState *state) { PROFILE_SCOPED() // locals RefCountedPtr<Graphics::VertexBuffer> vb; RefCountedPtr<Graphics::IndexBuffer> ib[3]; // see if we have this size of indent in the cache already const vector2f vsize(size[0], size[1]); MapOutdentBuffers::iterator bufIt = s_outdentBuffers.find(vsize); if (bufIt != s_outdentBuffers.end()) { // found it vb = bufIt->second.vb; ib[0] = bufIt->second.ib[0]; ib[1] = bufIt->second.ib[1]; ib[2] = bufIt->second.ib[2]; } else { // generate it const vector3f vertices[] = { /* 0 */ vector3f(0, 0, 0), /* 1 */ vector3f(0, size[1], 0), /* 2 */ vector3f(size[0], size[1], 0), /* 3 */ vector3f(size[0], 0, 0), /* 4 */ vector3f(BORDER_WIDTH, BORDER_WIDTH, 0), /* 5 */ vector3f(BORDER_WIDTH, size[1] - BORDER_WIDTH, 0), /* 6 */ vector3f(size[0] - BORDER_WIDTH, size[1] - BORDER_WIDTH, 0), /* 7 */ vector3f(size[0] - BORDER_WIDTH, BORDER_WIDTH, 0) }; const Uint32 indices[] = { 0, 1, 5, 0, 5, 4, 0, 4, 7, 0, 7, 3, 3, 7, 6, 3, 6, 2, 1, 2, 6, 1, 6, 5, 4, 5, 6, 4, 6, 7 }; // create buffer Graphics::VertexBufferDesc vbd; vbd.attrib[0].semantic = Graphics::ATTRIB_POSITION; vbd.attrib[0].format = Graphics::ATTRIB_FORMAT_FLOAT3; vbd.numVertices = 8; vbd.usage = Graphics::BUFFER_USAGE_STATIC; // Upload data vb.Reset(Screen::GetRenderer()->CreateVertexBuffer(vbd)); TPos* vtxPtr = vb->Map<TPos>(Graphics::BUFFER_MAP_WRITE); assert(vb->GetDesc().stride == sizeof(TPos)); for (Uint32 i = 0; i < 8; i++) { vtxPtr[i].pos = vertices[i]; } vb->Unmap(); // indices Uint32 IndexStart = 0; Uint32 IndexEnd = 12; Uint32 NumIndices = 12; ib[0].Reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); IndexStart += NumIndices; NumIndices = 12; IndexEnd += NumIndices; ib[1].Reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); IndexStart += NumIndices; NumIndices = 6; IndexEnd += NumIndices; ib[2].Reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); TOutdentBuffers tib; tib.vb = vb; tib.ib[0] = ib[0]; tib.ib[1] = ib[1]; tib.ib[2] = ib[2]; s_outdentBuffers[vsize] = tib; } // Draw it! Screen::flatColorMaterial->diffuse = Color(153,153,153,255); Screen::GetRenderer()->DrawBufferIndexed(vb.Get(), ib[0].Get(), state, Screen::flatColorMaterial); Screen::flatColorMaterial->diffuse = Colors::bgShadow; Screen::GetRenderer()->DrawBufferIndexed(vb.Get(), ib[1].Get(), state, Screen::flatColorMaterial); Screen::flatColorMaterial->diffuse = Colors::bg; Screen::GetRenderer()->DrawBufferIndexed(vb.Get(), ib[2].Get(), state, Screen::flatColorMaterial); }
void BasicShapes::CreateTangentSphere( _Out_ ID3D11Buffer **vertexBuffer, _Out_ ID3D11Buffer **indexBuffer, _Out_opt_ unsigned int *vertexCount, _Out_opt_ unsigned int *indexCount ) { const int numSegments = 64; const int numSlices = numSegments / 2; const int numVertices = (numSlices + 1) * (numSegments + 1); std::unique_ptr<TangentVertex[]> sphereVertices(new TangentVertex[numVertices]); for (int slice = 0; slice <= numSlices; slice++) { float v = (float)slice/(float)numSlices; float inclination = v * PI_F; float y = cos(inclination); float r = sin(inclination); for (int segment = 0; segment <= numSegments; segment++) { float u = (float)segment/(float)numSegments; float azimuth = u * PI_F * 2.0f; int index = slice * (numSegments+1) + segment; sphereVertices[index].pos = float3(r*sin(azimuth), y, r*cos(azimuth)); sphereVertices[index].tex = float2(u, v); sphereVertices[index].uTan = float3(cos(azimuth), 0, -sin(azimuth)); sphereVertices[index].vTan = float3(cos(inclination)*sin(azimuth), -sin(inclination), cos(inclination)*cos(azimuth)); } } const int numIndices = numSlices * (numSegments-2) * 6; std::unique_ptr<unsigned short[]> sphereIndices(new unsigned short[numIndices]); unsigned int index = 0; for (int slice = 0; slice < numSlices; slice++) { unsigned short sliceBase0 = (unsigned short)((slice)*(numSegments+1)); unsigned short sliceBase1 = (unsigned short)((slice+1)*(numSegments+1)); for (int segment = 0; segment < numSegments; segment++) { if (slice > 0) { sphereIndices[index++] = sliceBase0 + segment; sphereIndices[index++] = sliceBase0 + segment + 1; sphereIndices[index++] = sliceBase1 + segment + 1; } if (slice < numSlices-1) { sphereIndices[index++] = sliceBase0 + segment; sphereIndices[index++] = sliceBase1 + segment + 1; sphereIndices[index++] = sliceBase1 + segment; } } } CreateTangentVertexBuffer( numVertices, sphereVertices.get(), vertexBuffer ); if (vertexCount != nullptr) { *vertexCount = numVertices; } CreateIndexBuffer( numIndices, sphereIndices.get(), indexBuffer ); if (indexCount != nullptr) { *indexCount = numIndices; } }
void DrawOutdent(const float size[2], Graphics::RenderState *state) { const vector3f vertices[] = { /* 0 */ vector3f(0,0,0), /* 1 */ vector3f(0,size[1],0), /* 2 */ vector3f(size[0],size[1],0), /* 3 */ vector3f(size[0],0,0), /* 4 */ vector3f(BORDER_WIDTH,BORDER_WIDTH,0), /* 5 */ vector3f(BORDER_WIDTH,size[1]-BORDER_WIDTH,0), /* 6 */ vector3f(size[0]-BORDER_WIDTH,size[1]-BORDER_WIDTH,0), /* 7 */ vector3f(size[0]-BORDER_WIDTH,BORDER_WIDTH, 0) }; const GLushort indices[] = { 0,1,5, 0,5,4, 0,4,7, 0,7,3, 3,7,6, 3,6,2, 1,2,6, 1,6,5, 4,5,6, 4,6,7 }; // create buffer Graphics::VertexBufferDesc vbd; vbd.attrib[0].semantic = Graphics::ATTRIB_POSITION; vbd.attrib[0].format = Graphics::ATTRIB_FORMAT_FLOAT3; vbd.numVertices = 8; vbd.usage = Graphics::BUFFER_USAGE_STATIC; // Upload data std::unique_ptr<Graphics::VertexBuffer> vb; vb.reset( Screen::GetRenderer()->CreateVertexBuffer(vbd) ); TPos* vtxPtr = vb->Map<TPos>(Graphics::BUFFER_MAP_WRITE); assert(vb->GetDesc().stride == sizeof(TPos)); for(Uint32 i=0 ; i<8 ; i++) { vtxPtr[i].pos = vertices[i]; } vb->Unmap(); // indices Uint32 IndexStart = 0; Uint32 IndexEnd = 12; Uint32 NumIndices = 12; std::unique_ptr<Graphics::IndexBuffer> ib[3]; ib[0].reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); IndexStart += NumIndices; NumIndices = 12; IndexEnd += NumIndices; ib[1].reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); IndexStart += NumIndices; NumIndices = 6; IndexEnd += NumIndices; ib[2].reset(CreateIndexBuffer(indices, IndexStart, IndexEnd, NumIndices)); // Draw it! Screen::flatColorMaterial->diffuse = Color(153,153,153,255); Screen::GetRenderer()->DrawBufferIndexed(vb.get(), ib[0].get(), state, Screen::flatColorMaterial); Screen::flatColorMaterial->diffuse = Colors::bgShadow; Screen::GetRenderer()->DrawBufferIndexed(vb.get(), ib[1].get(), state, Screen::flatColorMaterial); Screen::flatColorMaterial->diffuse = Colors::bg; Screen::GetRenderer()->DrawBufferIndexed(vb.get(), ib[2].get(), state, Screen::flatColorMaterial); }
BOOL CMeshConverter::ConvertMesh(void) { int i; int iVertexBufferIndex; void* pvFirstNewVert; int iStartVertex; CMeshVertexArray* pcMeshVertexBuffer; CArrayVertexBufferArrayFormat cArray; CVertexBufferArrayFormat* pcVertexBufferFormatArray; int j; CVertexArray* pcVertexArray; int iMaterial; int iStartIndex; int iNumFaces; int iNumIndices; CVertexBufferExtended* pcVertexBuffer; int iFormat; if (!CreateIndexBuffer()) { return FALSE; } CollectVertexBuffersByFormat(&cArray); mpcGraphicsObject->AddPrimitives(cArray.NumElements()); iStartIndex = 0; for (i = 0; i < cArray.NumElements(); i++) { pcVertexBufferFormatArray = cArray.Get(i); iFormat = pcVertexBufferFormatArray->mpsVertexType->iD3DVertexFormat; pcVertexBuffer = mpcGraphicsObject->AddVertexBuffer(iFormat, pcVertexBufferFormatArray->miNumVerticies, TRUE, &iVertexBufferIndex); if (pcVertexBuffer == NULL) { return FALSE; } pvFirstNewVert = pcVertexBuffer->pvLockedBuffer; if (!pvFirstNewVert) { return FALSE; } iStartVertex = 0; for (j = 0; j < pcVertexBufferFormatArray->macMeshVertexArrays.NumElements(); j++) { pcMeshVertexBuffer = pcVertexBufferFormatArray->macMeshVertexArrays.Get(j); pcVertexArray = pcMeshVertexBuffer->GetVertexArray(); PopulatePositions(pcVertexBufferFormatArray->mpsVertexType, pvFirstNewVert, pcVertexArray); PopulateNormals(pcVertexBufferFormatArray->mpsVertexType, pvFirstNewVert, pcVertexArray); PopulateColours(pcVertexBufferFormatArray->mpsVertexType, pvFirstNewVert, pcVertexArray); PopulateUVs(pcVertexBufferFormatArray->mpsVertexType, pvFirstNewVert, pcVertexArray); PopulateVertexWeights(pcVertexBufferFormatArray->mpsVertexType, pvFirstNewVert, pcVertexArray); iMaterial = pcMeshVertexBuffer->GetFaceType()->GetMaterial(); iNumFaces = pcMeshVertexBuffer->GetFaceIndicies()->NumElements(); iNumIndices = pcMeshVertexBuffer->GetFaceIndicies()->NumElements() * 3; if (!SetGraphicsPrimitive(i, iMaterial, iNumFaces, iNumIndices, iStartIndex, iStartVertex, iVertexBufferIndex)) { return FALSE; } iStartIndex += iNumIndices; iStartVertex += pcMeshVertexBuffer->GetVertexArray()->GetSize(); } gcD3D.UnlockVertexBuffer(pcVertexBuffer); } cArray.Kill(); return TRUE; }
void Mesh::LoadMesh(CTSTR lpName/*, BOOL bMeshList*/) { traceIn(Mesh::LoadMesh); assert(lpName); DWORD i; isStatic = 1; /*if(bMeshList) MeshList << this;*/ String strFilePath; if(!Engine::ConvertResourceName(lpName, TEXT("models"), strFilePath)) return; //----------------------------------------------- XFileInputSerializer modelData; if(!modelData.Open(strFilePath)) { AppWarning(TEXT("Could not open model file \"%s\""), (TSTR)strFilePath); return; } DWORD ver; modelData << ver; if(ver != MODELFILE_VER && ver != 0x100) { AppWarning(TEXT("'%s': Bad model file version"), strFilePath); modelData.Close(); return; } strName = lpName; //----------------------------------------------- VBData *vbData = new VBData; vbData->TVList.SetSize(1); vbData->TVList[0].SetWidth(2); List<UVCoord> LMCoords; TVertList tv; Vect::SerializeList(modelData, vbData->VertList);//modelData << vbData->VertList; Vect::SerializeList(modelData, vbData->NormalList);//modelData << vbData->NormalList; modelData << *vbData->TVList[0].GetV2(); if(ver == 0x100) //remove { modelData << LMCoords; if(LMCoords.Num()) { vbData->TVList.SetSize(2); vbData->TVList[1].SetWidth(2); vbData->TVList[1].GetV2()->TransferFrom(LMCoords); } } else { modelData << tv; if(tv.Num()) { vbData->TVList.SetSize(2); vbData->TVList[1].TransferFrom(tv); } } Vect::SerializeList(modelData, vbData->TangentList);//modelData << vbData->TangentList; nVerts = vbData->VertList.Num(); VertBuffer = CreateVertexBuffer(vbData, FALSE); //----------------------------------------------- modelData << nFaces; FaceList = (Face*)Allocate(nFaces*sizeof(Face)); modelData.Serialize(FaceList, nFaces*sizeof(Face)); IdxBuffer = CreateIndexBuffer(GS_UNSIGNED_LONG, FaceList, nFaces*3); //----------------------------------------------- modelData << nEdges; EdgeList = (Edge*)Allocate(nEdges*sizeof(Edge)); modelData.Serialize(EdgeList, nEdges*sizeof(Edge)); //----------------------------------------------- modelData << DefaultMaterialList; modelData << nSections; SectionList = (DrawSection*)Allocate(nSections*sizeof(DrawSection)); modelData.Serialize(SectionList, nSections*sizeof(DrawSection)); //----------------------------------------------- modelData << bounds; modelData.Close(); //----------------------------------------------- if(engine->InEditor()) { DWORD *wireframeIndices = (DWORD*)Allocate(nEdges*2*sizeof(DWORD)); for(i=0; i<nEdges; i++) { DWORD curPos = i*2; wireframeIndices[curPos] = EdgeList[i].v1; wireframeIndices[curPos+1] = EdgeList[i].v2; } WireframeBuffer = CreateIndexBuffer(GS_UNSIGNED_LONG, wireframeIndices, nEdges*2); } traceOut; }
/// <summary> /// Creates a <see cref="VertexBuffer"/> filled with <see cref="VertexPositionNormal"/> vertices forming a cylinder. /// </summary> /// <remarks> /// Draw with an INDEXED TriangleList. The values to use are as follows: /// Vertex Count = slices * (stacks + 1) + 2 /// Primitive Count = slices * (stacks + 1) * 2 /// </remarks> /// <param name="device">The <see cref="GraphicsDevice"/> that is associated with the created cylinder.</param> /// <param name="bottomRadius">Radius at the negative Y end. Value should be greater than or equal to 0.0f.</param> /// <param name="topRadius">Radius at the positive Y end. Value should be greater than or equal to 0.0f.</param> /// <param name="length">Length of the cylinder along the Y-axis.</param> /// <param name="slices">Number of slices about the Y axis.</param> /// <param name="stacks">Number of stacks along the Y axis.</param> void Shape3D::CreateCylinder(float bottomRadius, float topRadius, float length, unsigned int slices, unsigned int stacks, const char* aShaderName) { // if both the top and bottom have a radius of zero, just return 0, because invalid if (bottomRadius <= 0 && topRadius <= 0) { } float sliceStep = 2.0f*D3DXFROMWINE_PI / slices; float heightStep = length / stacks; float radiusStep = (topRadius - bottomRadius) / stacks; float currentHeight = -length / 2; unsigned int vertexCount = (stacks + 1) * slices + 2; //cone = stacks * slices + 1 unsigned int triangleCount = (stacks + 1) * slices * 2; //cone = stacks * slices * 2 + slices unsigned int indexCount = triangleCount * 3; float currentRadius = bottomRadius; VertexPositionNormal* vertices=new VertexPositionNormal[vertexCount]; // Start at the bottom of the cylinder unsigned int currentVertex = 0; vertices[currentVertex++] = VertexPositionNormal(D3DXFROMWINEVECTOR3(0, currentHeight, 0), D3DXFROMWINEVECTOR3(0, -1, 0)); for (unsigned int i = 0; i <= stacks; i++) { float sliceAngle = 0; for (unsigned int j = 0; j < slices; j++) { float x = currentRadius * (float)cosf(sliceAngle); float y = currentHeight; float z = currentRadius * (float)sinf(sliceAngle); D3DXFROMWINEVECTOR3 position = D3DXFROMWINEVECTOR3(x, y, z); D3DXFROMWINEVECTOR3 normal = D3DXFROMWINEVECTOR3(x, y, z); D3DXFROMWINEVec3Normalize(&normal, &normal); vertices[currentVertex++] = VertexPositionNormal(position, normal); sliceAngle += sliceStep; } currentHeight += heightStep; currentRadius += radiusStep; } vertices[currentVertex++] = VertexPositionNormal(D3DXFROMWINEVECTOR3(0, length / 2, 0), D3DXFROMWINEVECTOR3(0, 1, 0)); // Create the actual vertex buffer object VertexBuffer=IRenderer::GetRendererInstance()->addVertexBuffer(VertexPositionNormal::VertexSizeInBytes * vertexCount, STATIC, vertices); delete[] vertices; IndexBuffer = CreateIndexBuffer(vertexCount, indexCount, slices); VertexCount = vertexCount; TriangleCount = triangleCount; Shader = FW3ShadersFactory::GetShader(aShaderName, "main", "main"); VertexDeclaration = FW3ShadersFactory::GetVertexFormat(eCylinder, Shader); VertexSizeInBytes = VertexPositionNormal::VertexSizeInBytes; }
void ComponentShape::CreateBuffer() { assert(CreateVertexBuffer()); assert(CreateIndexBuffer()); }
/// <summary> /// Creates a <see cref="VertexBuffer"/> filled with <see cref="VertexPositionNormal"/> vertices forming a sphere. /// </summary> /// <remarks> /// Draw with an INDEXED TriangleList. The values to use are as follows: /// Vertex Count = slices * (stacks - 1) + 2 /// Primitive Count = slices * (stacks - 1) * 2 /// </remarks> /// <param name="device">The <see cref="GraphicsDevice"/> that is associated with the created sphere.</param> /// <param name="radius">Radius of the sphere. This value should be greater than or equal to 0.0f.</param> /// <param name="slices">Number of slices about the Y axis.</param> /// <param name="stacks">Number of stacks along the Y axis. Should be 2 or greater. (stack of 1 is just a cylinder)</param> void Shape3D::CreateSphere(float radius, unsigned int slices, unsigned int stacks, const char* aShaderName) { float sliceStep = 2.0f*D3DXFROMWINE_PI / slices; float stackStep = D3DXFROMWINE_PI / stacks; unsigned int vertexCount = slices * (stacks - 1) + 2; unsigned int triangleCount = slices * (stacks - 1) * 2; unsigned int indexCount = triangleCount * 3; VertexPositionNormal* sphereVertices=new VertexPositionNormal[vertexCount]; unsigned int currentVertex = 0; sphereVertices[currentVertex++] = VertexPositionNormal(D3DXFROMWINEVECTOR3(0, -radius, 0), D3DXFROMWINEVECTOR3(0, -1, 0)); float stackAngle = D3DXFROMWINE_PI - stackStep; for (unsigned int i = 0; i < stacks - 1; i++) { float sliceAngle = 0; for (unsigned int j = 0; j < slices; j++) { //NOTE: y and z were switched from normal spherical coordinates because the sphere is "oriented" along the Y axis as opposed to the Z axis float x = (float)(radius * sinf(stackAngle) * cosf(sliceAngle)); float y = (float)(radius * cosf(stackAngle)); float z = (float)(radius * sinf(stackAngle) * sinf(sliceAngle)); D3DXFROMWINEVECTOR3 position = D3DXFROMWINEVECTOR3(x, y, z); D3DXFROMWINEVECTOR3 normal = D3DXFROMWINEVECTOR3(x, y, z); D3DXFROMWINEVec3Normalize(&normal, &normal); sphereVertices[currentVertex++] = VertexPositionNormal(position, normal); sphereVertices[currentVertex++] = VertexPositionNormal(position, normal); sliceAngle += sliceStep; } stackAngle -= stackStep; } sphereVertices[currentVertex++] = VertexPositionNormal(D3DXFROMWINEVECTOR3(0, radius, 0), D3DXFROMWINEVECTOR3(0, 1, 0)); // Create the actual vertex buffer LOG_FNLN; LOG_PRINT("VertexSize=%x\n", VertexPositionNormal::VertexSizeInBytes); LOG_PRINT("vertexCount=%x\n", vertexCount); LOG_PRINT("sphereVertices=%x\n", sphereVertices); VertexBuffer=IRenderer::GetRendererInstance()->addVertexBuffer(VertexPositionNormal::VertexSizeInBytes * vertexCount, STATIC, sphereVertices); delete[] sphereVertices; IndexBuffer = CreateIndexBuffer(vertexCount, indexCount, slices); VertexCount = vertexCount; TriangleCount = triangleCount; Shader = FW3ShadersFactory::GetShader(aShaderName, "main", "main"); VertexDeclaration = FW3ShadersFactory::GetVertexFormat(eSphere, Shader); VertexSizeInBytes = VertexPositionNormal::VertexSizeInBytes; }
HRESULT CDXUTSDKMesh::CreateFromMemory(ID3D11Device* pDev11, BYTE* pData, UINT /*DataBytes*/, bool /*bCreateAdjacencyIndices*/, bool bCopyStatic) { HRESULT hr = E_FAIL; m_pDev11 = pDev11; m_bCopyStatic = bCopyStatic; // Set outstanding resources to zero m_NumOutstandingResources = 0; if (bCopyStatic) { SDKMESH_HEADER* pHeader = (SDKMESH_HEADER*)pData; SIZE_T StaticSize = (SIZE_T)(pHeader->HeaderSize + pHeader->NonBufferDataSize); m_pHeapData = new BYTE[StaticSize]; if (!m_pHeapData) return hr; m_pStaticMeshData = m_pHeapData; CopyMemory(m_pStaticMeshData, pData, StaticSize); } else { m_pHeapData = pData; m_pStaticMeshData = pData; } // Pointer fixup m_pMeshHeader = (SDKMESH_HEADER*)m_pStaticMeshData; #ifdef _XBOX SwapSDKMeshHeader(m_pMeshHeader); #endif m_pVertexBufferArray = (SDKMESH_VERTEX_BUFFER_HEADER*)(m_pStaticMeshData + m_pMeshHeader->VertexStreamHeadersOffset); #ifdef _XBOX SwapVertexBufferHeaderArray(m_pVertexBufferArray, m_pMeshHeader->NumVertexBuffers); #endif m_pIndexBufferArray = (SDKMESH_INDEX_BUFFER_HEADER*)(m_pStaticMeshData + m_pMeshHeader->IndexStreamHeadersOffset); #ifdef _XBOX SwapIndexBufferHeaderArray(m_pIndexBufferArray, m_pMeshHeader->NumIndexBuffers); #endif m_pMeshArray = (SDKMESH_MESH*)(m_pStaticMeshData + m_pMeshHeader->MeshDataOffset); #ifdef _XBOX SwapMeshArray(m_pMeshArray, m_pMeshHeader->NumMeshes); #endif m_pSubsetArray = (SDKMESH_SUBSET*)(m_pStaticMeshData + m_pMeshHeader->SubsetDataOffset); #ifdef _XBOX SwapSubsetArray(m_pSubsetArray, m_pMeshHeader->NumTotalSubsets); #endif m_pFrameArray = (SDKMESH_FRAME*)(m_pStaticMeshData + m_pMeshHeader->FrameDataOffset); #ifdef _XBOX SwapFrameArray(m_pFrameArray, m_pMeshHeader->NumFrames); #endif m_pMaterialArray = (SDKMESH_MATERIAL*)(m_pStaticMeshData + m_pMeshHeader->MaterialDataOffset); #ifdef _XBOX SwapMaterialArray(m_pMaterialArray, m_pMeshHeader->NumMaterials); #endif // Setup subsets for (UINT i = 0; i < m_pMeshHeader->NumMeshes; i++) { m_pMeshArray[i].pSubsets = (UINT*)(m_pStaticMeshData + m_pMeshArray[i].SubsetOffset); m_pMeshArray[i].pFrameInfluences = (UINT*)(m_pStaticMeshData + m_pMeshArray[i].FrameInfluenceOffset); } // error condition if (m_pMeshHeader->Version != SDKMESH_FILE_VERSION) { hr = E_NOINTERFACE; goto Error; } // Setup buffer data pointer BYTE* pBufferData = pData + m_pMeshHeader->HeaderSize + m_pMeshHeader->NonBufferDataSize; // Get the start of the buffer data UINT64 BufferDataStart = m_pMeshHeader->HeaderSize + m_pMeshHeader->NonBufferDataSize; // Create VBs m_ppVertices = new BYTE*[m_pMeshHeader->NumVertexBuffers]; for (UINT i = 0; i < m_pMeshHeader->NumVertexBuffers; i++) { BYTE* pVertices = NULL; pVertices = (BYTE*)(pBufferData + (m_pVertexBufferArray[i].DataOffset - BufferDataStart)); #ifdef _XBOX SwapBuffer((DWORD *)pVertices, m_pVertexBufferArray[i].SizeBytes); #endif if (pDev11) CreateVertexBuffer(pDev11, &m_pVertexBufferArray[i], pVertices); m_ppVertices[i] = pVertices; } // Create IBs m_ppIndices = new BYTE*[m_pMeshHeader->NumIndexBuffers]; for (UINT i = 0; i < m_pMeshHeader->NumIndexBuffers; i++) { BYTE* pIndices = NULL; pIndices = (BYTE*)(pBufferData + (m_pIndexBufferArray[i].DataOffset - BufferDataStart)); #ifdef _XBOX SwapBuffer((DWORD *)pIndices, m_pIndexBufferArray[i].SizeBytes); #endif if (pDev11) CreateIndexBuffer(pDev11, &m_pIndexBufferArray[i], pIndices); m_ppIndices[i] = pIndices; } // Create a place to store our bind pose frame matrices m_pBindPoseFrameMatrices = new MATRIX[m_pMeshHeader->NumFrames]; if (!m_pBindPoseFrameMatrices) goto Error; // Create a place to store our transformed frame matrices m_pTransformedFrameMatrices = new MATRIX[m_pMeshHeader->NumFrames]; if (!m_pTransformedFrameMatrices) goto Error; m_pWorldPoseFrameMatrices = new MATRIX[m_pMeshHeader->NumFrames]; if (!m_pWorldPoseFrameMatrices) goto Error; hr = S_OK; Error: return hr; }
void BasicShapes::CreateReferenceAxis( _Out_ ID3D11Buffer **vertexBuffer, _Out_ ID3D11Buffer **indexBuffer, _Out_opt_ unsigned int *vertexCount, _Out_opt_ unsigned int *indexCount ) { BasicVertex axisVertices[] = { { float3( 0.500f, 0.000f, 0.000f), float3( 0.125f, 0.500f, 0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.125f, 0.500f, 0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.125f, 0.500f, 0.500f), float2(0.250f, 0.250f) }, { float3( 0.500f, 0.000f, 0.000f), float3( 0.125f,-0.500f, 0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.125f,-0.500f, 0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.125f,-0.500f, 0.500f), float2(0.250f, 0.250f) }, { float3( 0.500f, 0.000f, 0.000f), float3( 0.125f,-0.500f,-0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.125f,-0.500f,-0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.125f,-0.500f,-0.500f), float2(0.250f, 0.250f) }, { float3( 0.500f, 0.000f, 0.000f), float3( 0.125f, 0.500f,-0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.125f, 0.500f,-0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.125f, 0.500f,-0.500f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.125f, 0.000f), float3(-0.125f, 0.000f, 0.000f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.000f,-0.125f), float3(-0.125f, 0.000f, 0.000f), float2(0.250f, 0.250f) }, { float3( 0.000f,-0.125f, 0.000f), float3(-0.125f, 0.000f, 0.000f), float2(0.250f, 0.250f) }, { float3( 0.000f, 0.000f, 0.125f), float3(-0.125f, 0.000f, 0.000f), float2(0.250f, 0.250f) }, { float3(-0.500f, 0.000f, 0.000f), float3(-0.125f, 0.500f, 0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.000f, 0.125f), float3(-0.125f, 0.500f, 0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.125f, 0.000f), float3(-0.125f, 0.500f, 0.500f), float2(0.250f, 0.500f) }, { float3(-0.500f, 0.000f, 0.000f), float3(-0.125f, 0.500f,-0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.125f, 0.000f), float3(-0.125f, 0.500f,-0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.000f,-0.125f), float3(-0.125f, 0.500f,-0.500f), float2(0.250f, 0.500f) }, { float3(-0.500f, 0.000f, 0.000f), float3(-0.125f,-0.500f,-0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.000f,-0.125f), float3(-0.125f,-0.500f,-0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f,-0.125f, 0.000f), float3(-0.125f,-0.500f,-0.500f), float2(0.250f, 0.500f) }, { float3(-0.500f, 0.000f, 0.000f), float3(-0.125f,-0.500f, 0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f,-0.125f, 0.000f), float3(-0.125f,-0.500f, 0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.000f, 0.125f), float3(-0.125f,-0.500f, 0.500f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.125f, 0.000f, 0.000f), float2(0.250f, 0.500f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.125f, 0.000f, 0.000f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.125f, 0.000f, 0.000f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.125f, 0.000f, 0.000f), float2(0.250f, 0.500f) }, { float3( 0.000f, 0.500f, 0.000f), float3( 0.500f, 0.125f, 0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.500f, 0.125f, 0.500f), float2(0.500f, 0.250f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f, 0.125f, 0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.500f, 0.000f), float3( 0.500f, 0.125f,-0.500f), float2(0.500f, 0.250f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f, 0.125f,-0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.500f, 0.125f,-0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.500f, 0.000f), float3(-0.500f, 0.125f,-0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.000f,-0.125f), float3(-0.500f, 0.125f,-0.500f), float2(0.500f, 0.250f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f, 0.125f,-0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.500f, 0.000f), float3(-0.500f, 0.125f, 0.500f), float2(0.500f, 0.250f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f, 0.125f, 0.500f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.000f, 0.125f), float3(-0.500f, 0.125f, 0.500f), float2(0.500f, 0.250f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.000f,-0.125f, 0.000f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.000f,-0.125f, 0.000f), float2(0.500f, 0.250f) }, { float3(-0.125f, 0.000f, 0.000f), float3( 0.000f,-0.125f, 0.000f), float2(0.500f, 0.250f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.000f,-0.125f, 0.000f), float2(0.500f, 0.250f) }, { float3( 0.000f,-0.500f, 0.000f), float3( 0.500f,-0.125f, 0.500f), float2(0.500f, 0.500f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f,-0.125f, 0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.500f,-0.125f, 0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f,-0.500f, 0.000f), float3(-0.500f,-0.125f, 0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f, 0.125f), float3(-0.500f,-0.125f, 0.500f), float2(0.500f, 0.500f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f,-0.125f, 0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f,-0.500f, 0.000f), float3(-0.500f,-0.125f,-0.500f), float2(0.500f, 0.500f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f,-0.125f,-0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f,-0.125f), float3(-0.500f,-0.125f,-0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f,-0.500f, 0.000f), float3( 0.500f,-0.125f,-0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.500f,-0.125f,-0.500f), float2(0.500f, 0.500f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f,-0.125f,-0.500f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f,-0.125f), float3( 0.000f, 0.125f, 0.000f), float2(0.500f, 0.500f) }, { float3(-0.125f, 0.000f, 0.000f), float3( 0.000f, 0.125f, 0.000f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f, 0.125f), float3( 0.000f, 0.125f, 0.000f), float2(0.500f, 0.500f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.000f, 0.125f, 0.000f), float2(0.500f, 0.500f) }, { float3( 0.000f, 0.000f, 0.500f), float3( 0.500f, 0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f, 0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.500f, 0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.000f, 0.500f), float3(-0.500f, 0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.125f, 0.000f), float3(-0.500f, 0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f, 0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.000f, 0.500f), float3(-0.500f,-0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f,-0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f,-0.125f, 0.000f), float3(-0.500f,-0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.000f, 0.500f), float3( 0.500f,-0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.500f,-0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f,-0.500f, 0.125f), float2(0.750f, 0.250f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.000f, 0.000f,-0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.000f, 0.000f,-0.125f), float2(0.750f, 0.250f) }, { float3(-0.125f, 0.000f, 0.000f), float3( 0.000f, 0.000f,-0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.000f, 0.000f,-0.125f), float2(0.750f, 0.250f) }, { float3( 0.000f, 0.000f,-0.500f), float3( 0.500f, 0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.500f, 0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f, 0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f, 0.000f,-0.500f), float3( 0.500f,-0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.500f,-0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.500f,-0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f, 0.000f,-0.500f), float3(-0.500f,-0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f,-0.125f, 0.000f), float3(-0.500f,-0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f,-0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f, 0.000f,-0.500f), float3(-0.500f, 0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3(-0.125f, 0.000f, 0.000f), float3(-0.500f, 0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f, 0.125f, 0.000f), float3(-0.500f, 0.500f,-0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f, 0.125f, 0.000f), float3( 0.000f, 0.000f, 0.125f), float2(0.750f, 0.500f) }, { float3(-0.125f, 0.000f, 0.000f), float3( 0.000f, 0.000f, 0.125f), float2(0.750f, 0.500f) }, { float3( 0.000f,-0.125f, 0.000f), float3( 0.000f, 0.000f, 0.125f), float2(0.750f, 0.500f) }, { float3( 0.125f, 0.000f, 0.000f), float3( 0.000f, 0.000f, 0.125f), float2(0.750f, 0.500f) }, }; unsigned short axisIndices[] = { 0, 2, 1, 3, 5, 4, 6, 8, 7, 9, 11, 10, 12, 14, 13, 12, 15, 14, 16, 18, 17, 19, 21, 20, 22, 24, 23, 25, 27, 26, 28, 30, 29, 28, 31, 30, 32, 34, 33, 35, 37, 36, 38, 40, 39, 41, 43, 42, 44, 46, 45, 44, 47, 46, 48, 50, 49, 51, 53, 52, 54, 56, 55, 57, 59, 58, 60, 62, 61, 60, 63, 62, 64, 66, 65, 67, 69, 68, 70, 72, 71, 73, 75, 74, 76, 78, 77, 76, 79, 78, 80, 82, 81, 83, 85, 84, 86, 88, 87, 89, 91, 90, 92, 94, 93, 92, 95, 94, }; CreateVertexBuffer( ARRAYSIZE(axisVertices), axisVertices, vertexBuffer ); if (vertexCount != nullptr) { *vertexCount = ARRAYSIZE(axisVertices); } CreateIndexBuffer( ARRAYSIZE(axisIndices), axisIndices, indexBuffer ); if (indexCount != nullptr) { *indexCount = ARRAYSIZE(axisIndices); } }
bool ModelLoader::Load( ID3D11Device* device, const std::string& filename, Vertex::VERTEX_TYPE type, Model& outModel, Skeleton* outSkeleton, AnimationController* outAnimationController ) { this->device = device; // reset extents minX = minY = minZ = FLT_MAX; maxX = maxY = maxZ = FLT_MIN; Assimp::Importer importer; Assimp::DefaultLogger::get()->info( "*********** Importing: "+filename+" ***********" ); scene = importer.ReadFile( filename, aiProcess_CalcTangentSpace| aiProcess_ImproveCacheLocality| aiProcess_MakeLeftHanded| aiProcess_FlipWindingOrder| aiProcess_Triangulate| aiProcess_JoinIdenticalVertices| aiProcess_SortByPType| aiProcess_FlipUVs| aiProcess_ValidateDataStructure| aiProcess_FindInvalidData ); if( !scene ) { Assimp::DefaultLogger::get()->error( importer.GetErrorString() ); return false; } if( !scene->HasMeshes() ) { Assimp::DefaultLogger::get()->error( "File contains no mesh" ); return false; } CreateIndexBuffer(); CreateVertexBuffer( type ); if( scene->HasAnimations() ) { this->skeleton = outSkeleton; this->animationController = outAnimationController; // if the model has animations then the caller must pass in a skeleton and a animation controller assert( outSkeleton!=nullptr ); assert( outAnimationController!=nullptr ); CreateSkeleton(); CreateBoneHierarchy(); CreateAnimations(); } outModel.SetIB( ib, indexCount ); outModel.SetVB( vb ); switch( type ) { case Vertex::BASIC_32: { outModel.SetVertexStride( sizeof( Vertex::Basic32 ) ); break; } case Vertex::POS_NORMAL_TEX_TAN: { outModel.SetVertexStride( sizeof( Vertex::PosNormalTexTan ) ); break; } case Vertex::POS_NORMAL_TEX_TAN_SKINNED: { outModel.SetVertexStride( sizeof( Vertex::PosNormalTexTanSkinned ) ); break; } default: assert( false ); // this should never happen } return true; }
bool LudoRenderer::DrawBlendedVertices(const CUSTOMVERTEX &vertices, const int numVertices, const int * const indices, const int numIndices, float factor, const LPDIRECT3DTEXTURE9 texture1, const LPDIRECT3DTEXTURE9 texture2) { // We need to reset the VFV, ideally, we should not be calling this every draw call // This call is here because the Helper Function DrawSubset is resetting the FVF //m_d3DDev->SetFVF(CUSTOMFVF); m_d3DDev->SetStreamSource(0, m_VertBuff, 0, sizeof(CUSTOMVERTEX)); if (numIndices > m_MaxNumIndices) { CreateIndexBuffer(numIndices); } if (numVertices > m_MaxNumVertex) { CreateVertexBuffer(numVertices); } int blendFactor = static_cast<int>(255 * factor); m_d3DDev->SetRenderState(D3DRS_TEXTUREFACTOR, D3DCOLOR_ARGB(blendFactor, 0, 0, 0)); if (texture1 != NULL) { m_d3DDev->SetTexture(0, texture1); m_d3DDev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0); m_d3DDev->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); m_d3DDev->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); } if (texture2 != NULL) { m_d3DDev->SetTexture(1, texture2); //m_d3DDev->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1); m_d3DDev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_BLENDFACTORALPHA); // or Add... m_d3DDev->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); // the texture for this stage with... m_d3DDev->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT); // the current argument passed down from stage 0 } VOID* pVoid = 0; m_VertBuff->Lock(0, 0, reinterpret_cast<void**>(&pVoid), NULL); //memcpy_s(pVoid, sizeof(CUSTOMVERTEX) * numVertices, reinterpret_cast<const BYTE * const * const>(&vertices), sizeof(CUSTOMVERTEX) * numVertices); memcpy(pVoid, reinterpret_cast<const BYTE * const * const>(&vertices), sizeof(CUSTOMVERTEX) * numVertices); m_VertBuff->Unlock(); if (indices == NULL) { m_d3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, numVertices - 2); } else { void* buffer = NULL; m_IndexBuff->Lock(0, 0, &buffer, NULL); //memcpy_s(buffer, numIndices * sizeof(int), indices, numIndices * sizeof(int)); memcpy(buffer, indices, numIndices * sizeof(int)); m_IndexBuff->Unlock(); m_d3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, numVertices, 0, numIndices - 2); } m_d3DDev->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); // Disable the second texture return true; }
void SkinMesh::LoadAnimations(CTSTR lpName) { traceIn(SkinMesh::LoadAnimations); assert(lpName); DWORD i, j, k; String strMeshPath; if(!Engine::ConvertResourceName(lpName, TEXT("models"), strMeshPath)) return; String strFilePath; strFilePath << GetPathWithoutExtension(strMeshPath) << TEXT(".xan"); //----------------------------------------------- XFileInputSerializer animData; if(!animData.Open(strFilePath)) { AppWarning(TEXT("Could not open animation file \"%s\""), (TSTR)strFilePath); return; } DWORD ver; animData << ver; if(ver != ANIMFILE_VER && ver != 0x100) { AppWarning(TEXT("'%s': Bad animation file version"), strFilePath); animData.Close(); return; } //----------------------------------------------- unsigned int numSequences; animData << numSequences; SequenceList.SetSize(numSequences); for(i=0; i<numSequences; i++) animData << SequenceList[i]; DWORD nBones; animData << nBones; BoneList.SetSize(nBones); for(i=0; i<nBones; i++) { Bone &bone = BoneList[i]; List<DWORD> RigidVerts; List<VWeight> BlendedVerts; DWORD nRigid, nBlended; //---------------------------- animData << bone.Pos << bone.Rot << nRigid << nBlended << bone.flags << bone.idParent; animData << RigidVerts; animData << BlendedVerts; if(bone.idParent != 0xFFFFFFFF) { bone.Parent = BoneList+bone.idParent; bone.LocalPos = bone.Pos - BoneList[bone.idParent].Pos; BoneList[bone.idParent].Children << &bone; } else { bone.Parent = NULL; bone.LocalPos = bone.Pos; bone.flags |= BONE_ROOT; } //---------------------------- bone.Weights.SetSize(nRigid); for(j=0; j<RigidVerts.Num(); j++) { VWeight &weight = bone.Weights[j]; weight.vert = RigidVerts[j]; weight.weight = 1.0f; } bone.Weights.AppendList(BlendedVerts); //---------------------------- DWORD nAnim; animData << nAnim; bone.seqKeys.SetSize(nAnim); for(j=0; j<nAnim; j++) { SeqKeys &keys = bone.seqKeys[j]; DWORD nPosKeys, nRotKeys; //---------------------------- animData << nRotKeys; keys.hasRotKeys = nRotKeys > 0; if(keys.hasRotKeys) { keys.lpRotKeys = (Quat*)Allocate(nRotKeys*sizeof(Quat)); animData.Serialize(keys.lpRotKeys, nRotKeys*sizeof(Quat)); keys.lpRotTans = (Quat*)Allocate(nRotKeys*sizeof(Quat)); for(k=0; k<nRotKeys; k++) { DWORD kp1 = (k == (nRotKeys-1)) ? k : (k+1); DWORD km1 = (k == 0) ? 0 : k-1; Quat &qk = keys.lpRotKeys[k]; Quat &qkp1 = keys.lpRotKeys[kp1]; Quat &qkm1 = keys.lpRotKeys[km1]; keys.lpRotTans[k] = qk.GetInterpolationTangent(qkm1, qkp1); } } //---------------------------- animData << nPosKeys; keys.hasPosKeys = nPosKeys > 0; if(keys.hasPosKeys) { keys.lpPosKeys = (Vect*)Allocate(nPosKeys*sizeof(Vect)); Vect::SerializeArray(animData, keys.lpPosKeys, nPosKeys); keys.lpPosTans = (Vect*)Allocate(nPosKeys*sizeof(Vect)); for(k=0; k<nPosKeys; k++) { DWORD kp1 = (k == (nPosKeys-1)) ? k : (k+1); DWORD km1 = (k == 0) ? 0 : k-1; Vect &pk = keys.lpPosKeys[k]; Vect &pkp1 = keys.lpPosKeys[kp1]; Vect &pkm1 = keys.lpPosKeys[km1]; keys.lpPosTans[k] = pk.GetInterpolationTangent(pkm1, pkp1); } } } } //----------------------------------------------- int num; animData << num; BoneExtensions.SetSize(num); BoneExtensionNames.SetSize(num); for(int i=0; i<num; i++) { animData << BoneExtensionNames[i]; animData << BoneExtensions[i]; } //----------------------------------------------- AnimatedSections.SetSize(nSections); if(ver == 0x100) //remove { UINT *indices = (UINT*)IdxBuffer->GetData(); List<UINT> adjustedIndices; adjustedIndices.CopyArray(indices, IdxBuffer->NumIndices()); VBData *vbd = VertBuffer->GetData(); List<Vect> &verts = vbd->VertList; //--------- // get vert data List<VertAnimInfo> vertInfo; vertInfo.SetSize(nVerts); for(int i=0; i<nBones; i++) { Bone &bone = BoneList[i]; for(int j=0; j<bone.Weights.Num(); j++) { VWeight &vertWeight = bone.Weights[j]; if(!vertInfo[vertWeight.vert].bones.HasValue(i)) { vertInfo[vertWeight.vert].bones << i; vertInfo[vertWeight.vert].weights << vertWeight.weight; } } } //--------- // remove excess bone influence from verts (let just set the max to 3 for this) /*for(int i=0; i<vertInfo.Num(); i++) { VertAnimInfo &vert = vertInfo[i]; while(vert.bones.Num() > 3) { float weakestWeight = M_INFINITE; UINT weakestID; for(int j=0; j<vert.bones.Num(); j++) { if(vert.weights[j] < weakestWeight) { weakestID = j; weakestWeight = vert.weights[j]; } } float weightAdjust = 1.0f/(1.0f-weakestWeight); vert.weights.Remove(weakestID); vert.bones.Remove(weakestID); for(int j=0; j<vert.weights.Num(); j++) vert.weights[j] *= weightAdjust; } }*/ for(int i=0; i<vertInfo.Num(); i++) { VertAnimInfo &vert = vertInfo[i]; for(int j=0; j<vert.bones.Num(); j++) { if(vert.weights[j] <= 0.15f) { float weightAdjust = 1.0f/(1.0f-vert.weights[j]); vert.weights.Remove(j); vert.bones.Remove(j); for(int k=0; k<vert.weights.Num(); k++) vert.weights[k] *= weightAdjust; --j; } } } //--------- // remove excess bone influence from tris (can only have 4 bones influencing any triangle) for(int i=0; i<nSections; i++) { DrawSection §ion = SectionList[i]; if(!section.numFaces) continue; for(int j=0; j<section.numFaces; j++) { UINT *triVertIDs = &indices[(section.startFace+j)*3]; List<UINT> bones; List<float> bestVertWeights; for(int k=0; k<3; k++) { VertAnimInfo &info = vertInfo[triVertIDs[k]]; for(int l=0; l<info.bones.Num(); l++) { UINT id = bones.FindValueIndex(info.bones[l]); if(id == INVALID) { bones.Add(info.bones[l]); bestVertWeights.Add(info.weights[l]); } else bestVertWeights[id] = MAX(bestVertWeights[id], info.weights[l]); } } while(bones.Num() > 4) { int removeBone, removeBoneID; float bestWeight = M_INFINITE; for(int k=0; k<bones.Num(); k++) { if(bestVertWeights[k] < bestWeight) { removeBone = bones[k]; removeBoneID = k; bestWeight = bestVertWeights[k]; } } for(int k=0; k<3; k++) { VertAnimInfo &info = vertInfo[triVertIDs[k]]; UINT id = info.bones.FindValueIndex(removeBone); if(id == INVALID) continue; float weightAdjust = 1.0f/(1.0f-info.weights[id]); info.weights.Remove(id); info.bones.Remove(id); for(int l=0; l<info.weights.Num(); l++) info.weights[l] *= weightAdjust; } bones.Remove(removeBoneID); bestVertWeights.Remove(removeBoneID); } } } //--------- // sort out sections of triangles that are influenced up to a max of 4 bones // also, duplicate shared verts VBData *newVBD = new VBData; newVBD->CopyList(*vbd); newVBD->TVList.SetSize(2); newVBD->TVList[1].SetWidth(4); newVBD->TVList[1].SetSize(nVerts); List<SubSectionInfo> newSubSections; for(int i=0; i<nSections; i++) { List<TriBoneInfo> triInfo; DrawSection §ion = SectionList[i]; if(!section.numFaces) continue; for(int j=0; j<section.numFaces; j++) { UINT *triVertIDs = &indices[(section.startFace+j)*3]; TriBoneInfo &newTri = *triInfo.CreateNew(); for(int k=0; k<3; k++) { VertAnimInfo &info = vertInfo[triVertIDs[k]]; for(int l=0; l<info.bones.Num(); l++) newTri.bones.SafeAdd(info.bones[l]); } } BitList UsedTris; UsedTris.SetSize(section.numFaces); DWORD nUsedTris = 0; while(nUsedTris != section.numFaces) { DWORD triSectionID = newSubSections.Num(); SubSectionInfo *curSubSecInfo = newSubSections.CreateNew(); curSubSecInfo->section = i; for(int j=0; j<triInfo.Num(); j++) { if(UsedTris[j]) continue; TriBoneInfo &tri = triInfo[j]; List<UINT> secBones; secBones.CopyList(curSubSecInfo->bones); BOOL bBadTri = FALSE; for(int k=0; k<tri.bones.Num(); k++) { secBones.SafeAdd(tri.bones[k]); if(secBones.Num() > 4) break; } if(secBones.Num() > 4) continue; DWORD triID = section.startFace+j; curSubSecInfo->bones.CopyList(secBones); curSubSecInfo->tris << triID; UINT *triVertIDs = &indices[triID*3]; for(int k=0; k<3; k++) { VertAnimInfo *info = &vertInfo[triVertIDs[k]]; UINT id = info->sections.FindValueIndex(triSectionID); if(id == INVALID) { UINT vertID; if(info->sections.Num() >= 1) //duplicate vertex { vertID = newVBD->DuplicateVertex(triVertIDs[k]); adjustedIndices[(triID*3)+k] = vertID; VertAnimInfo &newVertInfo = *vertInfo.CreateNew(); info = &vertInfo[triVertIDs[k]]; //reset pointer newVertInfo.bones.CopyList(info->bones); newVertInfo.weights.CopyList(info->weights); newVertInfo.sections << triSectionID; } else vertID = triVertIDs[k]; info->sections << triSectionID; info->sectionVertIDs << vertID; List<Vect4> &vertWeights = *newVBD->TVList[1].GetV4(); for(int l=0; l<4; l++) { if(l >= curSubSecInfo->bones.Num()) vertWeights[vertID].ptr[l] = 0.0f; else { UINT boneID = curSubSecInfo->bones[l]; UINT weightID = info->bones.FindValueIndex(boneID); if(weightID == INVALID) vertWeights[vertID].ptr[l] = 0.0f; else vertWeights[vertID].ptr[l] = info->weights[weightID]; } } } else adjustedIndices[(triID*3)+k] = info->sectionVertIDs[id]; } UsedTris.Set(j); ++nUsedTris; } } for(int j=0; j<triInfo.Num(); j++) triInfo[j].FreeData(); } //--------- // create animated draw sections and create new index buffer DWORD curTriID = 0; List<UINT> newIndices; for(int i=0; i<newSubSections.Num(); i++) { SubSectionInfo &subSecInfo = newSubSections[i]; AnimSection &animSection = AnimatedSections[subSecInfo.section]; AnimSubSection &subSection = *animSection.SubSections.CreateNew(); subSection.numBones = subSecInfo.bones.Num(); for(int j=0; j<subSecInfo.bones.Num(); j++) subSection.bones[j] = subSecInfo.bones[j]; subSection.startFace = curTriID; for(int j=0; j<subSecInfo.tris.Num(); j++) { UINT *tri = &adjustedIndices[subSecInfo.tris[j]*3]; newIndices << tri[0] << tri[1] << tri[2]; ++curTriID; } subSection.numFaces = curTriID-subSection.startFace; subSecInfo.FreeData(); } //rebuild original bone data for(int i=0; i<BoneList.Num(); i++) BoneList[i].Weights.Clear(); for(int i=0; i<vertInfo.Num(); i++) { for(int j=0; j<vertInfo[i].bones.Num(); j++) { VWeight weight; weight.vert = i; weight.weight = vertInfo[i].weights[j]; BoneList[vertInfo[i].bones[j]].Weights << weight; } vertInfo[i].FreeData(); } delete VertBuffer; delete IdxBuffer; UINT numIndices; UINT *indexArray; newIndices.TransferTo(indexArray, numIndices); nVerts = newVBD->VertList.Num(); IdxBuffer = CreateIndexBuffer(GS_UNSIGNED_LONG, indexArray, numIndices); VertBuffer = CreateVertexBuffer(newVBD); } else { for(int i=0; i<nSections; i++) animData << AnimatedSections[i].SubSections; } animData.Close(); traceOut; }
bool LudoRenderer::DrawPlanet(float orbitPosition, float planetScale, float planetOrbitRadius, const CUSTOMVERTEX &vertices, const int numVertices, const int * const indices, const int numIndices, const LPDIRECT3DTEXTURE9 texture) { // We need to reset the VFV, ideally, we should not be calling this every draw call // This call is here because the Helper Function DrawSubset is resetting the FVF m_d3DDev->SetFVF(CUSTOMFVF); m_d3DDev->SetStreamSource(0, m_VertBuff, 0, sizeof(CUSTOMVERTEX)); if (numIndices > m_MaxNumIndices) { CreateIndexBuffer(numIndices); } if (numVertices > m_MaxNumVertex) { CreateVertexBuffer(numVertices); } if (texture != NULL) { m_d3DDev->SetTexture(0, texture); } // Transform the planet to its final size and position // I.S.R.O.T (Identity, Scale, Local Rotatation, Orbit (Translation + Rotation), Translation D3DXMATRIX matWorld; D3DXMatrixIdentity(&matWorld); D3DXMATRIX scale; D3DXMatrixScaling(&scale, planetScale, planetScale, planetScale); matWorld *= scale; D3DXMATRIX orbit; D3DXMatrixTranslation(&orbit, planetOrbitRadius, 0.0f, 0.0f); D3DXMATRIX orbitRot; D3DXMatrixRotationZ(&orbitRot, -orbitPosition); orbit *= orbitRot; matWorld *= orbit; D3DXMATRIX translation; D3DXMatrixTranslation(&translation, 0.0f, GROUND_POS, 0.0f); matWorld *= translation; m_d3DDev->SetTransform(D3DTS_WORLD, &matWorld); VOID* pVoid = 0; m_VertBuff->Lock(0, 0, reinterpret_cast<void**>(&pVoid), NULL); memcpy_s(pVoid, sizeof(CUSTOMVERTEX) * numVertices, reinterpret_cast<const BYTE * const * const>(&vertices), sizeof(CUSTOMVERTEX) * numVertices); m_VertBuff->Unlock(); if (indices == NULL) { m_d3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, numVertices - 2); } else { void* buffer = NULL; m_IndexBuff->Lock(0, 0, &buffer, NULL); memcpy_s(buffer, numIndices * sizeof(int), indices, numIndices * sizeof(int)); m_IndexBuff->Unlock(); m_d3DDev->DrawIndexedPrimitive(D3DPT_TRIANGLESTRIP, 0, 0, numVertices, 0, numIndices - 2); } // Reset the World Matrix D3DXMatrixIdentity(&matWorld); m_d3DDev->SetTransform(D3DTS_WORLD, &matWorld); return true; }
//============================================================================= // constructor //============================================================================= MeshCylinder::MeshCylinder(const f32& in_block_width, const f32& in_block_height, const u32& in_width_count, const u32& in_height_count) :Mesh(true, true) , size_(in_width_count * in_block_width, in_height_count *in_block_height) , block_size_(in_block_width, in_block_height) , width_count_(in_width_count) , height_count_(in_height_count) , index_count_(0) , indexs_(nullptr) , division_width_(1) , division_height_(1) { auto directx9 = GET_DIRECTX9_DEVICE(); vertex_count_ = 4 * width_count_*height_count_; index_count_ = 4 * width_count_*height_count_ + 2 * (width_count_ - 1)*height_count_ + 2 * (height_count_ - 1); indexs_ = new u32[width_count_*height_count_]; memset(indexs_, 0, sizeof(u32)*width_count_*height_count_); // create vertex buffer directx9->CreateVertexBuffer(sizeof(VERTEX) * vertex_count_, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &direct3dvertexbuffer9_, NULL); // create index buffer directx9->CreateIndexBuffer(sizeof(u32) * index_count_, D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_MANAGED, &direct3dindexbuffer9_, NULL); division_height_ = 10; u32* index = nullptr; u32 rect_count = vertex_count_ / 4; u32 count = 0; direct3dindexbuffer9_->Lock(NULL, NULL, (void**)&index, NULL); for (u32 i = 0; i < height_count_; ++i) { for (u32 j = 0; j < width_count_; ++j) { index[count + 0] = (i * width_count_ + j) * 4 + 0; index[count + 1] = (i * width_count_ + j) * 4 + 1; index[count + 2] = (i * width_count_ + j) * 4 + 2; index[count + 3] = (i * width_count_ + j) * 4 + 3; count += 4; if (j != (width_count_ - 1)) { index[count + 0] = (i * width_count_ + j) * 4 + 3; index[count + 1] = (i * width_count_ + j) * 4 + 4; count += 2; } else if (i != (height_count_ - 1)) { index[count + 0] = (i * width_count_ + j) * 4 + 3; index[count + 1] = ((i + 1) * width_count_) * 4 + 0; count += 2; } } } direct3dindexbuffer9_->Unlock(); primitive_count_ = index_count_ - 2; primitive_type_ = D3DPT_TRIANGLESTRIP; stride_ = sizeof(VERTEX); UpdateVertexBuffer_(); }
void BasicShapes::CreateBox( float3 r, _Out_ ID3D11Buffer **vertexBuffer, _Out_ ID3D11Buffer **indexBuffer, _Out_opt_ unsigned int *vertexCount, _Out_opt_ unsigned int *indexCount ) { BasicVertex boxVertices[] = { // FLOOR {float3(-r.x, -r.y, r.z), float3(0.0f, 1.0f, 0.0f), float2(0.0f, 0.0f)}, {float3( r.x, -r.y, r.z), float3(0.0f, 1.0f, 0.0f), float2(1.0f, 0.0f)}, {float3(-r.x, -r.y, -r.z), float3(0.0f, 1.0f, 0.0f), float2(0.0f, 1.5f)}, {float3( r.x, -r.y, -r.z), float3(0.0f, 1.0f, 0.0f), float2(1.0f, 1.5f)}, // WALL {float3(-r.x, r.y, r.z), float3(0.0f, 0.0f, -1.0f), float2(0.0f, 0.0f)}, {float3( r.x, r.y, r.z), float3(0.0f, 0.0f, -1.0f), float2(2.0f, 0.0f)}, {float3(-r.x, -r.y, r.z), float3(0.0f, 0.0f, -1.0f), float2(0.0f, 1.5f)}, {float3( r.x, -r.y, r.z), float3(0.0f, 0.0f, -1.0f), float2(2.0f, 1.5f)}, // WALL {float3(r.x, r.y, r.z), float3(-1.0f, 0.0f, 0.0f), float2(0.0f, 0.0f)}, {float3(r.x, r.y, -r.z), float3(-1.0f, 0.0f, 0.0f), float2(r.y, 0.0f)}, {float3(r.x, -r.y, r.z), float3(-1.0f, 0.0f, 0.0f), float2(0.0f, 1.5f)}, {float3(r.x, -r.y, -r.z), float3(-1.0f, 0.0f, 0.0f), float2(r.y, 1.5f)}, // WALL {float3( r.x, r.y, -r.z), float3(0.0f, 0.0f, 1.0f), float2(0.0f, 0.0f)}, {float3(-r.x, r.y, -r.z), float3(0.0f, 0.0f, 1.0f), float2(2.0f, 0.0f)}, {float3( r.x, -r.y, -r.z), float3(0.0f, 0.0f, 1.0f), float2(0.0f, 1.5f)}, {float3(-r.x, -r.y, -r.z), float3(0.0f, 0.0f, 1.0f), float2(2.0f, 1.5f)}, // WALL {float3(-r.x, r.y, -r.z), float3(1.0f, 0.0f, 0.0f), float2(0.0f, 0.0f)}, {float3(-r.x, r.y, r.z), float3(1.0f, 0.0f, 0.0f), float2(r.y, 0.0f)}, {float3(-r.x, -r.y, -r.z), float3(1.0f, 0.0f, 0.0f), float2(0.0f, 1.5f)}, {float3(-r.x, -r.y, r.z), float3(1.0f, 0.0f, 0.0f), float2(r.y, 1.5f)}, // CEILING {float3(-r.x, r.y, -r.z), float3(0.0f, -1.0f, 0.0f), float2(-0.15f, 0.0f)}, {float3( r.x, r.y, -r.z), float3(0.0f, -1.0f, 0.0f), float2( 1.25f, 0.0f)}, {float3(-r.x, r.y, r.z), float3(0.0f, -1.0f, 0.0f), float2(-0.15f, 2.1f)}, {float3( r.x, r.y, r.z), float3(0.0f, -1.0f, 0.0f), float2( 1.25f, 2.1f)}, }; unsigned short boxIndices[] = { 0, 2, 1, 1, 2, 3, 4, 6, 5, 5, 6, 7, 8, 10, 9, 9, 10, 11, 12, 14, 13, 13, 14, 15, 16, 18, 17, 17, 18, 19, 20, 22, 21, 21, 22, 23, }; CreateVertexBuffer( ARRAYSIZE(boxVertices), boxVertices, vertexBuffer ); if (vertexCount != nullptr) { *vertexCount = ARRAYSIZE(boxVertices); } CreateIndexBuffer( ARRAYSIZE(boxIndices), boxIndices, indexBuffer ); if (indexCount != nullptr) { *indexCount = ARRAYSIZE(boxIndices); } }