OpenGL :: 3D (Part 1/2) :: Material 4
list of contents :
- The real on the screen
- Perspective
- Place the camera
The real on the screen
When you describe your 3D scene in OpenGL (to blow glVertex ), you describe the world as it is in absolute terms, in its own mark.
To the real world on the screen, we must give some indications OpenGL:
* This step is automatically performed in the creation of the window. We will see in Part II how to change the split-screen (several screens rendered on the same window).
Based on this information, OpenGL can determine changes in subjecting the real world objects in three dimensions to draw 2 on the screen dimensions.
In the real world you work at vertices and on the screen in pixels. The drawing up of you may have noticed that I did not reveal any Z screen. It was just not to confuse you because the screen is 2 dimensional. Z but the screen is still to give a pixel depth information.
Answer: the one you want ! Indeed, when we think what that something is big or small, really? Without reference it is difficult to visually determine the size of an object. We know it is small because there is another object by which we know the size. We knowcompare .
How do I know that something is away? Because when we move, its size does not vary much and because of the time we get close.example in OpenGL that "1" is 1 meter as 1 mm or as 1 km is the same as long as the proportions are maintained and that the speeds of movement are appropriate. Indeed, if we faced a cube of "1" in width and only support once the button "forward" moves us from 10000 will say: I will be very fast, or the object was (as we just lose it) very very small indeed. however it is sometimes useful to refer to known units. If one unit is 1 meter OpenGL, then it will be easier to use a physics engine that uses real units.
To the real world on the screen, we must give some indications OpenGL:
- defining which area of the window will be used for rendering *;
- define the projection mode (eg perspective);
- place the camera in the real world.
* This step is automatically performed in the creation of the window. We will see in Part II how to change the split-screen (several screens rendered on the same window).
Based on this information, OpenGL can determine changes in subjecting the real world objects in three dimensions to draw 2 on the screen dimensions.
In the real world you work at vertices and on the screen in pixels. The drawing up of you may have noticed that I did not reveal any Z screen. It was just not to confuse you because the screen is 2 dimensional. Z but the screen is still to give a pixel depth information.
The mark X, Y, Z of the screen is not straightforward: if we apply the rule of the right hand.The Z axis should move the screen towards us. However, the agreement was made in image synthesis the Z axis goes into the screen.
What is the unit of distance in OpenGL?
Answer: the one you want ! Indeed, when we think what that something is big or small, really? Without reference it is difficult to visually determine the size of an object. We know it is small because there is another object by which we know the size. We knowcompare .
How do I know that something is away? Because when we move, its size does not vary much and because of the time we get close.example in OpenGL that "1" is 1 meter as 1 mm or as 1 km is the same as long as the proportions are maintained and that the speeds of movement are appropriate. Indeed, if we faced a cube of "1" in width and only support once the button "forward" moves us from 10000 will say: I will be very fast, or the object was (as we just lose it) very very small indeed. however it is sometimes useful to refer to known units. If one unit is 1 meter OpenGL, then it will be easier to use a physics engine that uses real units.
Perspective
The most important passage is in 3D projection . To move from a world in 3D describes a window with 2D pixel we must lose a dimension and therefore project. projection method that we use in 3D perspective. The prospect is defined by the pyramid below:
Pyramid clipping
This pyramid is called the pyramid clipping . That is to say, any object that is not within the blue zone will not be drawn. More shape to define how to project objects on the screen by drawing a parallel with the actual pyramid whose summit the eye of the user and cut his screen :
actual pyramid (here seen from above)
The ratio (given in the diagram above) is a ratio between the width and height. For a TV this ratio is 4/3 times of 16/9. Sizes for standard computer (1024x768, 800x600, 640x480), it is also 4/3. But your window is not obliged to use the whole screen and can have its own proportions and there are more screens with special ratios. It is therefore important not to take for granted in your applications and always defined in terms of the size of the window that you create (I will make a schedule SDL on how to detect the available modes for full screen). parameters near and far determine the minimum and maximum distances of objects.Outside this interval objects are not displayed. This problem may arise if the value is far too small, the user may see objects disappear when it goes away, or worse see the scene suddenly appear. We will see in Part II how to use the fog to deal with this problem. At the moment a large value (vis-à-vis the size of your scene) will be good enough. The only parameter that can escape here is the angle . This is the viewing angle between the planes up and down the pyramid (often denoted fovy for field of view on the y axis). Generally used a value around 70 °. When the output of Half-Life 2 there was a controversy over the angle misnomer (90 °) which made some people uncomfortable. To see the influence of this angle, I have 3 is made of the same scene with different angles:
To set the perspective projection mode as simply calling the function
Eg for a 640x480 window:Code: C + +
- Select
Here is our new code to initialize our OpenGL application:Code: C + +
- Select
Pyramid clipping
This pyramid is called the pyramid clipping . That is to say, any object that is not within the blue zone will not be drawn. More shape to define how to project objects on the screen by drawing a parallel with the actual pyramid whose summit the eye of the user and cut his screen :
actual pyramid (here seen from above)
The ratio (given in the diagram above) is a ratio between the width and height. For a TV this ratio is 4/3 times of 16/9. Sizes for standard computer (1024x768, 800x600, 640x480), it is also 4/3. But your window is not obliged to use the whole screen and can have its own proportions and there are more screens with special ratios. It is therefore important not to take for granted in your applications and always defined in terms of the size of the window that you create (I will make a schedule SDL on how to detect the available modes for full screen). parameters near and far determine the minimum and maximum distances of objects.Outside this interval objects are not displayed. This problem may arise if the value is far too small, the user may see objects disappear when it goes away, or worse see the scene suddenly appear. We will see in Part II how to use the fog to deal with this problem. At the moment a large value (vis-à-vis the size of your scene) will be good enough. The only parameter that can escape here is the angle . This is the viewing angle between the planes up and down the pyramid (often denoted fovy for field of view on the y axis). Generally used a value around 70 °. When the output of Half-Life 2 there was a controversy over the angle misnomer (90 °) which made some people uncomfortable. To see the influence of this angle, I have 3 is made of the same scene with different angles:
We note that if you give a small angle , it gives an effect of zoom . And I will not deny that this is exactly what is used in games for zoom (binoculars or sniper).
OpenGL application
To set the perspective projection mode as simply calling the function
gluPerspective (fovy, ratio, near, far);
Eg for a 640x480 window:Code: C + +
- Select
1 | gluPerspective ( 70 , ( two ) 640 / 480 , 1 , 1000 ); |
This call changes the current matrix, even if it is not the projection matrix. We must therefore make sure to select the matrix GL_PROJECTION , initialize and then call gluPerspective .
Here is our new code to initialize our OpenGL application:Code: C + +
- Select
1 2 3 4 5 6 | SDL_Init ( SDL_INIT_VIDEO ) SDL_WM_SetCaption ( "SDL GL |
As the definition of perspective does the matrix GL_PROJECTION we can simply initialize once and not for each image. Of course, if the ratio of the window should change dynamically should redefine risk perspective see all objects distorted.
Place the camera
Now that we know how to plan, it would be nice to place the view anywhere in the scene. For this we use a kind of camera with the virtual call:
CAMx , Camy and Camz define the position of the camera; CIBLEX , cibleY and target define the position of the camera fixed point (the corresponding point will be at the center of the display) VertX , Verty and Vertz define the vector vertical. following three images taken with different settings of the camera so that you understand the value of each parameter:
gluLookAt (1.5, 1.5, 5, 0, 0, 0, 0, 1, 0)
The camera is (1.5,1.5,5) and looks (0,0,0). gluLookAt (1.5, 1.5, 5 , 1.5, 1.5, 0, 0, 1, 0) The camera is (1.5,1.5,5) and looks (1.5,1.5,0), that is to say, right before it (unlike the previous image). gluLookAt (1.5, 1.5, 5, 0, 0, 0, 1, 1, 0) and the same position as before, but his eyes vertical vector is (1,1,0), or the scene was thought with a vertical (0,1,0), the image is "leaning" on the side.
You saw the call to gluLookAt lets you define the vertical that you want for your scene. The whole thing is to be consistent and to design your scene accordingly.
Nevertheless, it is much more convenient to choose as a vector of vertical marker base X (1,0,0) , Y (0,1,0) or Z (0,0,1) .
Good question. One or both are valid choices: we can imagine the transition to three e dimension as the addition of depth as well as the addition of height . In the world of computer graphics it is not uncommon to see one or the other . Going forward , I will try to respect the choice Z = vertical as it has been in many games and OpenGL seems to have helped (we shall see in the chapter on quadrics ...).
Editor FarCry , Z is the vertical
Place the camera move back to everyone so it is centered on the camera and oriented along the axis of the eye. This logically affects the matrix GL_MODELVIEW and you will not be surprised therefore that the appeal of the camera is just after resettingGL_MODELVIEW .Code: C + +
- Select
gluLookAt (CAMx, Camy, Camz, Ciblex cibleY, target, VertX, verty, Vertz);
CAMx , Camy and Camz define the position of the camera; CIBLEX , cibleY and target define the position of the camera fixed point (the corresponding point will be at the center of the display) VertX , Verty and Vertz define the vector vertical. following three images taken with different settings of the camera so that you understand the value of each parameter:
gluLookAt (1.5, 1.5, 5, 0, 0, 0, 0, 1, 0)
The camera is (1.5,1.5,5) and looks (0,0,0). gluLookAt (1.5, 1.5, 5 , 1.5, 1.5, 0, 0, 1, 0) The camera is (1.5,1.5,5) and looks (1.5,1.5,0), that is to say, right before it (unlike the previous image). gluLookAt (1.5, 1.5, 5, 0, 0, 0, 1, 1, 0) and the same position as before, but his eyes vertical vector is (1,1,0), or the scene was thought with a vertical (0,1,0), the image is "leaning" on the side.
The importance of vertical
You saw the call to gluLookAt lets you define the vertical that you want for your scene. The whole thing is to be consistent and to design your scene accordingly.
Nevertheless, it is much more convenient to choose as a vector of vertical marker base X (1,0,0) , Y (0,1,0) or Z (0,0,1) .
Between Y and Z which is the best choice?
Good question. One or both are valid choices: we can imagine the transition to three e dimension as the addition of depth as well as the addition of height . In the world of computer graphics it is not uncommon to see one or the other . Going forward , I will try to respect the choice Z = vertical as it has been in many games and OpenGL seems to have helped (we shall see in the chapter on quadrics ...).
Editor FarCry , Z is the vertical
When the call?
Place the camera move back to everyone so it is centered on the camera and oriented along the axis of the eye. This logically affects the matrix GL_MODELVIEW and you will not be surprised therefore that the appeal of the camera is just after resettingGL_MODELVIEW .Code: C + +
- Select
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | void Draw () { glClear ( GL_COLOR_BUFFER_BIT ); glMatrixMode ( GL_MODELVIEW ); glLoadIdentity ( ); gluLookAt ( 3 , 3 , 3 , 0 , 0 , 0 , 0 , 0 , 1 ); / / Example / * 3D drawing here * / glFlush (); SDL_GL_SwapBuffers (); } |
That now you know how it is possible to move from a 3D world to a 2D screen and the camera will know.
To summarize, our new program for drawing skeleton of 3D:Code: C + +
- Select
To summarize, our new program for drawing skeleton of 3D:Code: C + +
- Select
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <SDL/SDL.h> # Include # include <GL/gl.h> <GL/glu.h> # include # include <cstdlib> void Draw (); int main ( int argc , char * argv []) { SDL_Event event ; SDL_Init ( SDL_INIT_VIDEO ) atexit ( SDL_Quit ) SDL_WM_SetCaption ( "SDL GL Application" , NULL ); SDL_SetVideoMode ( 640 , 480 , 32 , SDL_OPENGL ); glMatrixMode ( GL_PROJECTION ); glLoadIdentity (); gluPerspective ( 70 , ( two ) 640 / 480 , 1 , 1000 ); Draw (); for (; ;) { SDL_WaitEvent ( & event ); switch ( event . kind ) { case SDL_Quit: exit ( 0 ); break ; } Draw (); } return 0 ; } void Draw () { glClear ( GL_COLOR_BUFFER_BIT ); glMatrixMode ( GL_MODELVIEW ); glLoadIdentity ( ); gluLookAt ( 3 , 3 , 3 , 0 , 0 , 0 , 0 , 0 , 1 ); / * 3D design * / glFlush (); SDL_GL_SwapBuffers (); } |
No comments:
Post a Comment