Esempio n. 1
0
// Render fancy water
bool TerrainRenderer::RenderFancyWater()
{
	PROFILE3_GPU("fancy water");

	// If we're using fancy water, make sure its shader is loaded
	if (!m->fancyWaterShader)
	{
		std::map<CStr, CStr> defNull;
		m->fancyWaterShader = g_Renderer.GetShaderManager().LoadProgram("water_high", defNull);
		if (!m->fancyWaterShader)
		{
			LOGERROR(L"Failed to load water shader. Falling back to non-fancy water.\n");
			g_Renderer.m_Options.m_FancyWater = false;
			return false;
		}
	}

	WaterManager* WaterMgr = g_Renderer.GetWaterManager();
	CLOSTexture& losTexture = g_Renderer.GetScene().GetLOSTexture();
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	double time = WaterMgr->m_WaterTexTimer;
	double period = 1.6;
	int curTex = (int)(time*60/period) % 60;

	m->fancyWaterShader->Bind();

	m->fancyWaterShader->BindTexture("normalMap", WaterMgr->m_NormalMap[curTex]);

	// Shift the texture coordinates by these amounts to make the water "flow"
	float tx = -fmod(time, 81.0)/81.0;
	float ty = -fmod(time, 34.0)/34.0;
	float repeatPeriod = WaterMgr->m_RepeatPeriod;

	// Set the proper LOD bias
#if !CONFIG2_GLES
	glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, g_Renderer.m_Options.m_LodBias);
