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);