加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Mesh.cpp 1.80 KB
一键复制 编辑 原始数据 按行查看 历史
edwardewang 提交于 2023-03-25 22:25 . finish model
//
// Created by edward on 23-3-25.
//
#include "Mesh.h"
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices, std::vector<Texture> textures)
: vertices_(std::move(vertices))
, indices_(std::move(indices))
, textures_(std::move(textures))
{
setup();
}
void Mesh::setup() {
glGenVertexArrays(1, &VAO_);
glGenBuffers(1, &VBO_);
glGenBuffers(1, &EBO_);
glBindVertexArray(VAO_);
glBindBuffer(GL_ARRAY_BUFFER, VBO_);
glBufferData(GL_ARRAY_BUFFER, vertices_.size() * sizeof(Vertex), vertices_.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, position));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, color));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, normal));
glEnableVertexAttribArray(2);
glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, texCoords));
glEnableVertexAttribArray(3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices_.size() * sizeof(unsigned int), indices_.data(), GL_STATIC_DRAW);/// copy data to EBO
/// set wireframe mode
// glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
/// unbind VBO -> is allowed, but not necessary
glBindBuffer(GL_ARRAY_BUFFER, 0);
/// unbind VAO
glBindVertexArray(0);
}
void Mesh::draw(Shader *shader) {
for (unsigned i = 0; i < textures_.size(); ++i) {
textures_[i].use(shader, i);
}
glBindVertexArray(VAO_);
glDrawElements(GL_TRIANGLES, indices_.size(), GL_UNSIGNED_INT, 0);
}
/*
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
*/
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化