Skip to content

Commit

Permalink
2.7.5 DirectDraw BGR fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
BiologyTools committed Dec 1, 2022
1 parent b901253 commit dc21788
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 107 deletions.
6 changes: 5 additions & 1 deletion Graphics/DBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,18 @@ public unsafe static Bitmap FromImage(RenderTarget renderTarget, System.Drawing.
int w = image.Width;
int h = image.Height;
Bitmap b = null;
// Loads from file using System.Drawing.Image
//For RGB images we switch to BGR
if(image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed && image.PixelFormat != System.Drawing.Imaging.PixelFormat.Format16bppGrayScale)
image = BufferInfo.SwitchRedBlue(image);
BitmapData d = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
using (var bitmap = (System.Drawing.Bitmap)image)
{

var bitmapProperties = new BitmapProperties(new PixelFormat(Format.R8G8B8A8_UNorm, AlphaMode.Ignore));
var size = new Size2(bitmap.Width, bitmap.Height);
if (image.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
{

b = new Bitmap(renderTarget, size, new DataPointer(d.Scan0, bitmap.Width * 4 * bitmap.Height), bitmap.Width * 4, bitmapProperties);
return b;
}
Expand Down
10 changes: 5 additions & 5 deletions Graphics/Graphics/DColorShader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,18 @@ private void ShuddownShader()
VertexShader?.Dispose();
VertexShader = null;
}
public bool Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, IntRange r, IntRange g, IntRange b)
public bool Render(DeviceContext deviceContext, int indexCount, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, IntRange r, IntRange g, IntRange b, float interval, float alpha)
{
// Set the shader parameters that it will use for rendering.
if (!SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, r, g, b))
if (!SetShaderParameters(deviceContext, worldMatrix, viewMatrix, projectionMatrix, r, g, b,interval, alpha))
return false;

// Now render the prepared buffers with the shader.
RenderShader(deviceContext, indexCount);

return true;
}
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, IntRange r, IntRange g, IntRange b)
private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix, Matrix viewMatrix, Matrix projectionMatrix, IntRange r, IntRange g, IntRange b, float interval, float alpha)
{
try
{
Expand All @@ -164,14 +164,14 @@ private bool SetShaderParameters(DeviceContext deviceContext, Matrix worldMatrix
f = ushort.MaxValue;
else
f = byte.MaxValue;

// Copy the matrices into the constant buffer.
DMatrixBuffer matrixBuffer = new DMatrixBuffer()
{
world = worldMatrix,
view = viewMatrix,
projection = projectionMatrix,
rMinMax = new Vector4((float)r.Min / f, (float)r.Max / f, 0, 0),
rMinMax = new Vector4((float)r.Min / f, (float)r.Max / f, interval, alpha),
gMinMax = new Vector4((float)g.Min / f, (float)g.Max / f, 0, 0),
bMinMax = new Vector4((float)b.Min / f, (float)b.Max / f, 0, 0)
};
Expand Down
6 changes: 3 additions & 3 deletions Graphics/Graphics/DDX11.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,14 @@ public bool Initialize(DSystemConfiguration configuration, IntPtr windowHandle)
// Setup the raster description which will determine how and what polygon will be drawn.
RasterizerStateDescription rasterDesc = new RasterizerStateDescription()
{
IsAntialiasedLineEnabled = true,
IsAntialiasedLineEnabled = false,
CullMode = CullMode.Back,
DepthBias = 0,
DepthBiasClamp = .0f,
IsDepthClipEnabled = true,
IsDepthClipEnabled = false,
FillMode = FillMode.Solid,
IsFrontCounterClockwise = false,
IsMultisampleEnabled = true,
IsMultisampleEnabled = false,
IsScissorEnabled = false,
SlopeScaledDepthBias = .0f
};
Expand Down
8 changes: 4 additions & 4 deletions Graphics/Graphics/DGraphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void ShutDown()
D3D?.ShutDown();
D3D = null;
}
public bool Frame(IntRange r, IntRange g, IntRange b)
public bool Frame(IntRange r, IntRange g, IntRange b, float interval, float alpha)
{
// Render the graphics scene.
return Render(r,g,b);
return Render(r,g,b,interval,alpha);
}
private bool Render(IntRange r, IntRange g, IntRange b)
private bool Render(IntRange r, IntRange g, IntRange b, float interval, float alpha)
{
// Clear the buffer to begin the scene.
D3D.BeginScene(0.0f, 0.0f, 0.0f, 1f);
Expand All @@ -101,7 +101,7 @@ private bool Render(IntRange r, IntRange g, IntRange b)
Model.Render(D3D.DeviceContext);

// Render the model using the color shader.
if (!ColorShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, r, g, b))
if (!ColorShader.Render(D3D.DeviceContext, Model.IndexCount, worldMatrix, viewMatrix, projectionMatrix, r, g, b, interval, alpha))
return false;

// Present the rendered scene to the screen.
Expand Down
4 changes: 2 additions & 2 deletions Graphics/Graphics/DModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private bool InitializeBuffer(Device device, BioImage im)
// Create the vertex array and load it with data.
var vertices = new DColorShader.DVertex[VertexCount];
ColorS col;
for (int z = im.SizeZ - 1; z >= 0; z--)
for (int z = 0; z < im.SizeZ; z++)
{
for (int y = im.SizeY-1; y >= 0; y--)
{
Expand All @@ -54,7 +54,7 @@ private bool InitializeBuffer(Device device, BioImage im)
vec.W = 0.5f;
vertices[ind] = new DColorShader.DVertex()
{
position = new Vector3((float)x / (float)im.SizeX, (float)y / (float)im.SizeY, ((float)z / im.SizeZ) * 0.1f),
position = new Vector3((float)x / (float)im.SizeX, (float)y / (float)im.SizeY, (z / (float)im.SizeZ)*(float)im.physicalSizeZ),
color = vec
};
}
Expand Down
42 changes: 20 additions & 22 deletions Graphics/Shaders/color-gs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,29 @@ float4 ToClipSpace(float2 screenPos, float originalClipPosW)
[maxvertexcount(6)]
void ColorGeometryShader(point v2g input[1], inout TriangleStream<g2f> triStream)
{
if(input[0].color.w != 0)
{
g2f v;
float2 screenPos = FromClipSpace(input[0].position);
float clipPosW = input[0].position.w;
v.color = input[0].color;
float s = 0.02;
v.position = ToClipSpace(screenPos + float2(-s, -s), clipPosW);
triStream.Append(v);
g2f v;
float2 screenPos = FromClipSpace(input[0].position);
float clipPosW = input[0].position.w;
v.color = input[0].color;
float s = 0.02;
v.position = ToClipSpace(screenPos + float2(-s, -s), clipPosW);
triStream.Append(v);

v.position = ToClipSpace(screenPos + float2(-s, s), clipPosW);
triStream.Append(v);
v.position = ToClipSpace(screenPos + float2(-s, s), clipPosW);
triStream.Append(v);

v.position = ToClipSpace(screenPos + float2(s, s), clipPosW);
triStream.Append(v);
triStream.RestartStrip();
v.position = ToClipSpace(screenPos + float2(s, s), clipPosW);
triStream.Append(v);
triStream.RestartStrip();

v.position = ToClipSpace(screenPos + float2(s, -s), clipPosW);
triStream.Append(v);
v.position = ToClipSpace(screenPos + float2(s, -s), clipPosW);
triStream.Append(v);

v.position = ToClipSpace(screenPos + float2(-s, -s), clipPosW);
triStream.Append(v);
v.position = ToClipSpace(screenPos + float2(-s, -s), clipPosW);
triStream.Append(v);

v.position = ToClipSpace(screenPos + float2(s, s), clipPosW);
triStream.Append(v);
triStream.RestartStrip();
}
v.position = ToClipSpace(screenPos + float2(s, s), clipPosW);
triStream.Append(v);
triStream.RestartStrip();

}
20 changes: 11 additions & 9 deletions Graphics/Shaders/color-vs.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -42,39 +42,41 @@ PixelInputType ColorVertexShader(VertexInputType input)
output.color = float4(0, 0, 0, 0);
// Change the position vector to be 4 units for proper matrix calculations.
input.position.w = 1.0f;
if ((input.color.x > rMinMax.x))
if ((input.color.x >= rMinMax.x))
{
float r = 1 / (rMinMax.y - rMinMax.x);
input.color.x *= r;
input.color.w = 0.8f;
input.color.w = (input.color.x * input.color.y * input.color.z) * (1 / rMinMax.w);
}
else
{
input.color.x = 0;
}
if (input.color.y > gMinMax.x)
if (input.color.y >= gMinMax.x)
{
float g = 1 / (gMinMax.y - gMinMax.x);
input.color.y *= g;
input.color.w = 0.8f;
input.color.w = (input.color.x * input.color.y * input.color.z) * (1 / rMinMax.w);
}
else
{
input.color.y = 0;
}
if (input.color.z > bMinMax.x)
if (input.color.z >= bMinMax.x)
{
float b = 1 / (bMinMax.y - bMinMax.x);
input.color.z *= b;
input.color.w = 0.8f;
input.color.w = (input.color.x * input.color.y * input.color.z) * (1 / rMinMax.w);
}
else
{
input.color.z = 0;
}
if (!(input.color.x > rMinMax.x) && !(input.color.y > gMinMax.x) && !(input.color.z > bMinMax.x))
input.color.w = 0.0f;
// Calculate the position of the vertex against the world, view, and projection matrices.
//if (!(input.color.x >= rMinMax.x) && !(input.color.y >= gMinMax.x) && !(input.color.z >= bMinMax.x))
// input.color.w = 0.0f;

input.position.z = input.position.z * rMinMax.z;
//Calculate the position of the vertex against the world, view, and projection matrices.
output.position = mul(input.position, worldMatrix);
output.position = mul(output.position, viewMatrix);
output.position = mul(output.position, projectionMatrix);
Expand Down
4 changes: 2 additions & 2 deletions Graphics/System/DSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public virtual bool Initialize(string title, int width, int height, bool vSync,
}
return result;
}
public bool Frame(IntRange r, IntRange g, IntRange b)
public bool Frame(IntRange r, IntRange g, IntRange b, float interval, float alpha)
{
// Check if the user pressed escape and wants to exit the application.
if (Input.IsKeyDown(Keys.Escape))
return false;
// Do the frame processing for the graphics object.
return Graphics.Frame(r,g,b);
return Graphics.Frame(r,g,b,interval,alpha);
}
public void ShutDown()
{
Expand Down
5 changes: 2 additions & 3 deletions Graphics/System/DSystemConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,10 @@ public DSystemConfiguration(string title, int width, int height, bool fullScreen
// Static Constructor
static DSystemConfiguration()
{
VerticalSyncEnabled = false;
VerticalSyncEnabled = true;
ScreenDepth = 1000.0f;
ScreenNear = 0.1f;
ScreenNear = -0.1f;
BorderStyle = FormBorderStyle.None;

ShaderFilePath = Application.StartupPath + @"\Graphics\Shaders\";
}
}
Expand Down
Loading

0 comments on commit dc21788

Please sign in to comment.