Nehe OpenGL tutorial (18)

Source: Internet
Author: User

Lesson 1

Secondary ry:

With secondary ry, you can easily create balls, discs, cylinders, and cones.
 
A quadratic surface is a method for drawing a composite object. This method usually does not require many triangles. We will use the code for Lesson 7. We will add 7 variables and modify the texture to add some changes:

Bool sp; // whether the Space key is pressed

Int Part1; // The starting angle of the disc
Int Part2; // end angle of the disc
Int p1 = 0; // incremental 1
Int P2 = 1; // incremental 1
Gluquadricobj * quadratic; // quadratic ry
Gluint object = 0; // ry identifier

Now let's get started with the initgl () function. We intend to add three lines of code to initialize our quadratic surface. The three lines of code will be added after you make the 1 light source effective, but before the return. The first line of code initializes the secondary surface and creates a pointer pointing to the secondary surface. If the secondary surface cannot be created, the pointer is null. The second line of code will create a smooth normal vector on the surface of the secondary surface, which will look better when the light goes up. Possible values are: glu_none and glu_flat. Finally, the texture ing on the surface of the secondary surface is effective.

Quadratic = glunewquadric (); // create a secondary ry
Gluquadricnormals (quadratic, glu_smooth); // use a smooth normal
Gluquadrictexture (quadratic, gl_true); // use texture

Now I decided to keep the cube in this lesson so that you can see how the texture maps to the Quadratic Surface object. In addition, I plan to define the code for drawing a cube as a separate function, so that when we define the function draw (), it will become less messy. Everyone should remember the code:
Glvoid gldrawcube () // plot the cube
{
Glbegin (gl_quads );
// Front
Glnormal3f (0.0f, 0.0f, 1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f, 1.0f );
// After
Glnormal3f (0.0f, 0.0f,-1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f );
// Above
Glnormal3f (0.0f, 1.0f, 0.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f, 1.0f, 1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f, 1.0f, 1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f );
// Below
Glnormal3f (0.0f,-1.0f, 0.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f,-1.0f,-1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f,-1.0f,-1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f );
// Right
Glnormal3f (1.0f, 0.0f, 0.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f );
// Left
Glnormal3f (-1.0f, 0.0f, 0.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f, 1.0f, 1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f );
Glend ();
}

The next step is to plot the function in the scenario. Here I will only write a simple example. And when I draw a part of the plate, I will use a static variable (a local variable that can retain its value no matter when you call it) to express a cool effect. For clarity, I will rewrite the drawglscene function.
You will notice that when I discuss these parameters in use, I ignore the first parameter (quadratic) of the current function ). This parameter will be used by all objects except cubes. So when I discuss these parameters, I ignore them.

Int drawglscene (glvoid)
{
//...
// This part is newly added
Switch (object) // specifies the secondary ry to be drawn.
{
Case 0: // draw a cube
Gldrawcube ();
Break;

The 2nd objects we created are a cylindrical object. Parameter 1 (1.0f) is the bottom radius of the cylinder, parameter 2 (1.0f) is the top radius of the cylinder, and parameter 3 (3.0f) is the height of the cylinder. Parameter 4 (32) is a weft (How many subdivisions are there around the Z axis), and parameter 5 (32) is a meridian (How many subdivisions are there along the Z axis ). The more segments the object is, the more detailed the object is. We can use the method of adding subdivision to increase the number of polygon of objects. Therefore, you can sacrifice the speed to change back the quality (change the quality by time). Most of the time, we can easily find a suitable "degree ".

Case 1: // draw a cylinder
Gltranslatef (0.0f, 0.0f,-1.5f );
Glucylinder (quadratic, 1.0f, 1.0f, 3.0f, 32, 32 );
Break;

Object 3 creates a CD-like tray. Parameter 1 (0.5f) is the incircle radius of the plate. If this parameter can be set to 0, no holes exist in the middle of the plate. The larger the incircle radius is. Parameter 2 (1.5f) indicates the outer circle radius, which must be greater than the inner circle radius. Parameter 3 (32) is the number of slices that make up the plate. This quantity can be imagined as the number of slices in the pizza. The more slices, the smoother the outer circle edge. The last parameter (32) is the number of rings that constitute the plate. The loop is like a track on a recording. These rings are subdivided from the inner circle radius to the outer circle radius. Again, the more segments, the slower the speed.

Case 2: // draw a disc
Gludisk (quadratic, 0.5f, 1.5f, 32, 32 );
Break;

I know that you have exhausted your energy to describe our 4th objects. It is the ball. It will be very easy to draw the ball. Parameter 1 indicates the radius of the ball. If you cannot understand the radius/diameter, you can understand the distance from the center of the object to the outside of the object. Here we use 1.3f as the radius. The next two parameters are subdivided. Like the cylinder, parameter 2 is the weft and parameter 3 is the Meridian. The more segments the ball looks smoother. Generally, the ball needs more segments to make them look smoother.

Case 3: // draw the ball
Glusphere (quadratic, 1.3f, 32, 32 );
Break;

The 4th objects we created were created using the same command as the cylinder we created. If you still remember, we can control the top radius and ground radius by controlling parameters 2 and 3. Therefore, we can set the radius of the top surface to 0 to draw a cone. If the radius of the top surface is 0, a vertex is created on the top surface. Therefore, in the following code, we make the radius of the top surface equal to 0, which creates a vertex and a cone.

Case 4: // draw a cone
Gltranslatef (0.0f, 0.0f,-1.5f );
Glucylinder (quadratic, 1.0f, 0.0f, 3.0f, 32, 32 );
Break;

Our 6th objects will be created by the glupartialdisk function. The object we intend to create uses some commands, which you will see clearly before creating the object. However, the command glupartialdisk has two new parameters. The 5th parameters are the starting angle of some plates we want to draw. parameter 6 is the rotation angle, that is, the rotation angle. We will increase the rotation angle, which will cause the plate to be slowly drawn on the screen clockwise. Once the rotation angle reaches 360 degrees, we will begin to increase the starting angle so that the plate looks like it is gradually erased. We will repeat these processes.

Case 5: // draw part of the disc
Part1 + = p1;
Part2 + = P2;

If (Part1> 359)
{
P1 = 0;
Part1 = 0;
P2 = 1;
Part2 = 0;
}
If (part 2> 359)
{
P1 = 1;
P2 = 0;
}
Glupartialdisk (quadratic, 0.5f, 1.5f, 32, Part1, part2-part1 );
Break;
};
//...
}

In the killglwindow () section of code, we need to delete the quadratic to free up system resources. We do this with the command gludeletequadratic.

Glvoid killglwindow (glvoid)
{
Gludeletequadric (quadratic); // deletes a secondary ry.

At the end, I enter the code on the keyboard. Only some checks on the remaining keys are added.

If (Keys [''] &! SP) // whether to press the space
{
SP = true; // Yes, draw the next quadratic body
Object ++;
If (Object> 5)
Object = 0;
}
If (! Keys ['']) // whether space is released
{
SP = false; // record this status
}

This is all. Now you can draw a Quadratic Surface in OpenGL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.