better grid

This commit is contained in:
codeimp 2007-11-11 13:51:01 +00:00
parent f0056b9745
commit a9d5c8ea35
3 changed files with 118 additions and 41 deletions

View file

@ -155,18 +155,27 @@ namespace CodeImp.DoomBuilder.Rendering
public void DrawGridLineH(int y, PixelColor c) public void DrawGridLineH(int y, PixelColor c)
{ {
int numpixels = visiblewidth >> 1; int numpixels = visiblewidth >> 1;
int offset = y & 0x01;
int ywidth = y * width;
// Draw all pixels on this line if((y >= 0) && (y < height))
for(int i = 0; i < numpixels; i++) DrawPixelSolid(i << 1, y, c); {
// Draw all pixels on this line
for(int i = 0; i < numpixels; i++) pixels[ywidth + ((i << 1) | offset)] = c;
}
} }
// This draws a dotted grid line vertically // This draws a dotted grid line vertically
public void DrawGridLineV(int x, PixelColor c) public void DrawGridLineV(int x, PixelColor c)
{ {
int numpixels = visibleheight >> 1; int numpixels = visibleheight >> 1;
int offset = x & 0x01;
// Draw all pixels on this line if((x >= 0) && (x < width))
for(int i = 0; i < numpixels; i++) DrawPixelSolid(x, i << 1, c); {
// Draw all pixels on this line
for(int i = 0; i < numpixels; i++) pixels[((i << 1) | offset) * width + x] = c;
}
} }
// This draws a pixel alpha blended // This draws a pixel alpha blended

View file

