Dynamically create meshes in opengl

Started by
3 comments, last by sniper227 8 years, 11 months ago

Hey so I created the code to create multiple meshes in opengl but it's not very dynamic.

I was wondering if there was a way to dynamically create pointer as of now this is how it looks.


CentralObjectManifest com[100];

//CentralObjectManifest is the structure

int obj_tot = 0;
com[obj_tot].vertices.push_back(...);

//.. rest of creation code here..

obj_tot +=1;

now the above works but it's not dynamic because if we get above 100 objects the program would crash. How would I make it dynamic I tried using


vector<CentralObjectManifest> com;

void createObject()
{
com.push_back(CentralObjectManifest());
// insertion code

obj_tot+=1;

}

but It keeps crashing saying the vector is out of range. how would I make this dynamic. Once again Thanks!

Advertisement


vector<CentralObjectManifest> com;

void createObject()
{
com.push_back(CentralObjectManifest());
// insertion code

obj_tot+=1;

}
but It keeps crashing saying the vector is out of range. how would I make this dynamic. Once again Thanks!


You're pushing an empty object into the vector and then trying to work with it.

The correct code would be something like this:


vector<CentralObjectManifest> com;

void createObject()
{
CentralObjectManifest temp;

temp.vertices.push_back(...);
//.. rest of creation code here..

com.push_back(temp);

obj_tot+=1;

}
I'm hoping the global "com" variable is just to make the code snippet smaller and you're actually wrapping it in a class. It would be nice to see all of the code relating to this issue.


vector<CentralObjectManifest> com;

void createObject()
{
com.push_back(CentralObjectManifest());
// insertion code

obj_tot+=1;

}
but It keeps crashing saying the vector is out of range. how would I make this dynamic. Once again Thanks!


You're pushing an empty object into the vector and then trying to work with it.

The correct code would be something like this:


vector<CentralObjectManifest> com;

void createObject()
{
CentralObjectManifest temp;

temp.vertices.push_back(...);
//.. rest of creation code here..

com.push_back(temp);

obj_tot+=1;

}
I'm hoping the global "com" variable is just to make the code snippet smaller and you're actually wrapping it in a class. It would be nice to see all of the code relating to this issue.

I'll try that out first and see if I still get issues. If i understand this correctly would I access the vertices vector like this: com[0].vertices ?

If i understand this correctly would I access the vertices vector like this: com[0].vertices ?


Yes, you can access individual vector elements as an array. However, if you plan to iterate through the vector, iterators are best:

for(auto it = com.begin();it != com.end();++it)
{
    it->vertices...; // Do something with the vertices....
}
That is functionally equivalent to this:


for(int i = 0;i < com.size();++i)
{
    com[i].vertices...; // Do something with the vertices....
}
There are benefits to using the iterator version, but I am less than clear on those benefits.

If i understand this correctly would I access the vertices vector like this: com[0].vertices ?


Yes, you can access individual vector elements as an array. However, if you plan to iterate through the vector, iterators are best:


for(auto it = com.begin();it != com.end();++it)
{
    it->vertices...; // Do something with the vertices....
}
That is functionally equivalent to this:


for(int i = 0;i < com.size();++i)
{
    com[i].vertices...; // Do something with the vertices....
}
There are benefits to using the iterator version, but I am less than clear on those benefits.

Hey so I got it working but I get a weird but everytime I add more than one mesh it messes up the faces and changes the index values. For example the first object would have the index value of 0 because I use color based selection to check and it returns 0 and when I add a second object that should be index 1 I click it and I get 0 for the second and 1 for the first? any ideas

Here's my code to load the model and render it. Note that if i go back to my regular CentralObjectSystem com[100]; pointer instead of the vector it comes out perfect...?