#endif

	const CCamera& camera = g_Renderer.GetViewCamera();
	CVector3D camPos = camera.m_Orientation.GetTranslation();

	// Bind reflection and refraction textures
	m->fancyWaterShader->BindTexture("reflectionMap", WaterMgr->m_ReflectionTexture);
	m->fancyWaterShader->BindTexture("refractionMap", WaterMgr->m_RefractionTexture);

	m->fancyWaterShader->BindTexture("losMap", losTexture.GetTexture());

	const CLightEnv& lightEnv = g_Renderer.GetLightEnv();
	m->fancyWaterShader->Uniform("ambient", lightEnv.m_TerrainAmbientColor);
	m->fancyWaterShader->Uniform("sunDir", lightEnv.GetSunDir());
	m->fancyWaterShader->Uniform("sunColor", lightEnv.m_SunColor.X);
	m->fancyWaterShader->Uniform("shininess", WaterMgr->m_Shininess);
	m->fancyWaterShader->Uniform("specularStrength", WaterMgr->m_SpecularStrength);
	m->fancyWaterShader->Uniform("waviness", WaterMgr->m_Waviness);
	m->fancyWaterShader->Uniform("murkiness", WaterMgr->m_Murkiness);
	m->fancyWaterShader->Uniform("fullDepth", WaterMgr->m_WaterFullDepth);
	m->fancyWaterShader->Uniform("tint", WaterMgr->m_WaterTint);
	m->fancyWaterShader->Uniform("reflectionTintStrength", WaterMgr->m_ReflectionTintStrength);
	m->fancyWaterShader->Uniform("reflectionTint", WaterMgr->m_ReflectionTint);
	m->fancyWaterShader->Uniform("translation", tx, ty);
	m->fancyWaterShader->Uniform("repeatScale", 1.0f / repeatPeriod);
	m->fancyWaterShader->Uniform("reflectionMatrix", WaterMgr->m_ReflectionMatrix);
	m->fancyWaterShader->Uniform("refractionMatrix", WaterMgr->m_RefractionMatrix);
	m->fancyWaterShader->Uniform("losMatrix", losTexture.GetTextureMatrix());
	m->fancyWaterShader->Uniform("cameraPos", camPos);

	for (size_t i = 0; i < m->visiblePatches.size(); ++i)
	{
		CPatchRData* data = m->visiblePatches[i];
		data->RenderWater(m->fancyWaterShader);
	}

	m->fancyWaterShader->Unbind();

	pglActiveTextureARB(GL_TEXTURE0);

	glDisable(GL_BLEND);

	return true;
}
Esempio n. 2
0
void TerrainRenderer::RenderSimpleWater()
{
#if !CONFIG2_GLES
	PROFILE3_GPU("simple water");

	WaterManager* WaterMgr = g_Renderer.GetWaterManager();
	CLOSTexture& losTexture = g_Game->GetView()->GetLOSTexture();

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	double time = WaterMgr->m_WaterTexTimer;
	double period = 1.6f;
	int curTex = (int)(time*60/period) % 60;

	WaterMgr->m_WaterTexture[curTex]->Bind();

	// Shift the texture coordinates by these amounts to make the water "flow"
	float tx = -fmod(time, 81.0)/81.0;
	float ty = -fmod(time, 34.0)/34.0;
	float repeatPeriod = 16.0f;

	// Perform the shifting by using texture coordinate generation
	GLfloat texgenS0[4] = { 1/repeatPeriod, 0, 0, tx };
	GLfloat texgenT0[4] = { 0, 0, 1/repeatPeriod, ty };
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGenfv(GL_S, GL_OBJECT_PLANE, texgenS0);
	glTexGenfv(GL_T, GL_OBJECT_PLANE, texgenT0);
	glEnable(GL_TEXTURE_GEN_S);
	glEnable(GL_TEXTURE_GEN_T);

	// Set up texture environment to multiply vertex RGB by texture RGB and use vertex alpha
	GLfloat waterColor[4] = { WaterMgr->m_WaterColor.r, WaterMgr->m_WaterColor.g, WaterMgr->m_WaterColor.b, 1.0f };
	glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, waterColor);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
	glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
	glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_TEXTURE);
	glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
	glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_CONSTANT);
	glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_COLOR);
	glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
	glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PRIMARY_COLOR_ARB);
	glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);

	// Multiply by LOS texture
	losTexture.BindTexture(1);
	CMatrix3D losMatrix = losTexture.GetTextureMatrix();
	GLfloat texgenS1[4] = { losMatrix[0], losMatrix[4], losMatrix[8], losMatrix[12] };
	GLfloat texgenT1[4] = { losMatrix[1], losMatrix[5], losMatrix[9], losMatrix[13] };
	glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
	glTexGenfv(GL_S, GL_OBJECT_PLANE, texgenS1);
	glTexGenfv(GL_T, GL_OBJECT_PLANE, texgenT1);
	glEnable(GL_TEXTURE_GEN_S);
	glEnable(GL_TEXTURE_GEN_T);

	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
	glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_ARB, GL_MODULATE);
	glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_ARB, GL_PREVIOUS);
	glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_ARB, GL_SRC_COLOR);
	glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_ARB, GL_TEXTURE);
	glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_ARB, GL_SRC_ALPHA);
	glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA_ARB, GL_REPLACE);
	glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA_ARB, GL_PREVIOUS);
	glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA_ARB, GL_SRC_ALPHA);

	// Set the proper LOD bias
	glTexEnvf(GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, g_Renderer.m_Options.m_LodBias);

	CShaderProgramPtr dummyShader = g_Renderer.GetShaderManager().LoadProgram("fixed:dummy");
	dummyShader->Bind();

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);

	for (size_t i = 0; i < m->visiblePatches.size(); ++i)
	{
		CPatchRData* data = m->visiblePatches[i];
		data->RenderWater(dummyShader);
	}

	glDisableClientState(GL_COLOR_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);

	dummyShader->Unbind();

	g_Renderer.BindTexture(1, 0);

	glDisable(GL_TEXTURE_GEN_S);
	glDisable(GL_TEXTURE_GEN_T);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	pglActiveTextureARB(GL_TEXTURE0_ARB);

	// Clean up the texture matrix and blend mode
	glDisable(GL_TEXTURE_GEN_S);
	glDisable(GL_TEXTURE_GEN_T);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	glDisable(GL_BLEND);
	glDisable(GL_TEXTURE_2D);
