02 August 2023

Windows 8-11 - start something automatically on start-up

For my work, I want a bat-file to run when Windows starts up. This starts up a local database server with my test database I use for all my development.

Startup folder

To do this, launch Explorer and go to %AppData%\Microsoft\Windows\Start Menu\Programs\Startup. Paste a shortcut to the file you want to run in this folder.

Run a command

Alternatively, you can run a command via Windows' Task Scheduler. Create a new task and set it for Windows 8 or 10. Add a trigger and set it to at log in. Add an action and enter the command including any arguments.

13 January 2022

SQL - check whether or not a column exists

This is a simple way for SQL Server and Sybase databases to check whether a column exists or not inside a query:

COL_LENGTH('Keywords', 'parent_id')

But you can't use that in a select clause. There is another trick though to select a column that may or may not exist:

SELECT (
  SELECT Keyw2.parent_id
  FROM (SELECT 0 AS parent_id) [Dummy]
  CROSS APPLY (SELECT FIRST parent_id FROM dba.Keywords WHERE keyword_id = Keyw.keyword_id) [Keyw2]
  ) [KeywordParent],
  ...
FROM dba.Keywords Keyw

The trick relies on not using a table alias in the subselect!

This can come in handy when columns may or may not exist depending on the application's database version.

07 January 2022

Visual Studio - High contrast syntax colors

When Windows is in a high contrast theme, Visual Studio displays all code in the same color. To change this, go to Tools > Options > Environment > Fonts and Colors and select Show settings for Text Editor. Now you can set the colors for identifiers (variable names), (user) type names, strings (text between quotes), literals, numbers, comments etc. This is my setup:

I've put this here so that next time I reinstall Visual Studio, I can refer to this screenshot to remember what colors I used.

Note that the Plain Text foreground is used for the caret's color.

02 March 2021

Math - Normalize a 3D vector

This method is part of the Vector3 class, and changes the vector into a unit vector.
void Normalize() {
  if (IsZero) return; //X, Y and Z are 0: it would set each component to NaN.
  float length = (float)Math.Sqrt(X * X + Y * Y + Z * Z);
  X /= length;
  Y /= length;
  Z /= length;
}

Math - Calculate a normal vector from three position vectors

This Vector3 constructor calculates a surface normal from three 3D vectors.
public Vector3(Vector3 a, Vector3 b, Vector3 c) {
  var u = b - a; //Requires an overloaded minus
  var v = c - a; //operator for two vector3's.
  X = (u.Y * v.Z) - (u.Z * v.Y);
  Y = (u.Z * v.X) - (u.X * v.Z);
  Z = (u.X * v.Y) - (u.Y * v.X);
  Normalize();
}

08 August 2020

Using Hiero Font Tool to create a distance field font to use with shaders

This article is about text rendering with a 3D graphics library, like OpenGL. A regular font atlas texture doesn't scale well. To render text that looks sharp at any size, you need a distance field texture atlas. Hiero is a tool that can create such a font. It requires Java.

  • Pick a font (system or file) and the characters you want (sample text).
  • Set Rendering to Java, so the effects are listed.
  • Remove the Color effect and add a Distance field effect.
  • Set it's Scale to 15 and Spread to 10. This high scale will make the sample slow to regenerate, so maybe do this last.
  • At the right-bottom, set X and Y to 0 and the four Padding values to 8.
  • Increase or decrease the Size of the glyphs, so they all fit on one page.

Now you can save the font. Hiero will create a png image of the glyphs and a fnt file describing their positions and sizes. With these, you can generate textured quads for your text. A specific fragment shader will use the distance fields in the texture to render sharp text.

21 July 2020

Math - Calculate a look-at matrix for OpenGL (II)

This is a simplification of the method to create a look-at or camera matrix from 2017. Based on DirectX documentation, it creates the matrix in one step, without multiplication. The matrix is created transposed for OpenGL. This method takes in a Vector3 Position and Direction.

This matrix doesn't work when looking straight up or down, because in that case the x-axis cross product fails. To solve this, manually set the x-axis in that case.

bool isVertical = direction.X == 0 && direction.Z == 0;
var up = new Vector3(0, 1, 0);
var zAxis = direction.Normalized();
var xAxis = isVertical ? new Vector3(1, 0, 0) : Vector3.CrossProduct(up, zAxis).Normalized();
var yAxis = Vector3.CrossProduct(zAxis, xAxis);

return new Matrix(
    xAxis.X, xAxis.Y, xAxis.Z, -Vector3.DotProduct(xAxis, position),
    yAxis.X, yAxis.Y, yAxis.Z, -Vector3.DotProduct(yAxis, position),
    zAxis.X, zAxis.Y, zAxis.Z, -Vector3.DotProduct(zAxis, position),
    0, 0, 0, 1
);