vendredi 20 septembre 2019

c++ Opengl vbo keyframe animation issue

// This is the first load function in main that there is no issue with.

   Model obj1(" stuff.obj"); 

// So above is an instance of a class in another header file being called as 'obj1'

// This call exactly below is the 2nd mesh. No problem here Also i'm planning to use it to load other models later.

 Model obj2(" stuff2.obj"); // So this is an instance of that same class being called as 'obj2'.

// Ok down below i'm gonna be attempting keyframe animation // by setting a new mesh to be loaded and prepared for when the draw function in MAIN() is called

 while (true)
{
    if (keystates[SDL_SCANCODE_RCTRL])         
        { 
 // This the problem right here below . THE THIRD LOAD It wont work. THIS ME TRYING TO LOAD THE 'obj2' again this time for a new mesh       
               Model obj2("stuff3.obj"); 
        }
}

// PLEASE NOTE READING THE DATA FROM THE OBJ FILE IS NOT THE PROBLEM AND SIMULTENOUSLY THE DATA IS ORGANIZED AND SENT TO BE PROCESSED BY A VAO & VBO FOR DRAWING

// down here is some code in a different header file from MAIN.

// So here is the function that draws the mesh. There is no issue with this to make stuff appear on screen // Except when i want new stuff to appear with the third load function in main()

draw mesh()
    {
        glBindVertexArray(this->VAO);       
        glDrawElements(GL_TRIANGLES, this->indices.size(), GL_UNSIGNED_INT, 0);                                     
        glBindVertexArray(0); 
    }

// The 1st two load function were using create() to set up vbo, vao to draw objects to screen. // While the third that failed was using NewMesh()which is below create().

void create()
    {
        // Create buffers/arrays
        glGenVertexArrays(1, &this->VAO);
        glGenBuffers(1, &this->VBO);
        glGenBuffers(1, &this->EBO);

        glBindVertexArray(this->VAO);

        // Load data into vertex buffers
        glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
        glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), &this->vertices[0], GL_DYNAMIC_DRAW);  // GL_DYNAMIC_DRAW  GL_STATIC_DRAW

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_DYNAMIC_DRAW);
        // Set the vertex attribute pointers
        // Vertex Positions
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)0);
        // Vertex Normals
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)offsetof(Vertex, Normal) );
        // Vertex Texture Coords
        glEnableVertexAttribArray(2);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)offsetof(Vertex, TexCoords) );
    //  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }


void NewMesh() 
    {  // Note NewMesh() does not work.     


        glBindVertexArray(this->VAO);  

        glBindBuffer(GL_ARRAY_BUFFER, this->VBO);

        glBufferData(GL_ARRAY_BUFFER, this->vertices.size() * sizeof(Vertex), &this->vertices[0], GL_DYNAMIC_DRAW);  // GL_DYNAMIC_DRAW  GL_STATIC_DRAW

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indices.size() * sizeof(GLuint), &this->indices[0], GL_DYNAMIC_DRAW);
        // Set the vertex attribute pointers

        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)0);
        // Vertex Normals
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)offsetof(Vertex, Normal));
        // Vertex Texture Coords
        glEnableVertexAttribArray(2);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)offsetof(Vertex, TexCoords));

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);

    }
`````

// WELL THATS IT. COULD SOME ONE PLZ TELL ME WHERE THE MESS UP IS AND GIVE & SHOW ME THE PROPER DIRECTION.
// and a demonstration example maybe? That would be very much appreciated.

So its pretty straight forward im having issue with 'ob2' THE 2nd instance of a class to load new models.


Aucun commentaire:

Enregistrer un commentaire