# Plane class

• 12-13-2006
bulldog
Plane class
Hey guys,

I have tried to write a generic plane class that when passed parameters will create a flat 2D plane. It all looks ok to me, but I get a run time error and I am not sure what is wrong.

I am pasting the main bits of code here, and attaching a picture that helps to explain how it works. Vertex3f is a class that holds 3 floats.

Code:

```bool Plane::Create(float sX, float sY, int dX, int dY, char *tex) {         texFile = tex;         sizeX = sX;         sizeY = sY;         divisionsX = dX;         divisionsY = dY;         int array_size = (divisionsX + 2) * (divisionsY + 2);         vertexBuffer = new Vector3f[array_size];         normalBuffer = new Vector3f[array_size];         texCoordBuffer = new Vector2f[array_size];         indexBuffer = new unsigned short[(((divisionsX+1)*2)+2)*(divisionsY+1)];         for (int i = 0; i < array_size; i++)         {                 normalBuffer[i].x = 0.0f;                 normalBuffer[i].y = 1.0f;                 normalBuffer[i].z = 0.0f;         }         //generate vertex and tex co-ords.         float vertex_incX = sizeX / (divisionsX + 1);         float vertex_incY = sizeY / (divisionsY + 1);         int vPos = 0;         for (int i = 0; i <= divisionsY + 1; i++)         {                 for (int j = 0; j <= divisionsX + 1; j++)                 {                         vertexBuffer[vPos].x = vertex_incX * j;                         vertexBuffer[vPos].y = 0.0f;                         vertexBuffer[vPos].z = vertex_incY * i;                         texCoordBuffer[vPos].x = (float)(1 / (divisionsX + 1)) * j;                         texCoordBuffer[vPos].y = (float)(1 / (divisionsY + 1)) * i;                         vPos++;                 }         }         //generate the index array.         int iPos = 0;         for (int j = 0; j <= divisionsY; j++)         {                 int start1 = j * (divisionsX + 2);                 int start2 = (j + 1) * (divisionsX + 2);                 for (int i = 0; i <= divisionsX+1; i++)                 {                         indexBuffer[iPos] = start1;                         iPos++;                         indexBuffer[iPos] = start2;                         iPos++;                         start1++;                         start2++;                 }         }         texID = TextureManager::getInstance()->LoadTexture(texFile);         return true; } void Plane::Draw() const {         glEnable(GL_TEXTURE_2D);         TextureManager::getInstance()->ApplyTexture(texID);         glEnableClientState(GL_VERTEX_ARRAY);         glVertexPointer(3, GL_FLOAT, 0, &vertexBuffer[0]);         glEnableClientState(GL_NORMAL_ARRAY);         glNormalPointer(GL_FLOAT, 0, &normalBuffer[0]);         glEnableClientState(GL_TEXTURE_COORD_ARRAY);         glTexCoordPointer(2, GL_FLOAT, 0, &texCoordBuffer[0]);                 int rowSize = ((divisionsX+1)*2)+2;         for (int i = 0; i< divisionsY + 1; i++)         {                 glDrawElements(GL_TRIANGLE_STRIP, rowSize, GL_UNSIGNED_INT, &indexBuffer[i*rowSize]);         }         glDisableClientState(GL_VERTEX_ARRAY);         glDisableClientState(GL_NORMAL_ARRAY);         glDisableClientState(GL_TEXTURE_COORD_ARRAY);         glDisable(GL_TEXTURE_2D); }```

The problem is apparently with this line:
Code:

glDrawElements(GL_TRIANGLE_STRIP, rowSize, GL_UNSIGNED_INT, &indexBuffer[i*rowSize]);

It gives the error:
Unhandled exception at 0x691ca718 in Zargons2.exe: 0xC0000005: Access violation reading location 0x018360bc.

Any ideas?