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 "rectangle.h"
17 #include "log.h"
18
19 namespace {
20 float g_vertices[] = {
21 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.5f, 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.0f,
22 -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.5f,
23 };
24
25 unsigned int g_indices[] = {0, 1, 3, 1, 2, 3};
26
27 char g_shadervsTransform[] = "#version 300 es\n"
28 "layout(location = 0) in vec3 aPos;\n"
29 "layout(location = 1) in vec3 aColor;\n"
30 "uniform mat4 transform;\n"
31 "out vec3 ourColor;\n"
32 "void main()\n"
33 "{\n"
34 "gl_Position = transform * vec4(aPos, 1.0);\n"
35 "ourColor = aColor;\n"
36 "}\n";
37
38 char g_shaderfsTransform[] = "#version 300 es\n"
39 "precision mediump float;\n"
40 "out vec4 fragColor;\n"
41 "in vec3 ourColor;\n"
42 "void main()\n"
43 "{\n"
44 "fragColor =vec4(ourColor, 1.0);\n"
45 "}\n";
46 } // namespace
47
Init()48 int32_t Rectangle::Init()
49 {
50 ourShader = new Shader(false, g_shadervsTransform, g_shaderfsTransform);
51 return 0;
52 }
53
54 // 绘图转换
Update()55 void Rectangle::Update()
56 {
57 glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
58 glClear(GL_COLOR_BUFFER_BIT);
59
60 ourShader->use();
61
62 glGenVertexArrays(1, &vao);
63 glGenBuffers(1, &vbo);
64 glGenBuffers(1, &ebo);
65
66 glBindVertexArray(vao);
67 glBindBuffer(GL_ARRAY_BUFFER, vbo);
68 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertices), g_vertices, GL_STATIC_DRAW);
69
70 glVertexAttribPointer(0, kThree, GL_FLOAT, GL_FALSE, kEight * sizeof(float), (void *)0);
71 glEnableVertexAttribArray(0);
72
73 glVertexAttribPointer(1, kThree, GL_FLOAT, GL_FALSE, kEight * sizeof(float), (void *)(kThree * sizeof(float)));
74 glEnableVertexAttribArray(1);
75
76 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
77 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(g_indices), g_indices, GL_STATIC_DRAW);
78
79 // 先旋转再位移
80 Matrix4x4 trans = Matrix4x4::Identity();
81 trans.MakeTranslation(Vector3(0.5f, -0.5f, 0.0f));
82
83 Matrix4x4 rotate = Matrix4x4::Identity();
84 rotate.MakeRoate(stride, Vector3(0.0f, 0.0f, 1.0f));
85
86 // 先旋转再位移
87 Matrix4x4 transform = Matrix4x4::Identity();
88 Matrix4x4::Multiply(trans, rotate, transform);
89
90 ourShader->use();
91 ourShader->SetMatrix4x4("transform", transform);
92
93 glBindVertexArray(vao);
94 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
95 glDrawElements(GL_TRIANGLES, kSix, GL_UNSIGNED_INT, 0);
96
97 // 画第二个图
98 trans = Matrix4x4::Identity();
99 trans.MakeTranslation(Vector3(-0.5f, 0.5f, 0.0f));
100 float scaleValue = sin(stride);
101 Matrix4x4 scale = Matrix4x4::Identity();
102 scale.MakeScale(Vector3(scaleValue, scaleValue, scaleValue));
103
104 transform = Matrix4x4::Identity();
105 Matrix4x4::Multiply(trans, scale, transform);
106
107 ourShader->SetMatrix4x4("transform", transform);
108 glDrawElements(GL_TRIANGLES, kSix, GL_UNSIGNED_INT, 0);
109 }
110
Quit(void)111 int32_t Rectangle::Quit(void)
112 {
113 glDeleteVertexArrays(1, &vao);
114 glDeleteBuffers(1, &vbo);
115 glDeleteBuffers(1, &ebo);
116 glDeleteProgram(ourShader->ID);
117
118 delete ourShader;
119 ourShader = nullptr;
120
121 LOGE("MagicCube Quit success.");
122 return 0;
123 }
124
Animate()125 void Rectangle::Animate() { stride = stride + 0.05; } // K0.05