Results 1 to 2 of 2

Thread: Direct State Access Instance attribute buffer specification

  1. #1
    Member Newbie
    Join Date
    Oct 2016
    Posts
    46

    Direct State Access Instance attribute buffer specification

    I have a model i want to render instances of. Every instance should have a different position and this should be provided from one continuous buffer of positions that will be read from the shader as a vertex attribute and that will advance for every instance.

    This means i have the usual vertex data buffer which is indexed by the element buffer and provides vertex attributes 0, 1 and 2 with data. But now i also have another buffer of positions (vec4s in my case) which should provide vertex attribute 3 with data. How can i do this using VAOs and direct state access?

    This was my attempt
    Code :
     
            glCreateVertexArrays(1, &instancingVAO);
    	glCreateBuffers(1, &positionVBO); //for instance positions
    	glCreateBuffers(1, &meshVBO); //for mesh data
    	glCreateBuffers(1, &meshIBO); //mesh data indices
     
    	glEnableVertexArrayAttrib(instancingVAO, 0);
    	glEnableVertexArrayAttrib(instancingVAO, 1);
    	glEnableVertexArrayAttrib(instancingVAO, 2);
    	glEnableVertexArrayAttrib(instancingVAO, 3);
     
    	glVertexArrayAttribBinding(instancingVAO, 0, 0);
    	glVertexArrayAttribBinding(instancingVAO, 1, 0);
    	glVertexArrayAttribBinding(instancingVAO, 2, 0);
     
    	glVertexArrayAttribBinding(instancingVAO, 3, 1);// i expected this binding to do the trick..
     
     
            ///Mesh Data
    	glVertexArrayVertexBuffer(instancingVAO, 0, meshVBO, 0, sizeof(OpenGL::glVertex)); //bound to binding 0
    	glVertexArrayElementBuffer(instancingVAO, meshIBO);
     
            //format specification
    	glVertexArrayAttribFormat(instancingVAO, 0, 3, GL_FLOAT, GL_FALSE, offsetof(OpenGL::glVertex, position));
    	glVertexArrayAttribFormat(instancingVAO, 1, 3, GL_FLOAT, GL_FALSE, offsetof(OpenGL::glVertex, normal));
    	glVertexArrayAttribFormat(instancingVAO, 2, 2, GL_FLOAT, GL_FALSE, offsetof(OpenGL::glVertex, uv));
     
            //static data upload
    	glNamedBufferStorage(meshVBO, sizeof(OpenGL::glVertex)*allMeshVertices.size(), &allMeshVertices[0], 0);
    	glNamedBufferStorage(meshIBO, sizeof(unsigned int)*allMeshIndices.size(), &allMeshIndices[0], 0);
     
     
    	///dynamic & per instance attributes
    	///position data
    	glVertexArrayVertexBuffer(instancingVAO, 1, positionVBO, 0, sizeof(glm::vec4)); //bound to binding 1
     
    	//format specification for the instance position
    	glVertexArrayAttribFormat(instancingVAO, 3, 4, GL_FLOAT, GL_FALSE, 0);
     
            //glVertexAttribDivisor does not have DSA, so:
    	glBindVertexArray(instancingVAO);
    	glVertexAttribDivisor(3, 1);
    	glBindVertexArray(0);
     
            //static data upload (only for debugging, later this buffer should be updated every frame)
    	glNamedBufferStorage(positionVBO, sizeof(glm::vec4)*positionBuffer.size(), &positionBuffer[0], 0);

  2. #2
    Member Newbie
    Join Date
    Oct 2016
    Posts
    46
    Wow, okay....

    right after posting this (which was after 2 hours of debugging) i found the solution.

    instead of

    Code :
            glBindVertexArray(instancingVAO);
    	glVertexAttribDivisor(3, 1);
    	glBindVertexArray(0);

    to say that this attribute should only change per instance, i had to say

    Code :
    glVertexArrayBindingDivisor(instancingVAO, 1, 1);

    to say that all attributes bound to this binding point (1) should change per instance (I guess).

Similar Threads

  1. Instancing with Direct State Access... glVertexArrayBindingDivisor?
    By Stradigos in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-10-2015, 11:22 PM
  2. VBO Instancing - Attribute per Vertex per Instance
    By error17 in forum OpenGL: Advanced Coding
    Replies: 6
    Last Post: 03-02-2014, 01:26 PM
  3. Direct State Access
    By ViolentHamster in forum OpenGL: General
    Replies: 43
    Last Post: 05-08-2012, 06:53 AM
  4. Direct State Access
    By resle in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 06-16-2010, 12:02 PM
  5. direct access to buffer objects from shader
    By sunefred in forum OpenGL: GLSL
    Replies: 4
    Last Post: 02-22-2010, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean