Objective-c – Any idea why glVertexPointer() wouldn’t draw anything when using GL_FLOAT

iphoneobjective copengl-es

Using GLfixed as my vertex number type the following code draws textures as expected:

GLfixed vertices[] = 
{
    (int)point.x,           (int)point.y + size.height,
    (int)point.x + size.width,  (int)point.y + size.height,
    (int)point.x,           (int)point.y,
    (int)point.x + size.width,  (int)point.y
};

glVertexPointer(2, GL_FIXED, 0, vertices);

I read in the OpenGL docs that GLfixed is the least efficient type and that I should be using GLfloat instead. But when I switched my code over to floats nothing gets drawn.

GLfloat vertices[] = 
{
    point.x,        point.y + size.height,
    point.x + size.width,   point.y + size.height,
    point.x,        point.y,
    point.x + size.width,   point.y
};

glVertexPointer(2, GL_FLOAT, 0, vertices);

Is there another flag I need to set in the OpenGL state machine to get this to behave as expected?

Best Answer

When I look at the documentation it looks like GL_FIXED is a fixed point 16:16 format. That means that the 16 upper bits of a GLfixed represents the integer part and the fraction is the lower 16 bits divided by 65536 (1 << 16).

To convert this to a float simply divide the number by 65536:

const float scale = 1.0f / 65536.0f;
GLfloat vertices[] = 
{
        point.x * scale               , (point.y + size.height) * scale,
        (point.x + size.width) * scale, (point.y + size.height) * scale,
        point.x * scale               , point.y * scale,
        (point.x + size.width) * scale, point.y * scale
};

glVertexPointer(2, GL_FLOAT, 0, vertices);

If you're vertex coordinates etc is also in GL_FIXEDformat you'll have to scale them as well.

Hope this helps.

Related Topic