Pergunta

I recently satrted learning OpenGL, and it seems pretty intuitive so far, but this I am stuck on. How can I change the settings so that OpenGL uses a pixel based coordinate system instead of what it uses now. For instance this code draws a rectangle about 10x20 pixels:

glBegin(GL_QUADS); {
     glVertex3f(player.x, player.y, 0);
     glVertex3f(player.x+0.05, player.y, 0);
     glVertex3f(player.x+0.05, player.y+0.1, 0);
     glVertex3f(player.x, player.y+0.1, 0);
}

How can I make it so that this code:

glBegin(GL_QUADS); {
     glVertex3f(player.x, player.y, 0);
     glVertex3f(player.x+10.0, player.y, 0);
     glVertex3f(player.x+10.0, player.y+20.0, 0);
     glVertex3f(player.x, player.y+20.0, 0);
}

would produce basically the same thing? Thanks.

EDIT: I saw someone suggest using glOrtho() somewhere, but they didn't give any info on how to do that. Would this work, and if so, how would I do it?

Foi útil?

Solução

Sorry if I sound brief; it turns out that Safari on the iPad throws away your typing when it does one of it's probably memory warning induced reloads.

Assumptions: you've tagged the question iPhone so you're talking about Cocoa Touch (with the origin in the top left, positive on the y axis being down). Similarly, you probably want points, not pixels - so 320x480 of them on the screen whether on a retina display or not.

glOrtho is exactly what you want - see the man page at http://www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html. So, you probably want to do this to your projection stack:

    glOrthof(0, 320, 480, 0, 1, 1000);

And then do this once on modelview:

    glTranslatef(0, 0, -500)

And submit all future geometry without a z component.

What that does is set up a viewport with no perspective (ie, z position doesn't affect object size), with the left edge of the screen at x=0, the right at x=320, the top at y=0 and the bottom at y=480. It arbitrarily puts the near clip plane at a distance of 1 and the far at a distance of 1000, since they have to be somewhere. But then it translates to halfway (okay, not quite exactly) between the two and so if you don't submit any more z coordinates then all your geometry will be visible and you've got some leeway for rotations around y or x if you want them.

Be warned though: glVertex calls aren't available on iOS as they aren't part of OpenGL ES. They're also deprecated in modern OpenGL. The short version is that they're really inefficient and hard for a driver to deal with quickly. glVertexPointer is the thing to use, passing in a C array of numbers.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top