简体   繁体   中英

OpenGl coordinate system is not at -1 to 1

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 out where the edge of the screen is. I know that windows normally have a system between 1 and -1, but mine seems to be more like 0.63 to -0.63. The player is shown as a box on the screen which has an x, y, and z location, but it will only move in 2D space.

I want to change the bounds so that they are between -1 and 1, not a odd value.

How can I do this?

Code has been uploaded to http://pastebin.com/jxd5YhHa .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM