Jump to content

  • Log In with Google      Sign In   
  • Create Account

OpenGL Terrain Grid Creation, Triangle Strips indexing issue


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 mynameisnafe   Members   -  Reputation: 252

Like
0Likes
Like

Posted 23 November 2012 - 06:52 AM

Okay, I feel like an idiot, I have an assignment due in some 96 hours, and I've not yet got to the interesting bit.

Heres the deal: I'm trying to create an array of vertices, a grid, so that I can edit the y value in GLSL (later, don't worry about that now).

the trouble I'm having, is indexing my grid into triangle strips. Getting all the info into a VAO of VBOs is fine, and creating the vertices array i fine, just the Triangle strip indexing is letting me down.. please help me find my error.. its probably a semantic as opposed to syntactic.

I'm getting an access violation.. apparently I'm reading -1008 indices too far?

PS: A GUVector4 is what you think it is. All verts are in the range 0-1.

PPS: Slack I know, but we're working on a vert per pixel basis, mainly for simplicity in setting up tex coords.

PPPS: #define TERRAIN_L_XZ 1024

int   index, x, z;
float  posX, posZ;

//the number of vertices we will need for our terrain mesh
m_verticesCount =   TERRAIN_L_XZ	   *   TERRAIN_L_XZ ;

//the number of indices required for triangle strips
m_indicesCount  = ( TERRAIN_L_XZ - 1 ) * ( TERRAIN_L_XZ * 2 ) ;

//create our arrays
vertices = new GUVector4[m_verticesCount];
texCoords = new GUVector2[m_verticesCount];
faceIndex = new GLuint[m_indicesCount];


//now to fill our arrays
// here we use a nested for loop and a seperate index to create a 1D arrays from 2D data sets..
index = 0;

for ( x = 0 ; x < TERRAIN_L_XZ ; x++ ) { 
  for ( z = 0 ; z < TERRAIN_L_XZ ; z++ ) {
    posX = ( float ) x;
    posZ = ( float ) z;
   
    vertices[index].x = posX / TERRAIN_L_XZ;
    vertices[index].z = posZ / TERRAIN_L_XZ;
    vertices[index].y = 0.0f;
    vertices[index].w = 1.0f;
    texCoords[index].x = posX / TERRAIN_L_XZ;
    texCoords[index].y = posZ / TERRAIN_L_XZ;
    index++;
  }
}


[b]//now we must index our array of positional data: this [i]should[/i] create triangle strips[/b]
index = 0;
int no_verts = TERRAIN_L_XZ * TERRAIN_L_XZ;					   // = 1,048,576
int no_indices= ( TERRAIN_L_XZ - 1 ) * ( TERRAIN_L_XZ * 2 ); // = 2,095,104

for( x = 0; x < TERRAIN_L_XZ ; x++ ) { //for each row
  for( z = 0; z < TERRAIN_L_XZ ; z++ ) { //for each vert in the row
   faceIndex[index] = z;	 
   index++ ;														  //   at the point of the access violation crash, index = 2,096,112
   faceIndex[index] = z + TERRAIN_L_XZ;		 //.. 2,095,104 - 2,096,112 = -1008 : i.e. no_indices - index = -1008
   index++ ;
  }
}

Please help! I have an assignment due in 96 hours and I'm getting panicky. I've done this many times with just a trianglelist in DirectX, so I should be feeling so far out of my depth!

Thanks in advance, if you'd like more info please ask :)

Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7496

Like
0Likes
Like

Posted 23 November 2012 - 07:07 AM

Your faceIndex array is only of the size
( TERRAIN_L_XZ - 1 ) * ( TERRAIN_L_XZ * 2 )
whereas your face filling loops are producing
TERRAIN_L_XZ*TERRAIN_L_XZ*2
indicies...

#3 mynameisnafe   Members   -  Reputation: 252

Like
0Likes
Like

Posted 23 November 2012 - 07:15 AM

Indeed! I was just about to edit to say i'd fixed it with making the indices array bigger :
( TERRAIN_L_XZ  ) * ( TERRAIN_L_XZ * 2 )

Now my problem is that my flat sqaure isn't rendering properly at all..

Anyways, SOLVED.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS