1

Follow examples online, here is the code in the vertex shader:

The attributes are defined as follows:

// Option 1:
layout (location = 0) in vec3 colorvec3;
layout (location = 1) in mat4x2 xyInBaseImageMat4x2;

out vec2 xyInElementTextureImage;
out vec3 elementTintColorVec3;

In the vertex shader, they are used:

// Option 1
vec2 xyInBaseImageVec2 = xyInBaseImageMat4x2[gl_VertexID];
gl_Position = vec4(xyInBaseImageVec2, 0.0, 1.0);
elementTintColorVec3 = colorvec3;

But the result is weird, totally random. Sometimes it is black, sometimes it is random shape.

But if I change to use:

// Option 2:
layout (location = 0) in vec3 colorvec3;
layout (location = 1) in vec2 xyInBaseImageVec2_p0;
layout (location = 2) in vec2 xyInBaseImageVec2_p1;
layout (location = 3) in vec2 xyInBaseImageVec2_p2;
layout (location = 4) in vec2 xyInBaseImageVec2_p3;

out vec2 xyInElementTextureImage;
out vec3 elementTintColorVec3;

In the vertex shader:

// Option 2:
vec2 xyInBaseImageVec2;
if (gl_VertexID==0) {
    xyInBaseImageVec2 = xyInBaseImageVec2_p0;
} else if (gl_VertexID==1) {
    xyInBaseImageVec2 = xyInBaseImageVec2_p1;
} else if (gl_VertexID==2) {
    xyInBaseImageVec2 = xyInBaseImageVec2_p2;
} else if (gl_VertexID==3) {
    xyInBaseImageVec2 = xyInBaseImageVec2_p3;
}
gl_Position = vec4(xyInBaseImageVec2, 0.0, 1.0);
elementTintColorVec3 = colorvec3;

Then it works as desired.

The data buffer for the color and vertex positions are the same for the two examples. [Update: add the feeding code below]

// refer to the code in: http://sol.gfxile.net/instancing.html
// refer to the code in: http://www.gamedev.net/page/resources/_/technical/opengl/opengl-instancing-demystified-r3226
// Case: instaced vertex positions. So each instance should have 4 vec2
{
    // in mat4x2;  later try in vec2[4] xyInBaseImage_vec2list if mat4x2 does not work.
    GLuint instanceVBO = instancevbo_4pts;
    int pos = 1;
    int componentsSize = 2;  // vec2 has 2 components
    // if it is mat, then componentsNum is the column number of the matrix; for example, for mat4x2 it is 4
    int componentsNum = 4; // mat4x2
    GLenum type = GL_FLOAT;
    GLboolean normalized = GL_FALSE;
    GLsizei stride = componentsSize * sizeof(GL_FLOAT) * componentsNum;
    char* pointerFirstComponentOffset = 0;
    int offsetInteger = 0;
    int byteSizeOfOneVertexAttribute = componentsSize * sizeof(GL_FLOAT);
    GLuint divisor = 1;  // 0 not instance
    glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
    for (int i = 0; i < componentsNum; i++) {
        glEnableVertexAttribArray(pos + i);
        // the offset can also be: (void*) (offsetInteger + i * byteSizeOfOneVertexAttribute)
        glVertexAttribPointer(pos + i, componentsSize, type,
                                                     normalized, stride, pointerFirstComponentOffset + i * byteSizeOfOneVertexAttribute  );
        glVertexAttribDivisor(pos + i, divisor);
    }
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

What's wrong with the first option?

user1914692
  • 3,033
  • 5
  • 36
  • 61

0 Answers0