Saturday, August 25, 2012

OpenGL :: Quadrics :: Material 7


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.

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:

StyleExplanationExample
GLU_POINTThe object will be drawn with points.User Image
GLU_LINEThe object will be drawn with lines.User Image
GLU_FILLThe object will be drawn with solid faces.User Image


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
1
2
3
4
glBindTexture ( GL_TEXTURE_2D , texture1 ) 
    GLUquadric *  params  =  gluNewQuadric () 
    / / draw the sphere ... (Forthcoming) 
    gluDeleteQuadric ( params );
User Image
Code: C + + - Select
1
2
3
4
5

       
    
    the sphere ... (Forthcoming) 
    gluDeleteQuadric ( params );
User Image


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.

Sphere



User Image


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:

User Image


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



User Image


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).


User Image
gluCylinder (params, 1,0,2,20,1);


Disk



User Image


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



User Image


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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
glBindTexture ( GL_TEXTURE_2D , texture1 );

    GLUquadric *  params  =  gluNewQuadric ();

    gluQuadricDrawStyle ( params , GLU_LINE ) 
    gluCylinder ( params , 1 , 1 , 2 , 20 , 1 );

    gluQuadricDrawStyle(params,GLU_FILL);
    gluQuadricTexture(params,GL_TRUE);
    glTranslated(0,0,1);
    gluSphere(params,0.75,20,20);

    gluDeleteQuadric ( params );
User Image

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! : Devil:
: Angel:

User Image
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



User Image


As you can see, the rocket is composed of four elements:
  1. an upper cone (texture rocket_top.jpg );
  2. cone means (texture rocket_middle.jpg );
  3. a lower cone (texture rocket_bottom.jpg );
  4. 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:
    1. create a GLUquadric ;
    2. to set the quadric it generates texture coordinates automatically;
    3. draw the first object;
    4. to translate at the base of the second object;
    5. draw the second object;
    6. etc..
    7. 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.


User Image


download the required files at : 

No comments:

Post a Comment

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin