Archived

This topic is now archived and is closed to further replies.

FatalXC

Vertex Array Problem

Recommended Posts

FatalXC    122
OK, hopefully somone can point out what I'm doing wrong here. I had been storing each polygons vertices with that polygon, but have now changed it to index into a main vertex array, which saves a huge amount of memory, as many of the vertices were repeated. I then decided to use vertex arrays so i now have my vertex array data stored like this:
    
double*		m_dVertexArray;	// Vertex array

  
When i load the number of vertices in the level from my file, the memory is allocated like so, with 3 doubles for each vertex:
  
// Create array of correct size

m_dVertexArray = new double[iVertexArraySize * 3];
  
But when I try rendering using vertex arrays I just get a complete load of jumbled polygons on the screen. So I tried to do the indexing into the array myself and that works fine using this code:
  
// Loop through the polygon list

for (CurrentPolygon; CurrentPolygon != NULL;)
{
    // Bind the polygons texture

    glBindTexture(GL_TEXTURE_2D, CurrentPolygon->TexID);

    // Draw polygon

    glBegin(GL_POLYGON);

    // Loop through all vertices and draw each one

    for (int i = 0; i < CurrentPolygon->GetVertexCount(); i++)
    {
        // Set texture coordinates

	glTexCoord2d(CurrentPolygon->Vertices[i].tex[0],
                     CurrentPolygon->Vertices[i].tex[1]);

	// Draw vertex i

        glVertex3d(m_dVertexArray[CurrentPolygon->Vertices[i].index],
                   m_dVertexArray[CurrentPolygon->Vertices[i].index + 1],
                   m_dVertexArray[CurrentPolygon->Vertices[i].index + 2]);
					
    }

    // Finished drawing polygon

    glEnd();

    // Move to next polygon in linked list

    CurrentPolygon = CurrentPolygon->GetNextPolygon();
}
  
But when i change the glVertex3d call to:
  
glArrayElement(CurrentPolygon->Vertices[i].index);
  
I get a load of garbage on the screen. The vertex array data is valid otherwise the first rendering code wouldn't work. I set up to use vertex arrays like this:
  
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_DOUBLE, 0, m_dVertexArray);
  
I can't figure out what I'm doing wrong, but it must be pretty obvious to someone with a bit of experience with vertex arrays, so...HELP!! (Sorry for the long post) FatalXC Edited by - FatalXC on June 29, 2001 11:37:08 PM

Share this post


Link to post
Share on other sites
FatalXC    122
Well, after posting the question I immediately got it working .
All i needed was to divide the array index by 3, It wanted the vertex number not the array element number!!!
Well I hope somone else won''t make this mistake now Next comes indexing whole polygons sorted by texture......

I would still like to know what people think of vertex arrays vs display lists, I would think that vertex arrays are faster, because way less coordinates have to be transformed but I''m not sure. What have you found to be fastest?

Thanks,

FatalXC



Share this post


Link to post
Share on other sites
zedzeek    528
u test and see what runs fastest for you. theoretical DL are faster but theory is not always right

http://members.xoom.com/myBollux

Share this post


Link to post
Share on other sites
RipTorn    722
I wouldn't use doubles if I were you. not only do they consume twice the memory, they are _MUCH_ slower to calculate with...

ohh, yeah, btw:
its nice to finally see someone else using the basic variable naming conventions

- you might want to consider using GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN, these are significantly faster. - but you would obviously need to convert your vertice order.

you said '...was to divide the array...' if you can, do NOT use divisions... try and use multiplication as much as possible.

Drawelements will be faster than a display list. If done right. Also, a DL uses a fair bit of extra memory. If you go as far as using VertexArrayRange, a DL won't come close

Edited by - RipTorn on June 30, 2001 7:36:57 AM

Share this post


Link to post
Share on other sites
Krippy2k    134
Well it depends on what you are doing.

Not that this applies to NVidia drivers as far as I know...

For what they are made for a DL cannot be beaten by vertex arrays, not even VAR. The best you can do with VAR is equal the speed of a DL for static environments if you don''t do your own culling. But you will rarely want to use DLs for your environment unless it is relatively small. The reason is that once you create your display list you cannot modify it. Therefore you cannot perform any type of culling on your own and it must process all vertices each frame.

If you combine techniques such as octrees and portal-based HSR with VAR you will blow Display Lists away because you cannot do it with display lists.

If all or most of your polygons are on the screen most of the time, a display list would probably be more efficient.

It also depends on the drivers for your video card, but this is the case for NVidias drivers anyways (information derived not from websites or flamewars but from many many many hours of personally testing them under different circumstances...)

Basically I use both methods in my code. It depends on the nature of the level being imported. I determine how large the level is, how many polygons and how concentrated they are. If it is a simple map with no dynamic objects I create a display list and if it is large with lots of polys and/or dynamic objects I will create my arrays.


Seeya
Krippy

Share this post


Link to post
Share on other sites
FatalXC    122
Riptorn: The division in there was temporary, I can do it beforehand no big deal. Yeah i know floats are half the storage space, but doubles are twice as accurate , so I decided to use doubles in my compiler and file format, but convert it when my engine reads it in, just haven''t done that yet
Yeah making my polygons into triangle strips is on my list of things to do...
MAN!! I sorted my polys by texture and it like doubled my framerate

Just a few wierd bugs to fix and I''ll post some screenshots of my work in progress...

Cheers,

FatalXC

Share this post


Link to post
Share on other sites
FatalXC    122
Weeeeeee!!! I figured how to turn my vertices into triangle strips, I made my index arrays work, and my texture coordinate arrays. And now I''m using floats not doubles.
It has gotten very speedy now, almost 150% better thn using huge numbers of calls to glVertex3d(). One more question though. Currently my rendering loop is drawing each polygon using a single glDrawElements() call. My polygons are sorted by texture. Since I have about 23 textures in use, I was thinking that I could draw all polygons that use each texture with a single call to glDrawElements() using GL_TRIANGLES (Obviously I would have to alter my index array drastically) This would cut down the number of OpenGL calls i make from about 3000 to 23, which is a good thing. But it would mean I would loose all advantages of using triangle strips, but would the fact that I''m making less calls balance this out so it would be overall faster????
And what about those awful flickery lines between polygons, would I get more of those??

Cheers,

FatalXC

Share this post


Link to post
Share on other sites
RipTorn    722
Um, using triangle strips to draw individual triangles (it sounds like this) is not a good idea Triangle strips are mainly used for drawing huge strings of tris'. (I can have up to 100 in one strip ).
If you do use triangles, you may want to consider using compiled vertex arrays, this reduces some of the overhead of re-calculating duplicate verticies...
I haven't found (in my experience) doubles to give too much of an advantage in terms of visual accuracy, but it's your choice if you use them or not.

Ohh yeah - also, remember, if you are using single triangles, although you will be using the same size arrays, your index's are going to be __MUCH__ larger, so it would be important to be using a small data type, (ie, unsigned shorts)... or, if you can (I doubt it though) unsigned bytes...

compiled vertex arrays would likly kill the lines you're taking about, but that may be a hardware issue...

ohh yeah, lol, its nice to see a few more NZ'ers here

Edited by - RipTorn on July 1, 2001 8:00:19 AM

Share this post


Link to post
Share on other sites