This repository has been archived on 2024-12-30. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2024CG-project-render/include/mesh_to_vao.h
github-classroom[bot] 686dcaf351
Initial commit
2024-11-29 09:50:03 +00:00

40 lines
1.3 KiB
C

#ifndef MESH_TO_VAO_H
#define MESH_TO_VAO_H
#include <Eigen/Core>
// Send a triangle mesh to the GPU using a vertex array object.
//
// Inputs:
// V #V by 3 list of 3D mesh vertex positions
// F #F by 3 list of triangle indices into V
// Outputs:
// VAO identifier of compiled vertex array object.
inline void mesh_to_vao(
const Eigen::Matrix< float,Eigen::Dynamic,3,Eigen::RowMajor> & V,
const Eigen::Matrix<GLuint,Eigen::Dynamic,3,Eigen::RowMajor> & F,
GLuint & VAO);
// Implementation
inline void mesh_to_vao(
const Eigen::Matrix< float,Eigen::Dynamic,3,Eigen::RowMajor> & V,
const Eigen::Matrix<GLuint,Eigen::Dynamic,3,Eigen::RowMajor> & F,
GLuint & VAO)
{
// Generate and attach buffers to vertex array
glGenVertexArrays(1, &VAO);
GLuint VBO, EBO;
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*V.size(), V.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*F.size(), F.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
#endif