OpenGL drawing multiple objects

Started by
2 comments, last by BitMaster 9 years, 6 months ago

I'm trying to make my opengl model manager with assimp.

Here's the calls which I use to create my vertex "buffer"


GLuint IGraphicsManager::CreateVertexBuffer( void *srcData ,unsigned int size)
{
	GLuint buffer;
	glGenBuffers(1,&buffer);
	glBindBuffer(GL_ARRAY_BUFFER,buffer);
	glBufferData(GL_ARRAY_BUFFER,size,srcData,GL_STATIC_DRAW);
	return buffer;
}

GLuint IGraphicsManager::CreateIndexBuffer( void *srcData,unsigned int size )
{
	GLuint buffer;
	glGenBuffers(1,&buffer);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,buffer);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,size,srcData,GL_STATIC_DRAW);
	return buffer;
}

Here's how I use them


	std::vector<StaticVertex> m_verts;
	std::vector<unsigned int> m_indices;
	if (!pModel->LoadMesh("sphere.3ds",m_verts,m_indices))exit(1);
	m_modelBuf = pGraphics->CreateVertexBuffer(&m_verts[0],sizeof(StaticVertex)*m_verts.size());
	m_indexBuf = pGraphics->CreateIndexBuffer(&m_indices[0],sizeof(unsigned int)*numIndices);
	numIndices = m_indices.size();


	std::vector<StaticVertex> m_roomVerts;
	std::vector<unsigned int> m_roomIndices;
	if(!pModel->LoadMesh("room.3ds",m_roomVerts,m_roomIndices))exit(1);
	m_roomBuf = pGraphics->CreateVertexBuffer(&m_roomVerts[0],sizeof(StaticVertex)*m_roomVerts.size());
	m_indexBuf = pGraphics->CreateIndexBuffer(&m_roomIndices[0],sizeof(unsigned int)*numRoomIndices);

the LoadMesh is something that I have already tested and it's working well

I use the following code to draw them


	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER,m_roomBuf);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_roomIndex);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(StaticVertex),0);
	glDrawElements(GL_TRIANGLES,numRoomIndices,GL_UNSIGNED_INT,(void*)0);

	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER,m_modelBuf);
	glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(StaticVertex),0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_indexBuf);
	glDrawElements(GL_TRIANGLES,numIndices,GL_UNSIGNED_INT,0);

Then when I come to the second glDrawElements call, the app crashed with error 0xc000005

What's that matter with them? I just can't find any memory access violation stuff to prove this exit code.

By the way, the vertex array object is something that really confuses me. Any tutorial that simply aims to draw multiple objects?

Advertisement

In your "How I use them", you're passing *numIndicies into an openGl function. Then you're setting numIndicies = me_indicies.size(). I suspect this is probably your problem.

What variable type is numIndicies?

- Eck

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

In your "How I use them", you're passing *numIndicies into an openGl function. Then you're setting numIndicies = me_indicies.size(). I suspect this is probably your problem.

What variable type is numIndicies?

- Eck

and i was passing 0 instead of (void*)0 in my glVertexAttribPointer... thx :)

That is not a problem. The literal 0 is implicitly convertible into the null-pointer of any type in both C and C++. Although if you have access to C++11 you should be using nullptr instead.

This topic is closed to new replies.

Advertisement