OpenGl coordinate system is not at -1 to 1

前端 未结 1 2001
予麋鹿
予麋鹿 2021-01-24 10:44

I am creating a basic game in OpenGl and C++ and want to make it so that when the player gets to the edge of the screen they can\'t move any further. I am having trouble working

相关标签:
1条回答
  • 2021-01-24 10:51

    If you aren't going to be dynamically changing your projection matrix, the easiest thing to do would be to call

    glScalef(.63f,.63f,1);

    on your projection matrix.

    You can then restrict movement based on these values.

    To compute the world space coordinates at any time you should make use of gluUnProject. assuming 'x' and 'y' are the width and height of your window respectively (the values you pass gluPerspective) you can find the world space coordinates like so:

    double world_llx,world_lly,world_llz;
    //world coordinates of lower left corner of window
    gluUnProject(0, 0, 0, view_mat, proj_mat, viewport,&world_llx,&world_lly,&world_llz);
    //world coordinate of upper right corner of window
    double world_urx,world_ury,world_urz;
    gluUnProject(x,y,0,view_mat,proj_mat,viewport,&world_urx,&world_ury,&world_urz);
    

    view_mat is your view matrix. proj_mat is your projection matrix. You can get both of these using glGetDouble* with GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX.

    The viewport parameter will probably have the same dimensions as your window. In any event, this is what you set with glViewport.

    This assumes your XZ plane is at z == 0.

    0 讨论(0)
提交回复
热议问题