OpenGL-渲染不同的顶点格式
Opengl - rendering different vertex formats
我正在寻找一种很好的方法来渲染具有不同顶点布局的网格对象,例如,为每个顶点布局定义一个渲染器类)。您可以在下面看到一些不同顶点格式的示例。
enum EVertexFormat
{
VERTEX_FORMAT_UNDEFINED = -1,
VERTEX_FORMAT_P1 = 0,
VERTEX_FORMAT_P1N1,
VERTEX_FORMAT_P1N1UV,
VERTEX_FORMAT_P1N1C1,
VERTEX_FORMAT_P1N1UVC1,
};
// the simplest possible vertex -- position only
struct SVertexP1
{
math::Vector3D m_position; // position of the vertex
};
struct SVertexP1N1
{
math::Vector3D m_position; // position of the vertex
math::Vector3D m_normal; // normal of the vertex
};
// a typical vertex format with position, vertex normal
// and one set of texture coordinates
struct SVertexP1N1UV
{
math::Vector3D m_position; // position of the vertex
math::Vector3D m_normal; // normal of the vertex
math::Vector2D m_uv; // (u,v) texture coordinate
};
struct SVertexP1N1C1
{
math::Vector3D m_position; // position of the vertex
math::Vector3D m_normal; // normal of the vertex
uint32_t m_color_u32; // color of the vertex
};
struct SVertexP1N1UVC1
{
math::Vector3D m_position; // position of the vertex
math::Vector3D m_normal; // normal of the vertex
math::Vector2D m_uv; // (u,v) texture coordinate
uint32_t m_color_u32; // color of the vertex
};
背景是,我想渲染不同的对象。其中一些是没有纹理坐标或正常的原始物(例如飞机,球体)。另一方面,我想渲染具有正常,纹理坐标等的更复杂的对象。是否有智能方法或设计以避免编程多个渲染器类,而是使用单个渲染器类?我知道,这也会影响着着色器。
您可以做的是给出每个顶点structs静态方法,也许称为EnableVertexAttribArray
或类似的东西。在这种静态方法中,您在GL_ARRAY_BUFFER
中设置了顶点布局,假设正确的数组缓冲区已绑定。
struct SVertexP1N1
{
math::Vector3D m_position; // position of the vertex
math::Vector3D m_normal; // normal of the vertex
static void EnableVertexAttribArray()
{
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SVertexP1N1), (const GLvoid*)offsetof(SVertexP1N1, m_position));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(SVertexP1N1), (const GLvoid*)offsetof(SVertexP1N1, m_normal));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
};
然后,您可以根据顶点结构进行顶点缓冲区的模板类。例如,
template <class VertexType> class vertex_buffer
{
public:
typedef VertexType vertex_type;
vertex_buffer()
{
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ibo);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
vertex_type::EnableVertexAttribArray(); // <--------
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
~vertex_buffer()
{
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
glDeleteBuffers(1, &m_ibo);
}
// ...
void draw()
{
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glDrawElements(GL_TRIANGLES, m_indices.size(), GL_UNSIGNED_INT, NULL);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
private:
GLuint m_vao;
GLuint m_vbo;
GLuint m_ibo;
std::vector<vertex_type> m_vertices;
std::vector<GLuint> m_indices;
}
相关文章:
- 如何循环打印顶点结构
- 如何在openssl-ecc中获取十六进制格式的私钥
- 将"打开的CV图像"中的"颜色"转换为整数格式
- TDateTime格式在C++Builder中不会更改
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 检查不带转换的扫描格式
- 当我尝试使用 sstream 和分面将 Boost Time_duration转换为字符串时,我没有得到所需的格式
- 是否可以从格式字符串中检索"width"
- clang格式:宏的缩进
- clang格式:禁用排序包含
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 将RGB图像保存为PPM格式
- 询问在设计我的手臂模拟器功能表示格式1
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 如何将strftime中的格式错误作为异常捕获
- 将CHW格式的浮点向量转换为cv::Mat
- Vulkan 中的动态顶点缓冲区格式设置
- OpenGL-渲染不同的顶点格式
- 为什么要为一个属性指定不同的顶点格式