PDA

View Full Version : Read Obj



aje1234l
11-21-2015, 03:30 PM
Hello, I have wrote a loader that reads vertices, textures and normals from a txt file. I export an obj from blender, i open the obj file with wordpad and copy-paste the data in my own loader. Vertices and textures are rendered as they should be, but normals not. Is something i did wrong?

Here is the loader.

//points
fscanf(file, "%d", &(o->nPoints));
for (i=1;i<=o->nPoints;i++)
{
fscanf(file, "%f", &(o->points[i].x));
fscanf(file, "%f", &(o->points[i].y));
fscanf(file, "%f", &(o->points[i].z));
}

//textures
fscanf(file, "%d", &(o->nTextures));
for (i=0;i<o->nTextures;i++)
{
fscanf(file, "%f", &(o->uv[i].u));
fscanf(file, "%f", &(o->uv[i].v));
}

//normals
fscanf(file, "%d", &(o->nNormals));
for (i=0;i<o->nNormals;i++)
{
fscanf(file, "%f", &(o->norm[i].x));
fscanf(file, "%f", &(o->norm[i].y));
fscanf(file, "%f", &(o->norm[i].z));
}


//planes
fscanf(file, "%d", &(o->nPlanes));
for (i=0;i<o->nPlanes;i++)
{
fscanf(file, "%d/", &(o->planes[i].p[0]));
fscanf(file, "%d/", &(o->planes[i].textures[0]));
fscanf(file, "%d", &(o->planes[i].normals[0]));

fscanf(file, "%d/", &(o->planes[i].p[1]));
fscanf(file, "%d/", &(o->planes[i].textures[1]));
fscanf(file, "%d", &(o->planes[i].normals[1]));

fscanf(file, "%d/", &(o->planes[i].p[2]));
fscanf(file, "%d/", &(o->planes[i].textures[2]));
fscanf(file, "%d", &(o->planes[i].normals[2]));
}

Here is the begin- end function

glBegin(GL_TRIANGLES);
for (i=0; i<o.nPlanes; i++){
for (j=0; j<3; j++){
glNormal3f(o.norm[o.planes[i].normals[j]].x,
o.norm[o.planes[i].normals[j]].y,
o.norm[o.planes[i].normals[j]].z);

glTexCoord2f(o.uv[o.planes[i].textures[j]].u,
o.uv[o.planes[i].textures[j]].v);

glVertex3f(o.points[o.planes[i].p[j]].x,
o.points[o.planes[i].p[j]].y,
o.points[o.planes[i].p[j]].z);
}
}
glEnd();

Here is the data

44
10.410240 10.054321 0.497755
10.427345 10.026627 0.011177
10.305275 154.757858 -0.001859
-0.599998 100.000000 0.000000
-0.599998 100.000000 0.400000
-0.599998 157.000000 0.000000
-0.599998 157.000000 0.400000
0.000000 100.000000 0.000000
0.000000 100.000000 0.400000
0.000000 157.000000 0.000000
0.000000 157.000000 0.400000
8.000008 162.000000 0.000000
8.000008 162.000000 0.500000
8.000008 164.000000 0.000000
8.000008 164.000000 0.500000
182.000000 162.000000 0.000000
182.000000 162.000000 0.500000
182.000000 164.000000 0.000000
182.000000 164.000000 0.500000
7.000000 154.752014 0.000000
7.000000 157.000000 0.000000
7.000000 157.000000 0.500000
175.000000 155.000000 0.000000
175.000000 155.000000 0.500000
175.000000 157.000000 0.000000
175.000000 157.000000 0.500000
7.000000 10.000000 0.000000
6.968704 10.064026 0.456224
6.931163 154.710632 0.340345
10.244026 154.718628 0.390144
36.999996 10.000000 0.000000
36.999996 7.000000 0.000000
7.000000 7.000000 0.000000
7.000000 9.954834 0.000000
36.999985 7.000000 0.500000
6.999992 7.000000 0.500000
7.000000 10.035126 0.503821
7.006104 7.001312 0.505755
36.998573 7.001312 0.505755
7.006104 10.034042 0.505755
36.998573 10.034042 0.505755
6.996253 154.778564 0.622399
7.007631 154.779800 -0.009233
7.696869 153.330002 0.148924

