<p>Seems like I had misunderstood instancing, that's why my code was acting strange: I was trying to use instancing while also trying to render different quads using different vertex data.</p>

CC BY-SA 3.0
2015-11-03T09:05:16.970Z
2015-11-03T10:01:38.730Z
2015-11-03T10:01:38.730Z
Darkilon
<p>I had posted about this issue in a <a href="https://computergraphics.stackexchange.com/questions/1663/incorrect-instanced-rendering">previous question</a>, however, I thought instancing was causing it, whereas even using normal uniforms or a SSBO (which's what I'm using right now) causes it, which means the problem is elsewhere.</p>

<p>I've been trying to figure out the cause, and I might've found out something: only the first quad's vertex data seems to be uploaded / used, somehow.</p>

<p>For example, if I were to place random position and texcoord vectors to my other quads, they would still render somewhat correctly. I said <strong>somewhat</strong> because the output is the following: </p>

<p><a href="https://i.sstatic.net/ULWbw.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/ULWbw.png" alt="middle one is way too stretched and the last one&#39;s UV coords are wrong"></a></p>

<p>The middle one is way too stretched and the last one's UV coords are wrong, since they aren't filling the whole quad, as you can see by the white background. So long as the first quad's were right, and whenever I changed the first quad's vertex data, it was propagated to the other ones as well, leading me to believe the other quads' vertex data are getting ignored and the first quad is being re-used.</p>

<p>My code:</p>

<pre><code>// VAO definition
glGenVertexArrays(1, &amp;VAO);
glBindVertexArray(VAO);
/*** VBO ***/
glGenBuffers(1, &amp;VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
/*** Vertex Format ***/
//Position Vertex
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
//Normal Vertex
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec3)));
//Texture Coordinate Vertex
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec3) * 2));
/*** EBO ***/
glGenBuffers(1, &amp;EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
/*** SSBO ***/
glGenBuffers(1, &amp;SSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);

// Quad adding function
auto iter = std::find(quads.begin(), quads.end(), quad);

if(iter == quads.end())
{
    quads.push_back(quad);

    for(unsigned int i = 0; i &lt; 4; i++)
        vertices.push_back(quad-&gt;vertices[i]);

    GLuint start = (indices.empty()) ? 0 : indices[indices.size() - 1] + 1;

    GLuint newIndices[] = {
        start, start + 1, start + 2,
        start, start + 2, start + 3
    };

    for(unsigned int i = 0; i &lt; 6; i++)
        indices.push_back(newIndices[i]);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &amp;vertices[0], GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &amp;indices[0], GL_DYNAMIC_DRAW);

    instanceData.push_back(quad-&gt;data);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
    glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(RenderingData) * quads.size(), &amp;instanceData[0], GL_DYNAMIC_DRAW);
    glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, SSBO, 0, sizeof(RenderingData) * quads.size());
}


// Rendering
program-&gt;Use();

program-&gt;SetStorageBlock("RenderingData", 0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture-&gt;GetTexture());
program-&gt;SetInteger("diffuseTextureArray", 0);

glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, quads.size());
</code></pre>

<p><strong>NOTE:</strong> I also tried to limit the vertex data uploading to the first quad, effectively making it the only one with valid data, a part from the per-instance data (<em>RenderingData</em> struct: a model matrix and an uint), which were all fed to the SSBO. This still lead to 3 quads being rendered, which shouldn't be the case under normal circumstances, right?</p>

<p><strong>EDIT:</strong> I made a sample code which replicates the problem; it should render a green quad and a red quad, however, it just renders 2 red quads. Also, even if you try to change the second quads' attributes (even like placing all 0s), it'll still just render. Somehow only the first quads' vertices are getting read and used for each subsequent quad.</p>

<p><strong>C++ code:</strong></p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;
#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include &lt;IL/il.h&gt;
#include &lt;IL/ilu.h&gt;
#include &lt;glm/glm.hpp&gt;
#include &lt;glm/gtc/matrix_transform.hpp&gt;
#include &lt;glm/gtc/type_ptr.hpp&gt;

struct Vertex{
    glm::vec2 pos, tex;
    glm::vec3 color;
};

struct Quad{
    std::vector&lt;Vertex&gt; vertices;
    glm::mat4 model;

    Quad(glm::vec2 pos, glm::vec2 size, const std::vector&lt;Vertex&gt; &amp;v){
        vertices = v;
        model = glm::mat4();
        model = glm::translate(model, glm::vec3(pos, 0.f));
        model = glm::scale(model, glm::vec3(size, 1.f));
    }
};

