# Set a good distance for full view directly with gluLookAt without glScalef

Hello,

I try to set a good distance for the full view of a particles disk and in the same time have a valid line scale which represents the current value for the scale of the disk as a function of zooming with mouse.

The scene has the following parameters :

``````   w_width = 600;
w_height = 600;
g_nearPlane = 0.1f;
g_farPlane = 1000.0f;

``````

I do graphics initialization of 3D scene with the following code sample :

``````    // Reset line scale value
lineScaleValue = 100.0f;
// Initialize View
glViewport(0, 0, w_width, w_height);

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

// Perspective with angle set to 45 degrees
gluPerspective(45.0f, (float) w_width / w_height, g_nearPlane, g_farPlane);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrixi

gluLookAt (0.0, 0.0, 3, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(0.03f, 0.03f, 0.03f);
``````

The above values for gluLookAt and glScalef have been chosen approximately, i.e manually without having an accurate calculation.

Here’s the result on this image :

[ATTACH=CONFIG]1261[/ATTACH]

I would like to have only a full view for the disk (white particles).
For this, I know the maximum and minimum in the (x,y) plane of the disk :
-25 < x <25 and -22 < y < 22. As you can see on above figure, the line scale has not a good value ( 100 kpc (kiloparsec) is too high if you consider that radius of disk is about 25 kpc).

from what I know, I could do (with Fov/2 = 45 degrees) :

``````  z_distance = 25*tan(pi/4) + dist = 50
``````

with dist = 25 to be in front of the disk.

``````  gluLookAt (0.0, 0.0, 3, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(0.03f, 0.03f, 0.03f);
``````

Can I do directly in my sample code :

``````    gluLookAt (0.0, 0.0, 50, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
``````

?

But this solution doesn’t work, the size of the disk seems to fill all the window but particles don’t display very well, as they were little points :

[ATTACH=CONFIG]2098[/ATTACH]

How can I fix this ?

If anyone sees where my error is in the code sample, can he report to me.

Thanks

PS : I have forgotten to say that I use the following gluLookAt function :

`````` void GLWidget::gluLookAt(GLfloat eyex, GLfloat eyey, GLfloat eyez, GLfloat centerx, GLfloat centery, GLfloat centerz, GLfloat upx, GLfloat upy, GLfloat upz)
{
GLfloat m;
GLfloat x, y, z;
GLfloat mag;

/* Make rotation matrix */

/* Z vector */
z = eyex - centerx;
z = eyey - centery;
z = eyez - centerz;
mag = sqrt(z * z + z * z + z * z);
if (mag) {          /* mpichler, 19950515 */
z /= mag;
z /= mag;
z /= mag;
}

/* Y vector */
y = upx;
y = upy;
y = upz;

/* X vector = Y cross Z */
x = y * z - y * z;
x = -y * z + y * z;
x = y * z - y * z;

/* Recompute Y = Z cross X */
y = z * x - z * x;
y = -z * x + z * x;
y = z * x - z * x;

/* mpichler, 19950515 */
/* cross product gives area of parallelogram, which is < 1.0 for
* non-perpendicular unit-length vectors; so normalize x, y here
*/

mag = sqrt(x * x + x * x + x * x);
if (mag) {
x /= mag;
x /= mag;
x /= mag;
}

mag = sqrt(y * y + y * y + y * y);
if (mag) {
y /= mag;
y /= mag;
y /= mag;
}

#define M(row,col)  m[col*4+row]
M(0, 0) = x;
M(0, 1) = x;
M(0, 2) = x;
M(0, 3) = 0.0;
M(1, 0) = y;
M(1, 1) = y;
M(1, 2) = y;
M(1, 3) = 0.0;
M(2, 0) = z;
M(2, 1) = z;
M(2, 2) = z;
M(2, 3) = 0.0;
M(3, 0) = 0.0;
M(3, 1) = 0.0;
M(3, 2) = 0.0;
M(3, 3) = 1.0;
#undef M
glMultMatrixf(m);

/* Translate Eye to Origin */
glTranslatef(-eyex, -eyey, -eyez);

}

``````

and the following gluPerspective function ;

``````    void GLWidget::gluPerspective( GLfloat fovY, GLfloat aspect, GLfloat zNear, GLfloat zFar )
{
const GLfloat pi = M_PI;
GLfloat fW, fH;

//fH = tan( (fovY / 2) / 180 * pi ) * zNear;
fH = tan( fovY / 360 * pi ) * zNear;
fW = fH * aspect;

glFrustum( -fW, fW, -fH, fH, zNear, zFar );
}

``````

Sorry, the second image attachment is :

[ATTACH=CONFIG]1267[/ATTACH]