void VertexBuffer::BindSegmentBuffer(unsigned int & vbuffer, const Segment & s) const { if (use_vao) { glBindVertexArray(s.vbuffer); #ifdef _WIN32 if (bind_ibo) { assert(s.vformat <= VertexAttrib::LastAttrib); assert(s.object < objects[s.vformat].size()); const Object & ob = objects[s.vformat][s.object]; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ob.ibuffer); } #endif vbuffer = s.vbuffer; } else { assert(s.vformat <= VertexAttrib::LastAttrib); assert(s.object < objects[s.vformat].size()); const Object & ob = objects[s.vformat][s.object]; glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ob.ibuffer); glBindBuffer(GL_ARRAY_BUFFER, ob.vbuffer); SetVertexFormat(VertexFormat::Get(ob.vformat)); vbuffer = ob.vbuffer; } }
void Mesh::DrawGeometry() { auto device = Device::GetInstance(); auto meshShader = ShaderStock::GetInstance()->GetMeshShader(); if (this->vertices.size() > 0 && this->triangles.size() > 0) { //TODO : add support for more vertex and material formats if (this->changed) { std::vector<VertexFormatItem> vfitems; vfitems.push_back(VertexFormatItem(0, DataType::FLOAT3, "POSITION", 0, 0)); vfitems.push_back(VertexFormatItem(sizeof(float) * 3, DataType::FLOAT3, "NORMAL", 0, 0)); vfitems.push_back(VertexFormatItem(sizeof(float) * 6, DataType::FLOAT2, "TEXCOORD", 0, 0)); this->vertexFormat = device->CreateVertexFormat(vfitems, meshShader, "Mesh", 0); std::vector<MeshVertex> data(this->vertices.size()); for (size_t i = 0; i < this->vertices.size(); i++) { data[i].x = this->vertices[i].x; data[i].y = this->vertices[i].y; data[i].z = this->vertices[i].z; data[i].nx = this->normals[i].x; data[i].ny = this->normals[i].y; data[i].nz = this->normals[i].z; data[i].u = this->uv[0][i].x; data[i].v = this->uv[0][i].y; } if (this->isDynamic == false) { this->vertexBuffer = device->CreateStaticVertexBuffer(sizeof(MeshVertex) * data.size(), &data[0]).Get(); this->indexBuffer = device->CreateStaticIndexBuffer(sizeof(uint32_t) * triangles.size(), &triangles[0]).Get(); } this->changed = false; } device->SetVertexFormat(this->vertexFormat); device->SetVertexBuffer(this->vertexBuffer, sizeof(MeshVertex), 0); device->SetIndexBuffer(this->indexBuffer); if (this->material.texture) { meshShader->SetTexture("g_texture", this->material.texture); meshShader->SetBoolean("g_hasTexture", true); } else { meshShader->SetBoolean("g_hasTexture", false); } meshShader->SetValue("g_material", &this->material, sizeof(Material) - sizeof(Texture*)); meshShader->CommitChanges(); device->DrawIndexed(PrimitiveType::TRIANGLE_LIST, this->triangles.size(), 0, 0); } for (size_t i = 0; i < this->subMeshes.size(); i++) this->subMeshes[i]->DrawGeometry(); }
void VertexBuffer::UploadBuffers( Object & object, const std::vector<unsigned int> & index_buffer, const std::vector<float> & vertex_buffer) { const VertexFormat & vformat = VertexFormat::Get(object.vformat); const unsigned int icapacity = object.icount * sizeof(unsigned int); const unsigned int vcapacity = object.vcount * vformat.stride; if (object.varray) glBindVertexArray(object.varray); if (object.ibuffer) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, object.ibuffer); if (object.icapacity > icapacity) { glBufferData(GL_ELEMENT_ARRAY_BUFFER, object.icapacity, NULL, GL_STATIC_DRAW); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, icapacity, &index_buffer[0]); } else { glBufferData(GL_ELEMENT_ARRAY_BUFFER, icapacity, &index_buffer[0], GL_STATIC_DRAW); object.icapacity = icapacity; } } assert(object.vbuffer); glBindBuffer(GL_ARRAY_BUFFER, object.vbuffer); if (object.vcapacity > vcapacity) { glBufferData(GL_ARRAY_BUFFER, object.vcapacity, NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, vcapacity, &vertex_buffer[0]); } else { glBufferData(GL_ARRAY_BUFFER, vcapacity, &vertex_buffer[0], GL_STATIC_DRAW); object.vcapacity = vcapacity; } SetVertexFormat(vformat); // reset buffer state if (object.varray) glBindVertexArray(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); }
void Terrain::DrawGeometry() { auto device = Device::GetInstance(); if (this->changed) { GenerateBuffers(); this->changed = false; } device->SetVertexFormat(this->vertexFormat); device->SetVertexBuffer(this->vb.Get(), sizeof(TerrainVertex), 0); device->SetIndexBuffer(this->ib.Get()); device->GetRenderState()->GetCurrentShader()->CommitChanges(); device->DrawIndexed(PrimitiveType::TRIANGLE_LIST, (this->xResolution - 1) * (this->yResolution - 1) * 6, 0, 0); }