Saturday, June 6, 2015

Transformation matrix that maps a window



I'm currently learning OpenGL at uni, and they give us questions to help us learn (these are not worth anything), however I'm stuck on this one question and would have to travel over an hour and a half to uni for an answer.


How do I do this question? Please include as many steps as you can, I want to be able to follow exactly how to do this.


Find the transformation that maps a window whose lower left corner is at (1,1) and upper right corner is at (3,5) onto:




  1. The entire device screen whose dimension is (600, 500)




  2. A viewport that has lower left corner at (100,100) and upper right corner at (400,400)





Edit: Damn sorry I should have added I am meant to find the matrix, so no code.



Answer



You need a combination of scale and translation matrices. You could first go to a "normalized" screen space (origin at 0,0 and scaling to 1,1) using, in pseudo-code:


MatToNormalized = Translation(-1, -1) x Scale(1/2, 1/4)

Then you can easily map to any kind of screen space. E.g. for question 1.:


MatToFullscreen = MatToNormalized x Scale(600, 500)

And for question 2.:



MatToViewport = MatToNormalized x Scale(300, 300) x Translation(100, 100)

EDIT


A Scilab dump with the actual matrices:


MatToNormalized = [1 0 0; 0 1 0; -1 -1 1] * [1/2 0 0; 0 1/4 0; 0 0 1]
0.5 0. 0.
0. 0.25 0.
- 0.5 - 0.25 1.

MatToFullscreen = MatToNormalized * [600 0 0; 0 500 0; 0 0 1]

300. 0. 0.
0. 125. 0.
- 300. - 125. 1.

TestMinFullScreen = [1 1 1] * MatToFullscreen
0. 0. 1.

TestMaxFullScreen = [3 5 1] * MatToFullscreen
600. 500. 1.


MatToViewport = MatToNormalized * [300 0 0; 0 300 0; 0 0 1] * [1 0 0; 0 1 0; 100 100 1]
150. 0. 0.
0. 75. 0.
- 50. 25. 1.

TestMinViewport = [1 1 1] * MatToViewport
100. 100. 1.

TestMaxViewport = [3 5 1] * MatToViewport
400. 400. 1.


Please note that Scilab uses a column-major notation, so if you want the row-major notation, just transpose the result (' is Scilab's transpose operator):


MatToFullscreen'
300. 0. - 300.
0. 125. - 125.
0. 0. 1.

MatToNormalized'
0.5 0. - 0.5
0. 0.25 - 0.25

0. 0. 1.

If you don't get why the translation and scale matrices are formatted this way, try Wikipedia.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...