@ -57,6 +57,7 @@ namespace CodeImp.DoomBuilder.Rendering
#region ================== Variables #region ================== Variables
// Rendertargets // Rendertargets
private Texture backtex;
private Texture structtex; private Texture structtex;
private Texture thingstex; private Texture thingstex;
@ -68,14 +69,14 @@ namespace CodeImp.DoomBuilder.Rendering
private Size windowsize; private Size windowsize;
private Size structsize; private Size structsize;
private Size thingssize; private Size thingssize;
private Size backsize;
// Geometry plotter // Geometry plotter
private Plotter plotter; private Plotter plotter;
// Vertices to present the textures // Vertices to present the textures
private FlatVertex[] structverts; private VertexBuffer screenverts;
private FlatVertex[] thingsverts; private FlatVertex[] backimageverts;
private FlatVertex[] backverts;
// Batch buffer for things rendering // Batch buffer for things rendering
private VertexBuffer thingsvertices; private VertexBuffer thingsvertices;
@ -154,7 +155,7 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.Device.SetRenderState(RenderState.ZEnable, false); graphics.Device.SetRenderState(RenderState.ZEnable, false);
// Render a background image? // Render a background image?
if((backverts != null) && (General.Map.Grid.Background.Texture != null)) if((backimageverts != null) && (General.Map.Grid.Background.Texture != null))
{ {
// Set renderstates // Set renderstates
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false); graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
@ -163,17 +164,35 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.Shaders.Display2D.Texture1 = General.Map.Grid.Background.Texture; graphics.Shaders.Display2D.Texture1 = General.Map.Grid.Background.Texture;
graphics.Shaders.Display2D.SetSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_BLEND_FACTOR, 1f); graphics.Shaders.Display2D.SetSettings(1f / windowsize.Width, 1f / windowsize.Height, FSAA_BLEND_FACTOR, 1f);
// Draw the background // Draw the background image
graphics.Shaders.Display2D.Begin(); graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(0); graphics.Shaders.Display2D.BeginPass(1);
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, backverts); graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, backimageverts);
graphics.Shaders.Display2D.EndPass(); graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End(); graphics.Shaders.Display2D.End();
} }
// From here on only using screen vertices
graphics.Device.SetStreamSource(0, screenverts, 0, sizeof(FlatVertex));
// Render things in back? // Render things in back?
if(!thingsfront) PresentThings(THINGS_BACK_ALPHA); if(!thingsfront) PresentThings(THINGS_BACK_ALPHA);
// Set renderstates
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, false);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, true);
graphics.Device.SetTexture(0, backtex);
graphics.Shaders.Display2D.Texture1 = backtex;
graphics.Shaders.Display2D.SetSettings(1f / backsize.Width, 1f / backsize.Height, 0f, 1f);
// Draw the background grid
graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(1);
//graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, backverts);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End();
// Set renderstates // Set renderstates
graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true); graphics.Device.SetRenderState(RenderState.AlphaBlendEnable, true);
graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false); graphics.Device.SetRenderState(RenderState.AlphaTestEnable, false);
@ -187,7 +206,8 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.Shaders.Display2D.Begin(); graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(0); graphics.Shaders.Display2D.BeginPass(0);
//try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts); } catch(Exception) { } //try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts); } catch(Exception) { }
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts); //graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, structverts);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass(); graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End(); graphics.Shaders.Display2D.End();
@ -219,7 +239,8 @@ namespace CodeImp.DoomBuilder.Rendering
graphics.Shaders.Display2D.Begin(); graphics.Shaders.Display2D.Begin();
graphics.Shaders.Display2D.BeginPass(0); graphics.Shaders.Display2D.BeginPass(0);
//try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts); } catch(Exception) { } //try { graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts); } catch(Exception) { }
graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts); //graphics.Device.DrawUserPrimitives<FlatVertex>(PrimitiveType.TriangleStrip, 0, 2, thingsverts);
graphics.Device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
graphics.Shaders.Display2D.EndPass(); graphics.Shaders.Display2D.EndPass();
graphics.Shaders.Display2D.End(); graphics.Shaders.Display2D.End();
} }
@ -257,8 +278,12 @@ namespace CodeImp.DoomBuilder.Rendering
// Trash rendertargets // Trash rendertargets
if(structtex != null) structtex.Dispose(); if(structtex != null) structtex.Dispose();
if(thingstex != null) thingstex.Dispose(); if(thingstex != null) thingstex.Dispose();
if(backtex != null) backtex.Dispose();
if(screenverts != null) screenverts.Dispose();
structtex = null; structtex = null;
thingstex = null; thingstex = null;
backtex = null;
screenverts = null;
// Trash things batch buffer // Trash things batch buffer
if(thingsvertices != null) thingsvertices.Dispose(); if(thingsvertices != null) thingsvertices.Dispose();
@ -271,6 +296,8 @@ namespace CodeImp.DoomBuilder.Rendering
public void CreateRendertargets() public void CreateRendertargets()
{ {
SurfaceDescription sd; SurfaceDescription sd;
DataStream stream;
FlatVertex[] verts;
// Destroy rendertargets // Destroy rendertargets
DestroyRendertargets(); DestroyRendertargets();
@ -282,6 +309,7 @@ namespace CodeImp.DoomBuilder.Rendering
// Create rendertargets textures // Create rendertargets textures
structtex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed); structtex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
thingstex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default); thingstex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default);
backtex = new Texture(graphics.Device, windowsize.Width, windowsize.Height, 1, Usage.None, Format.A8R8G8B8, Pool.Managed);
// Get the real surface sizes // Get the real surface sizes
sd = structtex.GetLevelDescription(0); sd = structtex.GetLevelDescription(0);
@ -290,10 +318,19 @@ namespace CodeImp.DoomBuilder.Rendering
sd = thingstex.GetLevelDescription(0); sd = thingstex.GetLevelDescription(0);
thingssize.Width = sd.Width; thingssize.Width = sd.Width;
thingssize.Height = sd.Height; thingssize.Height = sd.Height;
sd = backtex.GetLevelDescription(0);
backsize.Width = sd.Width;
backsize.Height = sd.Height;
// Setup screen vertices // Create vertex buffers
structverts = CreateScreenVerts(structsize); screenverts = new VertexBuffer(graphics.Device, 4 * sizeof(FlatVertex), Usage.Dynamic | Usage.WriteOnly, VertexFormat.None, Pool.Default);
thingsverts = CreateScreenVerts(thingssize);
// Make screen vertices
stream = screenverts.Lock(0, 4 * sizeof(FlatVertex), LockFlags.Discard | LockFlags.NoSystemLock);
verts = CreateScreenVerts(structsize);
stream.WriteRange<FlatVertex>(verts);
screenverts.Unlock();
stream.Dispose();
} }
// This makes screen vertices for display // This makes screen vertices for display
@ -622,7 +659,7 @@ namespace CodeImp.DoomBuilder.Rendering
!(General.Map.Grid.Background is NullImage)) !(General.Map.Grid.Background is NullImage))
{ {
// Make vertices // Make vertices
backverts = CreateScreenVerts(windowsize); backimageverts = CreateScreenVerts(windowsize);
// Determine map coordinates for view window // Determine map coordinates for view window
ltpos = GetMapCoordinates(new Vector2D(0f, 0f)); ltpos = GetMapCoordinates(new Vector2D(0f, 0f));
@ -634,24 +671,48 @@ namespace CodeImp.DoomBuilder.Rendering
// Calculate UV coordinates // Calculate UV coordinates
// NOTE: backimagesize.y is made negative to match Doom's coordinate system // NOTE: backimagesize.y is made negative to match Doom's coordinate system
backverts[0].u = ltpos.x / backimagesize.x; backimageverts[0].u = ltpos.x / backimagesize.x;
backverts[0].v = ltpos.y / -backimagesize.y; backimageverts[0].v = ltpos.y / -backimagesize.y;
backverts[1].u = rbpos.x / backimagesize.x; backimageverts[1].u = rbpos.x / backimagesize.x;
backverts[1].v = ltpos.y / -backimagesize.y; backimageverts[1].v = ltpos.y / -backimagesize.y;
backverts[2].u = ltpos.x / backimagesize.x; backimageverts[2].u = ltpos.x / backimagesize.x;
backverts[2].v = rbpos.y / -backimagesize.y; backimageverts[2].v = rbpos.y / -backimagesize.y;
backverts[3].u = rbpos.x / backimagesize.x; backimageverts[3].u = rbpos.x / backimagesize.x;
backverts[3].v = rbpos.y / -backimagesize.y; backimageverts[3].v = rbpos.y / -backimagesize.y;
} }
else else
{ {
// No background image // No background image
backverts = null; backimageverts = null;
} }
} }
// This renders all grid
private void RenderBackgroundGrid()
{
Plotter gridplotter;
LockedRect lockedrect;
// Lock background rendertarget memory
lockedrect = backtex.LockRectangle(0, LockFlags.NoSystemLock);
// Create a plotter
gridplotter = new Plotter((PixelColor*)lockedrect.Data.DataPointer.ToPointer(), lockedrect.Pitch / sizeof(PixelColor), backsize.Height, backsize.Width, backsize.Height);
gridplotter.Clear();
// Render normal grid
RenderGrid(General.Map.Grid.GridSize, General.Colors.Grid, gridplotter);
// Render 64 grid
if(General.Map.Grid.GridSize <= 64) RenderGrid(64f, General.Colors.Grid64, gridplotter);
// Done
backtex.UnlockRectangle(0);
lockedrect.Data.Dispose();
}
// This renders the grid // This renders the grid
private void RenderGrid(float size, PixelColor c) private void RenderGrid(float size, PixelColor c, Plotter gridplotter)
{ {
Vector2D ltpos, rbpos; Vector2D ltpos, rbpos;
Vector2D pos = new Vector2D(); Vector2D pos = new Vector2D();
@ -673,7 +734,7 @@ namespace CodeImp.DoomBuilder.Rendering
{ {
pos.y = y; pos.y = y;
pos = pos.GetTransformed(translatex, translatey, scale, -scale); pos = pos.GetTransformed(translatex, translatey, scale, -scale);
plotter.DrawGridLineH((int)pos.y, c); gridplotter.DrawGridLineH((int)pos.y, c);
} }
// Draw all vertical grid lines // Draw all vertical grid lines
@ -681,7 +742,7 @@ namespace CodeImp.DoomBuilder.Rendering
{ {
pos.x = x; pos.x = x;
pos = pos.GetTransformed(translatex, translatey, scale, -scale); pos = pos.GetTransformed(translatex, translatey, scale, -scale);
plotter.DrawGridLineV((int)pos.x, c); gridplotter.DrawGridLineV((int)pos.x, c);
} }
} }
} }
@ -704,14 +765,7 @@ namespace CodeImp.DoomBuilder.Rendering
if(clearstructs) plotter.Clear(); if(clearstructs) plotter.Clear();
// Redraw grid when structures image was cleared // Redraw grid when structures image was cleared
if(clearstructs) if(clearstructs) RenderBackgroundGrid();
{
// Render normal grid
RenderGrid(General.Map.Grid.GridSize, General.Colors.Grid);
// Render 64 grid
if(General.Map.Grid.GridSize <= 64) RenderGrid(64f, General.Colors.Grid64);
}
// Always trash things batch buffer // Always trash things batch buffer
if(thingsvertices != null) thingsvertices.Dispose(); if(thingsvertices != null) thingsvertices.Dispose();

View file

@ -44,7 +44,7 @@ float4 addcolor(float4 c1, float4 c2)
} }
// Pixel shader // Pixel shader
float4 ps_main(PixelData pd) : COLOR float4 ps_fsaa(PixelData pd) : COLOR
{ {
// Take this pixel's color // Take this pixel's color
float4 c = tex2D(texture1samp, pd.uv); float4 c = tex2D(texture1samp, pd.uv);
@ -66,12 +66,26 @@ float4 ps_main(PixelData pd) : COLOR
else return float4(c.rgb, c.a * settings.w); else return float4(c.rgb, c.a * settings.w);
} }
// Pixel shader
float4 ps_normal(PixelData pd) : COLOR
{
// Take this pixel's color
float4 c = tex2D(texture1samp, pd.uv);
return float4(c.rgb, c.a * settings.w);
}
// Technique for shader model 2.0 // Technique for shader model 2.0
technique SM20 technique SM20
{ {
pass p0 pass p0
{ {
VertexShader = null; VertexShader = null;
PixelShader = compile ps_2_0 ps_main(); PixelShader = compile ps_2_0 ps_fsaa();
}
pass p1
{
VertexShader = null;
PixelShader = compile ps_2_0 ps_normal();
} }
} }