Plane class

This is a discussion on Plane class within the Game Programming forums, part of the General Programming Boards category; Hey guys, I have tried to write a generic plane class that when passed parameters will create a flat 2D ...

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    4

    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?
    Attached Images Attached Images  

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21