查看: 993|回复: 0

OSG中的混合的问题

[复制链接]

该用户从未签到

发表于 2015-7-5 22:17:09 | 显示全部楼层 |阅读模式
最近做关于OSG和GLSL结合,设计到混合的问题,始终无法实现,下面是我的一些源码:
GLSL源码:
//强制要求支持一个特定版本的GLSL版本  
#version 440  
//struct  vbo_struct{
//                vec3 VertexPosition;  
//                vec3 VertexColor;
//        };
//in vbo_struct vbo;
layout(location = 0) in vec3 VertexPosition;
layout(location = 1) in vec3 VertexColor;
//uniform int renderProcess;
uniform mat4 matModelView;
uniform mat4 matProjection;

out vec3 Color;
out float renderProcess;
  
void main()  
{  
    vec3 tVertexPosition;
   
    if( gl_InstanceID == 1 )
    {
        tVertexPosition = VertexPosition + vec3(2.0,0.0,0.0);
        Color = VertexColor;
        renderProcess = 0.0;
    }

    if( gl_InstanceID == 0 )
    {
        tVertexPosition = VertexPosition - vec3(2.0,0.0,0.0);
        Color = vec3(0.0,0.0,0.0);
        renderProcess = 1.0;
    }

    gl_Position = matProjection * matModelView * vec4(tVertexPosition,1.0);
}


#version 440  
  
in vec3 Color;
in float renderProcess;
  
out vec4 FragColor;
  
void main()  
{  
   // vec4 tempFragColor;

    if( renderProcess == 0.0 ){
        FragColor = vec4(Color,1.0);
        //FragColor = vec4(0.0,0.0,0.0,0.0);
    }

    if( renderProcess == 1.0 ){
        FragColor = vec4(0.0,1.0,1.0,1.0);
    }
   
    //FragColor = tempFragColor;
}

OSG的部分源码:

#include "../Common/Common.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>

//#define GLEW_STATIC 1
//#define GLUT_DISABLE_ATEXIT_HACK

//#include <GL/glew.h>
//#include <GL/glut.h>
//#include <core\core.hpp>  
//#include <highgui\highgui.hpp>  
//#include <imgproc\imgproc.hpp>  
//#include <ddraw.h>
//using namespace cv;

#include <osgViewer/Viewer>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osg/AnimationPath>
#include <osg/Texture2D>
#include <osgUtil/Optimizer>
#include <osg/BufferObject>
#include <osg/CoordinateSystemNode>
#include <osg/BlendFunc>
#include <osg/BlendEquation>
#ifdef _DEBUG
#pragma comment(lib,"../Debug/Common.lib")
#else
#pragma comment(lib,"../Release/Common.lib")
#endif

float positionData[] = {  
    0.0f, 0.0f, 0.0f,  
    0.0f, 5.0f, 0.0f,  
    5.0f, 0.0f, 0.0f };  
//颜色数组  
float colorData[] = {  
        0.0f, 0.0f, 1.0f,  
        0.0f, 1.0f, 0.0f,  
        1.0f, 0.0f, 0.0f};

osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT> *array_xyz;
osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT> *array_color;

//在geometry回调函数中导入绘制消息
//
class DrawCallbackOpenGL :public osg:rawable::DrawCallback
{
public:
        //重写虚函数,在其中完成回调代码
        virtual void drawImplementation(osg::RenderInfo& renderInfo,const osg::Drawable* drawable) const
        {
                _geom->setVertexArray(array_xyz);
                for( int renderCount = 0 ; renderCount < 2 ; ++renderCount ){
                }
        }

        void setGeomNode(osg::Geometry* geom)
        {
                _geom = geom;
        }

private:
        osg::Geometry* _geom;
};

/*更新每次Uniform*/
class MVPMatrixCallback : public osg::Uniform::Callback
{
public:

        MVPMatrixCallback(osg::Camera* camera):mCamera(camera)
        {//
        }

