在3D场景中定位对象,然后确定用户单击了什么 [英] Positioning objects in a 3D Scene and then figuring out what the user clicked on

查看:79
本文介绍了在3D场景中定位对象,然后确定用户单击了什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGL ES 2.0在C ++中构建跨平台游戏.目前的目标是iPhone.我是编码游戏的新手,但不是编码的新手.

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.

我有一个代表场景的对象.有5个场景.一次仅显示一个场景.一个场景就像一个游戏关卡.每个场景都具有用于渲染,游戏逻辑,鼠标和键盘处理的所有代码.触摸屏处理就像单击鼠标一样.

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 of models to render

每个模型都具有: 顶点列表 一个GLSL程序来渲染自己 质地 一个模型"矩阵

Each Model has: The vertex list A GLSL program to render itself A texture A "model" matrix

我使用正交投影矩阵.

I use an Orthographic projection matrix.

由于我们使用的是GLES 2,因此我必须手动执行几乎所有操作.包括矩阵.因此,我不使用glPushMatrix,glPopMatrix,glTranslate等.它们不可用.相反,我有自己的函数,它们可以对自己的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)设置投影矩阵(使用正交矩阵)

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)保留视图矩阵作为单位矩阵

2) Leave the view matrix as the identity matrix

3)加载每个模型

4)每个模型的模型"矩阵默认为恒等矩阵

4) Each model's "model" matrix is defaults to the identity matrix

5)现在,我对每个模型进行缩放,以使其彼此成比例地位于右侧.缩放功能可调整模型的模型"矩阵.

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)将每个模型移到适当位置

6) Move each model into place

model->modelMatrix.Translate(2.0, 0.0, 0.0);

7)渲染每个模型

{
    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?

当我尝试glUnProject鼠标/触摸屏坐标时,就会出现dicey部分.

The dicey part comes when I try to glUnProject the mouse/touch-screen coordinates.

1)获取鼠标/触摸屏坐标

1) Get Mouse/Touch-Screen coordinates

2)取消投影.

现在我有了相对于View矩阵的坐标.这是单位矩阵.这意味着将x,y坐标转换为最大垂直和水平范围正好为1.0的坐标系.

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.

如何确定单击了哪个对象?

为了回答我的最后一个问题,我需要更好地了解我的位置.

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.

推荐答案

您可以:

1.计算与模型的直线相交,这取决于模型的复杂程度.

1 . compute a line intersection with your model, it depends how complex your model is.

2.在屏幕外的pixmap中渲染标签图像:您可以使用作为对象标签的纯色,而不是使用光和纹理进行绘制.选取尖的像素,该值给出对象.您必须为要检测的对象分配标签,并为所有其他对象和背景分配默认值.

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.

我希望这会有所帮助.

这篇关于在3D场景中定位对象,然后确定用户单击了什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