Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: normal arrays crashes

  1. #1
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    Pori, Finland
    Posts
    191

    normal arrays crashes

    I use compiled vertext arrays for rendering my models, but when I use normal arrays, program crashes.

    Sample 1 that works:
    Code :
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     
      glVertexPointer(3, GL_FLOAT, 0, VertPosArray);
      glTexCoordPointer(2, GL_FLOAT, 0, VertTexCoordArray);
     
      glLockArraysEXT(0, lNumVerts);
     
      //render models using glDrawArrays()
     
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
     
      glUnlockArraysEXT();
    Sample 2 that crashed my program:
    Code :
      glEnableClientState(GL_VERTEX_ARRAY);
      glEnableClientState(GL_TEXTURE_COORD_ARRAY);
      glEnableClientState(GL_NORMAL_ARRAY);
     
      glVertexPointer(3, GL_FLOAT, 0, VertPosArray);
      glTexCoordPointer(2, GL_FLOAT, 0, VertTexCoordArray);
      glNormalPointer(3, GL_FLOAT, VertNormalArray);
     
      glLockArraysEXT(0, lNumVerts);
     
      //render models using glDrawArrays()
     
      glDisableClientState(GL_NORMAL_ARRAY);
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      glDisableClientState(GL_VERTEX_ARRAY);
     
      glUnlockArraysEXT();
    My arrays looks like this:
    t_Vec3f *VertPosArray;
    t_Vec2f *VertTexCoordArray;
    t_Vec3f *VertNormalArray;

    and I allocate the memory for each when I load the model. All the arrays are same sized.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: normal arrays crashes

    Your call to glNormalPointer is wrong. The first parameter is the data type, the second is the stride, and third is the pointer.

  3. #3
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    Pori, Finland
    Posts
    191

    Re: normal arrays crashes

    Oh yes, thanks. I really should see those prototypes before I use them

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    Pori, Finland
    Posts
    191

    Re: normal arrays crashes

    Something else come up:
    The normals are somehow interpreted wrong, lighting is not looking right.

    What does the parameter 'stride' do?
    When I use glNormalPointer(GL_FLOAT, 0, VertNormalArray);, it crashes, I use the same stride value for other arrays and for them it works fine.
    When I use glNormalPointer(GL_FLOAT, 1, VertNormalArray);, it doesn't crash, but lighting is NOT ok.

    I'm sure that the normal values are ok.

    [This message has been edited by blender (edited 05-12-2002).]

  5. #5
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: normal arrays crashes

    The stride is the distance in bytes from the start of one entry (normal/vertex/color, dependeing on what the array contains) to the start of the next entry.

    Say you have a vertex array, containing three components, stored as float, like this.
    Code :
    GLfloat myArray[] = {x0, y0, z0, x1, y1, z1, x2, y2, z2, ...};
    The distance in byte between the start of two consecutive vertices is 3*sizeof(float), which is usually 12 bytesm and the stride is therefore 12.

    Now, say you want to, for some reason, pad each vertex with an extra float. You then get this vertex array,
    Code :
    GLfloat myArray[] = {x0, y0, z0, pad0, x1, y1, z1, pad1, x2, y2, z2, pad2...};
    The stride is now 4*sizeof(float), or usually 16 bytes.

    A stride of zero, as in your code, is a special case, and means the entries are tightly packed together, i.e. there is no "empty" space between two vertices. In my first example, you can set stride to either 12 (the actual stride), or zero (the special case), since there's no space between two vertices. In the second example, you must set stirde to 16, cause they are no longer tightly packed together.

    Most certainly your problem has to do with you not allocating enough memory for your normal array, and OpenGL tries to poke outside the allocated buffer. Why stride=1 works, but not stride=0, well, I leave that one as an excercise to you.

  6. #6
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    Pori, Finland
    Posts
    191

    Re: normal arrays crashes

    Most certainly your problem has to do with you not allocating enough memory for your normal array, and OpenGL tries to poke outside the allocated buffer.
    Certainly not. Here's a piece of code:

    Code :
      VertPosArray       = new t_Vec3f[lNumVerts];
      VertTexCoordArray  = new t_Vec2f[lNumVerts];
      VertNormalArray    = new t_Vec3f[lNumVerts];
     
      // Check for errors
     
      fread(VertPosArray, sizeof(t_Vec3f), lNumVerts, f);
      fread(VertTexCoordArray, sizeof(t_Vec2f), lNumVerts, f);
      fread(VertNormalArray, sizeof(t_Vec3f), lNumVerts, f);
    So, I'm pretty sure there are enough memory allocated.

  7. #7
    Senior Member Regular Contributor
    Join Date
    Jul 2001
    Posts
    442

    Re: normal arrays crashes

    Often if you are performing transforms that require scale, your normals will be scaled also, leaving them incorrect. This is nothing to do with vertex buffers. You should either ensure your normals aren't scaled manually, or use lEnable ( GL_RESCALE_NORMAL ). This will slow down your T&L though, so try and remove the scaling if you can.

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: normal arrays crashes

    So, I'm pretty sure there are enough memory allocated.
    I can only see that you are allocating lNumVerts number of normals, but I don't see any proof that you aren't telling OpenGL to draw more than lNumVerts vertices. So you may still be poking outide allocated memory, becuse you either try to draw too many vertices, or your index array has a few indices that are too large (larger than lNumVerts - 1 that is).

    Currupt lighting can also be caused by incorrect normals. In other words, your code to calculate normals is incorrect, assuming you don't load them from file, or anything like that.

  9. #9
    Senior Member Regular Contributor
    Join Date
    Mar 2002
    Location
    Pori, Finland
    Posts
    191

    Re: normal arrays crashes

    Firstly, I'm not using index arrays and when I simply tried:

    glDrawArrays(GL_TRIANGLES, 0, lNumVerts);

    the results were same.

    Secondly, I'm sure about the normals, they are loaded from a model file.

    Thirdly, when I rendered in the 'old fashion'
    way with exactly the same arrays the lighting was ok.

  10. #10
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: normal arrays crashes

    OK, I think we got to the stage where we need to see some real code. If the code isn't too long, post it here. If it is, you can mail it to me, and I have a look at it. You find my address in my profile.

    I can see you are not poking outside the arrays, and the normals seems to be correct since it works in the 'old fashion' way. I take that to be glVertex/glNormal by the way .

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with Vertex/Normal Arrays
    By MightyLampshade in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 12-15-2010, 08:54 PM
  2. vertex buffer arrays, one normal per polygon?
    By bensmith87 in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 04-09-2008, 05:21 PM
  3. Is there such a thing as 'Normal Arrays'?
    By esuvs in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 02-13-2007, 04:42 AM
  4. vertex/texcoord/index/normal arrays
    By Structural in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 12-11-2002, 04:46 AM
  5. vertex arrays: one normal per point?
    By grady in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 10-05-2000, 06:12 AM

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