//Load
int COS::LoadTriDAE(string filename, int objects)
{
	string info_t;
	ifstream dae;
	string line;
	int dummy = 0;
	int counter = 0;
	string temp = " ", second = " ", normal = " ";


	//
	CentralObjectManifest temp_obj;

	//
	dae.open(filename, ios::in);

	while (!dae.eof())
	{
		getline(dae, line);
	
		if (line.find("      <mesh>") != line.npos)
		{
			for (int i = 0; i < 2; i++)
			{
				getline(dae, line);
			}
			if (line.find("          <float_array id") != line.npos)
			{
				
				//cout<<"hey";
				while (line != "</float_array>")
				{
					getline(dae, line);

					sscanf_s(line.c_str(), "%f %f %f", &temp_obj.Object.Triangle.xyz[0], &temp_obj.Object.Triangle.xyz[1], &temp_obj.Object.Triangle.xyz[2]);
					temp_obj.Object.Triangle.vertices.push_back(temp_obj.Object.Triangle.xyz[0]);
					temp_obj.Object.Triangle.vertices.push_back(temp_obj.Object.Triangle.xyz[1]);
					temp_obj.Object.Triangle.vertices.push_back(temp_obj.Object.Triangle.xyz[2]);


				}
			}
		}
		if (line.find("        <triangles count=") != line.npos)
		{
			int start = line.find("<p>");
			int end = line.find("</p>");
			start = start + 4;
			end = end + 1;
			int length = end - start;

			string info = line.substr(start, length);

			stringstream ss(info);
			int x;
			while (ss >> x) {
				temp_obj.Object.Triangle.faces.push_back(x);

			}

		}
		if (line.find("          <float_array id") != line.npos)
		{
			cout << dummy << endl;
			//cout << "uv";
			if (dummy == 0)
			{
				while (line != "</float_array>")
				{

					getline(dae, line);
					sscanf_s(line.c_str(), "%f %f %f", &temp_obj.Object.Triangle.norms.normals[0], &temp_obj.Object.Triangle.norms.normals[1], &temp_obj.Object.Triangle.norms.normals[2]);
					temp_obj.Object.Triangle.norms.normal.push_back(temp_obj.Object.Triangle.norms.normals[0]);
					temp_obj.Object.Triangle.norms.normal.push_back(temp_obj.Object.Triangle.norms.normals[1]);
					temp_obj.Object.Triangle.norms.normal.push_back(temp_obj.Object.Triangle.norms.normals[2]);
				}
			}
			if (dummy == 1)
			{
				
				while (line != "</float_array>")
				{
					
					getline(dae, line);

					sscanf_s(line.c_str(), "%f %f", &temp_obj.Object.Triangle.Texture.uv[0], &temp_obj.Object.Triangle.Texture.uv[1]);
					temp_obj.Object.Triangle.Texture.tex.push_back(temp_obj.Object.Triangle.Texture.uv[0]);
					temp_obj.Object.Triangle.Texture.tex.push_back(temp_obj.Object.Triangle.Texture.uv[1]);



				}
			}
			dummy += 1;
		}
		if (line.find("    <image id=") != line.npos)
		{
			int start = line.find("<init_from>");
			int end = line.find("</init_from>");
			start += 18;
			
			int len = end - start;
			 info_t= line.substr(start, len);
			 info_t = ReplaceAll(info_t, "\\", "/");
			 temp_obj.Object.Triangle.Texture.texpath = info_t.c_str();
			// temp_obj.Object.Triangle.Texture.TexID = LoadTGATexture((char *)temp_obj.Object.Triangle.Texture.texpath.c_str());


			 temp_obj.Object.Triangle.Texture.is_textured= true;
		}

	}
	dae.close();
	
	temp_obj.Object.Triangle.draw = true;
	temp_obj.type = MESH;
	temp_obj.Object.frames = 2;
	//cout << "tex path"<<temp_obj.Object.Triangle.Texture.texpath;
	
		
	
	GenerateColors(temp_obj.Object.Triangle.rgb[0], temp_obj.Object.Triangle.rgb[1], temp_obj.Object.Triangle.rgb[2]);
	//temp_obj.eb.rgb[3] = 0.2f;
	
	glGenBuffers(1, &temp_obj.Object.Triangle.vbo);
	glBindBuffer(GL_ARRAY_BUFFER, temp_obj.Object.Triangle.vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*temp_obj.Object.Triangle.vertices.size(), &temp_obj.Object.Triangle.vertices[0], GL_STATIC_DRAW);

	
	glGenBuffers(1, &temp_obj.Object.Triangle.vio);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, temp_obj.Object.Triangle.vio);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*temp_obj.Object.Triangle.faces.size(), &temp_obj.Object.Triangle.faces[0], GL_STATIC_DRAW);
	glEnable(GL_TEXTURE_2D);
	com.push_back(temp_obj);
	
	obj_tot = obj_tot + 1;
	return 0;
}


void CentralObjectSystem::RenderFullScene(int obj)
{

	glPushMatrix();
	//bind VBOS
	glVertexPointer(3, GL_FLOAT, 0, 0);
	glBindBuffer(GL_ARRAY_BUFFER, out_vertex[obj].vbo);
	
	glEnableClientState(GL_VERTEX_ARRAY);
	
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, out_vertex[obj].vio);

	//Texture check only applies if object has texture
	if (out_vertex[obj].has_texture)
	{
		glTexCoordPointer(2, GL_FLOAT, 0, 0);
		glBindBuffer(GL_ARRAY_BUFFER, out_vertex[obj].vto);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
		glColor3d(0.5, 0.5, 1);
		glBindTexture(GL_TEXTURE_2D, out_vertex[obj].TexID[0]);
	}

	if (!out_vertex[obj].has_texture)
			//insert color creation code here
	glTranslatef(2*obj_tot, 2, 0);
	//Code to change object red if selected
	if (out_vertex[obj].sel_col == false)
		glColor3ub(out_vertex[obj].rgb[0], out_vertex[obj].rgb[1], out_vertex[obj].rgb[2]);
	else if (out_vertex[obj].sel_col == true)
		glColor3ub(255, 0, 0);

	//Actual draw call
	glDrawElements(GL_TRIANGLES, out_vertex[obj].indices.size(), GL_UNSIGNED_INT, 0);



	glDisableClientState(GL_VERTEX_ARRAY);
	if (out_vertex[obj].has_texture)
		glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glPopMatrix();

}

//render 

This topic is closed to new replies.

Advertisement