manasij7479

vao&vbo

Jun 21st, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. //vao.h
  2. #ifndef MM_VAO
  3. #define MM_VAO
  4. #include <vector>
  5. #include<map>
  6. #include "vbo.h"
  7. #include "program.h"
  8. namespace mm
  9. {
  10.     class Vao
  11.     {
  12.     public:
  13.         Vao();
  14.         Vao(GLuint handle_):handle(handle_){};
  15.         Vbo& createAttribute
  16.         (
  17.             Program& prog,std::string name,GLint size, GLenum type,
  18.             GLboolean normalized=GL_FALSE, GLsizei stride=0, const GLvoid * pointer=0
  19.         );//The return value is to be used to populate the vbo
  20.        
  21.         GLuint getHandle();
  22.     private:
  23.         GLuint handle;
  24.         std::vector<Vbo> vbo_group;
  25.         std::map<std::string,unsigned int> attr_name_map;
  26.     };
  27. }
  28. #endif
  29. //vao.cpp
  30. #include "vao.h"
  31. namespace mm
  32. {
  33.     Vao::Vao()
  34.     {
  35.         glGenVertexArrays(1,&handle);
  36.     }
  37.     Vbo& Vao::createAttribute
  38.     (
  39.         Program& prog, std::string name, GLint size, GLenum type,
  40.         GLboolean normalized, GLsizei stride, const GLvoid* pointer
  41.     )
  42.     {
  43.         GLuint attr_location = vbo_group.size();
  44.         attr_name_map[name]=attr_location;
  45.         glBindVertexArray(handle);
  46.        
  47.         GLuint vbo_handle;
  48.         glGenBuffers(1,&vbo_handle);
  49.         auto vbo= new Vbo(vbo_handle);
  50.         vbo_group.push_back(*vbo);
  51.         vbo->setParent(handle);
  52.        
  53.         glVertexAttribPointer(attr_location,size,type,normalized,stride,pointer);
  54.         glEnableVertexAttribArray(attr_location);
  55.         glBindAttribLocation(prog.getHandle(),attr_location,name.c_str());
  56.        
  57.         glBindVertexArray(0);
  58.         return *vbo;
  59.     }
  60.     GLuint Vao::getHandle()
  61.     {
  62.         return handle;
  63.     }
  64.  
  65. }
  66. //vbo.h
  67. #ifndef MM_VBO
  68. #define MM_VBO
  69. #include <GL/glew.h>
  70. namespace mm
  71. {
  72.     class Vbo
  73.     {
  74.     public:
  75.        
  76.         Vbo();
  77.         Vbo(GLuint handle_):handle(handle_){};
  78.         void bufferData(GLenum target, GLsizeiptr size, const GLvoid * data, GLenum usage);
  79. //      void bufferSubData(/**/);///TODO
  80.        
  81.         GLuint getHandle();
  82.         void setParent(GLuint handle_);//DangerousDontEverUse
  83.     private:
  84.         GLuint handle;
  85.         GLuint parent_vao;
  86.     };
  87. }
  88. #endif
  89. //vbo.cpp
  90. #include "vbo.h"
  91. namespace mm
  92. {
  93.     void Vbo::setParent(GLuint handle_)
  94.     {
  95.         parent_vao = handle_;
  96.     }
  97.     GLuint Vbo::getHandle()
  98.     {
  99.         return handle;
  100.     }
  101.     void Vbo::bufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage)
  102.     {
  103.         glBindVertexArray(parent_vao);
  104.        
  105.         glBindBuffer(target,handle);
  106.         glBufferData(target,size,data,usage);
  107.        
  108. //      glBindBuffer(target,0);
  109.         glBindVertexArray(0);
  110.     }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment