JoshKlint

10-02-2010, 04:00 PM

I am having trouble constructing an orthographic matrix for 2D drawing. I am using this code to make my matrix:

float right = width;

float left = 0;

float top = 0;

float bottom = height;

orthomatrix[0].x = -2.0/(right-left);

orthomatrix[0].y = 0;

orthomatrix[0].z = 0;

orthomatrix[0].w = -(right+left)/(right-left);

orthomatrix[1].x = 0;

orthomatrix[1].y = 2.0/(top-bottom);

orthomatrix[1].z = 0;

orthomatrix[1].w = -(top+bottom)/(top-bottom);

orthomatrix[2].x = 0;

orthomatrix[2].y = 0;

orthomatrix[2].z = 2.0/(Zfar-Znear);

orthomatrix[2].w = -(Zfar+Znear)/(Zfar-Znear);

orthomatrix[3].x = -1.0;

orthomatrix[3].y = +1.0;

orthomatrix[3].z = 0;

orthomatrix[3].w = 1;

I would like to have it so that vertex float coordinates correspond perfectly to pixels, so the user can create a vertex array and use the pixel coordinates for vertex XY positions.

I can get correct values by adding 0.5 to the vertex positions and dividing by the graphics viewport width and height. Can anyone help me make a matrix so that a vertex at position 5,7,0 will appear at pixel 5,7, where the top pixel of the viewport is 0?

Thanks.

float right = width;

float left = 0;

float top = 0;

float bottom = height;

orthomatrix[0].x = -2.0/(right-left);

orthomatrix[0].y = 0;

orthomatrix[0].z = 0;

orthomatrix[0].w = -(right+left)/(right-left);

orthomatrix[1].x = 0;

orthomatrix[1].y = 2.0/(top-bottom);

orthomatrix[1].z = 0;

orthomatrix[1].w = -(top+bottom)/(top-bottom);

orthomatrix[2].x = 0;

orthomatrix[2].y = 0;

orthomatrix[2].z = 2.0/(Zfar-Znear);

orthomatrix[2].w = -(Zfar+Znear)/(Zfar-Znear);

orthomatrix[3].x = -1.0;

orthomatrix[3].y = +1.0;

orthomatrix[3].z = 0;

orthomatrix[3].w = 1;

I would like to have it so that vertex float coordinates correspond perfectly to pixels, so the user can create a vertex array and use the pixel coordinates for vertex XY positions.

I can get correct values by adding 0.5 to the vertex positions and dividing by the graphics viewport width and height. Can anyone help me make a matrix so that a vertex at position 5,7,0 will appear at pixel 5,7, where the top pixel of the viewport is 0?

Thanks.