-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af23c68
commit b03d518
Showing
20 changed files
with
4,533 additions
and
279 deletions.
There are no files selected for viewing
444 changes: 444 additions & 0 deletions
444
TgcViewer/Examples/Shaders/WorkshopShaders/F1Circuit.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Windows.Forms; | ||
using TgcViewer.Example; | ||
using TgcViewer; | ||
using Microsoft.DirectX.Direct3D; | ||
using Microsoft.DirectX; | ||
using TgcViewer.Utils.TgcSceneLoader; | ||
using System.Drawing; | ||
using TgcViewer.Utils.TgcGeometry; | ||
using TgcViewer.Utils.Terrain; | ||
using TgcViewer.Utils.Input; | ||
using TgcViewer.Utils.Shaders; | ||
using TgcViewer.Utils; | ||
|
||
namespace Examples.Shaders.WorkshopShaders | ||
{ | ||
|
||
public class FullScreenQuad : TgcExample | ||
{ | ||
string MyMediaDir; | ||
string MyShaderDir; | ||
List<TgcMesh> meshes; | ||
Effect effect; | ||
Surface g_pDepthStencil; // Depth-stencil buffer | ||
Texture g_pRenderTarget; | ||
VertexBuffer g_pVBV3D; | ||
public float time; | ||
|
||
|
||
public override string getCategory() | ||
{ | ||
return "Shaders"; | ||
} | ||
|
||
public override string getName() | ||
{ | ||
return "Workshop-FullScreenQuad"; | ||
} | ||
|
||
public override string getDescription() | ||
{ | ||
return "full screen quad"; | ||
} | ||
|
||
public override void init() | ||
{ | ||
GuiController.Instance.CustomRenderEnabled = true; | ||
time = 0; | ||
|
||
Device d3dDevice = GuiController.Instance.D3dDevice; | ||
MyMediaDir = GuiController.Instance.ExamplesDir + "Shaders\\WorkshopShaders\\Media\\"; | ||
MyShaderDir = GuiController.Instance.ExamplesDir + "Shaders\\WorkshopShaders\\Shaders\\"; | ||
|
||
//Cargamos un escenario | ||
TgcSceneLoader loader = new TgcSceneLoader(); | ||
TgcScene scene = loader.loadSceneFromFile(GuiController.Instance.ExamplesMediaDir + "MeshCreator\\Scenes\\Deposito\\Deposito-TgcScene.xml"); | ||
meshes = scene.Meshes; | ||
|
||
//Cargar Shader personalizado | ||
string compilationErrors; | ||
effect = Effect.FromFile(GuiController.Instance.D3dDevice, | ||
GuiController.Instance.ExamplesDir + "Shaders\\WorkshopShaders\\Shaders\\FullQuad.fx", | ||
null, null, ShaderFlags.PreferFlowControl, null, out compilationErrors); | ||
if (effect == null) | ||
{ | ||
throw new Exception("Error al cargar shader. Errores: " + compilationErrors); | ||
} | ||
//Configurar Technique dentro del shader | ||
effect.Technique = "DefaultTechnique"; | ||
|
||
//Camara en primera personas | ||
GuiController.Instance.FpsCamera.Enable = true; | ||
GuiController.Instance.FpsCamera.setCamera(new Vector3(-182.3816f, 82.3252f, -811.9061f), new Vector3(-182.0957f, 82.3147f, -810.9479f)); | ||
|
||
g_pDepthStencil = d3dDevice.CreateDepthStencilSurface(d3dDevice.PresentationParameters.BackBufferWidth, | ||
d3dDevice.PresentationParameters.BackBufferHeight, | ||
DepthFormat.D24S8, | ||
MultiSampleType.None, | ||
0, | ||
true); | ||
|
||
// inicializo el render target | ||
g_pRenderTarget = new Texture(d3dDevice, d3dDevice.PresentationParameters.BackBufferWidth | ||
, d3dDevice.PresentationParameters.BackBufferHeight, 1, Usage.RenderTarget, | ||
Format.X8R8G8B8, Pool.Default); | ||
|
||
effect.SetValue("g_RenderTarget", g_pRenderTarget); | ||
|
||
// Resolucion de pantalla | ||
effect.SetValue("screen_dx", d3dDevice.PresentationParameters.BackBufferWidth); | ||
effect.SetValue("screen_dy", d3dDevice.PresentationParameters.BackBufferHeight); | ||
|
||
CustomVertex.PositionTextured[] vertices = new CustomVertex.PositionTextured[] | ||
{ | ||
new CustomVertex.PositionTextured( -1, 1, 1, 0,0), | ||
new CustomVertex.PositionTextured(1, 1, 1, 1,0), | ||
new CustomVertex.PositionTextured(-1, -1, 1, 0,1), | ||
new CustomVertex.PositionTextured(1,-1, 1, 1,1) | ||
}; | ||
//vertex buffer de los triangulos | ||
g_pVBV3D = new VertexBuffer(typeof(CustomVertex.PositionTextured), | ||
4, d3dDevice, Usage.Dynamic | Usage.WriteOnly, | ||
CustomVertex.PositionTextured.Format, Pool.Default); | ||
g_pVBV3D.SetData(vertices, 0, LockFlags.None); | ||
|
||
GuiController.Instance.Modifiers.addBoolean("activar_efecto", "Activar efecto", true); | ||
|
||
} | ||
|
||
|
||
public override void render(float elapsedTime) | ||
{ | ||
Device device = GuiController.Instance.D3dDevice; | ||
Control panel3d = GuiController.Instance.Panel3d; | ||
float aspectRatio = (float)panel3d.Width / (float)panel3d.Height; | ||
|
||
time += elapsedTime; | ||
bool activar_efecto = (bool)GuiController.Instance.Modifiers["activar_efecto"]; | ||
|
||
//Cargar variables de shader | ||
|
||
// dibujo la escena una textura | ||
effect.Technique = "DefaultTechnique"; | ||
// guardo el Render target anterior y seteo la textura como render target | ||
Surface pOldRT = device.GetRenderTarget(0); | ||
Surface pSurf = g_pRenderTarget.GetSurfaceLevel(0); | ||
if (activar_efecto) | ||
device.SetRenderTarget(0, pSurf); | ||
// hago lo mismo con el depthbuffer, necesito el que no tiene multisampling | ||
Surface pOldDS = device.DepthStencilSurface; | ||
// Probar de comentar esta linea, para ver como se produce el fallo en el ztest | ||
// por no soportar usualmente el multisampling en el render to texture. | ||
if (activar_efecto) | ||
device.DepthStencilSurface = g_pDepthStencil; | ||
|
||
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); | ||
device.BeginScene(); | ||
|
||
//Dibujamos todos los meshes del escenario | ||
foreach (TgcMesh m in meshes) | ||
{ | ||
m.render(); | ||
} | ||
device.EndScene(); | ||
pSurf.Dispose(); | ||
|
||
if (activar_efecto) | ||
{ | ||
// restuaro el render target y el stencil | ||
device.DepthStencilSurface = pOldDS; | ||
device.SetRenderTarget(0, pOldRT); | ||
|
||
// dibujo el quad pp dicho : | ||
device.BeginScene(); | ||
effect.Technique = "PostProcess"; | ||
effect.SetValue("time", time); | ||
device.VertexFormat = CustomVertex.PositionTextured.Format; | ||
device.SetStreamSource(0, g_pVBV3D, 0); | ||
effect.SetValue("g_RenderTarget", g_pRenderTarget); | ||
|
||
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0); | ||
effect.Begin(FX.None); | ||
effect.BeginPass(0); | ||
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2); | ||
effect.EndPass(); | ||
effect.End(); | ||
|
||
GuiController.Instance.Text3d.drawText("FPS: " + HighResolutionTimer.Instance.FramesPerSecond, 0, 0, Color.Yellow); | ||
device.EndScene(); | ||
} | ||
|
||
} | ||
|
||
public override void close() | ||
{ | ||
foreach (TgcMesh m in meshes) | ||
{ | ||
m.dispose(); | ||
} effect.Dispose(); | ||
g_pRenderTarget.Dispose(); | ||
g_pVBV3D.Dispose(); | ||
g_pDepthStencil.Dispose(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.