Note that there are some explanatory texts on larger screens.

plurals
  1. POPositioning objects in a 3D Scene and then figuring out what the user clicked on
    primarykey
    data
    text
    <p>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.</p> <p>I'm confused about how to architect the game. <strong>But specifically, I'm asking about how to setup the objects needed to position models in the scene.</strong></p> <p>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.</p> <p>Each Scene has: It's own projection matrix. It's own "View" matrix -- currently not used -- always set to the identity matrix A list of models to render</p> <p>Each Model has: The vertex list A GLSL program to render itself A texture A "model" matrix</p> <p>I use an Orthographic projection matrix. </p> <p>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.</p> <p>OK here's where things get weird for me.</p> <p>1) Setup the projection matrix (using an orthographic matrix)</p> <pre><code>_math_matrix_ortho(&amp;this-&gt;ProjectionMatrix, 0.0, 1.0, 0.0, (float)height / (float)width, -1.0f, 1.0f); </code></pre> <p>2) Leave the view matrix as the identity matrix</p> <p>3) Load each model</p> <p>4) Each model's "model" matrix is defaults to the identity matrix</p> <p>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. </p> <pre><code>model-&gt;modelMatrix.Scale(0.25, 0.25, 0.25); </code></pre> <p>6) Move each model into place</p> <pre><code>model-&gt;modelMatrix.Translate(2.0, 0.0, 0.0); </code></pre> <p>7) Render each model</p> <pre><code>{ glUseProgram(this-&gt;_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-&gt;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-&gt;_vMatrix.m); GL_CHECK_ERROR(); glUniformMatrix4fv(_mMatrixLoc, 1, GL_FALSE, this-&gt;_mMatrix.m); GL_CHECK_ERROR(); glDrawArrays ( GL_TRIANGLE_STRIP, 0, 4); GL_CHECK_ERROR(); } </code></pre> <p>What seems odd to me right off: <strong>Is it appropriate to be translating the Model's matrix?</strong></p> <p>The dicey part comes when I try to glUnProject the mouse/touch-screen coordinates. </p> <p>1) Get Mouse/Touch-Screen coordinates</p> <p>2) Un-project them. </p> <p>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. </p> <p><strong>How do I figure out what object was clicked?</strong></p> <p>It seems that in order to answer my last question, I need to better understand where I positioned it. </p> <p>I think I've over-designed this. Help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload