OpenGL笔记十二之实现三角形在屏幕横向上往复运动的动画
—— 2024-07-14 晚上
bilibili赵新政老师的教程看后笔记
code review!
文章目录
- OpenGL笔记十二之实现三角形在屏幕横向上往复运动的动画
- 1.运行
- 2.vs
- 3.fs
- 4.main.cpp的关键部分
1.运行
2.vs
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
uniform float time;
uniform float speed;
out vec3 color;
void main()
{
float dx = 0.3;
float offsetX = sin(time * speed)*dx;
gl_Position = vec4(aPos.x + offsetX, aPos.y, aPos.z, 1.0);
color = aColor;
}
3.fs
#version 330 core
out vec4 FragColor;
in vec3 color;
void main()
{
FragColor = vec4(color, 1.0f);
}
4.main.cpp的关键部分
代码
void render() {
//执行opengl画布清理操作
GL_CALL(glClear(GL_COLOR_BUFFER_BIT));
//1 绑定当前的program
shader->begin();
shader->setFloat("time", glfwGetTime());
shader->setFloat("speed", 5.0);
//2 绑定当前的vao
GL_CALL(glBindVertexArray(vao));
//3 发出绘制指令
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
shader->end();
}
int main() {
if (!app->init(800, 600)) {
return -1;
}
app->setResizeCallback(OnResize);
app->setKeyBoardCallback(OnKey);
//设置opengl视口以及清理颜色
GL_CALL(glViewport(0, 0, 800, 600));
GL_CALL(glClearColor(0.2f, 0.3f, 0.3f, 1.0f));
prepareShader();
prepareVAO();
while (app->update()) {
render();
}
app->destroy();
return 0;
}