In this section we first implement view control (including pan/rotate/zoom), as we have already said, by LookAt a function, or by translate+rotate two functions, can achieve the control of the view (two functions of the way is relatively simple, but not enough versatility, Because this is more like a canvas to visit the model of selling a house, you can move the model casually up and down to see the outside, but if you want to like the first-person view of the house to climb the windows, the roof, drilling tunnels, this perspective is certainly not enough. Can imagine the CS game, the entire map scene must be unique, each game player can control a set of perspectives, the angle of view of the LookAt nine parameters led to can look up, overlooking, left and right sweep and so on, interested readers can go to carefully study the implementation method)
?
However, it is not convenient to use the button control, many times we need to like the SolidWorks software to be able to control with the mouse keyboard, I set some simple rules:
The mouse can control the view before and after flipping around
Press and hold the shift+ mouse to control the view and left and right panning
Mouse wheel to control view Zoom
Hold down CTRL and the mouse wheel controls the rotation of the view around Z
?
According to these rules, we need to add a set of event bindings when the control is initialized (note that you do it in C # Auto-completion, otherwise you do not know how to write, such as binding to the KeyDown event, the response is KeyEventArgs e, we can collect keyboard keys, If you write it yourself, you don't know what the parameter type is.
?
The binding of these events you can also select the specified control in the main window, view all of his events, double-click into (but not conform to the object-oriented notation, if we refine the class to write a new project, you have to drag the OpenGL control every time, and then switch to here, Double-click a single event to build, and then copy and paste the code? Certainly inefficient, it is better to define a class yourself, and then each time you initialize the Openglcontrol type to pass in the past, all event responses are completed at once.
?
In so many event responses, let's look at a few simple groups, mouse-over and mouse-out to determine whether the mouse is on the control (in order to prevent the mouse from clicking the button elsewhere in the window, the operation will also affect the view, we define as long as the mouse leaves the control, the global variable is set to False, The view is no longer modified). The mouse is pressed and the mouse is lifted to define whether to collect the current position of the mouse (also to prevent false positives, assuming that if the mouse moves inside the control, only the left button pressed after the move operation will have an impact, do not press the button to move the mouse and not modify the view, Of course, every time you mousedown, you have to refresh the current position = the position before the record)
?
The mouse movement event is actually produces the effect the event, we each time the mouse moves in a certain direction, if the expectation view follows the change, then actually simply captures the mouse position the difference, then overlays to the translate or the rotate three parameters to be able (notice actually is two parameters, Because we simulate three-dimensional effect on a two-dimensional computer screen, the mouse will only have x, y values, so it can only map to XY's translation or rotation around XY, so there will be additional rules to define Z's pan-zoom, z rotation-rotate view .
?
Talk about the zoom view, the most convenient is to use the mouse wheel, the mouse wheel roll and roll, E.delta value is positive and negative, in order to prevent too large when zooming too fast, so if it has been viewchangetranslate very small, scaling a bit slower (also can be made a function, The farther the smaller the faster the zoom, so that the user experience is more relevant to rotate around the z axis or more important, in order to prevent too many rules, I still use the mouse wheel implementation (need to press the CTRL key), to where the basic view control is completed.
?
Then take a closer look at the Pushmatrix and Popmatrix methods, assuming that we're going to draw a small robot that uses just three hexahedral to represent the head and two arms of the robot (note that a set of push and pop packages is used when trying to draw a small module). We'll cover the detailed drawing process later
?
When drawing a basic object, note that no matter how much your object is, your brush remains the same (only translate and rotate can modify the position and posture of the brush), so I can artificially adjust the size of the red cube block.
?
Next draw the arm, if you expect the arm to stick to the Red cube block of two, then you have to translate the brush to the next cube to be drawn in the center of the central position (we do not consider the posture, after the gesture of the text, if not using rotate, the posture will always remain perpendicular to the canvas), Notice the effect of the brush moving to the new position carefully
?
When drawing the B-arm, it is possible to move directly from the place where a arm is drawn (the width of the entire red cube is shifted), but only for simple geometric objects, and if you draw very complex polygons, many times we don't know where the brushes are, So it is necessary to draw from the previous step where the brush has not yet begun to move (because B arm and a arm are symmetrical, so long as the positive and negative number of some translate to change on the line) from the x-x1 and x-x2 process basically similar, symmetry (which is also the more important in programming thinking, do not create new problems, X1 direct to X2 is a new problem, even more simple than x-x2, because we have x-x1 experience can be applied, you can directly x-x2)
?
Let the whole animated element move, in fact, just add some variables to the entire drawing process (I just did a simple demonstration here, such as two arm swing, you can actually add legs, eyes, waist, etc.)
?
Finally apply a little motion control idea, let two rotate arm from point A to point B, ask at the same time to go in place (or to consider more factors, such as AB two points no gap, or only a rotating arm gap), the general idea can be summed up as:
1 difference between acquisition start and end point (array)
2 Gets the maximum value in the difference array (farthest distance)
3 Total time based on a given speed (maximum distance/given speed)
4 the speed of each axis is re-calculated according to the total time (front and rear distance per axis/total time)
5 Perform motion in thread (with a timer, generate a new time at a certain time and calculate each new position with S=S0 + v*t formula)
?
Summary: So far the simple introduction of OpenGL is over, there is a lot of code to introduce OpenGL simulation (c + + more, because C # compared to C + + performance is much worse, so large-scale games, 3D processing software will use C + + as the ultimate programming tool, May C # is nested in the upper layer interaction), but it does not prevent us from deepening our understanding of OpenGL through C # (the core idea is unchanged, and C # development, validation is more efficient). Readers need to master:
1 understanding OpenGL's basic drawing process, panning, rotating, zooming
2 Understanding OpenGL drawing basic objects, the principle of combining objects, such as small swing-arm robot implementation principle
3 View the online master code, for some specific functions to do in-depth research, OpenGL in addition to draw two-dimensional three-dimensional objects, but also to do dynamic simulation, model rendering, learn this part of the content is very meaningful
?
?
For more instructional videos and downloads, please pay attention to the following information:
My Youku Space:
Http://i.youku.com/acetaohai123
?
My online forum:
http://csrobot.gz01.bdysite.com/
?
Problem Exchange:
qq:910358960
Email:[email protected]
?
?
C # application video Tutorial 2.4 OpenGL Virtual Simulation Introduction