class Renderer{
    private:
        GLuint VAO, VBO, instanceVBO, EBO;
        std::vector&lt;Quad*&gt; quads;
        std::vector&lt;Vertex&gt; vertices;
        std::vector&lt;glm::mat4&gt; models;
        std::vector&lt;GLuint&gt; indices;

    public:
        Renderer(){
            /*** VAO ***/
            glGenVertexArrays(1, &amp;VAO);
            glBindVertexArray(VAO);
            /*** Per-vertex VBO ***/
            glGenBuffers(1, &amp;VBO);
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
            glEnableVertexAttribArray(0);   // Position
            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
            glEnableVertexAttribArray(1);   // Tex Coord
            glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec2)));
            glEnableVertexAttribArray(2);   // Color
            glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec2) * 2));
            /*** Per-instance VBO ***/
            glGenBuffers(1, &amp;instanceVBO);
            glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
            glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
            glEnableVertexAttribArray(3);   // Model matrix - row1
            glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), nullptr);
            glVertexAttribDivisor(3, 1);
            glEnableVertexAttribArray(4);   // Model matrix - row2
            glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4)));
            glVertexAttribDivisor(4, 1);
            glEnableVertexAttribArray(5);   // Model matrix - row3
            glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4) * 2));
            glVertexAttribDivisor(5, 1);
            glEnableVertexAttribArray(6);   // Model matrix - row4
            glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4) * 3));
            glVertexAttribDivisor(6, 1);
            /*** EBO ***/
            glGenBuffers(1, &amp;EBO);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
        }
        ~Renderer(){
            glDeleteBuffers(1, &amp;VBO);
            glDeleteBuffers(1, &amp;instanceVBO);
            glDeleteBuffers(1, &amp;EBO);
            glDeleteVertexArrays(1, &amp;VAO);
        }
        void AddQuad(Quad *const quad){
            quads.push_back(quad);

            for(unsigned int i = 0; i &lt; 4; i++)
                vertices.push_back(quad-&gt;vertices[i]);

            models.push_back(quad-&gt;model);

            GLuint start = ((indices.empty()) ? 0 : indices[indices.size() - 1] + 1);

            GLuint newIndices[] = {
                start,  start + 1,  start + 2,
                start,  start + 3,  start + 2
            };

            for(unsigned int i = 0; i &lt; 6; i++)
                indices.push_back(newIndices[i]);

            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &amp;vertices[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * quads.size(), &amp;models[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &amp;indices[0], GL_DYNAMIC_DRAW);
        }
        void Render(GLuint &amp;program){
            glUseProgram(program);

            glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, quads.size());
        }
        void CheckQuads(){
            for(unsigned int i = 0; i &lt; quads.size(); i++)
            {
                std::cout &lt;&lt; "Quad n." &lt;&lt; i &lt;&lt; "\n";
                for(unsigned int j = 0; j &lt; 4; j++)
                {
                    std::cout &lt;&lt; "\tVert " &lt;&lt; j &lt;&lt; ":\n";
                    std::cout &lt;&lt; "\t" &lt;&lt; quads[i]-&gt;vertices[j].pos.x &lt;&lt; " " &lt;&lt; quads[i]-&gt;vertices[j].pos.y &lt;&lt; "\n";
                    std::cout &lt;&lt; "\t" &lt;&lt; quads[i]-&gt;vertices[j].tex.x &lt;&lt; " " &lt;&lt; quads[i]-&gt;vertices[j].tex.y &lt;&lt; "\n";
                    std::cout &lt;&lt; "\t" &lt;&lt; quads[i]-&gt;vertices[j].color.r &lt;&lt; " " &lt;&lt; quads[i]-&gt;vertices[j].color.g &lt;&lt; " " &lt;&lt; quads[i]-&gt;vertices[j].color.b &lt;&lt; "\n";
                }
            }
        }
};

GLuint GenerateShader(const std::string &amp;filename, GLenum shaderType)
{
    GLuint shader;
    std::ifstream file(filename.c_str());
    std::stringstream ss;
    ss &lt;&lt; file.rdbuf();
    file.close();
    const GLchar *source = ss.str().c_str();
    shader = glCreateShader(shaderType);
    glShaderSource(shader, 1, &amp;source, nullptr);
    glCompileShader(shader);
    return shader;
}

