Matthew Chen
Matthew Chen

Reputation: 209

What data type should I use for my texture coordinates in OpenGL ES?

I notice that the default data type for texture coordinates in the OpenGL docs is GLfloat, but much of the sample code I see written by experienced iphone developers uses GLshort or GLbyte. Is this an optimization?

GLfloat vertices[] = {    
  // Upper left
  x1, y2,
  // Lower left
  x1, y1,
  // Lower right
  x2, y1,
  // Upper right
  x2, y2,
};
glTexCoordPointer(2, GL_FLOAT, 0, iconSTs);

vs.

GLbyte vertices[] = {    
  // Upper left
  x1, y2,
  // Lower left
  x1, y1,
  // Lower right
  x2, y1,
  // Upper right
  x2, y2,
};
glTexCoordPointer(2, GL_BYTE, 0, iconSTs);

Upvotes: 2

Views: 1376

Answers (2)

noop
noop

Reputation: 308

I don't recommend using byte/short cordinates on iPhone, especially if you're a beginner. Just use floats. They take more space, but it doesn't matter until you need to render thousands of vertices at maximum speed. Also, you'll have to change texture matrix to represent fractional values with integer texture coords, otherwise they'll only point to texture corners (0, 1). This adds complexity to your code.

I believe they are NOT faster by themselves, at least on iPhone. They can be somewhat faster because of smaller memory bandwidth necessary to read thousands of them in a single call. This optimization isn't worth doing if you writing your first engine. Unless you are a very experienced engine coder and going to push the hardware to its limits, this will never be the main bottleneck in your code, bringing negligible performance benefits.

Situation can probably be different for some other OGL-ES devices, since OpenGL pipline is partially implemented in software and some devices lack FPU and therefore unable to process floats quickly. That's why some Android games try to avoid float type altogether (in 2009).

Upvotes: 3

slurdge
slurdge

Reputation: 423

GLFloat vs. GLByte is a matter a size taken by your vertices.
In OpenGL, you can specify which is the underlying datatype of your geometry.
I did not program on iphone, but I'm pretty sure they do it to save space (and maybe computation time).
Float is 4 bytes, short is 2 bytes, byte is... 1 byte :)
Of course you loose precision with less bits.

Upvotes: 0

Related Questions