CDx11VertexBuffer::CDx11VertexBuffer(ID3D11Device* device, CDx11ContextManager& contextManager, const VERTEX_BUFFER_DESCRIPTOR& descriptor) : CVertexBuffer(descriptor) , m_contextManager(contextManager) { HRESULT result = S_OK; uint32 vertexSize = descriptor.GetVertexSize(); { D3D11_BUFFER_DESC bufferDesc = {}; bufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; bufferDesc.ByteWidth = vertexSize * descriptor.vertexCount; bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; result = device->CreateBuffer(&bufferDesc, nullptr, &m_vertexBuffer); assert(SUCCEEDED(result)); } { D3D11_BUFFER_DESC bufferDesc = {}; bufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER; bufferDesc.ByteWidth = sizeof(uint16) * descriptor.indexCount; bufferDesc.Usage = D3D11_USAGE_DYNAMIC; bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; result = device->CreateBuffer(&bufferDesc, nullptr, &m_indexBuffer); assert(SUCCEEDED(result)); } }
CCubeMesh::CCubeMesh() { VERTEX_BUFFER_DESCRIPTOR bufferDesc = GenerateVertexBufferDescriptor(24, 36, VERTEX_BUFFER_HAS_POS | VERTEX_BUFFER_HAS_UV0); const auto& posVertexItem = bufferDesc.GetVertexItem(VERTEX_ITEM_ID_POSITION); const auto& uv0VertexItem = bufferDesc.GetVertexItem(VERTEX_ITEM_ID_UV0); m_primitiveType = PRIMITIVE_TRIANGLE_LIST; m_primitiveCount = 12; m_vertexBuffer = CGraphicDevice::GetInstance().CreateVertexBuffer(bufferDesc); uint8* vertices = reinterpret_cast<uint8*>(m_vertexBuffer->LockVertices()); for(unsigned int i = 0; i < 24; i++) { *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = CVector3(&s_positions[i * 3]); *reinterpret_cast<CVector2*>(vertices + uv0VertexItem->offset) = CVector2(&s_texCoords[i * 2]); vertices += bufferDesc.GetVertexSize(); } m_vertexBuffer->UnlockVertices(); uint16* indices = m_vertexBuffer->LockIndices(); memcpy(indices, s_indices, sizeof(s_indices)); m_vertexBuffer->UnlockIndices(); }
CGlEsVertexBuffer::CGlEsVertexBuffer(const VERTEX_BUFFER_DESCRIPTOR& descriptor, bool useVertexArray) : CVertexBuffer(descriptor) { //Create vertex buffer { glGenBuffers(1, &m_vertexBuffer); CHECKGLERROR(); glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); CHECKGLERROR(); glBufferData(GL_ARRAY_BUFFER, descriptor.GetVertexBufferSize(), NULL, GL_DYNAMIC_DRAW); CHECKGLERROR(); glBindBuffer(GL_ARRAY_BUFFER, 0); CHECKGLERROR(); } //Create index buffer { glGenBuffers(1, &m_indexBuffer); CHECKGLERROR(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBuffer); CHECKGLERROR(); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(uint16) * descriptor.indexCount, NULL, GL_DYNAMIC_DRAW); CHECKGLERROR(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); CHECKGLERROR(); } //Create vertex array if(useVertexArray) { glGenVertexArraysOES(1, &m_vertexArray); CHECKGLERROR(); glBindVertexArrayOES(m_vertexArray); CHECKGLERROR(); BindBuffers(); glBindVertexArrayOES(0); } }
CSphereMesh::CSphereMesh() { unsigned int vertexCount = (SPHERE_HEIGHT - 2) * SPHERE_WIDTH + 2; unsigned int triangleCount = (SPHERE_HEIGHT - 2) * (SPHERE_WIDTH - 1) * 2; unsigned int indexCount = triangleCount * 3; VERTEX_BUFFER_DESCRIPTOR bufferDesc = GenerateVertexBufferDescriptor(vertexCount, indexCount, VERTEX_BUFFER_HAS_POS | VERTEX_BUFFER_HAS_NRM); const auto& posVertexItem = bufferDesc.GetVertexItem(VERTEX_ITEM_ID_POSITION); const auto& nrmVertexItem = bufferDesc.GetVertexItem(VERTEX_ITEM_ID_NORMAL); m_primitiveType = PRIMITIVE_TRIANGLE_LIST; m_primitiveCount = triangleCount; m_boundingSphere.radius = 1; m_vertexBuffer = CGraphicDevice::GetInstance().CreateVertexBuffer(bufferDesc); uint8* vertices = reinterpret_cast<uint8*>(m_vertexBuffer->LockVertices()); { for(unsigned int j = 1; j < SPHERE_HEIGHT - 1; j++) { for(unsigned int i = 0; i < SPHERE_WIDTH; i++) { float theta = static_cast<float>(j) / static_cast<float>(SPHERE_HEIGHT - 1) * M_PI; float phi = static_cast<float>(i) / static_cast<float>(SPHERE_WIDTH - 1) * M_PI * 2; CVector3 position; position.x = sin(theta) * cos(phi); position.y = cos(theta); position.z = -sin(theta) * sin(phi); *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = position; *reinterpret_cast<CVector3*>(vertices + nrmVertexItem->offset) = position; vertices += bufferDesc.GetVertexSize(); } } { CVector3 position(0, 1, 0); *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = position; *reinterpret_cast<CVector3*>(vertices + nrmVertexItem->offset) = position; vertices += bufferDesc.GetVertexSize(); } { CVector3 position(0, -1, 0); *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = position; *reinterpret_cast<CVector3*>(vertices + nrmVertexItem->offset) = position; vertices += bufferDesc.GetVertexSize(); } } m_vertexBuffer->UnlockVertices(); uint16* indices = m_vertexBuffer->LockIndices(); for(unsigned int j = 0; j < SPHERE_HEIGHT - 3; j++) { for(unsigned int i = 0; i < SPHERE_WIDTH - 1; i++) { (*indices++) = (j + 0) * SPHERE_WIDTH + (i + 0); (*indices++) = (j + 1) * SPHERE_WIDTH + (i + 1); (*indices++) = (j + 0) * SPHERE_WIDTH + (i + 1); (*indices++) = (j + 0) * SPHERE_WIDTH + (i + 0); (*indices++) = (j + 1) * SPHERE_WIDTH + (i + 0); (*indices++) = (j + 1) * SPHERE_WIDTH + (i + 1); } } for(unsigned int i = 0; i < SPHERE_WIDTH - 1; i++) { (*indices++) = (SPHERE_HEIGHT - 2) * SPHERE_WIDTH; (*indices++) = i; (*indices++) = i + 1; (*indices++) = (SPHERE_HEIGHT - 2) * SPHERE_WIDTH + 1; (*indices++) = (SPHERE_HEIGHT - 3) * SPHERE_WIDTH + i + 1; (*indices++) = (SPHERE_HEIGHT - 3) * SPHERE_WIDTH + i; } m_vertexBuffer->UnlockIndices(); }
CDx11Effect::D3D11InputLayoutPtr CDx11UberEffect::CreateInputLayout(const VERTEX_BUFFER_DESCRIPTOR& descriptor) { typedef std::vector<D3D11_INPUT_ELEMENT_DESC> InputElementArray; InputElementArray inputElements; if(const auto& item = descriptor.GetVertexItem(VERTEX_ITEM_ID_POSITION)) { D3D11_INPUT_ELEMENT_DESC inputElement = {}; inputElement.SemanticName = "POSITION"; inputElement.SemanticIndex = 0; inputElement.Format = DXGI_FORMAT_R32G32B32_FLOAT; inputElement.AlignedByteOffset = item->offset; inputElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputElement.InstanceDataStepRate = 0; inputElements.push_back(inputElement); } if(const auto& item = descriptor.GetVertexItem(VERTEX_ITEM_ID_NORMAL)) { D3D11_INPUT_ELEMENT_DESC inputElement = {}; inputElement.SemanticName = "NORMAL"; inputElement.SemanticIndex = 0; inputElement.Format = DXGI_FORMAT_R32G32B32_FLOAT; inputElement.AlignedByteOffset = item->offset; inputElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputElement.InstanceDataStepRate = 0; inputElements.push_back(inputElement); } if(const auto& item = descriptor.GetVertexItem(VERTEX_ITEM_ID_UV0)) { D3D11_INPUT_ELEMENT_DESC inputElement = {}; inputElement.SemanticName = "TEXCOORD"; inputElement.SemanticIndex = 0; inputElement.Format = DXGI_FORMAT_R32G32_FLOAT; inputElement.AlignedByteOffset = item->offset; inputElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputElement.InstanceDataStepRate = 0; inputElements.push_back(inputElement); } if(const auto& item = descriptor.GetVertexItem(VERTEX_ITEM_ID_UV1)) { D3D11_INPUT_ELEMENT_DESC inputElement = {}; inputElement.SemanticName = "TEXCOORD"; inputElement.SemanticIndex = 1; inputElement.Format = DXGI_FORMAT_R32G32_FLOAT; inputElement.AlignedByteOffset = item->offset; inputElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputElement.InstanceDataStepRate = 0; inputElements.push_back(inputElement); } if(const auto& item = descriptor.GetVertexItem(VERTEX_ITEM_ID_COLOR)) { D3D11_INPUT_ELEMENT_DESC inputElement = {}; inputElement.SemanticName = "COLOR"; inputElement.SemanticIndex = 0; inputElement.Format = DXGI_FORMAT_R8G8B8A8_UNORM; inputElement.AlignedByteOffset = item->offset; inputElement.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA; inputElement.InstanceDataStepRate = 0; inputElements.push_back(inputElement); } D3D11InputLayoutPtr inputLayout; HRESULT result = m_device->CreateInputLayout(inputElements.data(), inputElements.size(), m_vertexShaderCode->GetBufferPointer(), m_vertexShaderCode->GetBufferSize(), &inputLayout); assert(SUCCEEDED(result)); return inputLayout; }
CConeMesh::CConeMesh() { unsigned int vertexCount = RADIAL_PARTS + 2; unsigned int triangleCount = RADIAL_PARTS * 2; unsigned int indexCount = triangleCount * 3; VERTEX_BUFFER_DESCRIPTOR bufferDesc = GenerateVertexBufferDescriptor(vertexCount, indexCount, VERTEX_BUFFER_HAS_POS | VERTEX_BUFFER_HAS_NRM); const auto& posVertexItem = bufferDesc.GetVertexItem(VERTEX_ITEM_ID_POSITION); const auto& nrmVertexItem = bufferDesc.GetVertexItem(VERTEX_ITEM_ID_NORMAL); m_primitiveType = PRIMITIVE_TRIANGLE_LIST; m_primitiveCount = triangleCount; m_boundingSphere.radius = sqrt(2.f); m_vertexBuffer = CGraphicDevice::GetInstance().CreateVertexBuffer(bufferDesc); uint8* vertices = reinterpret_cast<uint8*>(m_vertexBuffer->LockVertices()); { //Bottom point { *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = CVector3(0, 0, 0); *reinterpret_cast<CVector3*>(vertices + nrmVertexItem->offset) = CVector3(0, -1, 0); vertices += bufferDesc.GetVertexSize(); } //Top point { *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = CVector3(0, 1, 0); *reinterpret_cast<CVector3*>(vertices + nrmVertexItem->offset) = CVector3(0, 1, 0); vertices += bufferDesc.GetVertexSize(); } for(unsigned int i = 0; i < RADIAL_PARTS; i++) { float theta = static_cast<float>(i) / static_cast<float>(RADIAL_PARTS) * M_PI * 2; CVector3 position(cos(theta), 0, sin(theta)); CVector3 normal(cos(theta), 0, sin(theta)); *reinterpret_cast<CVector3*>(vertices + posVertexItem->offset) = position; *reinterpret_cast<CVector3*>(vertices + nrmVertexItem->offset) = normal; vertices += bufferDesc.GetVertexSize(); } } m_vertexBuffer->UnlockVertices(); uint16* indices = m_vertexBuffer->LockIndices(); //Actual Cone Part for(unsigned int i = 0; i < RADIAL_PARTS; i++) { unsigned int currentIndex = i; unsigned int nextIndex = (i + 1) % RADIAL_PARTS; (*indices++) = 0; (*indices++) = 2 + currentIndex; (*indices++) = 2 + nextIndex; } //Bottom Cap for(unsigned int i = 0; i < RADIAL_PARTS; i++) { unsigned int currentIndex = i; unsigned int nextIndex = (i + 1) % RADIAL_PARTS; (*indices++) = 2 + currentIndex; (*indices++) = 1; (*indices++) = 2 + nextIndex; } m_vertexBuffer->UnlockIndices(); }