0
votes

I'm new to OpenGL. Hence I require some assistance in the matter described below. I'm not sure how to produce viewport coordinates with respect to screen coordinates as well as producing it in c++ since I used to deal with Java.

In this question I need to implement the function worldToViewportTransform.

The function implements a 2D orthographic projection matrix, which is used for the (world)window-to-viewport transformation. In OpenGL this matrix is defined by gluOrtho2D.

Input are the coordinates of the world-window (winLeft, winRight, winBottom, winTop), the top-left corner of the viewport (window) on the screen (windowX, windowY), and the size of the viewport (window) on the screen (windowWidth, windowHeight).

Output are the values A, B, C and D which constitute the world-to-viewport transformation.

The answer needs to use the function format below - copy it and fill out the missing code. The function uses pointer variables for the values A, B, C and D since Coderunner does not seem to accept C++ notation - the code segment below converts the pointer variables to double values and back, so you don't need to understand how pointers work.

void worldToViewportTransform (double winLeft, double winRight, double winBottom, double winTop,

                              int windowX, int windowY, int windowWidth, int windowHeight,

                             double* APtr, double* BPtr, double* CPtr, double* DPtr)

{ 

    double A=*APtr, B=*BPtr, C=*CPtr, D=*DPtr;

       <INSERT YOUR CODE HERE>

    *APtr=A; *BPtr=B; *CPtr=C; *DPtr=D; 

}

Particular Test case should produces the output:(u,v)=(-200,367)

//Code for Testing
double A, B, C, D;
double winLeft=1.5, winRight=4.5, winBottom=0.0, winTop=3.0;
int windowX=100, windowY=100, windowWidth=600, windowHeight=400;

worldToViewportTransform(winLeft, winRight, winBottom, winTop,
                         windowX, windowY, windowWidth, windowHeight,
 &A, &B, &C, &D);

// Test cases

double x, y; // world coordinates

int u, v; // window coordinates

x=0.0f; y=1.0f;

u=(int) floor(A*x+C+0.5f);

v=(int) floor(B*y+D+0.5f);

printf("(u,v)=(%d,%d)",u,v);
1

1 Answers

0
votes

The function implements a 2D orthographic projection matrix, which is used for the (world)window-to-viewport transformation. In OpenGL this matrix is defined by gluOrtho2D.

No! gluOrtho2D/glOrtho is not doing that. These functions setup a orthographic projection matrix, which purpose is to transform from view-space into clip-space.

Then an implicit clip-space to NDC-space transform happens behind the scenes.

Finally the NDC-space coordinates are transformed to window coordinates in the viewport range.

Input are the coordinates of the world-window (winLeft, winRight, winBottom, winTop), the top-left corner of the viewport (window) on the screen (windowX, windowY), and the size of the viewport (window) on the screen (windowWidth, windowHeight).

Your nomenclature seems a little bit off.

The usual convention is that the viewport defines the target rectangle within the window, specified in window-relative coordinates. In OpenGL the window coordinate (0,0) being the lower-left corner of the window.

Window coordinates are usually relative to its parent window; hence for top-level window relative to the screen coordinates. In usual screen coordinate systems (0,0) is the upper-left.

Output are the values A, B, C and D which constitute the world-to-viewport transformation.

It's unclear what you actually want, but my best educated guess is, that you want to recreate the OpenGL transformation chain. Of course if you're using shaders, everything could be done in there. But in practice you'll probably just want to follow the chain

r_clip = P · M · r_in
r_NDC = r_clip/r_clip.w
r_viewport = (r_NDC.xy + 1)*viewport.width_height/2 + viewport.xy

where P is your projection matrix, for example the matrix produced by glOrtho.