我在Android中使用OpenSceneGraph和GLES2。
我有两个摄像头:一个用于场景,另一个用于HUD。场景摄像头是我的osgViewer中的主摄像头。我的麻烦在于HUD的相机。我正在尝试将OSG几何体添加到2D场景(HUD相机),但似乎无法让它显示出来。
我正在使用自定义着色器来操纵几何体的位置。据我所知,投影矩阵是正确的,但我认为问题可能在于视图矩阵,因为我使用的是两台摄像机。我已经尝试将HUD的视图设置为场景的初始视图,但无济于事。如果我错了,请纠正我。
// New camera for the HUD
hud_camera = new osg::Camera;
// Set the camera's view properties
hud_camera->setProjectionMatrix(
osg::Matrix::ortho2D(0, 1280, 0, 696));
hud_camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
hud_camera->setViewMatrix(osg::Matrix::identity());
hud_camera->setViewport(0, 0, 1280, 696);
// Clear the depth buffer
hud_camera->setClearMask(GL_DEPTH_BUFFER_BIT);
// Draw the subgraph after the main camera view and don't allow this camera
// to grab evente focurs from the main camera
hud_camera->setRenderOrder(osg::Camera::POST_RENDER);
hud_camera->setAllowEventFocus(false);
osg::Geode * geode;
osg::Geometry * geom;
osg::Vec3Array * vertices;
osg::Program * program;
// Create the geode and geometry to hold the crosshair image
geode = new osg::Geode;
geom = new osg::Geometry;
geode->addDrawable(geom);
hud_camera->addChild(geode);
// Set the vertices
vertices = new osg::Vec3Array;
vertices->push_back(osg::Vec3(0.0, 0.0, 0.0));
vertices->push_back(osg::Vec3(1.0, 0.0, 0.0));
vertices->push_back(osg::Vec3(1.0, 0.0, 1.0));
vertices->push_back(osg::Vec3(0.0, 0.0, 1.0));
geom->setVertexArray(vertices);
// set colors
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(1.0, 0.0, 0.0, 1.0));
colors->push_back(osg::Vec4(0.0, 1.0, 0.0, 1.0));
colors->push_back(osg::Vec4(0.0, 0.0, 1.0, 1.0));
colors->push_back(osg::Vec4(1.0, 0.0, 1.0, 1.0));
geom->setVertexAttribArray(7, colors);
geom->setVertexAttribBinding(7, osg::Geometry::BIND_PER_VERTEX);
// Set primitive set
geom->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));
geom->setUseVertexBufferObjects(true);
// Vertex Shader
char vertSource[] =
"attribute vec4 osg_Vertex;\n"
"attribute vec4 a_col;"
"uniform mat4 osg_ModelViewProjectionMatrix;\n"
"uniform mat4 proj_matrix;\n"
"uniform mat4 view_matrix;\n"
"varying vec4 v_col;"
"void main(void)\n"
"{\n"
"gl_Position = view_matrix * proj_matrix * osg_Vertex;\n"
"v_col = a_col;\n"
"}\n";
// Fragment shader
char fragSource[] =
"precision mediump float;\n"
"varying vec4 v_col;"
"void main(void)\n"
"{\n"
"gl_FragColor = v_col;\n"
"}\n";
// Set the shaders
program = new osg::Program;
program->setName("Crosshair Shader");
program->addShader(new osg::Shader(osg::Shader::VERTEX, vertSource));
program->addShader(new osg::Shader(osg::Shader::FRAGMENT, fragSource));
program->addBindAttribLocation("a_col", 7);
osg::StateSet* state = geode->getOrCreateStateSet();
state->addUniform(
new osg::Uniform("proj_matrix", hud_camera->getProjectionMatrix()));
state->addUniform(
new osg::Uniform("view_matrix", hud_camera->getViewMatrix());
// Add the program shaders
geode->getOrCreateStateSet()->setAttributeAndModes(
program, osg::StateAttribute::ON );
// Needs no lighting or any depth for a 2D image
geode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
state->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
之后我将 hud_camera 作为孩子附加到场景数据中。除了观察场景中的特定点之外,场景摄像机也没有什么特别的。
我确实知道正在创建几何体。在顶点着色器中使用它:
gl_Position = osg_ModelViewProjectionMatrx * osg_Vertex
我可以在场景的左边看到它。
答案 0 :(得分:0)
首先,您不应该执行view * proj * vertex
,它应该是proj * view * vertex
。
对于hud,我认为根本不需要有视图矩阵。我的理解是你只想在屏幕上画一个平面四边形?
如果您的投影矩阵将(0,0)映射到(1280,696),那么您可以在此坐标系中绘制对象(从(0,0)到(640,696)的四边形将覆盖左半部分屏幕等)。
或者如果你想在空间中绘制从(0,0)到(1,1)的东西,那么只需覆盖投影矩阵来映射这个空间。对于像HUD那样简单的事物来处理视图矩阵对我来说没有多大意义。