Results 1 to 9 of 9

Thread: my vertex arrays code! what is wrong here?

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2000
    Posts
    8

    my vertex arrays code! what is wrong here?

    hi.. i asked about vertex arrays a day ago, now i can see sum polys on the screen, but its all messed up! im putting my main vertex array code here.. can anyone help me out to find whats wrong? i guess its my indices calculation, but right now i have no idea how to fix! anyone have any idea?
    thanks in advance!

    heres the code:


    float *VertexArray;
    float *IndicesArray;
    int mapWidth=256;
    int mapHeight=256;

    void setup()
    {
    // vertex array: size of map * 3 (3 elements makes a vertex: x, y , z)
    VertexArray = new float [mapWidth * mapHeight * 3];

    // indices array: size of map * 4 (im drawing quads, so its 4 indexes to 4
    vertices that makes a quad, right?)
    IndexArray = new unsigned int [mapWidth * mapHeight * 4];
    }


    /* generate terrain's vertices&indices.. get each pixel and calculate the
    height at that point and save it in the VertexArray */

    void generate_landscape()
    {
    int index_array=0;

    //
    // Generate terrain's vertices
    //
    for(int tileZ = 0; tileZ < mapHeight; tileZ++)
    {
    for(int tileX = 0; tileX < mapWidth; tileX++)
    {
    VertexArray[index_array+0] = tileX;
    VertexArray[index_array+1] = GetHeight(tileX, tileZ);
    VertexArray[index_array+2] = tileZ;

    index_array+=3;
    }
    }

    //
    // calculate the vertex indices so can i can use glDrawElements to draw the
    terrain!
    //

    int index_indices=0;
    for(int tileZ = 0; tileZ < mapHeight; tileZ++)
    {
    for(int tileX = 0; tileX < mapWidth; tileX++)
    {
    IndexArray[index_indices+0] = (tileZ * mapWidth) + tileX;
    IndexArray[index_indices+1] = ((tileZ+1) * mapWidth) + tileX;
    IndexArray[index_indices+2] = ((tileZ+1) * mapWidth) + (tileX+1);
    IndexArray[index_indices+3] = (tileZ * mapWidth) + (tileX+1);

    index_indices+=4;
    }
    }
    }


    /* render landscape using vertex arrays & glDrawElements() */

    void render_landscape()
    {
    // enable vertex arrays
    glEnableClientState(GL_VERTEX_ARRAY);

    // feed opengl the color, vertices array & indices array
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    glVertexPointer(3, GL_FLOAT, 0, Landscape.VertexArray);
    glDrawElements(GL_QUADS, Landscape.mapWidth*Landscape.mapHeight*4,
    GL_UNSIGNED_INT, Landscape.IndexArray);

    // disable
    glDisableClientState(GL_VERTEX_ARRAY);
    }

  2. #2
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: my vertex arrays code! what is wrong here?

    3rd parameter of glVertexPointer should be 12 I believe not 0. It's 12 bytes from the first X coord to the second in the array. (3 x 4byte floats)

    Nutty

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2000
    Posts
    26

    Re: my vertex arrays code! what is wrong here?

    Originally posted by Nutty:
    3rd parameter of glVertexPointer should be 12 I believe not 0. It's 12 bytes from the first X coord to the second in the array. (3 x 4byte floats)

    Nutty
    No, that is not true - the actual one was right.

    [CS]
    -<[CS]>-

  4. #4
    Member Contributor
    Join Date
    Aug 2000
    Posts
    68

    Re: my vertex arrays code! what is wrong here?

    Just a thought but could it be that that coords for the vertices are not stored properly. I ran into this problem once where faces were rendering nut incorrectly. I discouvered that the vertex array was actuall storing the first coords of the first vertex of each face in a block at the start of the file so instead of x1,y1,z1,x2,y2... I had x1,x2,x3,....
    As I said thught its just a thought

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: my vertex arrays code! what is wrong here?

    CS, I am a bit puzzled by this. Reading the Red Book seems to indicate that it should be zero. However I have an application which uses the following structure
    Code :
    typedef struct {
     
    	hlGVector3	v;				//The vertex position.
     
    	hlGVector3	n;				//The vertex normal.
     
    	hlGTVector	t;				//The vertex texture coords.
     
    }hlGVertex;
    And I dynamically allocate an array of them.
    If the size of the actual vertices is ignored, and it's only the size in bytes between them, then surely I would use 20 for my stride. (12 bytes for the normal, (3 floats) and 8 bytes for the coords (2 floats) ) This does not work. I have to use 32. (It definitly works with 32, as used by glNormalPointer and glTexCoordPointer.

    32 bytes is the stride between the first vertex X coord and the 2nd vertex X coord. So sureley using the same prinicple on an array of floats should yield the value of 12??

    Or am I being stupid here and overlooking something?..

    Or is it a bug in my driver. I'm using Detonator 6.47 on a geforce 256.

    Nutty

  6. #6
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: my vertex arrays code! what is wrong here?

    Hmmm.. Got a flat undulating mesh being rendered with glDrawElements, and the use of glVertexPointer, and glColorPointer.

    Both vertices and colors are arrays of 3 float component Vertex structures. 12 bytes in size.

    I get no difference in visuals or performance, when either 12 or 0 is used for stride on both pointer functions.

    Someone please tell me what is going on here!

    Nutty

  7. #7
    Member Contributor
    Join Date
    Feb 2000
    Location
    Brisbane,QLD, Australia
    Posts
    99

    Re: my vertex arrays code! what is wrong here?

    This is an oldy but the reason u have no difference with 12 bytes or 0 is that using 0 tells openGL to assume the vertices are tightly packed in which they are in your particular case.

    If you were to use

    float x,y,z,pad then you would need to use a stride of 16.

    btw a note for performance align to a 16byte boundry by using a padder in your vertex data.

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Mar 2001
    Posts
    3,574

    Re: my vertex arrays code! what is wrong here?

    How about a good old-fashioned glEnable(GL_VERTEX_ARRAY)?

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: my vertex arrays code! what is wrong here?

    Man oh man, this thread is old....

    You reckon a structure of 16 bytes, will render faster than if I use 12 byte structures?

    Hmmmm..

    I'll have to test it.

    Nutty

Similar Threads

  1. arrays of arrays as vertex output/fragment input
    By tsojtsoj in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 05-17-2018, 08:02 PM
  2. Vertex Arrays, interleaved arrays, mouseMove func
    By Nibirue in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-13-2012, 11:17 AM
  3. Big problem show arrays with vertex arrays
    By Rondey in forum OpenGL: Basic Coding
    Replies: 13
    Last Post: 03-15-2009, 10:09 AM
  4. Vertex Arrays: Interleaved or seperated arrays?
    By Jan in forum OpenGL: Advanced Coding
    Replies: 16
    Last Post: 06-21-2005, 05:25 AM
  5. wrong lighting with Vertex Arrays
    By steve_j in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 03-24-2005, 06:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean