Nehe OpenGL Tutorial Lesson 18th: Two-time geometry

Source: Internet
Author: User

Go from "translation" Nehe OpenGL tutorial

Objective

Statement, this Nehe OpenGL tutorial series of articles by 51 blog yarin Translation (2010-08-19), this blog for reprint and a little collation and modification. Thank you for compiling Nehe's OpenGL pipeline tutorial, as well as yarn translation finishing.

Nehe OpenGL Lesson 18th: Two geometry

Two times geometry:

With two times of geometry, you can easily create balls, discs, cylinders and cones.

Two-times surface is a method of drawing composite objects, which usually does not require many triangles. We're going to use the code for lesson seventh. We're going to add 7 variables and modify textures to add some variation:

BOOL SP; Does the SPACEBAR press

int part1; The starting angle of the disc
int part2; End angle of the disc
int p1=0; Increment 1
int p2=1; Increment 1
Gluquadricobj *quadratic; Two times geometry
Gluint object=0; Two-time Geometry identifier

Okay, let's start with the INITGL () function. We're going to add 3 lines of code to initialize our two-time surface. These 3 lines of code will increase after you make the 1th light source valid, but before you return. The first line of code initializes the two-second surface and creates a pointer to the two-second surface.

, if the two-time surface cannot be created, then the pointer is null. The second line of code will create a smooth normal vector on the surface of the two-second surface, so that it will look better when the light is illuminated. Some of the other possible values are: Glu_none and Glu_flat. Finally we make in two

The texture mapping of the secondary surface surface is valid.

Quadratic=glunewquadric (); Create two geometry
Gluquadricnormals (quadratic, glu_smooth); Using Smooth normals
Gluquadrictexture (quadratic, gl_true); Using textures

Now I decide to keep the cube in this lesson so that you can see how the texture is mapped to the two-time surface object. And I'm going to define the code that draws the cube as a separate function so that when we define the function draw () it will become less cluttered. Everyone should

It's time to remember these codes:

Glvoid Gldrawcube ()//Draw 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);
Behind
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 the scene drawing function, where I'll just 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 his value regardless of whether you call him at any time) to express a very cool effect.

I'm going to rewrite the Drawglscene function for the sake of clarity.
You will notice that when I discuss these parameters that are being used, I omit the first argument (quadratic) of the current function. This parameter will be used by all objects except the cube. So I ignored it when I was discussing these parameters.

int Drawglscene (glvoid)
{
//...
This part is the newly added
Switch (object)//draw which geometry two times
{
Case 0://Draw Cube
Gldrawcube ();
Break

The 2nd object we created is a cylinder. Parameter 1 (1.0F) is the base 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 the latitude (how many segments surround the z-axis), and the parameter 5 (32) is the warp

(number of subdivisions along the z-axis). The more subdivide the object, the more detailed it is. We can increase the number of polygons for an object by increasing the subdivision method. So you can sacrifice speed for quality (in time for quality), and most of the time we can easily find a suitable "degree".

Case 1://Draw Cylinder
Gltranslatef (0.0f,0.0f,-1.5f);
Glucylinder (quadratic,1.0f,1.0f,3.0f,32,32);
Break

Object 3 will create a CD-like plate. Parameter 1 (0.5F) is the inner circle radius of the plate, the parameter can be 0, it means that there is no hole in the middle of the plate, the bigger the radius of the inner circle. Parameter 2 (1.5F) indicates the radius of the outer circle, which must be larger than the radius of the inner circle. Parameter 3 (32) is a constituent

The number of slices in the plate, which can be imagined as the number of slices in the pizza pie. The more slices, the smoother the edges of the outer circle. The last parameter (32) is the number of rings that make up the plate. The ring is like a track on a record, a ring set. These rings are subdivided from the radius of the inner circle to the radius of the outer circle.

Again, the more segments, the slower the speed.

Case 2://Draw Disc
Gludisk (quadratic,0.5f,1.5f,32,32);
Break

Our 4th object, I know you're draining your energy to describe it. is the ball. Drawing the ball will become very simple. Parameter 1 is the radius of the ball. If you do not understand the radius/diameter and so on, you can understand the distance from the center of the object to the outside of the object, where we use 1.3F as the radius. Pick up

Down two parameters are subdivided, and the same as the cylinder, parameter 2 is the latitude, parameter 3 is the meridian. The more segments the ball looks smoother, the more often the ball needs to be subdivided to make them look smooth.

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

The 4th object we created is created using the same commands we used to create the cylinder, and if you remember, we can control the top radius and the ground radius by controlling the parameters 2 and three. So we can draw a cone with a top radius of zero and a radius of 0 for the top face

A point is created on the top face. So in the code below, we make the top radius equal to 0, which creates a point and creates our cone.

Case 4://Draw Cone
Gltranslatef (0.0f,0.0f,-1.5f);
Glucylinder (quadratic,1.0f,0.0f,3.0f,32,32);
Break

Our 6th object will be created by the Glupartialdisk function. The object we are going to create uses a few commands that you will see clearly before we create the object. But the command Glupartialdisk has two new parameters. The 5th parameter is the one we want to

The starting angle of the part of the plate drawn, parameter 6 is the rotation angle, that is, the angle of the turn. We are going to increase the rotation angle, which will cause the plate to be drawn slowly in the clockwise direction on the screen. Once the rotation angle reaches 360 degrees we will begin to increase the starting angle so that the plate looks like it is being

Gradually erase the same. We will repeat these processes.

Case 5://Draw some discs
PART1+=P1;
PART2+=P2;

if (part1>359)
{
p1=0;
part1=0;
P2=1;
part2=0;
}
if (part2>359)
{
P1=1;
p2=0;
}
Glupartialdisk (QUADRATIC,0.5F,1.5F,32,32,PART1,PART2-PART1);
Break
};

//...
}

In the Killglwindow () sections 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); Delete Geometry two times

In the end, I give the keyboard input code. Just add some checks on the remaining keys.

if (keys["] &&!sp)//space is pressed
{
Sp=true; Yes, draw the next two geometry
object++;
if (object>5)
object=0;
}
if (!keys["])//Space is released
{
Sp=false; Record this status
}

Original source code and version of the download:

Http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=18

Nehe OpenGL Tutorial Lesson 18th: Two-time geometry

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.