1 /*
2 * Copyright (C) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "render_mesh.h"
17
18 namespace OHOS {
19 namespace Media {
RenderMesh(RenderContext * context,const std::vector<std::vector<float>> & meshData)20 RenderMesh::RenderMesh(RenderContext* context, const std::vector<std::vector<float>>& meshData)
21 {
22 context_ = context;
23 vboIds_ = new GLuint[2];
24 meshData_ = meshData;
25 }
26
~RenderMesh()27 RenderMesh::~RenderMesh()
28 {
29 if (vboIds_ != nullptr) {
30 const int32_t ARGS_TWO = 2;
31 glDeleteBuffers(ARGS_TWO, vboIds_);
32 delete[] vboIds_;
33 }
34 if (vaoId_) {
35 glDeleteVertexArrays(1, &vaoId_);
36 }
37 }
38
Bind(std::shared_ptr<RenderGeneralProgram> shader)39 void RenderMesh::Bind(std::shared_ptr<RenderGeneralProgram> shader)
40 {
41 const int32_t ARGS_TWO = 2;
42 if (vaoId_ == 0) {
43 glGenVertexArrays(1, &vaoId_);
44 glBindVertexArray(vaoId_);
45 glGenBuffers(ARGS_TWO, vboIds_);
46 glBindBuffer(GL_ARRAY_BUFFER, vboIds_[0]);
47 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * meshData_[0].size(), meshData_[0].data(), GL_DYNAMIC_DRAW);
48 int position = shader->GetAttributeLocation("aPosition");
49 glEnableVertexAttribArray(position);
50 glVertexAttribPointer(position, RENDERMESH_VERTEX_SIZE, GL_FLOAT, GL_FALSE,
51 sizeof(GLfloat) * RENDERMESH_VERTEX_SIZE, 0);
52 glBindBuffer(GL_ARRAY_BUFFER, 0);
53
54 glBindBuffer(GL_ARRAY_BUFFER, vboIds_[1]);
55 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * meshData_[1].size(), meshData_[1].data(), GL_DYNAMIC_DRAW);
56 int textureCoord = shader->GetAttributeLocation("aTextureCoord");
57 glEnableVertexAttribArray(textureCoord);
58 glVertexAttribPointer(textureCoord, RENDERMESH_TEXCOORD_SIZE, GL_FLOAT, GL_FALSE,
59 sizeof(GLfloat) * RENDERMESH_TEXCOORD_SIZE, 0);
60 glBindBuffer(GL_ARRAY_BUFFER, 0);
61 glBindVertexArray(0);
62 }
63 glBindVertexArray(vaoId_);
64 }
65 }
66 }