OpenGL---instance sphere drawing cone

Source: Internet
Author: User

Use OpenGL interface to create the corresponding solid ball, cone, ring directly. The effect is as follows:



header File

You need to import the corresponding GL library:


You can increase the number of slices of a circular line with a diameter distribution on the z axis based on "+" (the z axis as the earth's axis, similar to a warp)stacks The number of lines around the z axis (similar to the Parallels on Earth)

Add Effect:


Reduced effect:


Full code:

#include "stdafx.h" #include "Windows.h" #include "math.h" #include "stdlib.h" #include "gl/gl.h" #include "gl/glu.h" #inc
Lude "gl/glut.h" static int slices = 16;

static int stacks = 16;

	static void resize (int width, int height) {const float AR = (float) width/(float) height;
	Glviewport (0, 0, width, height);
	Glmatrixmode (gl_projection);
	Glloadidentity ();

	Glfrustum (-ar, AR,-1.0, 1.0, 2.0, 100.0);
	Glmatrixmode (Gl_modelview);
Glloadidentity ();
	} static void Display () {const Double t = glutget (glut_elapsed_time)/1000.0;

	Const double A = t*90.0; Glclear (Gl_color_buffer_bit |
	Gl_depth_buffer_bit);

	Glcolor3d (0, 1, 0);
	Glpushmatrix ();
	Gltranslated (-2.4, 1.2,-6);
	Glrotated (60, 1, 0, 0);
	Glrotated (A, 0, 0, 1);

	Glutsolidsphere (1, slices, stacks);//solid Ball glpopmatrix ();
	Glpushmatrix ();
	gltranslated (0, 1.2,-6);
	Glrotated (60, 1, 0, 0);
	Glrotated (A, 0, 0, 1);

	Glutsolidcone (1, 1, slices, stacks);//Solid cone glpopmatrix ();
	Glpushmatrix ();
	Gltranslated (2.4, 1.2,-6); GLrotated (60, 1, 0, 0);
	Glrotated (A, 0, 0, 1);

	Glutsolidtorus (0.2, 0.8, slices, stacks);//Solid Ring Ring Glpopmatrix ();
	Glpushmatrix ();
	Gltranslated (-2.4,-1.2,-6);
	Glrotated (60, 1, 0, 0);
	Glrotated (A, 0, 0, 1);

	Glutwiresphere (1, slices, stacks);//Grid Sphere Glpopmatrix ();
	Glpushmatrix ();
	gltranslated (0,-1.2,-6);
	Glrotated (60, 1, 0, 0);
	Glrotated (A, 0, 0, 1);

	Glutwirecone (1, 1, slices, stacks);//Grid cone Glpopmatrix ();
	Glpushmatrix ();
	Gltranslated (2.4,-1.2,-6);
	Glrotated (60, 1, 0, 0);
	Glrotated (A, 0, 0, 1);

	Glutwiretorus (0.2, 0.8, slices, stacks);//Grid Ring Glpopmatrix ();
Glutswapbuffers ();
		} static void key (unsigned char key, int x, int y) {switch (key) {case 27:case ' Q ': Exit (0);

	Break
		Case ' + ': slices++;
		stacks++;

	Break
			Case '-': if (slices>3 && stacks>3) {slices--;
		stacks--;
	} break;
} glutpostredisplay ();

} static void Idle () {Glutpostredisplay ();}
Const Glfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f}; ConsT glfloat light_diffuse[] = {1.0f, 1.0f, 1.0f, 1.0f};
Const Glfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};

Const Glfloat light_position[] = {2.0f, 5.0f, 5.0f, 0.0f};
Const Glfloat mat_ambient[] = {0.7f, 0.7f, 0.7f, 1.0f};
Const Glfloat mat_diffuse[] = {0.8f, 0.8f, 0.8f, 1.0f};
Const Glfloat mat_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};

Const Glfloat high_shininess[] = {100.0f};
	/**//* Program Entry point */int main (int argc, char *argv[]) {glutinit (&ARGC, argv);
	Glutinitwindowsize (640, 480);
	Glutinitwindowposition (10, 10); Glutinitdisplaymode (Glut_rgb | glut_double |

	Glut_depth);

	Glutcreatewindow ("Freeglut Shapes");
	Glutreshapefunc (resize);
	Glutdisplayfunc (display);
	Glutkeyboardfunc (key);

	Glutidlefunc (idle);
	Glclearcolor (1, 1, 1, 1);
	Glenable (Gl_cull_face);

	Glcullface (Gl_back);
	Glenable (gl_depth_test);

	Gldepthfunc (gl_less);
	Glenable (GL_LIGHT0);
	Glenable (gl_normalize);
	Glenable (gl_color_material);

	Glenable (gl_lighting); GLLIGHTFV (Gl_ligHT0, Gl_ambient, light_ambient);
	GLLIGHTFV (Gl_light0, Gl_diffuse, Light_diffuse);
	GLLIGHTFV (Gl_light0, Gl_specular, light_specular);

	GLLIGHTFV (Gl_light0, gl_position, light_position);
	GLMATERIALFV (Gl_front, gl_ambient, mat_ambient);
	GLMATERIALFV (Gl_front, Gl_diffuse, Mat_diffuse);
	GLMATERIALFV (Gl_front, Gl_specular, mat_specular);

	GLMATERIALFV (Gl_front, gl_shininess, high_shininess);

	Glutmainloop ();
return exit_success; }


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.