Advertisement
Guest User

Untitled

a guest
Feb 8th, 2011
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1.  
  2. void NLVertexBufferObject::createQuads(const glm::vec2& size, float z, int count_quads, NLShader* shader, NLTexture* texture)
  3. {
  4. // If already present delete the buffers
  5. if ( m_vertices != NULL )
  6. {
  7. glDeleteBuffers(1, &m_VertexDataObj);
  8. glDeleteVertexArrays(1, &m_VertexArrayObj);
  9. delete [] m_vertices;
  10. }
  11.  
  12. // Number of Vertices
  13. m_data_size = count_quads*4;
  14.  
  15. // Create Array
  16. m_vertices = new NLVertexData[m_data_size];
  17.  
  18. // Copy relevant values
  19. m_shader = shader;
  20. m_texture = texture;
  21. m_size = size;
  22.  
  23. // Make Quads
  24. int i = 0;
  25. int x = 0;
  26. int y = 0;
  27. for ( x=0; x != count_quads; x++ )
  28. {
  29.  
  30. m_vertices[i+0].x = 0.0f;
  31. m_vertices[i+0].y = 0.0f;
  32. m_vertices[i+0].z = z;
  33.  
  34. m_vertices[i+0].s = 0.0f;
  35. m_vertices[i+0].t = 1.0f;
  36.  
  37. // --
  38.  
  39. m_vertices[i+1].x = 0.0f;
  40. m_vertices[i+1].y = m_size.y;
  41. m_vertices[i+1].z = z;
  42. m_vertices[i+1].s = 0.0f;
  43. m_vertices[i+1].t = 0.0f;
  44.  
  45. // --
  46.  
  47. m_vertices[i+2].x = m_size.x;
  48. m_vertices[i+2].y = 0.0f;
  49. m_vertices[i+2].z = z;
  50. m_vertices[i+2].s = 1.0f;
  51. m_vertices[i+2].t = 1.0f;
  52.  
  53. // --
  54.  
  55. m_vertices[i+3].x = m_size.x;
  56. m_vertices[i+3].y = m_size.y;
  57. m_vertices[i+3].z = z;
  58. m_vertices[i+3].s = 1.0f;
  59. m_vertices[i+3].t = 0.0f;
  60.  
  61. i += 4;
  62. }
  63.  
  64. // Bind Shader and Texture
  65. m_shader->attach();
  66.  
  67. if ( m_texture != NULL ){
  68. m_texture->attach();
  69. }
  70.  
  71. // Create Vertex Array
  72. glGenVertexArrays(1, &m_VertexArrayObj);
  73. glBindVertexArray(m_VertexArrayObj);
  74.  
  75. // Create 1 VBO
  76. glGenBuffers(1, &m_VertexDataObj);
  77. glBindBuffer(GL_ARRAY_BUFFER, m_VertexDataObj);
  78.  
  79. // Bind Vertex data
  80. glBufferData(GL_ARRAY_BUFFER, m_data_size*sizeof(NLVertexData), m_vertices, GL_STREAM_DRAW);
  81.  
  82. // Unbind
  83. glBindVertexArray(0);
  84. glBindBuffer(GL_ARRAY_BUFFER, 0);
  85.  
  86. // Export Texture ID
  87. if ( m_texture != NULL ){
  88. int NL_TextureID = m_shader->getUniformLocation("NL_TextureID");
  89. glUniform1i(NL_TextureID, 0);
  90. }
  91.  
  92. if ( m_texture != NULL ){
  93. m_texture->detach();
  94. }
  95. m_shader->detach();
  96.  
  97. }
  98.  
  99. void NLVertexBufferObject::renderObject(const glm::vec3& position)
  100. {
  101. // Bind
  102. if ( m_texture != NULL ){
  103. m_texture->attach();
  104. }
  105. m_shader->attach();
  106.  
  107. // Send position
  108. m_shader->sendPosition(glm::vec2(position.x, position.y));
  109.  
  110. glBindVertexArray(m_VertexArrayObj);
  111. glBindBuffer(GL_ARRAY_BUFFER, m_VertexDataObj);
  112.  
  113. // Enable Attribute Sets
  114. glEnableVertexAttribArray(0);
  115. glEnableVertexAttribArray(1);
  116. glEnableVertexAttribArray(2);
  117.  
  118. // Define Attribute Sets
  119. glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
  120. glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(4*sizeof(float)));
  121. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)(8*sizeof(float)));
  122.  
  123. // Draw
  124. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  125.  
  126. // Disable Attribute Sets
  127. glDisableVertexAttribArray(0);
  128. glDisableVertexAttribArray(1);
  129. glDisableVertexAttribArray(2);
  130.  
  131. // Unbind
  132. glBindBuffer(GL_ARRAY_BUFFER, 0);
  133. glBindVertexArray(0);
  134.  
  135. m_shader->detach();
  136. if ( m_texture != NULL ){
  137. m_texture->detach();
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement