Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: Creating a model class

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    5

    Question Creating a model class

    I'm trying to create a model class which would carry one VAO and 2 VBOs, but I'm not quite sure if it should have its vertices data saved for later usage. Something like for shape changing or idk
    By the way, how do I pass vertices as an argument? In my code its not working.
    Here is my code:

    Code :
    	class LEOPModel {
     
    		GLuint VAO_;
    		GLuint VBO1, VBO2;
     
    	public:
     
    		LEOPModel() {
     
    			glGenVertexArrays(1, &VAO_);
     
    		}
     
    		void insertVBO(GLfloat *vertices, GLfloat *vertices_2) {
     
    			glGenBuffers(1, &VBO1);
    			glGenBuffers(1, &VBO2);
     
    			glBindVertexArray(VAO_);
     
    			glBindBuffer(GL_ARRAY_BUFFER, VBO1);
    			glEnableVertexAttribArray(0);
    			glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
    			glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    			//
    			glBindBuffer(GL_ARRAY_BUFFER, VBO2);
    			glEnableVertexAttribArray(1);
    			glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
    			glBufferData(GL_ARRAY_BUFFER, sizeof(vertices_2), vertices_2, GL_STATIC_DRAW);
     
    			glBindVertexArray(0);
     
    		}
     
    		void draw() {
     
    			glBindVertexArray(VAO_);
     
    			glDrawArrays(GL_TRIANGLES, 0, 6);
     
    			glBindVertexArray(0);
     
    		}
     
    		GLuint getVBO(int at) {
     
    			return VBO1;
    		}
     
    	};

    thanks.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,829
    Quote Originally Posted by Cofyka View Post
    By the way, how do I pass vertices as an argument? In my code its not working.
    Code :
    		void insertVBO(GLfloat *vertices, GLfloat *vertices_2) {
    ...
    			glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    Applying the sizeof operator to a pointer returns the size of the pointer (typically 4 or 8), not the data it points to. You'll need to pass the array length as a parameter, or use a std::vector.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    5
    Quote Originally Posted by GClements View Post
    Applying the sizeof operator to a pointer returns the size of the pointer (typically 4 or 8), not the data it points to. You'll need to pass the array length as a parameter, or use a std::vector.
    But it's not working.
    Code :
    	le_opengl::LEOPModel model;
    	model.insertVBO(colors_, sizeof(colors_), shape_, sizeof(shape_));
     
    void insertVBO(GLfloat *vertices, int size, GLfloat *vertices_2, int size_2) {
     
    			glGenBuffers(1, &VBO1);
    			glGenBuffers(1, &VBO2);
     
    			glBindVertexArray(VAO_);
     
    			glBindBuffer(GL_ARRAY_BUFFER, VBO1);
    			glEnableVertexAttribArray(0);
    			glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
    			glBufferData(GL_ARRAY_BUFFER, size, &vertices, GL_STATIC_DRAW);
    			//
    			glBindBuffer(GL_ARRAY_BUFFER, VBO2);
    			glEnableVertexAttribArray(1);
    			glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid *)0);
    			glBufferData(GL_ARRAY_BUFFER, size_2, vertices_2, GL_STATIC_DRAW);
     
    			glBindVertexArray(0);
     
    		}

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,829
    Quote Originally Posted by Cofyka View Post
    But it's not working.
    Code :
    	le_opengl::LEOPModel model;
    	model.insertVBO(colors_, sizeof(colors_), shape_, sizeof(shape_));
    What are colors_ and shape_?

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    5
    Static variables.
    Code :
    GLfloat shape_[3 * 6]{
     
    	-0.5f, 0.5f, 0.0f,
    	0.5f, -0.5f, 0.0f,
    	-0.5f, -0.5f, 0.0f,
     
    	0.5f, 0.5f, 0.0f,
    	0.5f, -0.5f, 0.0f,
    	-0.5f, 0.5f, 0.0f
     
    };
     
    GLfloat colors_[3 * 6]{
     
    	0.5f, 0.0f, 0.0f,
    	0.0f, 0.0f, 0.0f,
    	0.0f, 0.0f, 0.0f,
     
    	1.0f, 0.0f, 0.0f,
    	0.0f, 0.0f, 0.0f,
    	0.5f, 0.0f, 0.0f
     
    };

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Jun 2013
    Posts
    2,829
    Another issue which I missed last time:

    Quote Originally Posted by Cofyka View Post
    Code :
    glBufferData(GL_ARRAY_BUFFER, size, &vertices, GL_STATIC_DRAW);
    The "&" shouldn't be there; you want the value of the pointer, not its address. For an array, it doesn't matter (an array used as a value "decays" to a pointer to its first element); for a pointer, it matters.

    The second call is correct:

    Quote Originally Posted by Cofyka View Post
    Code :
    glBufferData(GL_ARRAY_BUFFER, size_2, vertices_2, GL_STATIC_DRAW);

  7. #7
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    5
    I noticed that after but still results are the same.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •