[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Rendering triangle meshes with OpenGL



On Thu, Sep 19, 2002 at 11:52:35PM -0300, Miguel A. Osorio wrote:
> Al Riddoch wrote:
> > 
> > >
> > >       Ok, I took a look at the glDrawElements() and the whole system behind
> > > it (setting up the arrays with glVertexPointer and the like, etc.) Thing
> > > is, if I got it right, glDrawElements uses an array of indeces to the
> > > other arrays (vertex, normal, texture coords) to draw primitives. But
> > > one thing with the ASE files is that there are more texture coordinates
> > > than vertices - in fact, I believe there are as many texture coords as
> > > faces, and not as vertices. So how can I use the texture coordinates
> > > array if it's bigger than the vertex and the normal array?
> > >
> > 
> > There is another call you can use which does not use an array of index,
> > I think its glDrawArrays().
> 
> 
> 	Sure, but I believe that in the case of data read from an ASE file,
> using glDrawElements is a more direct approach. glDrawArrays should do
> fine if your vertex data, for instance, is already ordered nicely to
> represent the triangles, which is not necessarily the case with this
> file format.
> 
> 
> > 
> > There should be the same number of texture coordinates as there are
> > vertices, but I am not sure what to do if there are more. Do the specs
> > for the file format give any clue as to why that might be the case?
> > 
> 
> 	I'm taking a look at this closely, but for now I'm sure there are more
> texture coordinates than vertices, unless there is some redundancy that
> I haven't noticed, but I believe that's not the case.
> 

Some verticies will represent end-points of a line which delineates a
texture - it's a point at which textures meet. Imagine a sphere with a
map of the earth mapped onto it. The verticies in the Atlantic will
have one world position and one texture position. The verticies along
the international date line will have one position, but TWO texture
coordinates. One set running down the left of the map, one set running
down the right of the map.

There is no easy way to fix this since OpenGL expects data to arrive
as one block per vertex, including the colour and the texture position.

The solution is to massage your data before using it. You will need to
duplicate the verticies with >1 texture. In other words, if the
equator had 12 points round it in the above example, you need to
create a 13th point with the left hand edge of the equator on it, and
modify the 12th point so it has the right hand edge only. And then
re-write the triangles so they all work.

The easiest way of doing this would be to create a new file format for
yourself and massage the file you have using Perl.

I'd suggest something in the rough form:

<count of verticies>
<x> <y> <z> [<u> <v> [<r> <g> <b> [<a>]]]
...
<count of triangles>
<a> <b> <c>
...

Where x,y,z are your position tuple. U,v are the optional texture
tuple and r,g,b are an optional colour tuple with an optional alpha
component.

Load the first lot into an array of structures, the second lot into an
array of ints, point the appropriate opengl arrays at the bits of the
structures and away you go.