开发者

How to make sure an object doesn't get out of visible range ->opengl->c

开发者 https://www.devze.com 2023-02-02 11:54 出处:网络
Imagine that you have some polygon on the center of the screen (let\'s say a square, considering a 2D world) and you have a function to redraw that polygon from the central point.

Imagine that you have some polygon on the center of the screen (let's say a square, considering a 2D world) and you have a function to redraw that polygon from the central point. Everytime you press up, down, left or right on the keyboard the shape will be redraw considering the key pressed (up or down will update y coords and left and right will update coords). Considering that you have: glutInitWindowSize(400,400); glutInitWind开发者_运维百科owPosition(400,200);

and I am using glMatrixMode(GL_MODELVIEW), how can I prevent from the shape exit from the sight of the camera?

PS: Until now I can only prevent from not exiting from the left and bottom, I test at coords refresh when redrawing if y or x will be negative (if they will be, the shape will be redrawed in the same position), but I cannot get a way to do the same with right and top edges of the window.


Sounds like you need to either set the far clipping plane of the camera to a greater distance, or make sure that your object does not go beyond the far clip property of the camera.


After reading your comments to another answer I had a thought. I'm not sure if it would be suitable, but it might be helpful.

As I understand it you have a view of an aircraft that the player is in control of and as the player moves the aircraft within the setting you do want the craft to move with respect to the view that the player has, but you do not want it to exit the view. My assumption is that the entire view is moving as the craft flies forward (this may be wrong).

My idea is that you always make the view move towards the center of the craft at rate which increases as the plane gets farther from the center of the view. As the craft gets far enough from the center to get near the edges the speed at which the camera will recenter on the craft will increase (and the speed limit of the camera movements is greater than the speed limit of the aircraft). As the camera closes in on the aircraft it will slow back down. (yes, this sounds a lot like special relativity)

You should be able to keep up with the distance from the center that the aircraft by first knowing that it was originally centered and also knowing what transformation you have done on it. You can update a transformation matrix that keeps up with the movements you apply to the aircraft and upon each update you can calculate an inverse operation that would move the camera back to the center of the camera's view, and then multiply that matrix by the scalar 1/2, and transform the entire scene by that. If you don't want the camera to rotate along with the direction of the aircraft then you just don't put turns into the matrix that keeps up with that. How much you want to move the camera each update (1/2 in my example) is up to you. I chose 1/2 because the part you apply and the part that you save for the next update are the same.

It's been a long time since I did any openGL, but I remember being able to read matrices back out of the engine, but that is easy enough to keep up with on your own.

This may not be at all what you wanted, but it seems like it might be helpful.


it sounds as though you need to keep track of the width and height of your graphical object, and then use some substraction to calculate the maximum x and y position that would be allowed.

is this a school assignment btw?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号