int main()
{
    /*** Window + Context ***/
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Incorrect Rendering Test", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    glewInit();
    glViewport(0, 0, 640, 480);
    glClearColor(0.f, 0.f, 0.f, 1.f);

    glm::mat4 projection = glm::ortho(0.f, 640.f, 480.f, 0.f, -1.f, 1.f);

    /*** Shader Program ***/
    GLuint program = glCreateProgram();
    glAttachShader(program, GenerateShader("vertex_shader.glsl", GL_VERTEX_SHADER));
    glAttachShader(program, GenerateShader("fragment_shader.glsl", GL_FRAGMENT_SHADER));
    glLinkProgram(program);

    glUseProgram(program);
    glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    /*** Renderer ***/
    Renderer *renderer = new Renderer();
    // Quad 1
    Vertex v1 = {glm::vec2(0.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    Vertex v2 = {glm::vec2(1.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    Vertex v3 = {glm::vec2(1.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    Vertex v4 = {glm::vec2(0.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    renderer-&gt;AddQuad(new Quad(glm::vec2(300, 10), glm::vec2(150, 150), {v1, v2, v3, v4}));
    // Quad 2
    Vertex v5 = {glm::vec2(0.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    Vertex v6 = {glm::vec2(1.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    Vertex v7 = {glm::vec2(1.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    Vertex v8 = {glm::vec2(0.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    renderer-&gt;AddQuad(new Quad(glm::vec2(30, 10), glm::vec2(150, 150), {v5, v6, v7, v8}));

    while(!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);

        glClear(GL_COLOR_BUFFER_BIT);

        renderer-&gt;Render(program);

        glfwSwapBuffers(window);
    }

    delete renderer;

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}
</code></pre>

<p><strong>vertex_shader.glsl:</strong></p>

<pre><code>#version 430 core

layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 color;
layout (location = 3) in mat4 model;

out vec2 uv;
out vec3 col;

uniform mat4 projection;

void main()
{
    uv = texCoord;
    col = color;

    gl_Position = projection * model * vec4(position, 0.f, 1.f);
}
</code></pre>

<p><strong>fragment_shader.glsl:</strong></p>

<pre><code>#version 430 core

in vec2 uv;
in vec3 col;
out vec4 color;

void main()
{
    color = vec4(col, 1.f);
}
</code></pre>

CC BY-SA 3.0
2015-11-01T11:38:06.240Z
I edited the question to include the image, code and link to the other question. However, in my opinion, it would have been better to edit the other question and include the new things you found out there. It is also encouraged to update the question to include what you have tried to solve the problem even after you posted it. [See our Help Center](http://computergraphics.stackexchange.com/help/no-one-answers).
Nero
CC BY-SA 3.0
2015-11-01T14:26:15.407Z
Oh, I did not know about this. Thank you for both editing my questions and providing advice.
Darkilon
<opengl><rendering><c++>
First quad being re-used?
Darkilon
# First quad being re-used?

(Asked by: username_0 on 2015-11-01)

I had posted about this issue in a [previous question](https://computergraphics.stackexchange.com/questions/1663/incorrect-instanced-rendering), however, I thought instancing was causing it, whereas even using normal uniforms or a SSBO (which's what I'm using right now) causes it, which means the problem is elsewhere.

I've been trying to figure out the cause, and I might've found out something: only the first quad's vertex data seems to be uploaded / used, somehow.

For example, if I were to place random position and texcoord vectors to my other quads, they would still render somewhat correctly. I said **somewhat** because the output is the following:

[![middle one is way too stretched and the last one's UV coords are wrong](https://i.sstatic.net/ULWbw.png)](https://i.sstatic.net/ULWbw.png)

The middle one is way too stretched and the last one's UV coords are wrong, since they aren't filling the whole quad, as you can see by the white background. So long as the first quad's were right, and whenever I changed the first quad's vertex data, it was propagated to the other ones as well, leading me to believe the other quads' vertex data are getting ignored and the first quad is being re-used.

My code:

```
// VAO definition
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
/*** VBO ***/
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
/*** Vertex Format ***/
//Position Vertex
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
//Normal Vertex
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec3)));
//Texture Coordinate Vertex
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec3) * 2));
/*** EBO ***/
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
/*** SSBO ***/
glGenBuffers(1, &SSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);

// Quad adding function
auto iter = std::find(quads.begin(), quads.end(), quad);

if(iter == quads.end())
{
    quads.push_back(quad);

    for(unsigned int i = 0; i < 4; i++)
        vertices.push_back(quad->vertices[i]);

    GLuint start = (indices.empty()) ? 0 : indices[indices.size() - 1] + 1;

    GLuint newIndices[] = {
        start, start + 1, start + 2,
        start, start + 2, start + 3
    };

    for(unsigned int i = 0; i < 6; i++)
        indices.push_back(newIndices[i]);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices[0], GL_DYNAMIC_DRAW);

    instanceData.push_back(quad->data);
    glBindBuffer(GL_SHADER_STORAGE_BUFFER, SSBO);
    glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(RenderingData) * quads.size(), &instanceData[0], GL_DYNAMIC_DRAW);
    glBindBufferRange(GL_SHADER_STORAGE_BUFFER, 0, SSBO, 0, sizeof(RenderingData) * quads.size());
}

// Rendering
program->Use();

program->SetStorageBlock("RenderingData", 0);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, texture->GetTexture());
program->SetInteger("diffuseTextureArray", 0);

glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, quads.size());

```

**NOTE:** I also tried to limit the vertex data uploading to the first quad, effectively making it the only one with valid data, a part from the per-instance data (*RenderingData* struct: a model matrix and an uint), which were all fed to the SSBO. This still lead to 3 quads being rendered, which shouldn't be the case under normal circumstances, right?

**EDIT:** I made a sample code which replicates the problem; it should render a green quad and a red quad, however, it just renders 2 red quads. Also, even if you try to change the second quads' attributes (even like placing all 0s), it'll still just render. Somehow only the first quads' vertices are getting read and used for each subsequent quad.

**C++ code:**

```
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#define GLEW_STATIC
#include "GL/glew.h"
#include "GLFW/glfw3.h"
#include <IL/il.h>
#include <IL/ilu.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>

struct Vertex{
    glm::vec2 pos, tex;
    glm::vec3 color;
};

struct Quad{
    std::vector<Vertex> vertices;
    glm::mat4 model;

    Quad(glm::vec2 pos, glm::vec2 size, const std::vector<Vertex> &v){
        vertices = v;
        model = glm::mat4();
        model = glm::translate(model, glm::vec3(pos, 0.f));
        model = glm::scale(model, glm::vec3(size, 1.f));
    }
};

class Renderer{
    private:
        GLuint VAO, VBO, instanceVBO, EBO;
        std::vector<Quad*> quads;
        std::vector<Vertex> vertices;
        std::vector<glm::mat4> models;
        std::vector<GLuint> indices;

    public:
        Renderer(){
            /*** VAO ***/
            glGenVertexArrays(1, &VAO);
            glBindVertexArray(VAO);
            /*** Per-vertex VBO ***/
            glGenBuffers(1, &VBO);
            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
            glEnableVertexAttribArray(0);   // Position
            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), nullptr);
            glEnableVertexAttribArray(1);   // Tex Coord
            glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec2)));
            glEnableVertexAttribArray(2);   // Color
            glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(glm::vec2) * 2));
            /*** Per-instance VBO ***/
            glGenBuffers(1, &instanceVBO);
            glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
            glBufferData(GL_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
            glEnableVertexAttribArray(3);   // Model matrix - row1
            glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), nullptr);
            glVertexAttribDivisor(3, 1);
            glEnableVertexAttribArray(4);   // Model matrix - row2
            glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4)));
            glVertexAttribDivisor(4, 1);
            glEnableVertexAttribArray(5);   // Model matrix - row3
            glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4) * 2));
            glVertexAttribDivisor(5, 1);
            glEnableVertexAttribArray(6);   // Model matrix - row4
            glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4) * 3));
            glVertexAttribDivisor(6, 1);
            /*** EBO ***/
            glGenBuffers(1, &EBO);
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, 0, nullptr, GL_DYNAMIC_DRAW);
        }
        ~Renderer(){
            glDeleteBuffers(1, &VBO);
            glDeleteBuffers(1, &instanceVBO);
            glDeleteBuffers(1, &EBO);
            glDeleteVertexArrays(1, &VAO);
        }
        void AddQuad(Quad *const quad){
            quads.push_back(quad);

            for(unsigned int i = 0; i < 4; i++)
                vertices.push_back(quad->vertices[i]);

            models.push_back(quad->model);

            GLuint start = ((indices.empty()) ? 0 : indices[indices.size() - 1] + 1);

            GLuint newIndices[] = {
                start,  start + 1,  start + 2,
                start,  start + 3,  start + 2
            };

            for(unsigned int i = 0; i < 6; i++)
                indices.push_back(newIndices[i]);

            glBindBuffer(GL_ARRAY_BUFFER, VBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ARRAY_BUFFER, instanceVBO);
            glBufferData(GL_ARRAY_BUFFER, sizeof(glm::mat4) * quads.size(), &models[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indices.size(), &indices[0], GL_DYNAMIC_DRAW);
        }
        void Render(GLuint &program){
            glUseProgram(program);

            glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0, quads.size());
        }
        void CheckQuads(){
            for(unsigned int i = 0; i < quads.size(); i++)
            {
                std::cout << "Quad n." << i << "\n";
                for(unsigned int j = 0; j < 4; j++)
                {
                    std::cout << "\tVert " << j << ":\n";
                    std::cout << "\t" << quads[i]->vertices[j].pos.x << " " << quads[i]->vertices[j].pos.y << "\n";
                    std::cout << "\t" << quads[i]->vertices[j].tex.x << " " << quads[i]->vertices[j].tex.y << "\n";
                    std::cout << "\t" << quads[i]->vertices[j].color.r << " " << quads[i]->vertices[j].color.g << " " << quads[i]->vertices[j].color.b << "\n";
                }
            }
        }
};

GLuint GenerateShader(const std::string &filename, GLenum shaderType)
{
    GLuint shader;
    std::ifstream file(filename.c_str());
    std::stringstream ss;
    ss << file.rdbuf();
    file.close();
    const GLchar *source = ss.str().c_str();
    shader = glCreateShader(shaderType);
    glShaderSource(shader, 1, &source, nullptr);
    glCompileShader(shader);
    return shader;
}

int main()
{
    /*** Window + Context ***/
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Incorrect Rendering Test", nullptr, nullptr);
    glfwMakeContextCurrent(window);
    glewExperimental = GL_TRUE;
    glewInit();
    glViewport(0, 0, 640, 480);
    glClearColor(0.f, 0.f, 0.f, 1.f);

    glm::mat4 projection = glm::ortho(0.f, 640.f, 480.f, 0.f, -1.f, 1.f);

    /*** Shader Program ***/
    GLuint program = glCreateProgram();
    glAttachShader(program, GenerateShader("vertex_shader.glsl", GL_VERTEX_SHADER));
    glAttachShader(program, GenerateShader("fragment_shader.glsl", GL_FRAGMENT_SHADER));
    glLinkProgram(program);

    glUseProgram(program);
    glUniformMatrix4fv(glGetUniformLocation(program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
    /*** Renderer ***/
    Renderer *renderer = new Renderer();
    // Quad 1
    Vertex v1 = {glm::vec2(0.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    Vertex v2 = {glm::vec2(1.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    Vertex v3 = {glm::vec2(1.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    Vertex v4 = {glm::vec2(0.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(1.f, 0.f, 0.f)};
    renderer->AddQuad(new Quad(glm::vec2(300, 10), glm::vec2(150, 150), {v1, v2, v3, v4}));
    // Quad 2
    Vertex v5 = {glm::vec2(0.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    Vertex v6 = {glm::vec2(1.f, 0.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    Vertex v7 = {glm::vec2(1.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    Vertex v8 = {glm::vec2(0.f, 1.f), glm::vec2(0.f, 0.f), glm::vec3(0.f, 1.f, 0.f)};
    renderer->AddQuad(new Quad(glm::vec2(30, 10), glm::vec2(150, 150), {v5, v6, v7, v8}));

    while(!glfwWindowShouldClose(window))
    {
        glfwPollEvents();

        if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);

        glClear(GL_COLOR_BUFFER_BIT);

        renderer->Render(program);

        glfwSwapBuffers(window);
    }

    delete renderer;

    glfwDestroyWindow(window);
    glfwTerminate();

    return 0;
}

```

**vertex\_shader.glsl:**

```
#version 430 core

layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texCoord;
layout (location = 2) in vec3 color;
layout (location = 3) in mat4 model;

out vec2 uv;
out vec3 col;

uniform mat4 projection;

void main()
{
    uv = texCoord;
    col = color;

    gl_Position = projection * model * vec4(position, 0.f, 1.f);
}

```

**fragment\_shader.glsl:**

```
#version 430 core

in vec2 uv;
in vec3 col;
out vec4 color;

void main()
{
    color = vec4(col, 1.f);
}

```

**Tags:** opengl, rendering, c++

> **Comments:**
> **username_1**: I edited the question to include the image, code and link to the other question. However, in my opinion, it would have been better to edit the other question and include the new things you found out there. It is also encouraged to update the question to include what you have tried to solve the problem even after you posted it. [See our Help Center](http://computergraphics.stackexchange.com/help/no-one-answers).  
> **username_0**: Oh, I did not know about this. Thank you for both editing my questions and providing advice.  

---

## 1 answers

### Answer by username_0 on 2015-11-03. Score: 1 | Accepted answer

Seems like I had misunderstood instancing, that's why my code was acting strange: I was trying to use instancing while also trying to render different quads using different vertex data.


---

