|
本帖最后由 张利维 于 2017-8-16 21:29 编辑
我想用几何着色器画一个立方体,大致思路是从osg传入一个顶点的坐标,然后再在几何着色器中进行偏移。于是我首先想以绘制一个正方形测试一下这个思路是否可行。(在几何着色器中通过绘制4个顶点的triangle_strip实现绘制一个正方形的效果)。
但结果发现当使用一个顶点作为输入时,没有任何内容显示出来,而传入两个顶点时却可以显示出两个正方形(但也存在一些问题,比如正方形在漫游过程中会被奇怪的裁剪掉一部分)。这让我很是纠结,为什么传入一个顶点的时候不行呢?
下面是我通过传入一个顶点,在几何着色器中绘制正方形的代码,不知道为啥就是显示不出来-->_<--。求大神指点迷津
另外,我看英文版的论坛上也有一个和我一样的问题,但是没有解决问题地址
- #include <osg/Geode>
- #include <osg/Geometry>
- #include <osgViewer/Viewer>
- #include <osg/MatrixTransform>
- #include <osgDB/ReadFile>
- #include <osgDB/WriteFile>
- #include <osg/ShapeDrawable>
- #include <osg\PolygonMode>
- #include <osg\LineWidth>
- #include <osg/ComputeBoundsVisitor>
- #include <osgUtil\Optimizer>
- #include <osg/CullFace>
- #include <osgViewer/ViewerEventHandlers>
- #include <osgGA/TrackballManipulator>
- #include "Voxelization.h"
- #include "DataAcquireNodeVisitor.h"
- #include "DrawUtils.h"
- #include <iostream>
- #include <fstream>
- const int OSG_WIDTH = 1280;
- const int OSG_HEIGHT = 960;
- const osg::Vec4f FOG_COLOR = osg::Vec4f(0.6f, 0.6f, 0.7f, 1.f);
- int main() {
- osgViewer::Viewer viewer;
- osg::ref_ptr<osg::Group> root = new osg::Group;
- root->addChild(createVoxel(viewer.getCamera()));
- viewer.setSceneData(root.get());
- viewer.setUpViewInWindow(100, 100, OSG_WIDTH, OSG_HEIGHT);
- //viewer.getCamera()->setClearColor(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
- /* depth settings */
- osg::StateSet* state = root->getOrCreateStateSet();
- state->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
- osg::Camera* camera = viewer.getCamera();
- camera->setClearColor(FOG_COLOR);
- camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- viewer.setCameraManipulator(new osgGA::TrackballManipulator);
- viewer.realize();
- while (!viewer.done()) {
- camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- viewer.frame();
- }
- return 0;
- }
复制代码
传入一个顶点
- osg::ref_ptr<osg::Geode> createVoxel(osg::Camera* camera)
- {
- osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
- //vertices->push_back(osg::Vec3(0.0f, 1.0f, 0.0f));
- vertices->push_back(osg::Vec3(1.0f, 0.0f, 0.0f));
- // create geometry
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
- geom->addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vertices->size()));
- geom->setUseDisplayList(false);
- geom->setVertexArray(vertices);
- // set attributes
- geom->setVertexAttribArray(0, vertices, osg::Array::BIND_PER_VERTEX);
-
- // create shader
- osg::ref_ptr<osg::Program> program = new osg::Program;
- osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX);
- if (!vertShader->loadShaderSourceFromFile("Shaders/voxelDraw.vert"))
- std::cerr << "Could not read VERTEX shader from file" << std::endl;
- program->addShader(vertShader);
- osg::ref_ptr<osg::Shader> geomShader = new osg::Shader(osg::Shader::GEOMETRY);
- if (!geomShader->loadShaderSourceFromFile("Shaders/voxelDraw.geom"))
- std::cerr << "Could not read GEOMETRY shader from file" << std::endl;
- program->addShader(geomShader);
- osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);
- if (!fragShader->loadShaderSourceFromFile("Shaders/voxelDraw.frag"))
- std::cerr << "Could not read FRAGMENT shader from file" << std::endl;
- program->addShader(fragShader);
- // geode
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable(geom.get());
- osg::StateSet* state = geode->getOrCreateStateSet();
- state->setAttributeAndModes(program.get(), osg::StateAttribute::ON);
- // add uniforms
- osg::Uniform* modelViewProjectionMatrix = new osg::Uniform(osg::Uniform::FLOAT_MAT4, "ModelViewProjectionMatrix");
- modelViewProjectionMatrix->setUpdateCallback(new ModelViewProjectionMatrixCallback(camera));
- state->addUniform(modelViewProjectionMatrix);
- osg::Uniform* viewportVector = new osg::Uniform(osg::Uniform::FLOAT_VEC2, "Viewport");
- viewportVector->setUpdateCallback(new ViewportCallback(camera));
- state->addUniform(viewportVector);
- osg::Uniform* cameraEye = new osg::Uniform(osg::Uniform::FLOAT_VEC4, "CameraEye");
- cameraEye->setUpdateCallback(new CameraEyeCallback(camera));
- state->addUniform(cameraEye);
- // state settings
- osg::LineWidth* lw = new osg::LineWidth;
- lw->setWidth(10.f);
- state->setAttribute(lw, osg::StateAttribute::ON);
- return geode.release();
- }
复制代码
顶点着色器
- #version 330
- uniform mat4 ModelViewProjectionMatrix;
- layout(location = 0) in vec4 position;
- out mat4 transform;
- out vec4 pos;
- void main()
- {
- gl_Position = ModelViewProjectionMatrix * position;
- transform = ModelViewProjectionMatrix;
- pos = position;
-
- }
复制代码
几何着色器
- #version 330
- layout(points) in;
- layout(triangle_strip,max_vertices = 8) out;
- uniform vec2 Viewport;
- in mat4 transform[];
- in vec4 pos[];
- out vec4 color;
- void main()
- {
- //这里我是想在世界坐标系下进行偏移,然后再进行视图变换和投影
- gl_Position = transform[0] * (pos[0] + vec4(0.0,0.0,0.0,1.0));EmitVertex();
- gl_Position = transform[0] * (pos[0] + vec4(0.5,0.0,0.0,1.0));EmitVertex();
- gl_Position = transform[0] * (pos[0] + vec4(0.0,0.0,0.5,1.0));EmitVertex();
- gl_Position = transform[0] * (pos[0] + vec4(0.5,0.0,0.5,1.0));EmitVertex();
- EndPrimitive();
- color = pos[0];
- }
复制代码
片段着色器
- #version 330
- in vec4 color;
- void main()
- {
- gl_FragColor = vec4(color.xyz,1.0);
- }
复制代码
下面是之前提到的,正方形被诡异的裁剪了的情况
|
-
|