Results 1 to 2 of 2

Thread: Trouble drawing from different VBOs

  1. #1
    Newbie Newbie
    Join Date
    May 2017
    Posts
    1

    Trouble drawing from different VBOs

    Hi,
    my program uses one VAO, 2 VBOs and 2 EBOs. The program draws from the VBO very oddly
    This is how the program should function:
    Code :
    //cube
            GLfloat vertices1[] = {
    		0.5f, 0.5f, 0.5f,
    		0.5f, 0.5f, -0.5f,
    		0.5f, -0.5f, 0.5f,
    		0.5f, -0.5f, -0.5f,
    		-0.5f, 0.5f, 0.5f,
    		-0.5f, 0.5f, -0.5f,
    		-0.5f, -0.5f, 0.5f,
    		-0.5f, -0.5f, -0.5f,
     
    	};
    	GLint indices1[]{
    		2, 0, 4,
    		4, 6, 2,
    		0, 2, 3,
    		3, 1, 0,
    		5, 1, 3,
    		3, 7, 5,
    		0, 1, 5,
    		4, 0, 5,
    		6, 4, 7,
    		4, 5, 7,
    		7, 3, 2,
    		7, 2, 6,
    	};
     
            //loaded model
    	std::vector<GLfloat> teddy_vertices;
    	std::vector<GLuint> teddy_indices;
    	loadOBJ("teddy.obj", teddy_vertices, teddy_indices); 
     
     
    	GLuint VAO, TEDDY, TEDDY_EBO, newVBO, newEBO;
    	glGenVertexArrays(1, &VAO);
    	glGenBuffers(1, &TEDDY);
    	glGenBuffers(1, &TEDDY_EBO);
    	glGenBuffers(1, &newVBO);
    	glGenBuffers(1, &newEBO);
     
    	glBindVertexArray(VAO);
    	glBindBuffer(GL_ARRAY_BUFFER, newVBO);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newEBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices1), indices1, GL_STATIC_DRAW);
     
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    	glEnableVertexAttribArray(0);
     
    	glBindBuffer(GL_ARRAY_BUFFER, TEDDY);
    	glBufferData(GL_ARRAY_BUFFER, teddy_vertices.size()*sizeof(GLfloat), &teddy_vertices.front(), GL_STATIC_DRAW);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    	glEnableVertexAttribArray(0);
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, TEDDY_EBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, teddy_indices.size() * sizeof(GLuint), &teddy_indices.front(), GL_STATIC_DRAW);
     
    	glBindBuffer(GL_ARRAY_BUFFER, 0); // Note that this is allowed, the call to glVertexAttribPointer registered VBO as the currently bound vertex buffer object so afterwards we can safely unbind
     
    	glBindVertexArray(0); 
     
    //...
     
    while (!glfwWindowShouldClose(window))
    	{
    		glfwPollEvents();
     
    		glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    		glm::mat4 model_matrix = glm::mat4(1.0f);
    		glm::mat4 view;
    		glm::mat4 tilt_view;
    		glm::mat4 projection;
     
    				view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);//cameraPos + cameraFront
     
    		projection = glm::perspective(fov, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
     
     
    		glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model_matrix));
    		glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
    		glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
    		glUniform4fv(color, 1, glm::value_ptr(glm::vec4(1, 1, 1, 1)));
     
    		glBindVertexArray(VAO);
     
    		if (!teddy_render){		
    			glBindBuffer(GL_ARRAY_BUFFER, TEDDY);
    			glDrawElements(
    				GL_TRIANGLES,
    				teddy_indices.size(),
    				GL_UNSIGNED_INT,
    				(void*)0
    				);
    			glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    		}
    		else{
    			glBindBuffer(GL_ARRAY_BUFFER, newVBO);
    			glDrawElements(
                                  GL_TRIANGLES, 
                                  36, 
                                  GL_UNSIGNED_INT, 0);
    			glBindBuffer(GL_ARRAY_BUFFER, 0);
                    }
    		glBindVertexArray(0);
     
    		glfwSwapBuffers(window);
    	}
     
    	glfwTerminate();
    	return 0;
    }

    this code only draws the loaded object incorrectly, and does not draw from the other VBO when wanted.

    If in the game loop the code is changed to :
    Code :
    glBindVertexArray(VAO);
     
                    //if (!teddy_render){		
    			glBindBuffer(GL_ARRAY_BUFFER, TEDDY);
    			glDrawElements(
    				GL_TRIANGLES,
    				teddy_indices.size(),
    				GL_UNSIGNED_INT,
    				(void*)0
    				);
    			glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    		//}
    		//else{
    		//	glBindBuffer(GL_ARRAY_BUFFER, newVBO);
    		//	glDrawElements(
                    //             GL_TRIANGLES, 
                    //              36, 
                    //             GL_UNSIGNED_INT, 0);
    		//	glBindBuffer(GL_ARRAY_BUFFER, 0);
                    //}

    then the loaded object renders. Then if in the configuration above the game loop the cube is configured after the object, the cube renders even though the loaded object is being bound in the game loop with the cube code still commented out:

    Code :
    glBindVertexArray(VAO);
     
    	glBindBuffer(GL_ARRAY_BUFFER, TEDDY);
    	glBufferData(GL_ARRAY_BUFFER, teddy_vertices.size()*sizeof(GLfloat), &teddy_vertices.front(), GL_STATIC_DRAW);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    	glEnableVertexAttribArray(0);
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, TEDDY_EBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, teddy_indices.size() * sizeof(GLuint), &teddy_indices.front(), GL_STATIC_DRAW);
     
    	glBindBuffer(GL_ARRAY_BUFFER, newVBO);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW);
     
    	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, newEBO);
    	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices1), indices1, GL_STATIC_DRAW);
     
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    	glEnableVertexAttribArray(0);
     
     
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    	glBindVertexArray(0);

    Thank you.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,660
    You need to read up on Vertex Array Objects (VAOs). You're apparently misunderstanding how they work.

    In particular, read up here about what happens to VAO state when you call glVertexAttribPointer.

    Hint: You only have 1 VAO in your code snippet, so...

Similar Threads

  1. Having trouble understanding VAOs and VBOs
    By tokongs in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 06-02-2017, 06:36 AM
  2. Having trouble with multiple VBOs with TBO in geometry shader
    By dw71273 in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 01-09-2014, 07:42 AM
  3. Trouble getting VBOs to work
    By FlayedOne in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 07-17-2009, 07:57 AM
  4. Trouble moving from one VBO to multiple VBOs
    By Aaron in forum OpenGL: Advanced Coding
    Replies: 3
    Last Post: 07-24-2007, 05:56 AM
  5. Trouble with VBOs + general trouble with speed.
    By PixelDuck in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 11-10-2004, 12:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean