Viewport Problem coordinate system

Hi everyone,

I’m doing a game for a project with university. After I loaded a blender model with “Assimp” I’m trying to move the object on the “x” axes but after a while the object goes out of the view.

I would like to find a way to know which “x” correspond to the left and right limit of the screen, in order to constrain the object to stay in the boundary of the viewport. Do you know how can I find these values?

To make things more clear there is the code that I use to move the object

void keyboard(unsigned char key, int x, int y)
{
    
    switch (key) {
    case 27:
        exit(1);
        break;

    case 'a':
        if(x_cord >-1.36)
          x_cord -= 0.1;

        glutPostRedisplay();
        break;
    case 'd':
        if (x_cord < scene_max.x)
           x_cord += 0.1;
        glutPostRedisplay();
        break;
    default:
        break;
    }
}

and here there is the display function that calls keyboard:

void display(void)
{
    float tmp;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0.f, 0.f, 3.f, 0.f, 0.f, -5.f, 0.f, 1.f, 0.f);

    // rotate it around the x axis
    glRotatef(angle, 1.f, 0.f, 0.f);
    glTranslatef(x_cord, y_cord, z_cord);
    // scale the whole asset to fit into our view frustum 
    tmp = scene_max.x - scene_min.x;
    tmp = aisgl_max(scene_max.y - scene_min.y, tmp);
    tmp = aisgl_max(scene_max.z - scene_min.z, tmp);
    tmp = 1.f / tmp;
    glScalef(tmp, tmp, tmp);

    // center the model
    glTranslatef(-scene_center.x, -scene_center.y, -scene_center.z);

    // if the display list has not been made yet, create a new one and
    // fill it with scene contents
    if (scene_list == 0) {
        scene_list = glGenLists(1);
        glNewList(scene_list, GL_COMPILE);
        // now begin at the root node of the imported data and traverse
        // the scenegraph by multiplying subsequent local transforms
        // together on GL's matrix stack.
        recursive_render(scene, scene->mRootNode, 1.0);
        glEndList();
    }

    glCallList(scene_list);

    glutSwapBuffers();

    //do_motion();
}

and this is the reshape function:

void reshape(int width, int height)
{
    W = width;
    const double aspectRatio = (float)width / height, fieldOfView = 45.0;

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fieldOfView, aspectRatio,
        1.0, 1000.0);  // Znear and Zfar 
    glViewport(0, 0, width, height);
}

thanks in advance :slight_smile:

After transformation by the model-view and projection matrices and projective division (dividing x,y,z by w), the clip volume is the signed unit cube (-1 <= x,y,z <= 1). So to find the clip volume in object space, transform the cube by the inverses of the model-view and projection matrices (in that order) and divide by w. The reference pages specify the matrices used by the various functions (gluLookAt, gluPerspective, etc).

The combination of the gluPerspective and gluLookAt comes out to:

left: x=-(3-z)*a/f
right: x=(3-z)*a/f
bottom: y=-(3-z)/f
top: y=(3-z)/f

Where a = aspectRatio and f = cot(fieldOfView/2). The (3-z) comes from the gluLookAt() being equivalent to glTranslatef(0,0,-3).