OpenGL programming easy to get started with the second geometry

Source: Internet
Author: User

In this chapter we'll talk about two times the drawing of geometric objects. There are several different ways to draw a two-time geometric object, and in this case we can see different ways of rendering different effects, as shown in Figure 15.

Example 13: This example uses the Glu library function to draw four geometric objects, namely cylinders, spheres, discs, and partial discs.

#include <windows.h>
#include <GL/glut.h>
#include <math.h>

/* Declare four two-time surface objects */
Gluquadricobj *quadobj1;
Gluquadricobj *quadobj2;
Gluquadricobj *quadobj3;
Gluquadricobj *quadobj4;

static float light_ambient[] = {0.1,0.1,0.1,1.0};
static float light_diffuse[] = {0.5,1.0,1.0,1.0};
static float light_position[] = {90.0,90.0,150.0,0.0};

static float front_mat_shininess[] = {60.0};
static float front_mat_specular[] = {0.2,0.2,0.2,1.0};
static float front_mat_diffuse[] = {0.5,0.5,0.28,1.0};

static float back_mat_shininess[] = {60.0};
static float back_mat_specular[] = {0.5,0.5,0.2,1.0};
static float back_mat_diffuse[] = {1.0,0.9,0.2,1.0};

Static float imodel_ambient[] = {1.0,1.0,1.0,1.0};
Static float imodel_twoside[] = {Gl_true};
Static float imodel_oneside[] = {Gl_false};

void Myinit (void)
{
/* Set Background color * *
Glclearcolor (1.0,1.0,1.0,1.0);

Glenable (gl_depth_test);
Gldepthfunc (gl_lequal);

/* Set the Light/*
GLLIGHTFV (gl_light0,gl_ambient,light_ambient);
GLLIGHTFV (Gl_light0,gl_diffuse,light_diffuse);
GLLIGHTFV (gl_light0,gl_position,light_position);

/* Set Material * *
GLMATERIALFV (Gl_front,gl_diffuse,front_mat_diffuse);
GLMATERIALFV (Gl_front,gl_specular,front_mat_specular);
GLMATERIALFV (gl_front,gl_shininess,front_mat_shininess);

GLMATERIALFV (Gl_back,gl_diffuse,back_mat_diffuse);
GLMATERIALFV (Gl_back,gl_specular,back_mat_specular);
GLMATERIALFV (gl_back,gl_shininess,back_mat_shininess);

/* Set the LIGHTING model parameters * *
GLLIGHTMODELFV (gl_light_model_ambient,imodel_ambient);
GLLIGHTMODELFV (Gl_light_model_two_side,imodel_twoside);

/* Activation Care * *
Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Glshademodel (Gl_smooth);
}

void Mydisplay (void)
{
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);

/* Create four two-time surface objects */
QuadObj1 = Glunewquadric ();
QuadObj2 = Glunewquadric ();
QuadObj3 = Glunewquadric ();
QuadObj4 = Glunewquadric ();

/* Draw a cylindrical body * *
Glpushmatrix ();
Gluquadricdrawstyle (Quadobj1,glu_fill);
Gluquadricnormals (Quadobj1,gl_flat);
Gluquadricorientation (Quadobj1,glu_inside);
Gluquadrictexture (quadobj1,gl_true);

glcolor3f (1.0,1.0,0.0);
Glrotatef (30,1.0,0.0,0.0);
Glrotatef (40,0.0,1.0,0.0);
Glucylinder (quadobj1,2.0,2.0,9.0,20.0,8.0);
Glpopmatrix ();

/* Draw a sphere * *
Glpushmatrix ();
Gluquadricdrawstyle (Quadobj2,glu_silhouette);
Gltranslatef ( -5.0,-1.0,0.0);
Glusphere (quadobj2,3.0,20.0,20.0);
Glpopmatrix ();

/* Draw a disc * *
Glpushmatrix ();
Gluquadricdrawstyle (Quadobj3,glu_line);
Gltranslatef ( -2.0,4.0,0.0);
Gludisk (quadobj3,2.0,5.0,15.0,10.0);
Glpopmatrix ();

/* Draw a part of the disc * *
Glpushmatrix ();
Gluquadricdrawstyle (Quadobj4,glu_point);
Gltranslatef ( -3.0,-7.0,0.0);
Glupartialdisk (quadobj4,2.0,5.0,15.0,10.0,10.0,100.0);
Glpopmatrix ();

/* Delete Four two-time surface object Objects * *
Gludeletequadric (QUADOBJ1);
Gludeletequadric (QUADOBJ2);
Gludeletequadric (QUADOBJ3);
Gludeletequadric (QUADOBJ4);
 
Glflush ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (45.0, (glfloat) w/(glfloat) h,1.0,50.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
Gltranslatef (0.0,0.0,-25.0);
}

int main (int argc,char * * argv)
{
/* Initialize * *
Glutinit (&ARGC,ARGV);
Glutinitdisplaymode (glut_single| glut_rgb| Glut_depth);
Glutinitwindowsize (400,400);
Glutinitwindowposition (100,100);

/* Create window * *
Glutcreatewindow ("DRAW quadric OBJECTS");

/* Draw and display * *
Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

Glutmainloop ();
return 0;
}


Glunewquadric creates a two-time object. This function creates and returns a pointer to the new two-time object. When calling two times the description and control function is pointing to this object. A return value of 0 indicates that there is not enough space to allocate to the object.

The Glquadricdrawstyle function specifies how objects are plotted two times. In this case, the cylinder is drawn in Glu_fill. The meaning is to draw the original two times object with the polygon, and the polygon is drawn counterclockwise. The sphere is drawn in the form of a gl_silhouette, that is, a series of lines in addition to the boundary to draw two times objects. The disc is drawn in Gl_line, that is, a series of lines to draw the object two times. The part of the disc is drawn in Gl_point, that is, the object is drawn two times with a series of points.

Glquadricnormals, specifies the normal vector type used by the two-time object.

glquadricorientation, specifies the inner or outer orientation of the object two times. The glu_outside is the default value, which indicates that two objects are plotted using the method pointing to the inner surface, and the glu_inside indicates that the object is plotted two times using the method pointing to the outside.


Figure 15: two times geometry


glquadrictexture Specifies whether the object uses textures two times. Gl_false is the default value.

void Glucylinder gluquadricobj *qobj,gldouble baseradius,gldouble topradius,gldouble height,glint slices,glint Stacks) to draw a cylindrical body.

Qobj indicates which two-time object is.

The radius of the Baseradius cylinder at the time of z=0.

The radius of the Topradius cylinder at the time of Z=height.

Height of the cylindrical body.

Slices the number of slices around the z axis.

Stacks the number of slices along the z axis. Stacks and slices are perpendicular.

void Glusphere (gluquadricobj *qobj,gldouble radius,glint slices,glint) draws a sphere.

Qobj indicates which two-time object is.

Radius sphere radii.

Slices the number of slices around the z axis.

Stacks the number of slices along the z axis.

void Gludisk (gluquadricobj *qobj,gldouble innerradius,gldouble outerradius,glint) draws a disk.

Qobj indicates which two-time object is.

The internal radius of the Innerradius disc may be 0.

The outer radius of the Outerradius disc.

Slices the number of slices around the z axis.

Loops Disc Concentric circle number.

void Glupartialdisk gluquadricobj *qobj,gldouble innerradius,gldouble outerradius,glint slices,glint loops,gldouble Startangle,gldouble Sweepangle) Draws a part of a disk.

StartAngle starting angle, the unit is degree.

Sweepangle Scan angle, unit for degrees.

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.