本文整理汇总了C#中Device.SetRenderTarget方法的典型用法代码示例。如果您正苦于以下问题:C# Device.SetRenderTarget方法的具体用法?C# Device.SetRenderTarget怎么用?C# Device.SetRenderTarget使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Device
的用法示例。
在下文中一共展示了Device.SetRenderTarget方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RenderDatabaseTree
public static TextureMatrixLayer[] RenderDatabaseTree(RasterDatabase.RasterDatabase db, Device device)
{
// setup device
device.RenderState.ZBufferEnable = false;
device.Indices = null;
device.VertexFormat = CustomVertex.TransformedTextured.Format;
//device.Transform.World = Matrix.Identity;
Surface rt0 = device.GetRenderTarget(0);
// setup template quad
CustomVertex.TransformedTextured[] tQuad = new CustomVertex.TransformedTextured[4];
foreach(DataLayer layer in db.Layers)
{
RectangleGroupQuadTree tree = db.ProduceLayerMipMap(layer, 2048);
Texture[][] textures = new Texture[tree.Depth][];
for (int i = 1; i <= tree.Depth; i++)
{
RectangleGroupQuadTree.GroupNode[] nodes;
tree.GetNodes(i, out nodes);
textures[i] = new Texture[nodes.Length];
// render each node to texture
int texIdx = 0;
foreach (RectangleGroupQuadTree.GroupNode node in nodes)
{
Texture texture = textures[i][texIdx++] = new Texture(device, node.NodeArea.Width,
node.NodeArea.Height, 0,
Usage.WriteOnly, Format.X8R8G8B8,
Pool.Managed);
device.SetRenderTarget(0, texture.GetSurfaceLevel(0));
device.Clear(ClearFlags.Target, Color.Black, 1, 0);
device.BeginScene();
// draw each rectangle quad
foreach (DataArea area in node.Rectangles)
{
// setup quad
tQuad[0] = new CustomVertex.TransformedTextured(area.Area.Left, area.Area.Top, 1, 1,
area.TexCoords.Left, area.TexCoords.Top);
tQuad[1] = new CustomVertex.TransformedTextured(area.Area.Right, area.Area.Top, 1, 1,
area.TexCoords.Right, area.TexCoords.Top);
tQuad[2] = new CustomVertex.TransformedTextured(area.Area.Left, area.Area.Bottom, 1, 1,
area.TexCoords.Left, area.TexCoords.Bottom);
tQuad[3] = new CustomVertex.TransformedTextured(area.Area.Right, area.Area.Bottom, 1, 1,
area.TexCoords.Right, area.TexCoords.Bottom);
// render quad
device.SetTexture(0, (Texture)area.Data);
device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, tQuad);
}
device.EndScene();
}
}
}
device.SetRenderTarget(0, rt0);
device.RenderState.ZBufferEnable = true;
return null;
}
示例2: drawPostProcess
/// <summary>
/// Se toma todo lo dibujado antes, que se guardo en una textura, y se le aplica un shader para borronear la imagen
/// </summary>
private void drawPostProcess(Device d3dDevice)
{
//Arrancamos la escena
d3dDevice.BeginScene();
//Ver si el efecto de oscurecer esta activado, configurar Technique del shader segun corresponda
bool activar_efecto = (bool)GuiController.Instance.Modifiers["activar_efecto"];
//Hacer blur
if (activar_efecto)
{
float deviation = (float)GuiController.Instance.Modifiers["deviation"];
Surface blurTempS = blurTempRT.GetSurfaceLevel(0);
//Gaussian blur horizontal
Vector2[] texCoordOffsets;
float[] colorWeights;
TgcPostProcessingUtils.computeGaussianBlurSampleOffsets15(blurTempS.Description.Width, deviation, 1, true, out texCoordOffsets, out colorWeights);
effect.Technique = "GaussianBlurPass";
effect.SetValue("texSceneRT", sceneRT);
effect.SetValue("gauss_offsets", TgcParserUtils.vector2ArrayToFloat2Array(texCoordOffsets));
effect.SetValue("gauss_weights", colorWeights);
d3dDevice.SetRenderTarget(0, blurTempS);
d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
screenQuad.render(effect);
//Gaussian blur vertical
TgcPostProcessingUtils.computeGaussianBlurSampleOffsets15(blurTempS.Description.Height, deviation, 1, false, out texCoordOffsets, out colorWeights);
effect.Technique = "GaussianBlurPass";
effect.SetValue("texSceneRT", blurTempRT);
effect.SetValue("gauss_offsets", TgcParserUtils.vector2ArrayToFloat2Array(texCoordOffsets));
effect.SetValue("gauss_weights", colorWeights);
d3dDevice.SetRenderTarget(0, pOldRT);
d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
screenQuad.render(effect);
blurTempS.Dispose();
}
//No hacer blur
else
{
effect.Technique = "DefaultTechnique";
effect.SetValue("texSceneRT", sceneRT);
d3dDevice.SetRenderTarget(0, pOldRT);
d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
screenQuad.render(effect);
}
//Como estamos en modo CustomRenderEnabled, tenemos que dibujar todo nosotros, incluso el contador de FPS
GuiController.Instance.Text3d.drawText("FPS: " + HighResolutionTimer.Instance.FramesPerSecond, 0, 0, Color.Yellow);
//Tambien hay que dibujar el indicador de los ejes cartesianos
GuiController.Instance.AxisLines.render();
//Terminamos el renderizado de la escena
d3dDevice.EndScene();
}
示例3: GetSceneImage
public Image GetSceneImage(Scene scene)
{
RecalculateData(scene);
pp = new PresentParameters();
pp.SwapEffect = SwapEffect.Discard;
pp.Windowed = true;
pp.BackBufferWidth = 512;
pp.BackBufferHeight = 512;
pp.BackBufferFormat = Format.A8R8G8B8;
if(d3d != null)
{
d3d.Dispose();
}
if(device != null)
{
device.Dispose();
}
d3d = new Direct3D();
device = new Device(d3d, 0, DeviceType.Hardware, handle, CreateFlags.HardwareVertexProcessing, pp);
device.SetRenderState(RenderState.Lighting, true);
for(int i = scene.lights.Count; i < maxLights; ++i)
{
device.EnableLight(i, false);
}
maxLights = scene.lights.Count;
Modeler.Transformations.BoundingBox bb = new Modeler.Transformations.BoundingBox(scene);
Camera cam = new Camera();
Vector3 dir = Vector3.Normalize(bb.minBB - bb.maxBB);
cam.position = bb.maxBB - 1 * (float)Math.Sqrt((bb.minBB.x - bb.maxBB.x) * (bb.minBB.x - bb.maxBB.x) +
(bb.minBB.y - bb.maxBB.y) * (bb.minBB.y - bb.maxBB.y) + (bb.minBB.z - bb.maxBB.z) * (bb.minBB.z - bb.maxBB.z)) * dir;
cam.lookAt = bb.maxBB;
cam.fovAngle = 40;
cam.rotateAngle = 0;
Vector3 oldDir = defLight.Direction;
Vector3 dirLight = new Vector3();
dirLight.X = dir.X * (float)Math.Cos(-Math.PI / 4) + dir.Z * (float)Math.Sin(-Math.PI / 4);
dirLight.Y = dir.Y;
dirLight.Z = dir.Z * (float)Math.Cos(-Math.PI / 4) - dir.X * (float)Math.Sin(-Math.PI / 4);
dir.Normalize();
defLight.Direction = dirLight;
device.SetLight(0, defLight);
device.EnableLight(0, true);
device.SetRenderState(RenderState.FillMode, FillMode.Solid);
device.SetRenderState(RenderState.CullMode, Cull.None);
device.SetRenderState(RenderState.ShadeMode, ShadeMode.Gouraud);
Mesh mesh = numIndices >= 3 ? new Mesh(device, (int)numIndices / 3, scene.points.Count, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems) : null;
VertexBuffer vb = mesh != null ? mesh.VertexBuffer : null;
IndexBuffer ib = mesh != null ? mesh.IndexBuffer : null;
if(mesh != null)
{
vb.Lock(0, 0, LockFlags.None).WriteRange(vertices);
vb.Unlock();
ib.Lock(0, 0, LockFlags.None).WriteRange(indices, 0, (int)numIndices);
ib.Unlock();
}
Mesh selMesh = numSelIndices >= 3 ? new Mesh(device, (int)numSelIndices / 3, scene.points.Count, MeshFlags.Managed | MeshFlags.Use32Bit, vertexElems) : null;
VertexBuffer selvb = selMesh != null ? selMesh.VertexBuffer : null;
IndexBuffer selib = selMesh != null ? selMesh.IndexBuffer : null;
if(selMesh != null)
{
selvb.Lock(0, 0, LockFlags.None).WriteRange(vertices);
selvb.Unlock();
selib.Lock(0, 0, LockFlags.None).WriteRange(selIndices, 0, (int)numSelIndices);
selib.Unlock();
}
Viewport viewport = new Viewport(0, 0, 512, 512, 0, 1);
Texture texture = new Texture(device, 64, 64, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
device.SetRenderTarget(0, texture.GetSurfaceLevel(0));
float camRotAngle = cam.rotateAngle;
float aspect = 1;
float camAngle = 2.0f * (float)Math.Atan(Math.Tan(Utilities.DegToRad(cam.fovAngle) / 2.0f) / aspect);
device.SetTransform(TransformState.View, Matrix.LookAtRH(
cam.position,
cam.lookAt,
Utilities.RotatePointAroundVector(new Vector3(0, 1, 0),
Vector3.Normalize(cam.lookAt - cam.position), camRotAngle)));
device.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH(
camAngle,
aspect,
0.01f,
//.........这里部分代码省略.........
示例4: GetBezierImage
public Image GetBezierImage(BezierSurface bezier)
{
pp = new PresentParameters();
pp.SwapEffect = SwapEffect.Discard;
pp.Windowed = true;
pp.BackBufferWidth = 512;
pp.BackBufferHeight = 512;
pp.BackBufferFormat = Format.A8R8G8B8;
if (d3dBezier != null)
{
d3dBezier.Dispose();
}
if (deviceBezier != null)
{
deviceBezier.Dispose();
}
d3dBezier = new Direct3D();
deviceBezier = new Device(d3d, 0, DeviceType.Hardware, handle, CreateFlags.HardwareVertexProcessing, pp);
deviceBezier.SetRenderState(RenderState.Lighting, true);
deviceBezier.SetLight(0, defLight);
deviceBezier.EnableLight(0, true);
deviceBezier.SetRenderState(RenderState.FillMode, FillMode.Solid);
deviceBezier.SetRenderState(RenderState.CullMode, Cull.None);
deviceBezier.SetRenderState(RenderState.ShadeMode, ShadeMode.Gouraud);
Vertex[] gridVertices = new Vertex[bezier.OutputPoints.Length];
Vertex[] controlVertices = new Vertex[bezier.ControlPoints.Length];
List<int>[] vertexTriangle = new List<int>[bezier.OutputPoints.Length];
Parallel.For(0, vertexTriangle.Length, index => vertexTriangle[index] = new List<int>());
/*for(int i = 0; i < vertexTriangle.Length; ++i)
{
vertexTriangle[i] = new List<int>();
}*/
int[] indices = new int[3 * bezier.triangles.Count];
//int[] selIndices = new int[3 * scene.triangles.Count];
uint numIndices = 0;
//uint numSelIndices = 0;
//bool[] selPoints = new bool[scene.points.Count];
//Parallel.For(0, selPoints.Length, index => selPoints[index] = false);
for (int i = 0; i < bezier.triangles.Count; i++)
{
indices[numIndices++] = (int)bezier.triangles[i].p1;
indices[numIndices++] = (int)bezier.triangles[i].p2;
indices[numIndices++] = (int)bezier.triangles[i].p3;
vertexTriangle[bezier.triangles[i].p1].Add(i);
vertexTriangle[bezier.triangles[i].p2].Add(i);
vertexTriangle[bezier.triangles[i].p3].Add(i);
}
// Liczenie normalnych siatki trojkątów
for (int i = 0; i < bezier.OutputPoints.Length; i++)
{
Vector3 normal = new Vector3();
foreach (int face in vertexTriangle[i])
{
normal += Utilities.CalculateNormal(bezier.OutputPoints[(int)bezier.triangles[face].p3], bezier.OutputPoints[(int)bezier.triangles[face].p2],
bezier.OutputPoints[(int)bezier.triangles[face].p1]);
}
normal.Normalize();
gridVertices[i].Position = new Vector3(bezier.OutputPoints[i].x, bezier.OutputPoints[i].y, bezier.OutputPoints[i].z);
gridVertices[i].Normal = normal;
gridVertices[i].Color = Color.Beige.ToArgb();
}
Mesh gridMesh = numIndices > 2 ? new Mesh(deviceBezier, (int)numIndices / 3, bezier.OutputPoints.Length, MeshFlags.Managed | MeshFlags.Use32Bit,
vertexElems) : null;
VertexBuffer vb = gridMesh != null ? gridMesh.VertexBuffer : null;
IndexBuffer ib = gridMesh != null ? gridMesh.IndexBuffer : null;
if (gridMesh != null)
{
vb.Lock(0, 0, LockFlags.None).WriteRange(gridVertices);
vb.Unlock();
ib.Lock(0, 0, LockFlags.None).WriteRange(indices, 0, (int)numIndices);
ib.Unlock();
}
Viewport viewport = new Viewport(0, 0, 512, 512, 0, 1);
Texture texture = new Texture(deviceBezier, 64, 64, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
deviceBezier.SetRenderTarget(0, texture.GetSurfaceLevel(0));
float aspect = (float)perspective.Width / perspective.Height;
deviceBezier.SetTransform(TransformState.View, Matrix.LookAtRH(
bezierCam.position,
bezierCam.lookAt,
//.........这里部分代码省略.........
示例5: RenderBloom
public void RenderBloom(Device device, Effect effect)
{
BrightPassFilter(device, effect);
Surface orgTarget = device.GetRenderTarget(0);
Vector2[] sampleOffsets = new Vector2[16];
Vector4[] sampleWeights = new Vector4[16];
float[] afSampleOffsets = new float[16];
Surface surfDest = BloomTex[1].GetSurfaceLevel(0);
SurfaceDescription desc = BrightPassTex.GetSurfaceLevel(0).Description;
Surface OriginalDS = device.DepthStencilSurface;
device.DepthStencilSurface = Renderer.Instance.RenderDepthSurface;
GetSampleOffsets_Bloom(desc.Width, afSampleOffsets, ref sampleWeights, 3.0f, 1.25f);
for (int i = 0; i < 16; i++)
{
sampleOffsets[i] = new Vector2(afSampleOffsets[i], 0f);
}
effect.Technique = "Bloom";
effect.SetValue("g_avSampleWeights", sampleWeights);
effect.SetValue<Vector2>(new EffectHandle("g_avSampleOffsets"), sampleOffsets);
device.SetRenderTarget(0, surfDest);
device.SetTexture(0, BrightPassTex);
device.SetSamplerState(0, SamplerState.MinFilter, (int)TextureFilter.Point);
device.SetSamplerState(0, SamplerState.MagFilter, (int)TextureFilter.Point);
Renderer.Instance.DrawFullScreenQuad();
device.SetTexture(0, null);
surfDest.Dispose();
surfDest = BloomTex[0].GetSurfaceLevel(0);
GetSampleOffsets_Bloom(desc.Height, afSampleOffsets, ref sampleWeights, 3.0f, 1.25f);
for (int i = 0; i < 16; i++)
{
sampleOffsets[i] = new Vector2(0f, afSampleOffsets[i]);
}
effect.Technique = "Bloom";
effect.SetValue("g_avSampleWeights", sampleWeights);
effect.SetValue<Vector2>(new EffectHandle("g_avSampleOffsets"), sampleOffsets);
device.SetRenderTarget(0, surfDest);
device.SetTexture(0, BloomTex[1]);
Renderer.Instance.DrawFullScreenQuad();
device.SetTexture(0, null);
device.SetRenderTarget(0, orgTarget);
device.DepthStencilSurface = OriginalDS;
}
示例6: BrightPassFilter
private void BrightPassFilter(Device device, Effect effect)
{
Surface OriginalDS = device.DepthStencilSurface;
device.DepthStencilSurface = Renderer.Instance.RenderDepthSurface;
Vector2[] sampleOffsets = new Vector2[16];
Surface brightPassSurface = BrightPassTex.GetSurfaceLevel(0);
SurfaceDescription backbufDescr = Renderer.Instance.RenderSurface.Description;
GetSampleOffsetsets_DownScale3x3(backbufDescr.Width / 2, backbufDescr.Height / 2, ref sampleOffsets);
effect.SetValue<Vector2>(new EffectHandle("g_avSampleOffsets"), sampleOffsets);
effect.Technique = "DownScale3x3_BrightPass_RGBE8";
Surface orgTarget = device.GetRenderTarget(0);
device.SetRenderTarget(0, brightPassSurface);
device.SetTexture(0, Renderer.Instance.RenderTexture);
device.SetTexture(1, Renderer.Instance.AvgLum);
device.SetSamplerState(0, SamplerState.MagFilter, (int)TextureFilter.Linear);
device.SetSamplerState(0, SamplerState.MinFilter, (int)TextureFilter.Linear);
Renderer.Instance.DrawFullScreenQuad();
device.SetTexture(0, null);
device.SetTexture(1, null);
device.SetRenderTarget(0, orgTarget);
device.DepthStencilSurface = OriginalDS;
}
示例7: MeasureLuminance
public void MeasureLuminance(Effect effect, Device device)
{
Surface OriginalRenderTarget = device.GetRenderTarget(0);
Vector2[] sampleOffsets = new Vector2[16];
Texture dest = ToneMapTex[nToneMapTex - 1];
SurfaceDescription destDescr = dest.GetLevelDescription(0);
SurfaceDescription sourceDescr = Renderer.Instance.RenderSurface.Description;
GetSampleOffsets_DownScale2x2_Lum(sourceDescr.Width, sourceDescr.Height, destDescr.Width, destDescr.Height, ref sampleOffsets);
effect.SetValue<Vector2>(new EffectHandle("g_avSampleOffsets"), sampleOffsets);
effect.Technique = "DownScale2x2_Lum_RGBE8";
Surface destSurface = dest.GetSurfaceLevel(0);
device.SetRenderTarget(0, destSurface);
Surface OriginalDS = device.DepthStencilSurface;
device.DepthStencilSurface = Renderer.Instance.RenderDepthSurface;
device.SetSamplerState(0, SamplerState.MagFilter, (int)TextureFilter.Linear);
device.SetSamplerState(0, SamplerState.MinFilter, (int)TextureFilter.Linear);
device.SetTexture(0, Renderer.Instance.RenderTexture);
Renderer.Instance.DrawFullScreenQuad();
device.SetTexture(0, null);
effect.Technique = "DownScale3x3_RGBE8";
for (int i = nToneMapTex - 1; i > 0; i--)
{
Surface source = ToneMapTex[i].GetSurfaceLevel(0);
dest = ToneMapTex[i - 1];
destSurface = dest.GetSurfaceLevel(0);
GetSampleOffsetsets_DownScale3x3(destSurface.Description.Width, destSurface.Description.Height, ref sampleOffsets);
effect.SetValue<Vector2>(new EffectHandle("g_avSampleOffsets"), sampleOffsets);
device.SetRenderTarget(0, destSurface);
device.SetTexture(0, ToneMapTex[i]);
//device.SetSamplerState(0, SamplerStageStates.MagFilter, (int)TextureFilter.Point);
//device.SetSamplerState(0, SamplerStageStates.MinFilter, (int)TextureFilter.Point);
Renderer.Instance.DrawFullScreenQuad();
device.SetTexture(0, null);
}
// Add calculated luminance to average
if (CurrentAvgToneMap == -1)
{
Rectangle avgrect = new Rectangle(new Point(), new Size(1, 1));
device.StretchRectangle(ToneMapTex[0].GetSurfaceLevel(0), avgrect, avgToneMap[0].GetSurfaceLevel(0), avgrect, TextureFilter.Point);
CurrentAvgToneMap = 0;
}
else
{
effect.Technique = "CalcAvgLum_RGBE8";
device.SetTexture(0, avgToneMap[CurrentAvgToneMap]); // "main" average
int other = (CurrentAvgToneMap == 0 ? 1 : 0);
device.SetTexture(1, ToneMapTex[0]); // new average to be added
Surface surf = avgToneMap[other].GetSurfaceLevel(0);
device.SetRenderTarget(0, surf); // unused "main" average texture used as rendertarget
Renderer.Instance.DrawFullScreenQuad();
device.SetTexture(0, null);
device.SetTexture(1, null);
CurrentAvgToneMap = other; // swap avgTonemap usage next frame
}
device.SetRenderTarget(0, OriginalRenderTarget);
device.DepthStencilSurface = OriginalDS;
// Read result
device.GetRenderTargetData(AverageLum.GetSurfaceLevel(0), LockableAverage.GetSurfaceLevel(0));
DataRectangle rect = LockableAverage.LockRectangle(0, LockFlags.ReadOnly);
Color4 color = new Color4(rect.Data.Read<int>());
LockableAverage.UnlockRectangle(0);
// Revert LogLum encoding
float invLogLumRange = 1.0f / (MaxLogLum + MinLogLum);
float logLumOffset = MinLogLum * invLogLumRange;
double avgLum = Math.Exp((color.Alpha) / (invLogLumRange + logLumOffset));
effect.SetValue("avgLogLum", avgLum);
//System.Console.WriteLine(avgLum);
}