veMaterial* veDeferredRenderPipeline::createSpotLightMaterial(veLight *light)
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass0 = new vePass;
    auto pass1 = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass0);
    technique->addPass(pass1);
    
    pass0->setRenderPass(vePass::FORWARD_PASS);
    pass0->depthTest() = true;
    pass0->depthWrite() = false;
    pass0->stencilTest() = true;
    pass0->cullFace() = false;
    pass0->stencilFunc() = { GL_ALWAYS, 0, 0, GL_ALWAYS, 0, 0 };
    pass0->stencilOp() = { GL_KEEP, GL_DECR_WRAP, GL_KEEP, GL_KEEP, GL_INCR_WRAP, GL_KEEP };
    pass0->blendFunc() = veBlendFunc::DISABLE;
    pass0->setShader(new veShader(veShader::VERTEX_SHADER, WORLD_BASED_LIGHT_VERTEX_SHADER));
    pass0->setShader(new veShader(veShader::FRAGMENT_SHADER, "layout(location=0) out vec4 fragColor;void main(){}"));
    pass0->addUniform(new veUniform("u_ModelViewProjectMat", MVP_MATRIX));
    pass0->setApplyFunctionCallback([]() {
        glClear(GL_STENCIL_BUFFER_BIT);
        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
    });
    
    initLightCommomParams(light, pass1);
    pass1->setRenderPass(vePass::FORWARD_PASS);
    pass1->depthTest() = false;
    pass1->depthWrite() = false;
    pass1->stencilTest() = true;
    pass1->cullFace() = true;
    pass1->cullFaceMode() = GL_FRONT;
    pass1->blendFunc().src = GL_ONE;
    pass1->blendFunc().dst = GL_ONE;
    pass1->blendEquation() = GL_FUNC_ADD;
    pass1->stencilFunc() = { GL_NOTEQUAL, 0, 0xFF, GL_NOTEQUAL, 0, 0xFF };
    pass1->setShader(new veShader(veShader::VERTEX_SHADER, WORLD_BASED_LIGHT_VERTEX_SHADER));
    pass1->setShader(new veShader(veShader::FRAGMENT_SHADER, SPOT_LIGHT_FRAGMENT_SHADER));
    pass1->addUniform(new veUniform("u_ModelViewProjectMat", MVP_MATRIX));
    pass1->addUniform(new veUniform("u_lightDirection", veVec3(0.0f)));
    pass1->addUniform(new veUniform("u_lightPosition", veVec3(0.0f)));
    pass1->addUniform(new veUniform("u_lightMatrix", veMat4::IDENTITY));
    pass1->addUniform(new veUniform("u_lightARI", 0.0f));
    pass1->addUniform(new veUniform("u_lightInnerAngleCos", 0.0f));
    pass1->addUniform(new veUniform("u_lightOuterAngleCos", 0.0f));
    pass1->addUniform(new veUniform("u_shadowTex", 4));
    pass1->setApplyFunctionCallback([]() {
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
    });
    
    return material;
}
Ejemplo n.º 2
0
void ShadowGroup::configRSMCamera()
{
    osg::ref_ptr<osg::StateSet> ss = _rsmCam->getOrCreateStateSet();
    ss->setAttributeAndModes(_rsmShader, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
    ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    ss->setMode(GL_BLEND, osg::StateAttribute::OFF);
    _rsmCam->setViewport(0, 0, _rsmTexWidth, _rsmTexHeight);
    
    // TODO: refactor
    _rsmCam->attach(osg::Camera::COLOR_BUFFER0, getDirLightDirFluxTexture(0));
    _rsmCam->attach(osg::Camera::COLOR_BUFFER1, getDirLightViewWorldPosTexture(0));
    
    _rsmCam->setClearColor(osg::Vec4(0, 0, 0, 1));
    _rsmCam->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    _rsmCam->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
    _rsmCam->setRenderOrder(osg::Camera::PRE_RENDER);
    _rsmCam->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    
    ss->addUniform(new osg::Uniform("u_lightViewInverseMatrix", osg::Matrixf()));
    ss->addUniform(new osg::Uniform("u_lightPos", osg::Vec3()));
    
    ss->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
    osg::ref_ptr<osg::CullFace> cullFace(new osg::CullFace);
    cullFace->setMode(osg::CullFace::BACK);
    ss->setAttribute(cullFace);
    
    osg::Vec2 depthTexSize(_depthTexWidth, _depthTexHeight);
    osg::ref_ptr<RSMCallback> rsmCallback(new RSMCallback(_mainCamera, _rsmCam, osg::Vec2(_rsmTexWidth, _rsmTexHeight), _sceneAABB));
    rsmCallback->setDirectionalLight(_giLight);
    ss->setUpdateCallback(rsmCallback);
    
    _rsmCam->addChild(_geoms);
}
veMaterial* veDeferredRenderPipeline::createIBLightMaterial(veLight *light)
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass);
    
    initLightCommomParams(light, pass);
    pass->setRenderPass(vePass::FORWARD_PASS);
    pass->depthTest() = false;
    pass->depthWrite() = false;
    pass->stencilTest() = false;
    pass->cullFace() = true;
    pass->cullFaceMode() = GL_BACK;
    pass->blendFunc().src = GL_ONE;
    pass->blendFunc().dst = GL_ONE;
    pass->blendEquation() = GL_FUNC_ADD;
    pass->setShader(new veShader(veShader::VERTEX_SHADER, SCREEN_BASED_LIGHT_VERTEX_SHADER));
    pass->setShader(new veShader(veShader::FRAGMENT_SHADER, IB_LIGHT_FRAGMENT_SHADER));
    pass->addUniform(new veUniform("u_lightDirection", veVec3::ZERO));
    pass->addUniform(new veUniform("u_specularMipMapCount", 1.0f));
    pass->addUniform(new veUniform("u_diffuseLighting", 4));
    pass->addUniform(new veUniform("u_specularLighting", 5));
    
    
    return material;
}
veMaterial* veDeferredRenderPipeline::createDirectionalLightMaterial(veLight *light)
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass);
    
    initLightCommomParams(light, pass);
    pass->setRenderPass(vePass::FORWARD_PASS);
    pass->depthTest() = false;
    pass->depthWrite() = false;
    pass->stencilTest() = false;
    pass->cullFace() = true;
    pass->cullFaceMode() = GL_BACK;
    pass->blendFunc().src = GL_ONE;
    pass->blendFunc().dst = GL_ONE;
    pass->blendEquation() = GL_FUNC_ADD;
    pass->setShader(new veShader(veShader::VERTEX_SHADER, SCREEN_BASED_LIGHT_VERTEX_SHADER));
    pass->setShader(new veShader(veShader::FRAGMENT_SHADER, DIRECTIONAL_LIGHT_FRAGMENT_SHADER));
    pass->addUniform(new veUniform("u_lightDirection", veVec3::ZERO));
    pass->addUniform(new veUniform("u_lightMatrixs", &veMat4::IDENTITY, 1));
    pass->addUniform(new veUniform("u_shadowCascadedLevels", 0.f));
    pass->addUniform(new veUniform("u_shadowCascadedCount", 0.f));
    pass->addUniform(new veUniform("u_shadowTex", 4));
    
    return material;
}
Ejemplo n.º 5
0
void ShadowGroup::addBasicShadowCam(osg::TextureRectangle *outDepthTex, osg::TextureRectangle *outFluxTex, osg::TextureRectangle *outPosTex, const osg::Matrixf &shadowMV, const osg::Matrixf &shadowMVP, DirectionalLight *dirLight)
{
    osg::ref_ptr<osg::Camera> cam(new osg::Camera);
    cam->setProjectionMatrix(_shadowProjection);
    
    osg::ref_ptr<osg::StateSet> ss = cam->getOrCreateStateSet();
    ss->setAttributeAndModes(_depthMapShader, osg::StateAttribute::ON | osg::StateAttribute::OVERRIDE);
    ss->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
    ss->setMode(GL_BLEND, osg::StateAttribute::OFF);
    cam->setViewport(0, 0, _depthTexWidth, _depthTexHeight);
    cam->attach(osg::Camera::COLOR_BUFFER0, outDepthTex);
    
//    if(_isGIEnabled)
//    {
//        cam->attach(osg::Camera::COLOR_BUFFER1, outFluxTex);
//        cam->attach(osg::Camera::COLOR_BUFFER2, outPosTex);
//    }
    
    cam->setClearColor(osg::Vec4(0, 0, 0, 1));
    cam->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    cam->setRenderTargetImplementation(osg::Camera::FRAME_BUFFER_OBJECT);
    cam->setRenderOrder(osg::Camera::PRE_RENDER);
    cam->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    cam->setViewMatrix(shadowMV);
   
    // 1) need to use osg::Matrixf as uniform, or invalid operation, since mat4 is float
    // 2) TODO: figure out why u_nearDistance cannot be passed to the shader
    // symptom: u_nearDistance seems to be "overriden" by the uniform of the same name defined in the geometry pass
    // Thus, for this reason, change u_nearDistance to u_nearDistance_s

    // to be modified in the callback
    ss->addUniform(new osg::Uniform("u_nearDistance_s", 0.0f));
    ss->addUniform(new osg::Uniform("u_zLength", 0.0f));
    
    ss->setMode(GL_CULL_FACE, osg::StateAttribute::ON);
    osg::ref_ptr<osg::CullFace> cullFace(new osg::CullFace);
    cullFace->setMode(osg::CullFace::BACK);
    ss->setAttribute(cullFace);
   
    osg::Vec2 depthTexSize(_depthTexWidth, _depthTexHeight);
    osg::ref_ptr<ShadowCallback> shadowCallback(new ShadowCallback(_mainCamera, cam, depthTexSize));
    shadowCallback->setDirectionalLight(dirLight);
    ss->setUpdateCallback(shadowCallback);
    
    cam->addChild(_geoms);
    _shadowCameras->addChild(cam);
}
veMaterial* veDeferredRenderPipeline::createAmbientLightMaterial()
{
    auto material = new veMaterial;
    auto technique = new veTechnique;
    auto pass = new vePass;
    material->addTechnique(technique);
    technique->addPass(pass);
    
    pass->setRenderPass(vePass::FORWARD_PASS);
    pass->depthTest() = false;
    pass->depthWrite() = false;
    pass->stencilTest() = false;
    pass->cullFace() = true;
    pass->cullFaceMode() = GL_BACK;
    pass->blendFunc().src = GL_ONE;
    pass->blendFunc().dst = GL_ONE;
    pass->blendEquation() = GL_FUNC_ADD;
    pass->setShader(new veShader(veShader::VERTEX_SHADER, SCREEN_BASED_LIGHT_VERTEX_SHADER));
    pass->setShader(new veShader(veShader::FRAGMENT_SHADER, AMBIENT_LIGHT_FRAGMENT_SHADER));
    pass->addUniform(new veUniform("u_ambient", veVec3::ZERO));
    pass->addUniform(new veUniform("u_RT1", 0));
    
    return material;
}
Ejemplo n.º 7
0
void LLDrawPoolWater::render(S32 pass)
{
	LLFastTimer ftm(FTM_RENDER_WATER);
	if (mDrawFace.empty() || LLDrawable::getCurrentFrame() <= 1)
	{
		return;
	}

	//do a quick 'n dirty depth sort
	for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
			 iter != mDrawFace.end(); iter++)
	{
		LLFace* facep = *iter;
		facep->mDistance = -facep->mCenterLocal.mV[2];
	}

	std::sort(mDrawFace.begin(), mDrawFace.end(), LLFace::CompareDistanceGreater());

	LLGLEnable blend(GL_BLEND);

	if ((mVertexShaderLevel > 0) && !sSkipScreenCopy)
	{
		shade();
		return;
	}

	LLVOSky *voskyp = gSky.mVOSkyp;

	stop_glerror();

	if (!gGLManager.mHasMultitexture)
	{
		// Ack!  No multitexture!  Bail!
		return;
	}

	LLFace* refl_face = voskyp->getReflFace();

	gPipeline.disableLights();
	
	LLGLDepthTest gls_depth(GL_TRUE, GL_FALSE);

	LLGLDisable cullFace(GL_CULL_FACE);
	
	// Set up second pass first
	mWaterImagep->addTextureStats(1024.f*1024.f);
	gGL.getTexUnit(1)->activate();
	gGL.getTexUnit(1)->enable(LLTexUnit::TT_TEXTURE);
	gGL.getTexUnit(1)->bind(mWaterImagep) ;

	LLVector3 camera_up = LLViewerCamera::getInstance()->getUpAxis();
	F32 up_dot = camera_up * LLVector3::z_axis;

	LLColor4 water_color;
	if (LLViewerCamera::getInstance()->cameraUnderWater())
	{
		water_color.setVec(1.f, 1.f, 1.f, 0.4f);
	}
	else
	{
		water_color.setVec(1.f, 1.f, 1.f, 0.5f*(1.f + up_dot));
	}

	glColor4fv(water_color.mV);

	// Automatically generate texture coords for detail map
	glEnable(GL_TEXTURE_GEN_S); //texture unit 1
	glEnable(GL_TEXTURE_GEN_T); //texture unit 1
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);

	// Slowly move over time.
	F32 offset = fmod(gFrameTimeSeconds*2.f, 100.f);
	F32 tp0[4] = {16.f/256.f, 0.0f, 0.0f, offset*0.01f};
	F32 tp1[4] = {0.0f, 16.f/256.f, 0.0f, offset*0.01f};
	glTexGenfv(GL_S, GL_OBJECT_PLANE, tp0);
	glTexGenfv(GL_T, GL_OBJECT_PLANE, tp1);

	gGL.getTexUnit(1)->setTextureColorBlend(LLTexUnit::TBO_MULT, LLTexUnit::TBS_TEX_COLOR, LLTexUnit::TBS_PREV_COLOR);
	gGL.getTexUnit(1)->setTextureAlphaBlend(LLTexUnit::TBO_REPLACE, LLTexUnit::TBS_PREV_ALPHA);

	gGL.getTexUnit(0)->activate();
	
	glClearStencil(1);
	glClear(GL_STENCIL_BUFFER_BIT);
	LLGLEnable gls_stencil(GL_STENCIL_TEST);
	glStencilOp(GL_KEEP, GL_REPLACE, GL_KEEP);
	glStencilFunc(GL_ALWAYS, 0, 0xFFFFFFFF);

	for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
		 iter != mDrawFace.end(); iter++)
	{
		LLFace *face = *iter;
		if (voskyp->isReflFace(face))
		{
			continue;
		}
		gGL.getTexUnit(0)->bind(face->getTexture());
		face->renderIndexed();
	}

	// Now, disable texture coord generation on texture state 1
	gGL.getTexUnit(1)->activate();
	gGL.getTexUnit(1)->unbind(LLTexUnit::TT_TEXTURE);
	gGL.getTexUnit(1)->disable();
	glDisable(GL_TEXTURE_GEN_S); //texture unit 1
	glDisable(GL_TEXTURE_GEN_T); //texture unit 1

	// Disable texture coordinate and color arrays
	gGL.getTexUnit(0)->activate();
	gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);

	stop_glerror();
	
	if (gSky.mVOSkyp->getCubeMap())
	{
		gSky.mVOSkyp->getCubeMap()->enable(0);
		gSky.mVOSkyp->getCubeMap()->bind();

		glMatrixMode(GL_TEXTURE);
		glLoadIdentity();
		LLMatrix4 camera_mat = LLViewerCamera::getInstance()->getModelview();
		LLMatrix4 camera_rot(camera_mat.getMat3());
		camera_rot.invert();

		glLoadMatrixf((F32 *)camera_rot.mMatrix);

		glMatrixMode(GL_MODELVIEW);
		LLOverrideFaceColor overrid(this, 1.f, 1.f, 1.f,  0.5f*up_dot);

		gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);

		for (std::vector<LLFace*>::iterator iter = mDrawFace.begin();
			 iter != mDrawFace.end(); iter++)
		{
			LLFace *face = *iter;
			if (voskyp->isReflFace(face))
			{
				//refl_face = face;
				continue;
			}

			if (face->getGeomCount() > 0)
			{					
				face->renderIndexed();
			}
		}

		gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);

		gSky.mVOSkyp->getCubeMap()->disable();
		
		gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
		gGL.getTexUnit(0)->enable(LLTexUnit::TT_TEXTURE);
		glMatrixMode(GL_TEXTURE);
		glLoadIdentity();
		glMatrixMode(GL_MODELVIEW);
		
	}

	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

    if (refl_face)
	{
		glStencilFunc(GL_NOTEQUAL, 0, 0xFFFFFFFF);
		renderReflection(refl_face);
	}

	gGL.getTexUnit(0)->setTextureBlendType(LLTexUnit::TB_MULT);
}