        virtual void operator()(osg::Uniform* uniform,osg::NodeVisitor* nv)
        {
                //每帧之前更新投影矩阵
                osg::Matrix ProMatrix = mCamera->getProjectionMatrix();
                uniform->set(ProMatrix);
        }
private:
        osg::Camera* mCamera;
};

//读取GLSL文件
osg::ref_ptr<osg:rogram> getProgram()
{
        osg::ref_ptr<osg::Program> program = new osg::Program;
        //读取点和片段GLSL文件
        osg::ref_ptr<osg::Shader> vertShader = new osg::Shader(osg::Shader::VERTEX);
        osg::ref_ptr<osg::Shader> fragShader = new osg::Shader(osg::Shader::FRAGMENT);

        if( !vertShader->loadShaderSourceFromFile("temp.vert") ){
                osg::notify(osg::NOTICE)<<"Load vert files not success"<<std::endl;
        }

        if( !fragShader->loadShaderSourceFromFile("temp.frag") ){
                osg::notify(osg::NOTICE)<<"Load frag files not success"<<std::endl;
        }


        //指定Shader索引
        program->addBindFragDataLocation("VertexPosition",0);
        program->addBindFragDataLocation("VertexColor",1);


        //将生成的Shader选入
        program->addShader(vertShader.get());
        program->addShader(fragShader.get());

        //

        return program;
}

//构建深度三角形模型
osg::ref_ptr<osg::Geometry> triangleScence()
{
        osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;//
//        GLuint vbo[2];
        //使用VBO
        osg::notify(osg::NOTICE)<<"Enable VBO"<<std::endl;
        geom->setUseVertexBufferObjects(true);
        geom->setSupportsDisplayList(false);
        geom->setUseDisplayList(false);
        geom->setFastPathHint(true);
        if( geom->computeFastPathsUsed() ) osg::notify(osg::NOTICE)<<"Enable fast path"<<std::endl;
        if( geom->areFastPathsUsed() ) osg::notify(osg::NOTICE)<<"fast path can used"<<std::endl;


        //添加顶点信息
        //osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT> *array_xyz= new osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT>( 3*3,positionData );
        array_xyz= new osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT>( 3*3,positionData );
        geom->setVertexArray(array_xyz);
        //添加颜色信息
        //osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT> *array_color= new osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT>( 3*3,colorData);
        array_color= new osg::TemplateArray<GLfloat,osg::Array::FloatArrayType,3,GL_FLOAT>( 3*3,colorData);
        geom->setColorArray(array_color);

        //绑定shader索引
        geom->setVertexAttribArray(0,array_xyz);
        geom->setVertexAttribBinding(0,osg::Geometry::BIND_PER_VERTEX);
        geom->setVertexAttribArray(1,array_color);
        geom->setVertexAttribBinding(1,osg::Geometry::BIND_PER_VERTEX);


        /*Ext*/
        /*osg::notify(osg::NOTICE)<<"Enable Extensions OpenGL"<<std::endl;
        osg::GraphicsContext* gc = viewer->getCamera()->getGraphicsContext();
        unsigned int contextID = gc->getState()->getContextID();
        //osg::GLBufferObject::Extensions* ext = geom->getOrCreateVertexBufferObject()->getGLBufferObject(contextID)->getExtensions(contextID,true);//获得扩展功能
        osg::GLBufferObject::Extensions* ext = osg::GLBufferObject::getExtensions(contextID,true);

        if( !ext->isBufferObjectSupported() ) osg::notify(osg::NOTICE)<<"GPU not supported OpenGL Extensions"<<std::endl;

        ext->glGenBuffers(2, vbo);
        ext->glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo[0]);
    ext->glBufferData(GL_ARRAY_BUFFER_ARB,3*3*sizeof(float),  
        positionData,GL_STATIC_READ);
        ext->glBindBuffer(GL_ARRAY_BUFFER_ARB, vbo[1]);
        ext->glBufferData(GL_ARRAY_BUFFER_ARB,3*3*sizeof(float),  
        colorData,GL_STATIC_READ);
                */
        //ext->isBufferObjectSupported();
       
        geom->addPrimitiveSet(new osg::DrawArrays(GL_TRIANGLES,0,3,2));

        return geom;
}

