Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. Drawing a simple triangle horror when using std::vector

Drawing a simple triangle horror when using std::vector

Scheduled Pinned Locked Moved Graphics
graphics
3 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Isawyouoo
    wrote on last edited by
    #1

    this is bullshit, the first VAO without std::vector get drawn but not the second with vector :confused: : Vector3f Vertices[4]; Vertices[0] = Vector3f(-1.0f, -1.0f, 0.5773f); Vertices[1] = Vector3f(0.0f, -1.0f, -1.15475f); Vertices[2] = Vector3f(1.0f, -1.0f, 0.5773f); Vertices[3] = Vector3f(0.0f, 1.0f, 0.0f); unsigned int Indices[] = { 0, 3, 1, 1, 3, 2, 2, 3, 0, 0, 1, 2 }; GLuint VBO[2]; GLuint IBO[2]; unsigned int id[2]; glGenVertexArrays(2, id); glGenBuffers(2, VBO); glGenBuffers(2, IBO); glBindVertexArray(id[0]); glBindBuffer(GL_ARRAY_BUFFER, VBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); struct Vertex { Vector3f position; //Vector2f texCoord; //Vector3f normal; //int Boneindices[4]; //float Weights[4]; }; vector Vertices2; Vertex* v=new Vertex(); v->position = Vector3f(-1.0f, -0.0f, 0); Vertices2.push_back(v); v->position = Vector3f(1.0f, -0.0f, -1); Vertices2.push_back(v); v->position = Vector3f(1.0f, -0.0f, 0); Vertices2.push_back(v); v->position = Vector3f(0.0f, 1.0f, 0.0f); Vertices2.push_back(v); struct Triangle { int indices[3]; }; Triangle* t = new Triangle(); t->indices[0]=0; t->indices[1]=3; t->indices[2]=1; vector Indices2; Indices2.push_back(t); t->indices[0] = 1; t->indices[1] = 3; t->indices[2] = 2; Indices2.push_back(t); t->indices[0] = 2; t->indices[1] = 3; t->indices[2] = 0; Indices2.push_back(t); t->indices[0] = 0; t->indices[1] = 1; t->indices[2] = 2; Indices2.push_back(t); glBindVertexArray(id[1]); glBindBuffer(GL_ARRAY_BUFFER, VBO[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)* Vertices2.size(), &Vertices2[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Triangle)* Indices2.size(), &Indices2[0], GL_STATIC_DRAW); for (size_t i = 0; i < 2; i++) { glBindVertexArray(id[i]); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); glBindVertexArray(0); }

    G L 2 Replies Last reply
    0
    • I Isawyouoo

      this is bullshit, the first VAO without std::vector get drawn but not the second with vector :confused: : Vector3f Vertices[4]; Vertices[0] = Vector3f(-1.0f, -1.0f, 0.5773f); Vertices[1] = Vector3f(0.0f, -1.0f, -1.15475f); Vertices[2] = Vector3f(1.0f, -1.0f, 0.5773f); Vertices[3] = Vector3f(0.0f, 1.0f, 0.0f); unsigned int Indices[] = { 0, 3, 1, 1, 3, 2, 2, 3, 0, 0, 1, 2 }; GLuint VBO[2]; GLuint IBO[2]; unsigned int id[2]; glGenVertexArrays(2, id); glGenBuffers(2, VBO); glGenBuffers(2, IBO); glBindVertexArray(id[0]); glBindBuffer(GL_ARRAY_BUFFER, VBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); struct Vertex { Vector3f position; //Vector2f texCoord; //Vector3f normal; //int Boneindices[4]; //float Weights[4]; }; vector Vertices2; Vertex* v=new Vertex(); v->position = Vector3f(-1.0f, -0.0f, 0); Vertices2.push_back(v); v->position = Vector3f(1.0f, -0.0f, -1); Vertices2.push_back(v); v->position = Vector3f(1.0f, -0.0f, 0); Vertices2.push_back(v); v->position = Vector3f(0.0f, 1.0f, 0.0f); Vertices2.push_back(v); struct Triangle { int indices[3]; }; Triangle* t = new Triangle(); t->indices[0]=0; t->indices[1]=3; t->indices[2]=1; vector Indices2; Indices2.push_back(t); t->indices[0] = 1; t->indices[1] = 3; t->indices[2] = 2; Indices2.push_back(t); t->indices[0] = 2; t->indices[1] = 3; t->indices[2] = 0; Indices2.push_back(t); t->indices[0] = 0; t->indices[1] = 1; t->indices[2] = 2; Indices2.push_back(t); glBindVertexArray(id[1]); glBindBuffer(GL_ARRAY_BUFFER, VBO[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)* Vertices2.size(), &Vertices2[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Triangle)* Indices2.size(), &Indices2[0], GL_STATIC_DRAW); for (size_t i = 0; i < 2; i++) { glBindVertexArray(id[i]); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); glBindVertexArray(0); }

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      Your vector contains pointers to vertex, not the actual vertices. In fact it contains the same pointer four times, so even if OpenGL can handle pointers to vertex data you only have the same vertex repeated. The index buffer suffers from the same problem.

      1 Reply Last reply
      0
      • I Isawyouoo

        this is bullshit, the first VAO without std::vector get drawn but not the second with vector :confused: : Vector3f Vertices[4]; Vertices[0] = Vector3f(-1.0f, -1.0f, 0.5773f); Vertices[1] = Vector3f(0.0f, -1.0f, -1.15475f); Vertices[2] = Vector3f(1.0f, -1.0f, 0.5773f); Vertices[3] = Vector3f(0.0f, 1.0f, 0.0f); unsigned int Indices[] = { 0, 3, 1, 1, 3, 2, 2, 3, 0, 0, 1, 2 }; GLuint VBO[2]; GLuint IBO[2]; unsigned int id[2]; glGenVertexArrays(2, id); glGenBuffers(2, VBO); glGenBuffers(2, IBO); glBindVertexArray(id[0]); glBindBuffer(GL_ARRAY_BUFFER, VBO[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[0]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); struct Vertex { Vector3f position; //Vector2f texCoord; //Vector3f normal; //int Boneindices[4]; //float Weights[4]; }; vector Vertices2; Vertex* v=new Vertex(); v->position = Vector3f(-1.0f, -0.0f, 0); Vertices2.push_back(v); v->position = Vector3f(1.0f, -0.0f, -1); Vertices2.push_back(v); v->position = Vector3f(1.0f, -0.0f, 0); Vertices2.push_back(v); v->position = Vector3f(0.0f, 1.0f, 0.0f); Vertices2.push_back(v); struct Triangle { int indices[3]; }; Triangle* t = new Triangle(); t->indices[0]=0; t->indices[1]=3; t->indices[2]=1; vector Indices2; Indices2.push_back(t); t->indices[0] = 1; t->indices[1] = 3; t->indices[2] = 2; Indices2.push_back(t); t->indices[0] = 2; t->indices[1] = 3; t->indices[2] = 0; Indices2.push_back(t); t->indices[0] = 0; t->indices[1] = 1; t->indices[2] = 2; Indices2.push_back(t); glBindVertexArray(id[1]); glBindBuffer(GL_ARRAY_BUFFER, VBO[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)* Vertices2.size(), &Vertices2[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Triangle)* Indices2.size(), &Indices2[0], GL_STATIC_DRAW); for (size_t i = 0; i < 2; i++) { glBindVertexArray(id[i]); glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, 0); glBindVertexArray(0); }

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Isawyouoo wrote:

        this is bullshit

        Well you wrote it. Look at the following line:

        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)* Vertices2.size(), &Vertices2[0], GL_STATIC_DRAW);

        The third parameter is supposed to be a pointer to the data you wish to copy, but you are passing a pointer to a pointer. The vector contains a list of pointers to vertex elements, so &Vertices2[0] is the address of the first pointer item in the vector. It should be:

        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)* Vertices2.size(), Vertices2[0], GL_STATIC_DRAW);

        Without the & on parameter three.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups