2015-05-18 20 views
5

Örgü objeleri farklı köşe düzenleri ile büyük çaba sarf etmek için iyi bir yol arıyorum (ör. Her köşe yerleşimi için bir oluşturucu sınıfı tanımlama). Aşağıda farklı köşe biçimlerinin bazı örneklerini görebilirsiniz.Opengl - farklı köşe formatları oluşturma

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 
}; 

Arka plan, farklı nesneler oluşturmak istiyorum. Bazıları, doku koordinatlarına veya normlarına sahip olmayan ilkellerdir (örneğin, uçaklar, küreler). Öte yandan, normalden daha karmaşık nesneler, doku koordinatları vb. Gibi daha karmaşık nesneler oluşturmak istiyorum. Birden fazla işleyici sınıfını programlamaktan kaçınmak ve bunun yerine tek bir işleyici sınıfı kullanmak için akıllı bir yol veya tasarım var mı? Bunun, gölgelendiricileri de etkileyeceğinin farkındayım.

cevap

3

Yapabilecekleriniz, vertex yapılarınızın her birine EnableVertexAttribArray veya benzeri bir şey olarak isimlendirilen statik bir yöntem vermektir. Bu statik yöntemde, GL_ARRAY_BUFFER numaralı köşe düzenini, doğru bir dizi arabelleğinin bağlı olduğunu varsayarsınız.

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); 
    } 
}; 

Sonra köşe yapı dayalı bir tepe tamponunun bir şablon sınıfını yapabilirsiniz. Örneğin,

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; 
} 
+0

oh bu iyi bir fikir. teşekkür ederim! – bobby