1

我在带有 GLES2 的 Android 中使用 OpenSceneGraph。

我有两台摄像机:一台用于场景,另一台用于 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

我可以在场景的左侧看到它。

4

1 回答 1

0

首先,你不应该表演view * proj * vertex,应该表演proj * view * vertex

对于一个平视显示器,我认为根本不需要视图矩阵。我的理解是你只想在屏幕上画一个平面四边形?

如果您的投影矩阵映射 (0,0) 到 (1280,696),那么您可以在这个坐标系中绘制对象(从 (0,0) 到 (640,696) 的四边形将覆盖屏幕的左半部分,等等)。

或者,如果您想在 (0,0) 到 (1,1) 的空间中绘制事物,则只需覆盖投影矩阵即可映​​射该空间。将视图矩阵用于像 HUD 这样简单的东西对我来说没有多大意义。

于 2012-09-28T15:58:44.660 回答