【发布时间】:2019-08-04 15:28:21
【问题描述】:
我正在编写一个 OpenGL 库,但偶然发现了一个关于多个顶点类型和顶点着色器的问题。我是否需要为每个处理其属性的新顶点类型编写一个新的顶点/片段着色器?还是我需要编写一个处理所有可能属性的顶点/片段着色器?
这些是我用于顶点类型的一些基本类“模式”。
struct simple_vertex
{
glm::vec3 position;
simple_vertex(glm::vec3 pos) {
position = pos;
}
simple_vertex() {
position = glm::vec3(0, 0, 0);
}
static void enable_attributes() {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(simple_vertex),
(const GLvoid *) offsetof(simple_vertex, position));
glEnableVertexAttribArray(0);
}
glm::vec3 get_position() const {
return position;
}
};
struct colored_vertex {//vertex that holds position and color data
glm::vec3 position;
glm::vec3 color;
colored_vertex(glm::vec3 pos, glm::vec3 c) {
color = c;
position = pos;
}
colored_vertex() {
color = glm::vec3(0, 0, 0);
position = glm::vec3(0, 0, 0);
}
static void enable_attributes() {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(colored_vertex),
(const GLvoid *) offsetof(colored_vertex, position));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(colored_vertex),
(const GLvoid *) offsetof(colored_vertex, color));
glEnableVertexAttribArray(1);
}
glm::vec3 get_position() const {
return position;
}
};
struct textured_vertex {//vertex that holds position and texture coordinates
glm::vec3 position;
glm::vec2 texture_coords;
textured_vertex(glm::vec3 pos, glm::vec2 text_coords) {
texture_coords = text_coords;
position = pos;
}
textured_vertex() {
texture_coords = glm::vec2(0, 0);
position = glm::vec3(0, 0, 0);
}
static void enable_attributes() {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(textured_vertex),
(const GLvoid *) offsetof(textured_vertex, position));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(textured_vertex),
(const GLvoid *) offsetof(textured_vertex, texture_coords));
glEnableVertexAttribArray(1);
}
glm::vec3 get_position() const {
return position;
}
};
struct normal_textured_vertex {//vertex that holds position normal and texture coordinates
glm::vec3 position;
glm::vec2 texture_coords;
glm::vec3 normal;
normal_textured_vertex(glm::vec3 pos, glm::vec2 text_coords, glm::vec3 n) {
texture_coords = text_coords;
position = pos;
normal = n;
}
normal_textured_vertex() {
texture_coords = glm::vec2(0, 0);
position = glm::vec3(0, 0, 0);
normal = glm::vec3(0, 0, 0);
}
static void enable_attributes() {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(normal_textured_vertex),
(const GLvoid *) offsetof(normal_textured_vertex, position));
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(normal_textured_vertex),
(const GLvoid *) offsetof(normal_textured_vertex, texture_coords));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(normal_textured_vertex),
(const GLvoid *) offsetof(normal_textured_vertex, normal));
glEnableVertexAttribArray(2);
}
glm::vec3 get_position() const {
return position;
}
};
【问题讨论】: