Archived

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

Structures, which are the best to use?

This topic is 5311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I am having a hard time deciding which structures to use in order to have my model data available with as few vertex calls as possible. If you have any suggestions I would really appreciate it. I have included a sample of what I am currently doing which works but is cumbersome. Code samples would also be very helpful.
typedef struct TVertex {
    float x, y, z;
    float r, g, b;
} Vertex;

typedef struct TVertices {
    Vertex vertex[4];
} Vertices;

Vertices **vertices, **colors;
Vertex **normals;

void __fastcall CalcVerts();
void __fastcall CalcNorms();
void __fastcall CalcColors();
void __fastcall CalcNorm(Vertex v1, Vertex v2, Vertex &Normal);
void __fastcall SetVertexColor(float scale, Vertex &Color);

void __fastcall VPanel::CalcVerts()
{
	for(DWORD x = 0; x < granularity - 1; x++)
    {
        float xs = (float)x / granularity;
        float gs = 1.0 / granularity;

		for(DWORD y = 0; y < granularity - 1; y++)
        {
            float ys = (float)y / granularity;

            for (int i = 0; i < 4; i++)
            {
                switch (i) {
                    case 0 :
                        vertices[x][y].vertex[i].x = xs;
                        vertices[x][y].vertex[i].y = ys;
                        vertices[x][y].vertex[i].z = smoords[y][x] / maxS;
                        break;
                    case 1 :
                        vertices[x][y].vertex[i].x = xs + gs;
                        vertices[x][y].vertex[i].y = ys;
                        vertices[x][y].vertex[i].z = smoords[y][x + 1] / maxS;
                        break;
                    case 2 :
                        vertices[x][y].vertex[i].x = xs + gs;
                        vertices[x][y].vertex[i].y = ys + gs;
                        vertices[x][y].vertex[i].z = smoords[y + 1][x + 1] / maxS;
                        break;
                    case 3 :
                        vertices[x][y].vertex[i].x = xs;
                        vertices[x][y].vertex[i].y = ys + gs;
                        vertices[x][y].vertex[i].z = smoords[y + 1][x] / maxS;
                        break;
                };
            }
        }
    }
}

	for (DWORD dx = 0; dx < granularity - 1.0; dx++)
    {
	   	for (DWORD dy = 0; dy < granularity - 1.0; dy++)
        {
            float Verts[4][3] = {
            {vertices[dx][dy].vertex[0].x, vertices[dx][dy].vertex[0].y, vertices[dx][dy].vertex[0].z},
            {vertices[dx][dy].vertex[1].x, vertices[dx][dy].vertex[1].y, vertices[dx][dy].vertex[1].z},
            {vertices[dx][dy].vertex[2].x, vertices[dx][dy].vertex[2].y, vertices[dx][dy].vertex[2].z},
            {vertices[dx][dy].vertex[3].x, vertices[dx][dy].vertex[3].y, vertices[dx][dy].vertex[3].z}};

            float Colors[4][3] = {
            {colors[dx][dy].vertex[0].r, colors[dx][dy].vertex[0].g, colors[dx][dy].vertex[0].b},
            {colors[dx][dy].vertex[1].r, colors[dx][dy].vertex[1].g, colors[dx][dy].vertex[1].b},
            {colors[dx][dy].vertex[2].r, colors[dx][dy].vertex[2].g, colors[dx][dy].vertex[2].b},
            {colors[dx][dy].vertex[3].r, colors[dx][dy].vertex[3].g, colors[dx][dy].vertex[3].b}};

            glBegin(FillMode);
            for (int i = 0; i < 4; i++)
            {
                glNormal3f(normals[dx][dy].x, normals[dx][dy].y, normals[dx][dy].z);
                glColor3fv(Colors[i]);
                glVertex3fv(Verts[i]);
            }
            glEnd();
        }
    }
Thanks, HendrixStart

Share this post


Link to post
Share on other sites
My vector struct

template <class T>
struct Vector4{
// PROPETIERS

union
{
T data[4];
struct
{
T x,y,z,w;
};
struct
{
T r,g,b,a;
};
};
// OPERATORS

Vector4 operator += (Vector4 &_v);
Vector4 operator -= (Vector4 &_v);
Vector4 operator *= (T _k);
Vector4 operator /= (T _k);
// CONSTRUCTORS

Vector4();
Vector4(T *_p);
Vector4(T _x, T _y, T _z, T _w=1.0);
Vector4(Vector4 &_v);
// METHODS

T Length();
Vector4 Normalize ();
void Unpack(T &_x, T &_y, T &_z, T &_w);
// FRIENDS

friend Vector4 operator + (const Vector4 &_v1,const Vector4 &_v2);
friend Vector4 operator - (const Vector4 &_v1,const Vector4 &_v2);
friend Vector4 operator / (const Vector4 &_v,T _k);
friend Vector4 operator - (const Vector4 &_v1,const Vector4 &_v2);
friend T operator * (const Vector4 &_v1,const Vector4 &_v2);
friend Vector4 operator * (const Vector4 &_v,T _f);
};

I hold vertexs, colors, ... in arrays.
And use glDrawArrays.





[edited by - cifko on June 3, 2003 1:47:43 PM]

[edited by - cifko on June 3, 2003 1:48:13 PM]

Share this post


Link to post
Share on other sites
The best format for performance is an array in the form of
VERTEX | NORMAL | COLOR | TEXTURE | VERTEX

or every component (that is a float/double array itself) in a separate array

VERTEX | VERTEX ...
NORMAL | NORMAL ...

Then use VertexArrays to pass the geometry and the fastest geometry is TRIANGLE_STRIP

In the first case you have to specify the stride (in byte) to ''jump'' the components (normal, color and tex when you pass vertex)

Hmmm...why you use fastcall for functions(void) with hundreds of line code ??? Try to compute your geometry ''off-line'' also with different structures and then ''compile'' it in float-arrays.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by blizzard999
Then use VertexArrays to pass the geometry and the fastest geometry is TRIANGLE_STRIP




Fastest geometry is geometry not drawn not tri-strips

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by blizzard999
Then use VertexArrays to pass the geometry and the fastest geometry is TRIANGLE_STRIP




Fastest geometry is geometry not drawn not tri-strips



Right !

Share this post


Link to post
Share on other sites