Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void NLVertexBufferObject::createQuads(const glm::vec2& size, float z, int count_quads, NLShader* shader, NLTexture* texture)
- {
- // If already present delete the buffers
- if ( m_vertices != NULL )
- {
- glDeleteBuffers(1, &m_VertexDataObj);
- glDeleteVertexArrays(1, &m_VertexArrayObj);
- delete [] m_vertices;
- }
- // Number of Vertices
- m_data_size = count_quads*4;
- // Create Array
- m_vertices = new NLVertexData[m_data_size];
- // Copy relevant values
- m_shader = shader;
- m_texture = texture;
- m_size = size;
- // Make Quads
- int i = 0;
- int x = 0;
- int y = 0;
- for ( x=0; x != count_quads; x++ )
- {
- m_vertices[i+0].x = 0.0f;
- m_vertices[i+0].y = 0.0f;
- m_vertices[i+0].z = z;
- m_vertices[i+0].s = 0.0f;
- m_vertices[i+0].t = 1.0f;
- // --
- m_vertices[i+1].x = 0.0f;
- m_vertices[i+1].y = m_size.y;
- m_vertices[i+1].z = z;
- m_vertices[i+1].s = 0.0f;
- m_vertices[i+1].t = 0.0f;
- // --
- m_vertices[i+2].x = m_size.x;
- m_vertices[i+2].y = 0.0f;
- m_vertices[i+2].z = z;
- m_vertices[i+2].s = 1.0f;
- m_vertices[i+2].t = 1.0f;
- // --
- m_vertices[i+3].x = m_size.x;
- m_vertices[i+3].y = m_size.y;
- m_vertices[i+3].z = z;
- m_vertices[i+3].s = 1.0f;
- m_vertices[i+3].t = 0.0f;
- i += 4;
- }
- // Bind Shader and Texture
- m_shader->attach();
- if ( m_texture != NULL ){
- m_texture->attach();
- }
- // Create Vertex Array
- glGenVertexArrays(1, &m_VertexArrayObj);
- glBindVertexArray(m_VertexArrayObj);
- // Create 1 VBO
- glGenBuffers(1, &m_VertexDataObj);
- glBindBuffer(GL_ARRAY_BUFFER, m_VertexDataObj);
- // Bind Vertex data
- glBufferData(GL_ARRAY_BUFFER, m_data_size*sizeof(NLVertexData), m_vertices, GL_STREAM_DRAW);
- // Unbind
- glBindVertexArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- // Export Texture ID
- if ( m_texture != NULL ){
- int NL_TextureID = m_shader->getUniformLocation("NL_TextureID");
- glUniform1i(NL_TextureID, 0);
- }
- if ( m_texture != NULL ){
- m_texture->detach();
- }
- m_shader->detach();
- }
- void NLVertexBufferObject::renderObject(const glm::vec3& position)
- {
- // Bind
- if ( m_texture != NULL ){
- m_texture->attach();
- }
- m_shader->attach();
- // Send position
- m_shader->sendPosition(glm::vec2(position.x, position.y));
- glBindVertexArray(m_VertexArrayObj);
- glBindBuffer(GL_ARRAY_BUFFER, m_VertexDataObj);
- // Enable Attribute Sets
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glEnableVertexAttribArray(2);
- // Define Attribute Sets
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4*sizeof(float), 0);
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 4*sizeof(float), (void*)(4*sizeof(float)));
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2*sizeof(float), (void*)(8*sizeof(float)));
- // Draw
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- // Disable Attribute Sets
- glDisableVertexAttribArray(0);
- glDisableVertexAttribArray(1);
- glDisableVertexAttribArray(2);
- // Unbind
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindVertexArray(0);
- m_shader->detach();
- if ( m_texture != NULL ){
- m_texture->detach();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement