Results 1 to 4 of 4

Thread: am I setting glVertexAttribPointer correctly?

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2017
    Posts
    11

    am I setting glVertexAttribPointer correctly?

    I'm trying to change my particle effects to use instancing, but it isn't working, the result is hard to describe, switching the usage of position and size in the vertex shaders produces no difference in outcome: some of the particles are correct, and some have the position and size reversed.

    Code :
    template<typename T>
    struct Point2
    {
    	T x,y;
    };
     
    struct RenderData
    {
    	Point2<GLfloat> position;
    	Point2<GLfloat> size;
    };
     
    GLuint vbo;
    std::vector<RenderData> renderData;

    Code :
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(RenderData) * renderData.size(), renderData.data(), GL_STREAM_DRAW);
     
    #define bind_float_array(name)\
    	glVertexAttribPointer(gl_##name, sizeof(renderData[0].name)/sizeof(GLfloat), GL_FLOAT, GL_FALSE,\
    		sizeof(renderData[0]) - sizeof(renderData[0].name),\
    		(void*) offsetof(RenderData, name));\
    	glEnableVertexAttribArray(gl_##name);\
    	glVertexAttribDivisor(gl_##name, 1);
     
    	bind_float_array(position);
    	bind_float_array(size);
     
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    Am I doing anything wrong there?

    And this is the vertex shader I'm using:
    Code :
    #version 400
     
    uniform mat4 projection_matrix;
    uniform mat4 modelview_matrix;
     
    in vec3 a_Vertex;
    in vec2 a_TexCoord0;
     
    in vec2 a_Position;
    in vec2 a_Size;
    in vec4 a_Color;
     
    out vec2 texCoord0;
     
    void main() 
    {
    	texCoord0 = a_TexCoord0;
    	vec3 vert = a_Vertex * vec3(a_Size, 1);
    	vert = vert + vec3(a_Position, 0);
    	vec4 pos = modelview_matrix * vec4(vert, 1.0);
    	gl_Position = projection_matrix * pos;
    }

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    3,100
    Quote Originally Posted by GeatMaster View Post
    Am I doing anything wrong there?
    Yes. Apart from over-using the preprocessor (in modern C++ code, almost anything beyond #include qualifies as over-use), the stride parameter is the offset in bytes between the start of one item and the start of the next, not the size of the "gap" between items. So it should just be sizeof(renderData[0]).

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2017
    Posts
    11
    Thank you!

  4. #4
    Senior Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    459
    Quote Originally Posted by GClements View Post
    Apart from over-using the preprocessor (in modern C++ code, almost anything beyond #include qualifies as over-use)
    Yes, plus the fact that, even in C, macros are often hard to debug.

Similar Threads

  1. glVertexAttribPointer
    By sajis997 in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 01-30-2015, 07:44 AM
  2. gl_error 1282 when setting glVertexAttribPointer
    By awright2009 in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 01-17-2011, 02:28 PM
  3. only using glVertexAttribPointer
    By FryderykC in forum OpenGL: GLSL
    Replies: 3
    Last Post: 02-15-2010, 03:56 PM
  4. setting up camera/viewing/perspective correctly
    By IMK123 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 11-27-2007, 09:16 AM
  5. glVertexAttribPointer
    By Jeff Russell in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 06-06-2007, 12:27 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