//设置相机坐标
osg::ref_ptr<osg::Camera> createCamera(int x, int y, int w, int h)
{
        osg::ref_ptr<osg::Camera> _camera = new osg::Camera;
        double aspect = (double) h / (double) w;

    _camera->setAllowEventFocus(false);
    _camera->setViewport(x, y, w, h);
    _camera->setClearColor(osg::Vec4(0,0,0,1.0));
    _camera->setClearMask(GL_COLOR_BUFFER_BIT);
    _camera->setRenderOrder(osg::Camera::POST_RENDER);
    _camera->setReferenceFrame(osg::Camera::ABSOLUTE_RF);
        if( w <= h ){
                _camera->setProjectionMatrixAsOrtho(-16.0f,16.0f,-16.0f*aspect,16.0f*aspect,0.0f,1000.0f);
        }
        else{
                _camera->setProjectionMatrixAsOrtho(-9.0f/aspect,9.0f/aspect,-9.0f,9.0f,0.0f,1000.0f);
        }
    _camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR);

    return _camera;
}
int main()
{
        osg::ref_ptr<osgViewer::Viewer> viewer = new osgViewer::Viewer;
        osg::ref_ptr<osg::Group> group = new osg::Group;
        osg::ref_ptr<osg::Program> shaderProgram = getProgram();
        osg::ref_ptr<osg::Geode> triangleGeode = new osg::Geode;
        osg::ref_ptr<osg::Camera> camera = createCamera(0,0,600,800);
        osg::ref_ptr<osg::Geometry> triangleGeom;
        osg::ref_ptr<osg::BlendFunc> bf = new osg::BlendFunc(osg::BlendFunc::ONE,osg::BlendFunc::ONE);
        osg::ref_ptr<osg::BlendEquation> be = new osg::BlendEquation(osg::BlendEquation::FUNC_ADD);

        triangleGeom = triangleScence();
        triangleGeode->addDrawable(triangleGeom.get());

        triangleGeode->getOrCreateStateSet()->setAttribute(shaderProgram,osg::StateAttribute::ON);
        triangleGeode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
        triangleGeode->getOrCreateStateSet()->setAttributeAndModes(be,osg::StateAttribute::ON );
        triangleGeode->getOrCreateStateSet()->setAttributeAndModes(bf,osg::StateAttribute::ON );

        group->addChild(triangleGeode.get());

        viewer->setSceneData(group.get());

        //设置视图模型矩阵
        camera->setViewMatrixAsLookAt(osg::Vec3f(0.0f,0.0f,10.0f),osg::Vec3f(0.0f,0.0f,-50.0f),osg::Vec3f(0.0f,1.0f,0.0f));
        triangleGeode->getOrCreateStateSet()->addUniform(new osg::Uniform("matModelView",camera->getViewMatrix()));
        //设置投影矩阵
        osg::ref_ptr<osg::Uniform> ProMatrixUniform = new osg::Uniform("matProjection",camera->getProjectionMatrix());
        ProMatrixUniform->setUpdateCallback(new MVPMatrixCallback(camera));//设置更新回调

        triangleGeode->getOrCreateStateSet()->addUniform(ProMatrixUniform);

        //设置不使用编译列表
        osg::DisplaySettings *ds = viewer->getDisplaySettings();
        ds->instance()->setCompileContextsHint(false);
        viewer->setDisplaySettings(ds);
        viewer->setKeyEventSetsDone(osgGA::GUIEventAdapter::KEY_G);

        viewer->realize();
       
        while(!viewer->done()){
                viewer->frame();
        }

        return 0;
}

绘制的效果如下:


绘制效果

绘制效果
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表