OpenGL ::
Quadrics
material 7
includes :
- Principle of use
- Quadrics
- Exercise: a rocket
Principle of use
Discussed immediately after it exists in OpenGL functions to draw all made a sphere, a cylinder, etc..
however these functions need information about the intentions of the encoder: should object to create texture, should it display only lines?
For this we use a field ( struct ) of parameters . These settings are stored in a variable of type GLUquadric that one must use a somewhat special.
This is not to us directly to create a variable of this type. You must use a call OpenGL we return a pointer to the GLUquadriccreated by OpenGL:Code: C + +-Select
We can now use the variable params created to set our objects and draw them.
This field is not manipulated directly, but only through functions. Once set it will be used for all calls drawing quadrics defined below.
Functions to define quadrics as we use to call glVertex to define the vertices of the objects. However, as we have no direct access to the code, and therefore glBegin , we can not specify such a glBegin (GL_LINES) we want that the display is done with lines. To do this we need to use function:
which defines what the display style. style can argue:
We saw in the previous chapter, to use textures must define texture coordinates with glTexCoord2d . If we want to use textures with our quadrics, you need to tell OpenGL that must also incorporate calls glTexCoord2d when asked to draw a quadric. We therefore use the function:
where texture can argue GL_TRUE (true for enable texture coordinates) or GL_FALSE (false: not to use texture coordinates).
No texture and no light , can not understand that it is a sphere. It is a principle of optics: understanding the shape of an object on a 2D image used for many color differences due to lighting ( Shape from Shading ). So we settle this problem in a few chapters when we see the light . Meanwhile, with the textures and movement we will really not difficult to discern the shape of our objects do not worry.
Even if we have not ourselves used malloc (or new in C + +) to create the GLUquadric must free the memory when you no longer want to use through the function (glimpsed in the code examples above):Code: C + +-Select
Now we can create a parameter field for our quadrics, the set and delete it. It's time to see what we are really interested, quadrics and drawing functions!
however these functions need information about the intentions of the encoder: should object to create texture, should it display only lines?
For this we use a field ( struct ) of parameters . These settings are stored in a variable of type GLUquadric that one must use a somewhat special.
Creating a variable GLUquadric
This is not to us directly to create a variable of this type. You must use a call OpenGL we return a pointer to the GLUquadriccreated by OpenGL:Code: C + +-Select
1 2 | GLUquadric * params , params = gluNewQuadric (); |
We can now use the variable params created to set our objects and draw them.
Setting GLUquadric
This field is not manipulated directly, but only through functions. Once set it will be used for all calls drawing quadrics defined below.
Display style
Functions to define quadrics as we use to call glVertex to define the vertices of the objects. However, as we have no direct access to the code, and therefore glBegin , we can not specify such a glBegin (GL_LINES) we want that the display is done with lines. To do this we need to use function:
gluQuadricDrawStyle (params, style);
which defines what the display style. style can argue:
Style | Explanation | Example |
---|---|---|
GLU_POINT | The object will be drawn with points. | |
GLU_LINE | The object will be drawn with lines. | |
GLU_FILL | The object will be drawn with solid faces. |
The default value is GLU_FILL . To use full faces so there is no need to call gluQuadricDrawStyle if no style has been defined previously.
Texture coordinates
We saw in the previous chapter, to use textures must define texture coordinates with glTexCoord2d . If we want to use textures with our quadrics, you need to tell OpenGL that must also incorporate calls glTexCoord2d when asked to draw a quadric. We therefore use the function:
gluQuadricTexture (params, texture);
where texture can argue GL_TRUE (true for enable texture coordinates) or GL_FALSE (false: not to use texture coordinates).
The default value is GL_FALSE , it is necessary to call initially gluQuadricTexture if we want to use our textures quadrics.
Code: C + + - Select
| |||
Code: C + + - Select
|
The first image that was supposed to be a sphere? Looks like a simple disk ...
No texture and no light , can not understand that it is a sphere. It is a principle of optics: understanding the shape of an object on a 2D image used for many color differences due to lighting ( Shape from Shading ). So we settle this problem in a few chapters when we see the light . Meanwhile, with the textures and movement we will really not difficult to discern the shape of our objects do not worry.
Removing the GLUquadric
Even if we have not ourselves used malloc (or new in C + +) to create the GLUquadric must free the memory when you no longer want to use through the function (glimpsed in the code examples above):Code: C + +-Select
1 | gluDeleteQuadric ( params ); |
Now we can create a parameter field for our quadrics, the set and delete it. It's time to see what we are really interested, quadrics and drawing functions!
Quadrics
To understand what are the faces and vertices generated by subsequent calls, all quadrics are represented in wireframe. I also include a 3D version of landmark to show that the Z axis (blue) is the main used by quadrics.
The influence of these two parameters is summarized by the image below:
Moreover, these two numbers are, the more accurate and the ball is indeed resembles a sphere. The value chosen (20x20) gives a satisfactory result.
gluCylinder (params, 1,0,2,20,1);
The hard part is as normal except that the disk is not necessarily 360 °.
You now understand the object GLUquadric is not a quadric, but simply a field parameters used when calling a function quadric drawing to specify the display mode. We can therefore use quite the same GLUquadric to draw quadrics while changing, if desired, the parameters along the way:
Sphere
gluSphere (GLUquadric * params, radius, slices, stacks);
- The first parameter is the type field GLUquadric we set earlier.
- The second radius is the simplest: the radius of the sphere.
- slices is the number of vertical slices that make up the sphere.
- stacks is also a number of bands but for horizontal slices.
The influence of these two parameters is summarized by the image below:
Moreover, these two numbers are, the more accurate and the ball is indeed resembles a sphere. The value chosen (20x20) gives a satisfactory result.
The cylinder and the cone
gluCylinder (GLUquadric * params, base, top, height, slices, stacks);
- The first parameter is always the same type field GLUquadric .
- Base is the radius of the cylinder bottom, top is the radius of the cylinder at the top. To get a true cylinder must therefore use the same value for 2, but using different values for base and top , we have a cone! (See drawing below)
- slices is, as for the sphere, the number of divisions around the Z axis and we choose a value of around 20 for the same reason.
- stacks here is not used much. Put a value different from 1 would not change the level of precision of the cylinder / cone (except when viewed in wireframe).
gluCylinder (params, 1,0,2,20,1);
Disk
gluDisk (GLUquadric * params, inner, outer, slices, loops);
- The first parameter is always the same type field GLUquadric .
- inner is the inner radius of the disc, often to 0 but can (as in the picture) have a different value.
- outer is the external radius of the disc.
- slices is, as before, the number of divisions around the axis Z.
- loops here is not used much. To a value of 1 would add different faces inside the disk but does not add precision visible (except in Wired).
The hard part
gluPartialDisk (GLUquadric * params, inner, outer, slices, loops, start, sweep);
The hard part is as normal except that the disk is not necessarily 360 °.
- start is the starting angle of the partial disk. Unfortunately for us, the designers of OpenGL does not follow the mathematical logic that wants the angles are expressed in the clockwise (counter clockwise) with 0 ° on the axis X. Here 0 ° to start the beginning of the disc space on the axis Y (green), 90 ° to the axis X (red).
- sweep is the angular distance between the start and end of the disk part (180 ° in the drawing above).
Accuracy given by slices applies here only to partial disk. It therefore serves to put anything of great value (20) if we use a partial disk of only 90 °. An accuracy of 5 may be sufficient to have the same quality result through an angle of 90 ° (relative to an accuracy of 20 to an angle of 360 °).
Same GLUquadric to draw several quadrics
You now understand the object GLUquadric is not a quadric, but simply a field parameters used when calling a function quadric drawing to specify the display mode. We can therefore use quite the same GLUquadric to draw quadrics while changing, if desired, the parameters along the way:
Code: C + + - Select
|
Exercise: a rocket
This chapter is not complicated but so far I've done all the work to you detailing the functions to use quadrics. Now you! To get your hands on these new features I suggest you create a rocket , based on those found in the game Half-Life first name. My goal is not to make you apprentice terrorists but just quadrics pros!
A rocket inspired by Half-Life
The textures are also drawn from Half-Life and slightly modified by me. You will find them in the pack below (and in the final zip).
As you can see, the rocket is composed of four elements:
To encode the rocket so we must proceed in two steps:
Do not forget to change between each quadric texture so you do not end up with a rocket uniformly ... ugly. Here you have all the tools to draw the rocket. Refer to the diagram though I give you to meet proportions. Any distorted rocket will not be accepted for use in the field! (Hum. ..) to you then!
I'll put the code here interesting. I assume that you know perfectly load textures and initialize the application. I provided of course the complete code in the archive.Code: C + +-Select
As you can see I have personally decided to make a function to draw the rocket. I can draw as many rockets as I want without burdening the code.
You can, if you wish, create a sphere representing the Earth (with texture EarthLow.jpg the final pack) around which the rocket would turn. Council : to turn the rocket around the Earth just to think carefully about the order to make changes. As this is not the purpose of this exercise, which is simple and fast, you can imagine the solution.
A rocket inspired by Half-Life
Textures
The textures are also drawn from Half-Life and slightly modified by me. You will find them in the pack below (and in the final zip).
Diagram of the rocket
As you can see, the rocket is composed of four elements:
- an upper cone (texture rocket_top.jpg );
- cone means (texture rocket_middle.jpg );
- a lower cone (texture rocket_bottom.jpg );
- and finally, it can not be seen too in the diagram, a disc (texture rocket_motor.jpg ).
A method for encoding
To encode the rocket so we must proceed in two steps:
- launch the program you must: load all the textures used;
- when drawing you must:
- create a GLUquadric ;
- to set the quadric it generates texture coordinates automatically;
- draw the first object;
- to translate at the base of the second object;
- draw the second object;
- etc..
- destroy GLUquadric.
Phase to translate is important. As we have seen in the diagrams of different types of quadrics, they are always drawn from (0,0,0) in the local frame . Using intelligent transformations, it is possible to place each quadric where desired.
Do not forget to change between each quadric texture so you do not end up with a rocket uniformly ... ugly. Here you have all the tools to draw the rocket. Refer to the diagram though I give you to meet proportions. Any distorted rocket will not be accepted for use in the field! (Hum. ..) to you then!
Correction
I'll put the code here interesting. I assume that you know perfectly load textures and initialize the application. I provided of course the complete code in the archive.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 | / * I chose to make a function Draw Rocket. well I can call several times, and in any position of the initial reference * / void DrawRocket () { glPushMatrix () / / so that the changes are reversible GLUquadric * params = gluNewQuadric () / / create the quadric gluQuadricTexture ( params , GL_TRUE ) / / texture coordinates activation glBindTexture ( GL_TEXTURE_2D , top ) / / texture high gluCylinder ( params , 0.5 , 0 , 1.6 , 20 , 1 ) / / cone 1 glBindTexture ( GL_TEXTURE_2D , middle ); glTranslated ( 0 , 0 , - 1.5 ) / / I'm going down to the second cone gluCylinder ( params , 0.15 , 0.5 , 5.1 , 20 , 1 ) / / 2 cone glBindTexture ( GL_TEXTURE_2D , bottom ); glTranslated ( 0 , 0 , - 0.25 ) / / I finally get down to the bottom (on the diagram) gluCylinder ( params , 0.3 , 0.15 , 0.25 , 20 , 1 ) / / cone 3 / / Position and even I draw the output disc flames glBindTexture ( GL_TEXTURE_2D , motor ) gluDisk ( params , 0 , 0.3 , 20 , 1 ) / / Disc 4 gluDeleteQuadric ( params ) / / I delete the quadric glPopMatrix () / / I commend hop as I found } void DrawGL () { glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode ( GL_MODELVIEW ); glLoadIdentity ( ) gluLookAt ( 3 , 4 , 2 , 0 , 0 , 0 , 0 , 0 , 1 ) / / I set the camera to a place DrawRocket () / / I draw the first rocket glTranslated ( 2 , 0 , 0 ) / / I move to the second rocket glRotated ( 90 , 1 , 0 , 0 ) / * I'll turn this one for its main axis is horizontal * / DrawRocket (); / / and I draw glFlush (); SDL_GL_SwapBuffers (); } |
As you can see I have personally decided to make a function to draw the rocket. I can draw as many rockets as I want without burdening the code.
Improvements
You can, if you wish, create a sphere representing the Earth (with texture EarthLow.jpg the final pack) around which the rocket would turn. Council : to turn the rocket around the Earth just to think carefully about the order to make changes. As this is not the purpose of this exercise, which is simple and fast, you can imagine the solution.
download the required files at :
No comments:
Post a Comment