Iphone OpenGL に wavefront オブジェクトをロードしました。
x/y 軸を中心に回転、パン、ズームイン/アウトできます。
私の仕事は - オブジェクトがタップされたとき、次のように画面上の 2D 中心座標を強調表示します (+ が可視オブジェクトの中心にあると想像してください)。
OpenGLオブジェクトをロードするとき、私はそれを保存します:
- ワールド内のオブジェクトの中心位置、
- x、y、z 位置オフセット、
- x、y、z 回転、
- ズームスケール。
ユーザーが画面をタップすると、タップされたオブジェクトを識別できます。ただし、ユーザーはオブジェクトのどこでもタップできるため、タップしたポイントは中心ではありません。
ユーザーがオブジェクトに触れると、対応するオブジェクトに見えるおおよその中心座標を見つけられるようにしたいと考えています。
どうやってやるの?
私が見つけたGoogleのほとんどのコードは、3D座標を2D座標に変換することを意図していますが、回転はしていません。
コード内のいくつかの変数:
Vertex3D centerPosition;
Vertex3D currentPosition;
Rotation3D currentRotation;
//centerPosition.x, centerPosition.y, centerPosition.z
//currentPosition.x, currentPosition.y, currentPosition.z
//currentRotation.x, currentRotation.y, currentRotation.z
前もって感謝します。
(どのオブジェクトをタップしたかを調べるには、各オブジェクトの色を別の色に変更します。これにより、ユーザーがタップした色がわかります。)
オブジェクト drawSelf 関数:
// Save the current transformation by pushing it on the stack
glPushMatrix();
// Load the identity matrix to restore to origin
glLoadIdentity();
// Translate to the current position
glTranslatef(currentPosition.x, currentPosition.y, currentPosition.z);
// Rotate to the current rotation
glRotatef(currentRotation.x, 1.0, 0.0, 0.0);
glRotatef(currentRotation.y, 0.0, 1.0, 0.0);
glRotatef(currentRotation.z, 0.0, 0.0, 1.0);
// Enable and load the vertex array
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, vertexNormals);
// Loop through each group
if (textureCoords != NULL)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(valuesPerCoord, GL_FLOAT, 0, textureCoords);
}
for (OpenGLWaveFrontGroup *group in groups)
{
if (textureCoords != NULL && group.material.texture != nil)
[group.material.texture bind];
// Set color and materials based on group's material
Color3D ambient = group.material.ambient;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (const GLfloat *)&ambient);
Color3D diffuse = group.material.diffuse;
glColor4f(diffuse.red, diffuse.green, diffuse.blue, diffuse.alpha);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (const GLfloat *)&diffuse);
Color3D specular = group.material.specular;
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, (const GLfloat *)&specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, group.material.shininess);
glDrawElements(GL_TRIANGLES, 3*group.numberOfFaces, GL_UNSIGNED_SHORT, &(group.faces[0]));
}
if (textureCoords != NULL)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
// Restore the current transformation by popping it off
glPopMatrix();