Sign in to follow this  
Icebone1000

vector subscript out of range

Recommended Posts

Icebone1000    1958
Im trying to read an .obj file Since Im suppose to load it on directx, I have to load it on a struct acceptable to directX( vposnormaltex ) The thing is, in DX, each vertex element have a pos, a tex and a normal..But the obj use face indices to "compress" the file.. What Im doing is load each vertex type on vectors: std::vector<FLOAT> Vvertices;//3em3 std::vector<FLOAT> Vnormals;//3em3 std::vector<FLOAT> Vtexcoords;//2em2 std::vector<UINT> Vfaces;//3em3 3em3 3em3 Until now everything is ok, I just have to create may vposnormaltex array verifying what pos/normal/tex I should get on the vectors.. Heres what Im doing:

struct vposnormaltex{

	XMFLOAT3 pos;
	XMFLOAT3 normal;
	XMFLOAT2 tex;

};

...

std::vector<FLOAT> Vvertices;//3em3
std::vector<FLOAT> Vnormals;//3em3
std::vector<FLOAT> Vtexcoords;//2em2
std::vector<UINT> Vfaces;//3em3 3em3 3em3
...



vposnormaltex *pV = new vposnormaltex[Vfaces.size()];


							for( INT i=0; i< Vfaces.size(); i+=3 ){

								
								//read 3 x 3, 
								pV[i/3].pos = XMFLOAT3( Vvertices[Vfaces[i]-1], Vvertices[Vfaces[i]+1-1], Vvertices[Vfaces[i]+2-1] );
								pV[i/3].tex = XMFLOAT2( Vtexcoords[Vfaces[i+1]-1], Vtexcoords[Vfaces[i+1]+1-1] );
								pV[i/3].normal = XMFLOAT3( Vnormals[Vfaces[i+2]-1], Vnormals[Vfaces[i+2]+1-1], Vnormals[Vfaces[i+2]+2-1] );


							}






The obj is triangulated, each face is divided as " 3/3/3 2/2/2 4/4/4 ", but everything is stored as a row on the vectors.. ----EDITED---- Just figured out the i++, it suppose to be i+=3.. But now I have a logic problem, I think it is skipping lines, can someone figure out whats the problem?

Share this post


Link to post
Share on other sites
iMalc    2466
Are you sire that Vfaces.size() is always a mutliple of three? If not...
Vfaces[i+1] is a buffer overrun if i equals Vfaces.size()-1
Vfaces[i+2] is a buffer overrun if i equals Vfaces.size()-2

This looks like a candiate for an assert. I.e.
assert(vfaces.size() % 3 == 0);
You're also overallocating pV by about a factor of three. The highest index you write into is (Vfaces.size()-1)/3 but you allocate the array of Vfaces.size() items.

Why aren't you using a std::vector for pV as well?

Share this post


Link to post
Share on other sites
Icebone1000    1958
Its a triangulated mesh, it have to be multiple of 3, heres the file:
Its acube, generated on maya and exported as .obj.


# This file uses centimeters as units for non-parametric coordinates.

mtllib mycube.mtl
g default
v -2.500000 -2.500000 2.500000
v 2.500000 -2.500000 2.500000
v -2.500000 2.500000 2.500000
v 2.500000 2.500000 2.500000
v -2.500000 2.500000 -2.500000
v 2.500000 2.500000 -2.500000
v -2.500000 -2.500000 -2.500000
v 2.500000 -2.500000 -2.500000
vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
s 1
g pCube1
usemtl initialShadingGroup
f 1/1/1 2/2/2 3/3/3
f 3/3/3 2/2/2 4/4/4
s 2
f 3/3/5 4/4/6 5/5/7
f 5/5/7 4/4/6 6/6/8
s 3
f 5/5/9 6/6/10 7/7/11
f 7/7/11 6/6/10 8/8/12
s 4
f 7/7/13 8/8/14 1/9/15
f 1/9/15 8/8/14 2/10/16
s 5
f 2/2/17 8/11/18 4/4/19
f 4/4/19 8/11/18 6/12/20
s 6
f 7/13/21 1/1/22 5/14/23
f 5/14/23 1/1/22 3/3/24





" but you allocate the array of Vfaces.size() items"
Ah! I see, each 3 elements of Vface is ONE vposnormaltex..so I suppose to allocate sizeof(Vface)/3..

What Im trying to do is read an entire face( witch means 3 elements on the vector Vface) per loop...The face element is used as index on the other vectors( lets say face return `1`, so I go to the first Vvertice, and read 3 vertices(x,y,z))

"Why aren't you using a std::vector for pV as well?" I dont see reason( other than getting over my mistakes ), I have to pass this to direcX.

Share this post


Link to post
Share on other sites
Icebone1000    1958
YAY \o/

I think now its everything ok:


for( INT i=0; i < Vfaces.size(); i+=3 ){

//i = 0-3-6-9(Vface)
//i/3 = 0-1-2-3(pV[])

//f 1/1/1 2/2/2 3/3/3
//f 3/3/3 2/2/2 4/4/4

//v -2.500000 -2.500000 2.500000
//v 2.500000 -2.500000 2.500000

//vt 0.375000 0.000000
//vt 0.625000 0.000000

//vn 0.000000 0.000000 1.000000
//vn 0.000000 0.000000 1.000000

//care here! face [3] '2/x/x' means Vvertice indice 6!:
//1/1/1 = v0 v1 v2 t0 t1 t2 n0 n1 n2
//2/2/2 = v1 v2 v3!!->ERROR MASTER
//2/2/2 = v3 v4 v5->Correct->face '2' * j = 4
//lets say face[10] = 2/x/x
// face '1' should be vertice 1
// face '2' should be veritce 4
// face '3' should be vertice 7
// face '4' should be vertice 10
// face '5' should be vertice 13
// pattern -> ((Vface[i] * 3) - 2)//-1 ->0 based index(so -3)

INT vert_indice = (( Vfaces[i ] )*3)-2-1;
INT tex_indice = (( Vfaces[i+1] )*2)-1-1;//note:texture have just 2 verts
INT normal_indice = (( Vfaces[i+2] )*3)-2-1;

pV[i/3].pos = XMFLOAT3( Vvertices[vert_indice], Vvertices[vert_indice+1], Vvertices[vert_indice+2] );
pV[i/3].tex = XMFLOAT2( Vtexcoords[tex_indice], Vtexcoords[tex_indice+1] );
pV[i/3].normal = XMFLOAT3( Vnormals[normal_indice],Vnormals[normal_indice+1],Vnormals[normal_indice+2] );







Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this