Positioning objects in a 3D Scene and then figuring out what the user clicked on
I'm building a cross-platform game in C++, using OpenGL ES 2.0. The target is iPhone at the moment. I'm a newbie to coding games, but not a newbie to coding.
I'm confused about how to architect the game. But specifically, I'm asking about how to setup the objects needed to position models in the scene.
I have an object that represents a scene. There are 5 scenes. Only one scene is shown at a time. A scene is like a game level. Each scene has all the code for rendering, game logic, mouse and keyboard handling. Touch-screen handling is handled like a mouse click.
Each Scene has: It's own projection matrix. It's own "View" matrix -- currently not used -- always set to the identity matrix A list o开发者_如何转开发f models to render
Each Model has: The vertex list A GLSL program to render itself A texture A "model" matrix
I use an Orthographic projection matrix.
Since we're using GLES 2, I have to do just about everything manually. Including the matrices. So I don't use glPushMatrix, glPopMatrix, glTranslate, etc. They aren't available. Instead, I have my own functions which operate on my own Matrix objects.
OK here's where things get weird for me.
1) Setup the projection matrix (using an orthographic matrix)
_math_matrix_ortho(&this->ProjectionMatrix, 0.0, 1.0, 0.0, (float)height / (float)width, -1.0f, 1.0f);
2) Leave the view matrix as the identity matrix
3) Load each model
4) Each model's "model" matrix is defaults to the identity matrix
5) I now scale each model to make them the right side in proportion to one another. The scaling function adjusts the Model's "model" matrix.
model->modelMatrix.Scale(0.25, 0.25, 0.25);
6) Move each model into place
model->modelMatrix.Translate(2.0, 0.0, 0.0);
7) Render each model
{
glUseProgram(this->_program.program);
GL_CHECK_ERROR();
glVertexAttribPointer(_positionLoc, 3, GL_FLOAT, GL_FALSE, 0, _vertices );
GL_CHECK_ERROR();
glVertexAttribPointer(_texCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
GL_CHECK_ERROR();
glEnableVertexAttribArray(_positionLoc);
GL_CHECK_ERROR();
glEnableVertexAttribArray(_texCoordLoc);
GL_CHECK_ERROR();
glBindTexture ( GL_TEXTURE_2D, _texture->getGLID() );
GL_CHECK_ERROR();
glUniform1i ( _texLoc, 0 );
GL_CHECK_ERROR();
glUniformMatrix4fv(_pMatrixLoc, 1, GL_FALSE, SceneManager::getInstance().getCurrentScene().ProjectionMatrix.m);
GL_CHECK_ERROR();
glUniformMatrix4fv(_vMatrixLoc, 1, GL_FALSE, this->_vMatrix.m);
GL_CHECK_ERROR();
glUniformMatrix4fv(_mMatrixLoc, 1, GL_FALSE, this->_mMatrix.m);
GL_CHECK_ERROR();
glDrawArrays ( GL_TRIANGLE_STRIP, 0, 4);
GL_CHECK_ERROR();
}
What seems odd to me right off: Is it appropriate to be translating the Model's matrix?
The dicey part comes when I try to glUnProject the mouse/touch-screen coordinates.
1) Get Mouse/Touch-Screen coordinates
2) Un-project them.
Now I have the coordinates relative to the View matrix. Which is the identity matrix. What that means is that x,y coords are translated to a coordinate system where the max vertical and horizonal extents are exactly 1.0.
How do I figure out what object was clicked?
It seems that in order to answer my last question, I need to better understand where I positioned it.
I think I've over-designed this. Help.
You can either :
1 . compute a line intersection with your model, it depends how complex your model is.
2 . render a label image in an off screen pixmap : instead of drawing with light and textures you use flat colors which are object labels. pick the pointed pixel, the value gives the object. You have to assign labels to objects you want to detect, and a default value for all the others and the background.
I hope this helps.
精彩评论