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_default_data.h"
17 #include "render_mesh.h"
18 #include "graphic/gl_utils.h"
19
20 namespace OHOS {
21 namespace Media {
22 namespace Effect {
RenderMesh(const std::vector<std::vector<float>> & meshData)23 RenderMesh::RenderMesh(const std::vector<std::vector<float>> &meshData)
24 {
25 vboIds_ = new GLuint[2];
26 meshData_ = meshData;
27 }
28
~RenderMesh()29 RenderMesh::~RenderMesh()
30 {
31 if (vboIds_ != nullptr) {
32 glDeleteBuffers(RENDERMESH_BUFFER_SIZE, vboIds_);
33 delete vboIds_;
34 vboIds_ = nullptr;
35 }
36 if (vaoId_) {
37 glDeleteVertexArrays(1, &vaoId_);
38 }
39 }
40
Bind(RenderGeneralProgram * shader)41 void RenderMesh::Bind(RenderGeneralProgram *shader)
42 {
43 if (shader == nullptr) {
44 return;
45 }
46 if (vaoId_ == 0) {
47 glGenVertexArrays(1, &vaoId_);
48 glBindVertexArray(vaoId_);
49 glGenBuffers(RENDERMESH_BUFFER_SIZE, vboIds_);
50 glBindBuffer(GL_ARRAY_BUFFER, vboIds_[0]);
51 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * meshData_[0].size(), meshData_[0].data(), GL_DYNAMIC_DRAW);
52 int position = shader->GetAttributeLocation("aPosition");
53 glEnableVertexAttribArray(position);
54 glVertexAttribPointer(position, RENDERMESH_VERTEX_SIZE, GL_FLOAT, GL_FALSE,
55 sizeof(GLfloat) * RENDERMESH_VERTEX_SIZE, 0);
56 glBindBuffer(GL_ARRAY_BUFFER, 0);
57
58 glBindBuffer(GL_ARRAY_BUFFER, vboIds_[1]);
59 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * meshData_[1].size(), meshData_[1].data(), GL_DYNAMIC_DRAW);
60 int textureCoord = shader->GetAttributeLocation("aTextureCoord");
61 glEnableVertexAttribArray(textureCoord);
62 glVertexAttribPointer(textureCoord, RENDERMESH_TEXCOORD_SIZE, GL_FLOAT, GL_FALSE,
63 sizeof(GLfloat) * RENDERMESH_TEXCOORD_SIZE, 0);
64 glBindBuffer(GL_ARRAY_BUFFER, 0);
65 glBindVertexArray(0);
66 GLUtils::CheckError(__FILE_NAME__, __LINE__);
67 }
68 glBindVertexArray(vaoId_);
69 }
70 } // namespace Effect
71 } // namespace Media
72 } // namespace OHOS