Parsing a OBJ file for opengl APIs

Hi all,

I exported a 8 vertex cube with a shader texture from a third party package to a wavefront Obj format.
The OBJ file contained a vt list of 42 uv pair coordinates. (see below)

I’m a newbie to opengl and learning texture mapping.
I’m having problems defining the texture array in openlg.

Currently, WIKI, Opengl has a graphic API that handles texture data in a way Carrara 5 OBJ file is not format to output. I’m a general purpose c# and c++ programmer learning graphics software rendering. I don’t want to learn custom UV mapping at this time. I know how to read the Carrara waveFront OBJ file v, vn, and f data. Also, the Material file data. I successfully parsed the obj and mtl files to render a cube with 2 difference material faces. Howerever, I’m not familiar enough to know all the ways opengl API input textures. But the most directed way is to reads in a texture map and overlay colors, in the form of an image map, at specific vertexes, or texCoord*.

So, my problem is translating the Carrara vt array in to something opengl understands. The first reply message is discussing advanced Carrara UV processing. Nevertheless, I appreciate it and will review it for future consideration. But it misses the intend of the original questions. Right now, I only want is to output a Carrara OBJ with a standard library texure and then read it in to opengl for simple rendering.

I did question this question with the follow concerns in the Carrara forum…

So, what is the uv pair in the vt export section parametizing?
Is the uv normalized range over the x, y, z span of the 3 faces vertexes?
The opengl image map is expecting a height, width and a parameter lile color to be specify in the image map.
How do the vt pairs specify the above?

Nevertheless, its really a waveFront import file question and this detial is hard to find.

The OBJ export file

Created By Carrara 5.0

mtllib Cube.Mtl

Number of vertices: 8

v 1 -1 -1.000143
v 1 -0.952445 1.000143
v -0.91973 -0.958356 0.999859
v -1 -0.905544 -0.983571
v -1 0.959717 0.999859
v -1 0.99142 -0.991999
v 1 0.95446 0.991431
v 1 1 -1.000143

Number of normals: 24

vn -0.022166 -0.999754 -0.000459
vn 1 0 0
vn -0.006256 -0.002193 -0.999978
vn 0.003075 -0.999713 0.023767
vn 1 0 0
vn -0.000162 0.004569 0.99999
vn -0.022571 -0.999745 -0.000849
vn 0.002046 0.002353 0.999995
vn -0.998354 -0.041781 0.039292
vn -0.047365 -0.998573 -0.024671
vn -0.999578 -0.021151 0.019891
vn -0.008495 -0.004443 -0.999954
vn -0.999595 -0.020734 0.019498
vn 0.004214 0.000176 0.999991
vn 0.002695 0.99987 0.015914
vn -1 0 0
vn -0.000687 0.999813 0.019324
vn -0.006252 -0.002189 -0.999978
vn -0.000813 0.99981 0.019451
vn 0.002057 0.002342 0.999995
vn 1 0 0
vn -0.004196 0.99973 0.02286
vn 1 0 0
vn -0.004072 0 -0.999992

Number of texture vertices: 42

vt 0.333333 0
vt 0.333333 1
vt 0 0.25
vt 0.333333 0
vt 0.333333 1
vt 0 0.25
vt 0.333333 0
vt 0.333333 1
vt 0 0.25
vt 0.666667 0
vt 0.666667 1
vt 1 0.25
vt 0.666667 0
vt 0.666667 1
vt 1 0.25
vt 0.666667 0
vt 0.666667 1
vt 1 0.25
vt 0.666667 0.25
vt 0.666667 0.25
vt 0.666667 0.25
vt 0.333333 0.25
vt 0.333333 0.25
vt 0.333333 0.25
vt 0.666667 0.5
vt 0.666667 0.5
vt 0.666667 0.5
vt 0.333333 0.5
vt 0.333333 0.5
vt 0.333333 0.5
vt 0.666667 0.75
vt 1 0.5
vt 0.666667 0.75
vt 1 0.5
vt 0.666667 0.75
vt 1 0.5
vt 0.333333 0.75
vt 0 0.5
vt 0.333333 0.75
vt 0 0.5
vt 0.333333 0.75
vt 0 0.5

g Cube
usemtl Texture_1

Number of Polygons: 6

f 1/1/1 2/10/4 3/19/7
f 5/25/13 6/28/16 4/23/11
f 6/29/17 5/27/15 7/31/19
f 8/39/23 7/35/21 2/14/5
f 7/34/20 5/26/14 3/20/8
f 1/9/3 4/24/12 6/30/18
usemtl Texture_0

Number of Polygons: 6

f 3/19/7 4/22/10 1/1/1
f 4/23/11 3/21/9 5/25/13
f 7/31/19 8/37/22 6/29/17
f 2/14/5 1/5/2 8/39/23
f 3/20/8 2/18/6 7/34/20
f 6/30/18 8/42/24 1/9/3

The mtl file

Created By Carrara 5.0

newmtl Texture_0
Ns 1160
d 1
Ni 0.001
illum 2
Ka 0.2 0.2 0.2
Kd 0.988296 0.445319 0.964858
Ks 0.56451 0.56451 0.56451

newmtl Texture_1
Ns 163
d 1
Ni 0.001
illum 2
Ka 0.2 0.2 0.2
Kd 0.972671 0.968765 0.078126
Ks 0.25 0.25 0.25

Is the uv normalized range over the x, y, z span of the 3 faces vertexes?

VT is a texture coordinate on the most recent usemtl image. It is normally in the range 0-1 for both u and v. 0 being the left/bottom of the 2D texture, 1 being the right/top. I am not sure what Carrara does with repeated textures.

OpenGl will interpolate between the UV’s of the 3 vertices of a triangle to give you a UV for a particular texel in the fragment shader that you can use as an index to read the colour from the texture.

Wiki describes it quite well Wavefront .obj file - Wikipedia

Thanks tonyo_au, I think Carrara do span the uv (0 - 1) range over the immediate triangle face… and I think this is customary… but not sure and that was a secondary question to Carrara.

My main concern is: the opengl texture uses a 2D image map… and expecting a height, width and a pixel parameter like color to be mapped to a specify texCoord location.
The problem is the vt values are a 1D array arbitrary length of x,y values and an OBJ file standard image descriptor. I’m unaware of how opengl APIs input vt values for rendering?

The problem is the vt values are a 1D array arbitrary length of x,y values

A face entry “f” has a set of up to 3 values for each vertex of the face. These are the indices into the vertex list “v”, the texture coordinate list “vt” and the normals list “vn”. A face can have any number of vertices but in practice it it either 3 for a triangle or 4 for a quad

I’m unaware of how opengl APIs input vt values for rendering?

Are you using shaders?

If not you use either glMultiTexCoord or gltexcoord2f

this will help for both

Thanks again tonyo_au, I’m using gltexcoord2f, also, a cursorly look at the sighted web page: they are inputing a image file… and I found lots of info on that… but not on inputing vt values. Again, I successfully parsed the obj and mtl files to render a cube with 2 difference material faces. But cannot apply the vt texture array.