Jump to content
  • Advertisement
Sign in to follow this  
gboxentertainment

OpenGL Reading binary file to vbo in OpenGL

This topic is 2073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently having trouble trying to create and load binary files of model vertex data into the vbo in OpenGL.

 

Here is the code for a simple test that I have done by writing 2D triangle data into a *.bin file:

 

	glm::vec2 points[6] =
	{
		glm::vec2( -0.5, -0.5 ), glm::vec2( 0.5, -0.5 ),
		glm::vec2( 0.5, 0.5 ), glm::vec2( 0.5, 0.5 ),
		glm::vec2( -0.5, 0.5 ), glm::vec2( -0.5, -0.5 )
	};
	
	std::ofstream fs("model.bin", std::ios::out | std::ios::binary | std::ios::app);
        for(int i = 0; i<6; i++)
	{
		fs.write((const char*)&points[i].x, sizeof(points[i].x));
		fs.write((const char*)&points[i].y, sizeof(points[i].y));
	}
	fs.close();

 

and here is the code for copying the data into the vbo:

 

	int length;
	char* data;
	ifstream is;
	is.open("model.bin", ios::binary);
	is.seekg(0, ios::end);
	length = is.tellg();
	is.seekg(0, ios::beg);
	data = new char[length];
	is.read(data, length);
	is.close();
 
	glGenBuffers(1, &vbo);
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);

 

 

All I am getting is a blank screen, instead of a large colored square at the centre of the window. What am I doing wrong?

Edited by gboxentertainment

Share this post


Link to post
Share on other sites
Advertisement

sizeof() returns the size of the expression passed to it; you pass a pointer and so it returns the size of the pointer which is probably 4 of 8 bytes. The problem is that pointers do not carry information about the size of the memory they point to, and furthermore, sizeof() is a compile-time expression but the size of the buffer is run-time dynamic. Pass length instead since that value reflects the size of the data.

Share this post


Link to post
Share on other sites

The significant performance issue is going to be the physical disk IO and not which logical method you use. Just don't make some stupid performance test where you don't take the physical disk IO into account and just keeps reading from any of the multiple levels of cache along the way to come to an irrelevant conclusion.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!