#endif
}
Esempio n. 3
0
// Render fancy water
bool TerrainRenderer::RenderFancyWater(const CShaderDefines& context, ShadowMap* shadow)
{
	PROFILE3_GPU("fancy water");
	
	WaterManager* WaterMgr = g_Renderer.GetWaterManager();
	CShaderDefines defines = context;
	
	WaterMgr->UpdateQuality();

	// If we're using fancy water, make sure its shader is loaded
	if (!m->fancyWaterShader || WaterMgr->m_NeedsReloading)
	{
		if (WaterMgr->m_WaterNormal)
			defines.Add(str_USE_NORMALS, str_1);
		if (WaterMgr->m_WaterRealDepth)
			defines.Add(str_USE_REAL_DEPTH, str_1);
		if (WaterMgr->m_WaterFoam)
			defines.Add(str_USE_FOAM, str_1);
		if (WaterMgr->m_WaterCoastalWaves && false)
			defines.Add(str_USE_WAVES, str_1);
		if (WaterMgr->m_WaterRefraction)
			defines.Add(str_USE_REFRACTION, str_1);
		if (WaterMgr->m_WaterReflection)
			defines.Add(str_USE_REFLECTION, str_1);
		if (shadow && WaterMgr->m_WaterShadows)
			defines.Add(str_USE_SHADOWS, str_1);

		m->wavesShader = g_Renderer.GetShaderManager().LoadProgram("glsl/waves", defines);
		if (!m->wavesShader)
		{
			LOGERROR(L"Failed to load waves shader. Deactivating waves.\n");
			g_Renderer.SetOptionBool(CRenderer::OPT_WATERCOASTALWAVES, false);
			defines.Add(str_USE_WAVES, str_0);
		}
		
		// haven't updated the ARB shader yet so I'll always load the GLSL
		/*if (!g_Renderer.m_Options.m_PreferGLSL && !superFancy)
			m->fancyWaterShader = g_Renderer.GetShaderManager().LoadProgram("arb/water_high", defines);
		else*/
			m->fancyWaterShader = g_Renderer.GetShaderManager().LoadProgram("glsl/water_high", defines);
		
		if (!m->fancyWaterShader)
		{
			LOGERROR(L"Failed to load water shader. Falling back to non-fancy water.\n");
			WaterMgr->m_RenderWater = false;
			return false;
		}
		WaterMgr->m_NeedsReloading = false;
	}
	
	CLOSTexture& losTexture = g_Renderer.GetScene().GetLOSTexture();

	GLuint depthTex;
	// creating the real depth texture using the depth buffer.
	if (WaterMgr->m_WaterRealDepth)
	{
		if (WaterMgr->m_depthTT == 0)
		{
			glGenTextures(1, (GLuint*)&depthTex);
			WaterMgr->m_depthTT = depthTex;
			glBindTexture(GL_TEXTURE_2D, WaterMgr->m_depthTT);
			
#if CONFIG2_GLES
			GLenum format = GL_DEPTH_COMPONENT;
#else
			GLenum format = GL_DEPTH_COMPONENT32;
#endif
			
			// TODO: use POT texture
			glTexImage2D(GL_TEXTURE_2D, 0, format, g_Renderer.GetWidth(), g_Renderer.GetHeight(),
						 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE,NULL);
		}
		glBindTexture(GL_TEXTURE_2D, WaterMgr->m_depthTT);
#if !CONFIG2_GLES
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE);
#endif
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		
		glCopyTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT, 0, 0, g_Renderer.GetWidth(), g_Renderer.GetHeight(), 0);
		
		glBindTexture(GL_TEXTURE_2D, 0);
	}
	// Calculating the advanced informations about Foam and all if the quality calls for it.
	/*if (WaterMgr->m_NeedInfoUpdate && (WaterMgr->m_WaterFoam || WaterMgr->m_WaterCoastalWaves))
	{
		WaterMgr->m_NeedInfoUpdate = false;
		WaterMgr->CreateSuperfancyInfo();
	}*/
	
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	double time = WaterMgr->m_WaterTexTimer;
	double period = 8;
	int curTex = (int)(time*60/period) % 60;
	int nexTex = (curTex + 1) % 60;

	GLuint FramebufferName = 0;

	// rendering waves to a framebuffer
	// TODO: reactivate this with something that looks good.
	if (false && WaterMgr->m_WaterCoastalWaves && WaterMgr->m_VBWaves && !g_AtlasGameLoop->running)
	{
		// Save the post-processing framebuffer.
		GLint fbo;
		glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, &fbo);

		pglGenFramebuffersEXT(1, &FramebufferName);
		pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, FramebufferName);
		
		GLuint renderedTexture;
		if (WaterMgr->m_waveTT == 0)
		{
			glGenTextures(1, &renderedTexture);
			WaterMgr->m_waveTT = renderedTexture;
			
			glBindTexture(GL_TEXTURE_2D, WaterMgr->m_waveTT);
			// TODO: use POT texture
			glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, (float)g_Renderer.GetWidth(), (float)g_Renderer.GetHeight(), 0,GL_RGBA, GL_UNSIGNED_BYTE, 0);

		}
		glBindTexture(GL_TEXTURE_2D, WaterMgr->m_waveTT);
		
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		
		pglFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, WaterMgr->m_waveTT, 0);
		
		glClearColor(0.5f,0.5f,1.0f,0.0f);
		glClear(GL_COLOR_BUFFER_BIT);
		
		// rendering
		m->wavesShader->Bind();
		m->wavesShader->BindTexture(str_waveTex, WaterMgr->m_Wave);
		m->wavesShader->Uniform(str_time, (float)time);
		m->wavesShader->Uniform(str_waviness, WaterMgr->m_Waviness);
		m->wavesShader->Uniform(str_mapSize, (float)(WaterMgr->m_TexSize));
		
		SWavesVertex *base=(SWavesVertex *)WaterMgr->m_VBWaves->m_Owner->Bind();
		GLsizei stride = sizeof(SWavesVertex);
		m->wavesShader->VertexPointer(3, GL_FLOAT, stride, &base[WaterMgr->m_VBWaves->m_Index].m_Position);
		m->wavesShader->TexCoordPointer(GL_TEXTURE0,2,GL_BYTE, stride,&base[WaterMgr->m_VBWaves->m_Index].m_UV);
		m->wavesShader->AssertPointersBound();

		u8* indexBase = WaterMgr->m_VBWavesIndices->m_Owner->Bind();
		glDrawElements(GL_TRIANGLES, (GLsizei) WaterMgr->m_VBWavesIndices->m_Count, GL_UNSIGNED_SHORT, indexBase + sizeof(u16)*(WaterMgr->m_VBWavesIndices->m_Index));

		g_Renderer.m_Stats.m_DrawCalls++;
		CVertexBuffer::Unbind();
		m->wavesShader->Unbind();
		
		// rebind post-processing frambuffer.
		pglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo);
		glBindTexture(GL_TEXTURE_2D, 0);
		
	}
	
	m->fancyWaterShader->Bind();

	
	// Shift the texture coordinates by these amounts to make the water "flow"
	float tx = -fmod(time, 81.0 / (WaterMgr->m_Waviness/20.0 + 0.8) )/(81.0/ (WaterMgr->m_Waviness/20.0 + 0.8) );
	float ty = -fmod(time, 34.0 / (WaterMgr->m_Waviness/20.0 + 0.8) )/(34.0/ (WaterMgr->m_Waviness/20.0 + 0.8) );
	
	float repeatPeriod = WaterMgr->m_RepeatPeriod;
	
	const CCamera& camera = g_Renderer.GetViewCamera();
	CVector3D camPos = camera.m_Orientation.GetTranslation();

	m->fancyWaterShader->BindTexture(str_normalMap, WaterMgr->m_NormalMap[curTex]);
	m->fancyWaterShader->BindTexture(str_normalMap2, WaterMgr->m_NormalMap[nexTex]);
	
	if (WaterMgr->m_WaterFoam || WaterMgr->m_WaterCoastalWaves)
	{
		m->fancyWaterShader->BindTexture(str_Foam, WaterMgr->m_Foam);
		m->fancyWaterShader->Uniform(str_mapSize, (float)(WaterMgr->m_TexSize));
	}
	if (WaterMgr->m_WaterRealDepth)
		m->fancyWaterShader->BindTexture(str_depthTex, WaterMgr->m_depthTT);
	if (WaterMgr->m_WaterCoastalWaves)
		m->fancyWaterShader->BindTexture(str_waveTex, WaterMgr->m_waveTT);
	if (WaterMgr->m_WaterReflection)
	m->fancyWaterShader->BindTexture(str_reflectionMap, WaterMgr->m_ReflectionTexture);
	if (WaterMgr->m_WaterRefraction)
	m->fancyWaterShader->BindTexture(str_refractionMap, WaterMgr->m_RefractionTexture);

	m->fancyWaterShader->BindTexture(str_losMap, losTexture.GetTextureSmooth());

	const CLightEnv& lightEnv = g_Renderer.GetLightEnv();

	// TODO: only bind what's really needed for that.
	m->fancyWaterShader->Uniform(str_sunDir, lightEnv.GetSunDir());
	m->fancyWaterShader->Uniform(str_sunColor, lightEnv.m_SunColor.X);
	m->fancyWaterShader->Uniform(str_color, WaterMgr->m_WaterColor);
	m->fancyWaterShader->Uniform(str_specularStrength, WaterMgr->m_SpecularStrength);
	m->fancyWaterShader->Uniform(str_waviness, WaterMgr->m_Waviness);
	m->fancyWaterShader->Uniform(str_murkiness, WaterMgr->m_Murkiness);
	m->fancyWaterShader->Uniform(str_tint, WaterMgr->m_WaterTint);
	m->fancyWaterShader->Uniform(str_reflectionTintStrength, WaterMgr->m_ReflectionTintStrength);
	m->fancyWaterShader->Uniform(str_reflectionTint, WaterMgr->m_ReflectionTint);
	m->fancyWaterShader->Uniform(str_translation, tx, ty);
	m->fancyWaterShader->Uniform(str_repeatScale, 1.0f / repeatPeriod);
	m->fancyWaterShader->Uniform(str_reflectionMatrix, WaterMgr->m_ReflectionMatrix);
	m->fancyWaterShader->Uniform(str_refractionMatrix, WaterMgr->m_RefractionMatrix);
	m->fancyWaterShader->Uniform(str_losMatrix, losTexture.GetTextureMatrix());
	m->fancyWaterShader->Uniform(str_cameraPos, camPos);
	m->fancyWaterShader->Uniform(str_fogColor, lightEnv.m_FogColor);
	m->fancyWaterShader->Uniform(str_fogParams, lightEnv.m_FogFactor, lightEnv.m_FogMax, 0.f, 0.f);
	m->fancyWaterShader->Uniform(str_time, (float)time);
	m->fancyWaterShader->Uniform(str_screenSize, (float)g_Renderer.GetWidth(), (float)g_Renderer.GetHeight(), 0.0f, 0.0f);
	m->fancyWaterShader->BindTexture(str_skyCube, g_Renderer.GetSkyManager()->GetSkyCube());

	if (shadow && WaterMgr->m_WaterShadows)
	{
		m->fancyWaterShader->BindTexture(str_shadowTex, shadow->GetTexture());
		m->fancyWaterShader->Uniform(str_shadowTransform, shadow->GetTextureMatrix());
		int width = shadow->GetWidth();
		int height = shadow->GetHeight();
		m->fancyWaterShader->Uniform(str_shadowScale, width, height, 1.0f / width, 1.0f / height);
	}

	for (size_t i = 0; i < m->visiblePatches.size(); ++i)
	{
		CPatchRData* data = m->visiblePatches[i];
		data->RenderWater(m->fancyWaterShader);
	}

	m->fancyWaterShader->Unbind();

	pglActiveTextureARB(GL_TEXTURE0);
	pglDeleteFramebuffersEXT(1, &FramebufferName);

	glDisable(GL_BLEND);

	return true;
}