fabio781

09-17-2015, 08:03 AM

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 :

2099

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.

Instead of

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 :

2098

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[16];

GLfloat x[3], y[3], z[3];

GLfloat mag;

/* Make rotation matrix */

/* Z vector */

z[0] = eyex - centerx;

z[1] = eyey - centery;

z[2] = eyez - centerz;

mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);

if (mag) { /* mpichler, 19950515 */

z[0] /= mag;

z[1] /= mag;

z[2] /= mag;

}

/* Y vector */

y[0] = upx;

y[1] = upy;

y[2] = upz;

/* X vector = Y cross Z */

x[0] = y[1] * z[2] - y[2] * z[1];

x[1] = -y[0] * z[2] + y[2] * z[0];

x[2] = y[0] * z[1] - y[1] * z[0];

/* Recompute Y = Z cross X */

y[0] = z[1] * x[2] - z[2] * x[1];

y[1] = -z[0] * x[2] + z[2] * x[0];

y[2] = z[0] * x[1] - z[1] * x[0];

/* 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[0] * x[0] + x[1] * x[1] + x[2] * x[2]);

if (mag) {

x[0] /= mag;

x[1] /= mag;

x[2] /= mag;

}

mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);

if (mag) {

y[0] /= mag;

y[1] /= mag;

y[2] /= mag;

}

#define M(row,col) m[col*4+row]

M(0, 0) = x[0];

M(0, 1) = x[1];

M(0, 2) = x[2];

M(0, 3) = 0.0;

M(1, 0) = y[0];

M(1, 1) = y[1];

M(1, 2) = y[2];

M(1, 3) = 0.0;

M(2, 0) = z[0];

M(2, 1) = z[1];

M(2, 2) = z[2];

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

}

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 :

2099

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.

Instead of

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 :

2098

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[16];

GLfloat x[3], y[3], z[3];

GLfloat mag;

/* Make rotation matrix */

/* Z vector */

z[0] = eyex - centerx;

z[1] = eyey - centery;

z[2] = eyez - centerz;

mag = sqrt(z[0] * z[0] + z[1] * z[1] + z[2] * z[2]);

if (mag) { /* mpichler, 19950515 */

z[0] /= mag;

z[1] /= mag;

z[2] /= mag;

}

/* Y vector */

y[0] = upx;

y[1] = upy;

y[2] = upz;

/* X vector = Y cross Z */

x[0] = y[1] * z[2] - y[2] * z[1];

x[1] = -y[0] * z[2] + y[2] * z[0];

x[2] = y[0] * z[1] - y[1] * z[0];

/* Recompute Y = Z cross X */

y[0] = z[1] * x[2] - z[2] * x[1];

y[1] = -z[0] * x[2] + z[2] * x[0];

y[2] = z[0] * x[1] - z[1] * x[0];

/* 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[0] * x[0] + x[1] * x[1] + x[2] * x[2]);

if (mag) {

x[0] /= mag;

x[1] /= mag;

x[2] /= mag;

}

mag = sqrt(y[0] * y[0] + y[1] * y[1] + y[2] * y[2]);

if (mag) {

y[0] /= mag;

y[1] /= mag;

y[2] /= mag;

}

#define M(row,col) m[col*4+row]

M(0, 0) = x[0];

M(0, 1) = x[1];

M(0, 2) = x[2];

M(0, 3) = 0.0;

M(1, 0) = y[0];

M(1, 1) = y[1];

M(1, 2) = y[2];

M(1, 3) = 0.0;

M(2, 0) = z[0];

M(2, 1) = z[1];

M(2, 2) = z[2];

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

}