89
0.996269 0.127661
0.000000 0.762649
-0.000000 0.000000
8.875000 0.000000
8.875175 0.764008
-37.625000 0.762740
-37.625000 -0.000000
1.000000 0.763865
1.000000 0.000000
-47.625000 0.510476
-47.625000 0.000000
1.000000 0.510475
-46.500000 0.000000
1.000000 0.511508
-46.500000 0.511508
1.000049 0.251500
-22.247950 0.506346
-22.244881 0.248422
0.997949 -0.001578
0.000000 0.755096
1.000000 0.997001
0.000000 1.000000
1.000000 0.755096
-7.500000 0.764267
1.000000 1.000000
-7.500000 1.000000
1.000000 0.764267
1.000591 1.001097
0.000591 0.996709
1.000591 0.763756
0.000591 0.763756
-6.875000 0.998331
1.000000 0.765859
-6.875000 0.765859
1.000000 0.767026
-32.875000 1.000000
0.000000 0.762678
36.500000 0.998674
-0.020999 0.988693
-32.875000 0.767026
0.000000 0.764713
36.500000 0.764713
0.000000 0.760689
1.000000 0.994105
1.000000 0.760689
1.000000 0.764794
-45.125000 0.999585
-45.125000 0.764794
1.000000 0.758495
1.000000 0.999598
-45.125000 1.000000
-45.125000 0.758495
1.000000 0.765818
1.000000 0.998568
0.000000 0.765818
0.000000 0.763043
0.000000 0.998109
1.000000 0.763043
0.000000 0.764102
1.000000 0.764102
0.997077 0.765388
0.999723 1.000179
-41.500000 0.999451
-41.500000 0.765388
-41.499722 0.763173
-41.499722 1.000000
1.000000 0.763174
-0.000000 0.763220
-34.750000 0.764932
-34.750000 1.000000
1.000000 0.771795
1.000000 0.768068
1.000000 0.996754
0.000000 0.768068
0.997354 0.772065
1.000000 0.229377
0.999940 0.229342
1.000000 0.056983
0.984076 0.000308
0.999143 0.000000
0.999143 0.019806
0.992320 0.019806
0.998337 0.039554
0.984076 0.039554
0.994694 0.127661
0.994694 0.146701
0.984076 0.128027
0.984076 0.109019
0.996269 0.108666




28
-0.000200 -0.001300 1.000000
-0.000100 0.000000 1.000000
-0.012100 0.000800 0.999900
-0.015000 0.000700 0.999900
0.000000 0.055000 0.998500
0.000700 0.000000 1.000000
0.000000 0.000000 1.000000
-1.000000 0.000000 -0.000000
0.000000 -1.000000 0.002600
0.000000 -1.000000 0.000000
1.000000 0.000000 0.002800
-0.001500 0.997700 -0.067200
0.000000 0.987500 -0.157400
0.988000 0.000800 0.154500
-0.997700 0.000000 -0.068400
0.999400 0.001200 0.035100
-0.980100 -0.000400 -0.198300
-0.999800 -0.003400 -0.018000
-1.000000 0.001700 0.000000
0.000000 1.000000 0.000000
0.001300 -1.000000 -0.001900
0.001300 -1.000000 0.000000
1.000000 0.000000 0.000000
0.000000 0.872600 0.488500
0.007600 -0.998300 0.057100
-0.004500 -0.990300 0.138700
-0.004200 0.992800 0.119900
-0.001700 0.995100 0.099300



51
37/1/1 36/2/1 39/3/1
41/4/2 37/1/2 39/3/2
28/5/3 1/6/3 29/7/3
1/6/4 30/8/4 29/7/4
22/9/5 42/10/5 26/11/5
26/11/6 42/10/6 24/8/6
13/12/7 19/13/7 15/14/7
13/12/7 17/8/7 19/13/7
9/15/7 11/16/7 7/17/7
9/15/7 7/17/7 5/18/7
36/19/8 34/20/8 33/21/8
36/19/8 37/22/8 34/20/8
39/23/9 33/24/9 32/25/9
39/23/10 36/26/10 33/24/10
32/27/11 31/28/11 39/29/11
39/29/11 31/28/11 41/30/11
31/24/12 34/31/12 41/32/12
41/32/13 34/31/13 37/33/13
3/24/14 30/34/14 2/35/14
28/36/15 20/37/15 27/38/15
30/34/16 1/39/16 2/35/16
28/40/17 29/41/17 20/37/17
42/42/18 21/43/18 43/21/18
42/42/19 22/44/19 21/43/19
26/45/20 25/24/20 21/46/20
26/45/20 21/46/20 22/47/20
42/48/21 43/49/21 23/50/21
42/48/22 23/50/22 24/51/22
24/52/23 23/53/23 25/21/23
24/52/23 25/21/23 26/54/23
13/55/8 14/24/8 12/56/8
13/55/8 15/57/8 14/24/8
19/58/23 17/59/23 16/53/23
19/60/20 18/61/20 14/62/20
19/60/20 14/62/20 15/63/20
17/64/10 12/53/10 16/65/10
17/64/10 13/66/10 12/53/10
9/7/10 4/21/10 8/24/10
9/7/10 5/67/10 4/21/10
9/68/23 8/69/23 10/24/23
9/68/23 10/24/23 11/70/23
11/71/20 10/72/20 6/21/20
11/71/20 6/21/20 7/73/20
5/74/8 6/69/8 4/24/8
5/74/8 7/68/8 6/69/8
18/21/23 19/58/23 16/53/23
37/75/24 40/76/24 41/77/24
2/78/25 1/79/25 27/80/25
1/81/26 28/82/26 27/83/26
29/84/27 30/85/27 20/86/27
30/87/28 3/88/28 20/89/28

GClements
11-21-2015, 03:47 PM
for (i=1;i<=o->nPoints;i++)

for (i=0;i<o->nTextures;i++)

for (i=0;i<o->nNormals;i++)


OBJ uses 1-based indices throughout. You appear to be using 1-based indices for positions but 0-based indices for texture coordinates and normals.

aje1234l
11-22-2015, 01:34 AM
Its not woking otherwise......

Carmine
11-22-2015, 05:56 PM
Its not woking otherwise...... Need more info. Can U post pictures of object in Blender and as your code renders it?

Also, did you consider using an obj loader that already exists?
I found 'tinyobj' to be pretty easy to work with.