| 732 | } |
| 733 | |
| 734 | void Mesh::init() { |
| 735 | if (isInitialized()) { |
| 736 | return; |
| 737 | } |
| 738 | |
| 739 | // NOTE: OpenGL 2.1 does not support vertex array functions |
| 740 | #ifdef VERTEX_ARRAYS_ENABLED |
| 741 | GL_CHECK_ERR(glGenVertexArrays(1, &vao)); |
| 742 | GL_CHECK_ERR(glBindVertexArray(vao)); |
| 743 | #endif |
| 744 | |
| 745 | // data buffers |
| 746 | numVertices = 0; |
| 747 | GL_CHECK_ERR(glGenBuffers((GLsizei)BufferType::Max, vbo)); |
| 748 | for (unsigned int c = 0; c < (unsigned int)BufferType::Max; ++c) { |
| 749 | if (data[c].size()) { |
| 750 | const auto& find = ElementsPerVBO.find((BufferType)c); |
| 751 | assert(find != ElementsPerVBO.end()); |
| 752 | numVertices = std::max(numVertices, (unsigned int)data[c].size() / find->second); |
| 753 | GL_CHECK_ERR(glBindBuffer(GL_ARRAY_BUFFER, vbo[c])); |
| 754 | GL_CHECK_ERR(glBufferData(GL_ARRAY_BUFFER, data[c].size() * sizeof(float), data[c].data(), GL_STATIC_DRAW)); |
| 755 | #ifdef VERTEX_ARRAYS_ENABLED |
| 756 | GL_CHECK_ERR(glVertexAttribPointer(c, find->second, GL_FLOAT, GL_FALSE, 0, nullptr)); |
| 757 | GL_CHECK_ERR(glEnableVertexAttribArray(c)); |
| 758 | #endif |
| 759 | } |
| 760 | } |
| 761 | #ifndef VERTEX_ARRAYS_ENABLED |
| 762 | GL_CHECK_ERR(glBindBuffer(GL_ARRAY_BUFFER, 0)); |
| 763 | #endif |
| 764 | |
| 765 | printlog("initialized mesh with %llu vertices", numVertices); |
| 766 | } |
| 767 | |
| 768 | void Mesh::destroy() { |
| 769 | if (vao) { |
no test coverage detected