James Andrews
James Andrews

Reputation: 3304

OpenGL ES2 Mesh breaks with more than 256 vertices

I'm trying to create a mesh out of triangle elements using OpenGL ES2 on the iPhone. The works fine until the number of vertices goes over 256. Over that number, the triangles go everywhere.

When I create a grid - 4x64 everything renders fine.

When I create a grid - 8x64 the top half is missing. It's like there was only space for 256 bytes so the second half of the array overwrites the first half.

This is the code I'm using to setup the vertex buffers:

// Grid size
NSInteger gridX = 4;
NSInteger gridY = 64;

_vertexSize = gridX * gridY * sizeof(BVertex);
_vertexData = (BVertex *) malloc(_vertexSize);


// The number of triangles (t) is: 2 * (gridSize - 1)^2
// The number of points (p) is 3 * t
_indexSize = 2 * 3 * (gridX - 1) * (gridY - 1);
_indexData = (GLubyte *) malloc(_indexSize);

// Code to add data to the arrays. I know this is working and 
// it's long and boilerplate so I'm not including it

GLuint vertexBuffer;
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, _vertexSize, _vertexData, GL_STATIC_DRAW);

GLuint indexBuffer;
glGenBuffers(1, &indexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indexSize, _indexData, GL_STATIC_DRAW);

This is the code to render:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _imageTexture);
glUniform1i(_textureUniform, 0);

// Draw the elements
glDrawElements(GL_TRIANGLES, _indexSize/sizeof(_indexData[0]), GL_UNSIGNED_BYTE, 0);

// Present our render buffer
[self.context presentRenderbuffer:GL_RENDERBUFFER]; 

Any insights would be greatly appreciated.

Upvotes: 0

Views: 107

Answers (1)

James Andrews
James Andrews

Reputation: 3304

I found the solution!

I was using the GLubyte type to store the indices. This type can only store integers up to 256 then it resets back to zero.

Upvotes: 3

Related Questions