Updating mesh after first display


I’ve been starting using OpenGL a few days ago, and I’d like to do a program that reads data from .obj files, and display it. Using arrow keys would change the currently loaded data for the next obj in folder.

I’ve been starting from the transparency example given on opengl-tutorial.org, and coded a way to load the new .obj by pressing left or right arrow keys. The object is updated, but there is no changes in the window, the one that is drawn is still the old one…

I won’t put my whole code (it is a bit too long, around 700 lines for the main functions), but here are the important parts for this (at least, I think).

This code is run once at the beginning, in the main() function, and once every time I want to change the displayed object, in a Key Callback.

The answer is probably very dumb, but I just can’t find it right now…

Thank you for your help !

Load the 3D object:

bool loadOBJNoTexture(
	const char * path, 
	std::vector<glm::vec3> & out_vertices, 
	std::vector<glm::vec2> & out_uvs,
	std::vector<glm::vec3> & out_normals
	printf("Loading OBJ file %s...\n", path);

	std::vector<unsigned int> vertexIndices, uvIndices, normalIndices;
	std::vector<glm::vec3> temp_vertices; 
	std::vector<glm::vec2> temp_uvs;
	std::vector<glm::vec3> temp_normals;

	FILE * file = fopen(path, "r");
	if( file == NULL ){
		printf("Impossible to open the file ! Are you in the right path ? See Tutorial 1 for details\n");
		return false;

	while( 1 ){

		char lineHeader[128];
		// read the first word of the line
		int res = fscanf(file, "%s", lineHeader);
		if (res == EOF)
			break; // EOF = End Of File. Quit the loop.

		// else : parse lineHeader
		if ( strcmp( lineHeader, "v" ) == 0 ){
			glm::vec3 vertex;
			fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z );
		}else if ( strcmp( lineHeader, "vt" ) == 0 ){
			glm::vec2 uv;
			fscanf(file, "%f %f\n", &uv.x, &uv.y );
			uv.y = -uv.y; // Invert V coordinate since we will only use DDS texture, which are inverted. Remove if you want to use TGA or BMP loaders.
		}else if ( strcmp( lineHeader, "vn" ) == 0 ){
			glm::vec3 normal;
			fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z );
		}else if ( strcmp( lineHeader, "f" ) == 0 ){
			std::string vertex1, vertex2, vertex3;
			unsigned int vertexIndex[3], normalIndex[3];
			int matches = fscanf(file, "%d//%d %d//%d %d//%d\n", &vertexIndex[0], &normalIndex[0], &vertexIndex[1], &normalIndex[1], &vertexIndex[2], &normalIndex[2] );
			if (matches != 6){
				printf("File can't be read by our NoTexture simple parser :-( Try exporting with other options\n");
				return false;
			uvIndices    .push_back(1);
			uvIndices    .push_back(1);
			uvIndices    .push_back(1);
			// Probably a comment, eat up the rest of the line
			char stupidBuffer[1000];
			fgets(stupidBuffer, 1000, file);

	// For each vertex of each triangle
	for( unsigned int i=0; i<vertexIndices.size(); i++ ){

		// Get the indices of its attributes
		unsigned int vertexIndex = vertexIndices[i];
		unsigned int uvIndex = uvIndices[i];
		unsigned int normalIndex = normalIndices[i];
		// Get the attributes thanks to the index
		glm::vec3 vertex = temp_vertices[ vertexIndex-1 ];
		glm::vec2 uv = glm::vec2();
		glm::vec3 normal = temp_normals[ normalIndex-1 ];
		// Put the attributes in buffers
		out_uvs     .push_back(uv);
		out_normals .push_back(normal);
	return true;

std::string filename = (std::string)(argv[1]) + "cold_" + std::to_string(current_model) + ".obj";
	struct stat buffer;
	while (stat(filename.c_str(), &buffer) != 0)
		current_model += 1;
		std::cout << "file " << filename << " does not exist. Skipping..." << std::endl;
		filename = (std::string)(argv[1]) + "cold_" + std::to_string(current_model) + ".obj";
	res = loadOBJNoTexture(filename.c_str(), vertices, uvs, normals);

Put object into VBO

	glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, indexed_vertices.size() * sizeof(glm::vec3), &indexed_vertices[0], GL_DYNAMIC_DRAW);

	glGenBuffers(1, &uvbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
	glBufferData(GL_ARRAY_BUFFER, indexed_uvs.size() * sizeof(glm::vec2), &indexed_uvs[0], GL_DYNAMIC_DRAW);

	glGenBuffers(1, &normalbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
	glBufferData(GL_ARRAY_BUFFER, indexed_normals.size() * sizeof(glm::vec3), &indexed_normals[0], GL_DYNAMIC_DRAW);

	// Generate a buffer for the indices as well
	glGenBuffers(1, &elementbuffer);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_DYNAMIC_DRAW);

Display the obtained VBO (code in the running loop placed after the other events) :

// 1rst attribute buffer : vertices
		glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
			0,		  // attribute
			3,		  // size
			GL_FLOAT, // type
			GL_FALSE, // normalized?
			0,		  // stride
			(void *)0 // array buffer offset

		// 2nd attribute buffer : UVs
		glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
			1,		  // attribute
			2,		  // size
			GL_FLOAT, // type
			GL_FALSE, // normalized?
			0,		  // stride
			(void *)0 // array buffer offset

		// 3rd attribute buffer : normals
		glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
			2,		  // attribute
			3,		  // size
			GL_FLOAT, // type
			GL_FALSE, // normalized?
			0,		  // stride
			(void *)0 // array buffer offset

		// Index buffer
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);

		// Draw the triangles !
			GL_TRIANGLES,	   // mode
			indices.size(),	   // count
			(void *)0		   // element array buffer offset

This is an OpenGL question, rather than a GLFW one, so you may also find help on forums which deal with that API.

It’s difficult to debug code with only a small portion, in future you may find that putting the project on Github, Gitlab, Bitbucket or another free repository service helpful.

In order to display the new data, you need to call glBindBuffer on the buffers and glBufferData to specify the source to upload the data. You don’t need to regenerate the buffers with glGenBuffers but can re-use the already generated ones. Thus you can use a function with code similar to that in the code you labelled Put object into VBO but without the glGenBuffers calls.