PDA

View Full Version : Real screen coordinates..



chronozphere
06-01-2007, 01:17 PM
Hi fellow-programmers 8)

I am using left-handed orthogonal projection... on the screen it looks like this:
.................+Y
..................|
..................|
..................|
-X _______0.0 ______ +X
..................|
..................|
..................|
.................-Y
I want normal screen coordinate which should look like this:

0.0___________ +X
.|
.|
.|
.|
+Y

What matrix do i need to convert this.
I was thinking of flipping the Y axis and translating the origin to the upper-left corner of the screen, but things aren't that simple :(

e.g when i use text, it is visible in the ortho view but not in my screen coordinate system. Propably because flipping Y, makes the mesh normals flip :(

Does someone know a way of converting to screen-coordinates... is it possible??? :?

Thank you in advance ;)

P.S i cant ASCII draw something with much spaces... this stupid editor removes them :(

JernejL
06-01-2007, 02:48 PM
in opengl?

Nitrogen
06-01-2007, 03:30 PM
Yea, what API are you using?

OpenGL provides the very convenient

glOrtho() command which does all this for you.

JernejL
06-01-2007, 03:51 PM
// Dwarf with Axe - GAMEDEV forums: 18 July 2002 6:12:57 PM
//
// There have been thousands of posts along the lines of
// "How do I do 2d in OpenGL" to "Duuuhde, I wunt too maek
// a two dee gaem in ohpun jee el; how do eye set uhp two dee???!?"
//
// I have developed a simple, nice, pretty way for all of you to have your 2D fun.

procedure GlEnable2D;
var
vport: array[0..3] of integer;
begin
glGetIntegerv(GL_VIEWPORT, @vPort);

glMatrixMode(GL_PROJECTION);
glPushMatrix;
glLoadIdentity;
glOrtho(0, vPort[2], 0, -vPort[3], -1, 1);

glMatrixMode(GL_MODELVIEW);
glPushMatrix;
glLoadIdentity;

// flip Y axis
glTranslatef(0, -vPort[3], 0);
end;

procedure GlDisable2D;
begin
glMatrixMode(GL_PROJECTION);
glPopMatrix;
glMatrixMode(GL_MODELVIEW);
glPopMatrix;
end;

this will turn on & off windows-like coordinate system in opengl which has coordinate system like you described and uses 1 unit = 1 pixel.

chronozphere
06-01-2007, 05:18 PM
Sorry... i should have told you guy's, i'm using DirectX :(

chronozphere
07-01-2007, 06:24 PM
anyone.. :? Such things should also be possible with DX isn't it?? :think:

JernejL
07-01-2007, 07:55 PM
sorry but most people use opengl since it works "better" with delphi than dx, but i'm sure you could make your own orthogonal projection matrix function based on opengl reference implementation on sgi's website.

JSoftware
07-01-2007, 08:23 PM
function orthomatrix(l,r,b,t: integer; zn,zf: single): tmatrix4x4;
begin
result := identitymatrix;
result[0] := 2.0 / (r-l);
result[5] := 2.0 / (t-b);
result[10] := -2.0 / (zf-zn);
result[12] := -(r+l) / (r-l);
result[13] := -(t+b) / (t-b);
result[14] := -(zf+zn) / (zf-zn);
result[15] := 1.0;
end;
This will probably work for you. It might need transposing as you use directx as it's made for opengl

chronozphere
09-01-2007, 06:25 PM
That will create the following matrix:

[2.0 / (r-l),0----------,0----------,0----------]
[0----------,2.0 / (t-b) ,0----------,0----------]
[0----------,0----------,-2.0 / (zf-zn) ,0----------]
[-(r+l) / (r-l),-(t+b) / (t-b),-(zf+zn) / (zf-zn),1]

The Orthogonal Matrix for D3D can be found here (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/directx9_c/D3DXMatrixOrthoLH.asp)
(I'm targetting the D3DXMatrixOrthoLH function)

I dont think it needs transposing, does it?? :)

Maybe a stupid question but what do the (r,l,t,b) parameters mean :? (i think it has something to do with the size of the window, but i need a more specific definition) :razz:

JSoftware
09-01-2007, 07:25 PM
right,left,top,bottom :)

with a 640x480 windows you would call it with params:
(0,640,480,0,znear,zfar)

This will ofcourse not produce exactly the result you want as the coordinate system will start in the bottomleft corner of the screen. I think that call it with the params: (0,640,0,480,znear,zfar) is what you want but I think that might invert the z-axis

chronozphere
09-01-2007, 07:29 PM
Thanx... i'll try that ;)

If it doesn't work.. i'll stick with the normal lefthanded coordinate system, because hey :razz:...
there is nothing wrong with it.