Computer Graphics-code_3

Source: Internet
Author: User

#include <iostream> #include <GL/glut.h> #include <cmath> #define PI 3.141592653#pragma Comment (lib, "  Opengl32.lib ") #pragma comment (lib," Glu32.lib ") #pragma comment (lib," Glut32.lib ") using namespace Std;int Wnd_w = 800 /2;int Wnd_h = 600/2;double aspect = wnd_w/wnd_h;double Ortho_left =-wnd_w/1.5;double Ortho_right = wnd_w/1.5;d ouble Ortho_bottom =-wnd_h/1.5;double Ortho_top = wnd_h/1.5;double near = -500.0;double far = 2000.0;double LookAt_ey ex = 125;double Lookat_eyey = 105;double Lookat_eyez = -175;double Lookat_centerx = 10;double LookAt_centery = 0;double Lo Okat_centerz = 0;double Lookat_upx = 0.25;double Lookat_upy = -0.5;double Lookat_upz = -2.5;class Point {//point structpub Lic:int X;int Y;int z; Point (): X (0), y (0), Z (0) {}point (int a, int b, int c): X (a), Y (b), Z (c) {}point (const point& SRC): x (src.x), Y (src . Y), Z (src.z) {}point& operator= (const point &src) {if (This! = &src) {this->x = Src.x;this->y = Src.y;t his-&Gt;z = src.z;} Return *this;}};/ /color tableglfloat black_color[] = {0.0, 0.0, 0.0, 1.0}; Glfloat red_color[] = {1.0,0.0,0.0,1.0}; Glfloat green_color[] = {0.0, 1.0, 0.0, 1.0}; Glfloat blue_color[] = {0.0, 0.0, 1.0, 1.0}; Glfloat white_color[] = {1.0, 1.0, 1.0, 1.0};  Glfloat grey_color[] = {0.5, 0.5, 0.5, 1.0};inline void gl_line (int x0, int y0, int z0, int x1, int y1, int z1, glfloat* color, int width = 1) {//As a straight line GLCOLOR4FV (color); gllinewidth (width); glbegin (gl_lines); glvertex3f (x0, y0, z0); glvertex3f ( x1, Y1, Z1); Glend ();} inline void Gl_line (Point P1, point P2, glfloat* color, int width = 1) {//As line GLCOLOR4FV (color); gllinewidth (width); Glbegin ( Gl_lines); Glvertex3d (p1.x, P1.y, p1.z); Glvertex3d (p2.x, P2.y, p2.z); Glend ();} void Draw_rect (Point P1, point P2, point P3, point P4, glfloat* color, int width = 1) {GLCOLOR4FV (color); Gllinewidth (width ); Glbegin (Gl_line_loop); Glvertex3d (p1.x, P1.y, p1.z); Glvertex3d (p2.x, P2.y, p2.z); Glvertex3d (p3.x, P3.y, p3.z); Glvertex3d (p4.x, P4.y, p4.z); Glend ();} void Draw_cube (glfloat* color, int width = 1) {point P1 (-100, 100, 100); Point P2 (100, 100, 100); Point P3 (100,-100, 100); Point P4 (-100,-100, 100); Point P5 (-100, 100,-100); Point P6 (100, 100,-100); Point P7 (100,-100,-100); Point P8 ( -100, -100, -100);d raw_rect (P1, p2, p3, p4, color, width);d raw_rect (P5, P6, p2, p1, color, width);d raw_rect (P8, p 7, P6, p5, color, width);d raw_rect (P4, P3, P7, P8, color, width);} inline double Transradian (int angle) {return angle*pi/180;//converts the angle to radians}//sphere void Draw_sphere (double R, int n, glfloat* colo r, int width = 1) {int longitude = 0;//latitude int latitude = 0;//Longitude int incremental = 360/N;GLCOLOR4FV (color); gllinewidth (width) ; for (longitude = 0; longitude <=; longitude + = incremental) {Glbegin (Gl_line_loop); for (latitude = -180; latitude <= 180; Latitude + = incremental) {glvertex3f (R*sin (Transradian)) Longitude (*cos (Transradian)), Latitude (R*sin ( Longitude) *sin (Transradian (latitude)), R*cos (Transradian (longitude)));} Glend ();} for (latitude=-180; Latitude <= 180; Latitude + = incremental) {Glbegin (Gl_line_loop); for (longitude = 0; longitude <=; longitude + = incremental) {Glvert ex3f (R*sin (Transradian (longitude)) *cos (Transradian (latitude)), R*sin (Transradian (longitude)) *sin (Transradian ( latitude)), R*cos (Transradian (longitude)));} Glend ();}} ellipsoid void Draw_oblong (double Rx, double Ry, double Rz, int n, glfloat* color, int width = 1) {int longitude = 0;//latitude int lat Itude = 0;//Longitude int incremental = 360/N;GLCOLOR4FV (color); gllinewidth (width); for (longitude = 0; longitude <=; long Itude + = incremental) {Glbegin (Gl_line_loop); for (latitude = -180; latitude <=; latitude + = incremental) {GLVERTEX3 F (Rx*sin (Transradian (longitude)) *cos (Transradian (latitude)), Ry*sin (Transradian (longitude)) *sin (Transradian ( latitude)), Rz*cos (Transradian (longitude)));} Glend ();} for (latitude = -180; latitude <=; latitude + = incremental) {Glbegin (Gl_line_loop); for (longitude = 0; Longitude &l t;= 180; Longitude + = incremental) {Glvertex3f (Rx*sin (Transradian (longitude)) *cos (Transradian (latitude)), Ry*sin (Transradian (longitude)) *sin (Transradian (latitude)), Rz*cos (Transradian (longitude)));} Glend ();}} ring void Draw_torus (double R, double R0, int nlong, int nlat, glfloat* color, int width = 1) {int longitude = 0;//latitude int lat  Itude = 0;//Longitude int incremental_long = 360/nlong;int Incremental_lat = 360/NLAT;GLCOLOR4FV (color); gllinewidth (width); for (longitude = -180; longitude <=; longitude + = Incremental_long) {Glbegin (Gl_line_loop); for (latitude = -180; latitude <=; latitude + = Incremental_lat) {glvertex3f (R0 + r*sin (Tran Sradian (longitude)) *cos (Transradian (latitude)), (R0 + r*sin (Transradian (longitude))) *sin (Transradian (latitude)), R*cos (Transradian (longitude)));} Glend ();} for (latitude = -180; latitude <=; latitude + = Incremental_lat) {glbegin (Gl_line_loop); for (longitude = -180; longi Tude <= 180; Longitude + = Incremental_long) {glvertex3f ((R0 + r*sin (Transradian (longitude))) *cos (Transradian (latitude)), (R0 + r*sin (Transradian (longitude))) *sin (Transradian (latitude)), R*cos (Transradian (longitude)));} Glend ();}}  void display () {glclear (gl_color_buffer_bit); Gl_line (0,-wnd_h, 0, 0, wnd_h, 0, Grey_color, 2);//Axis gl_line (-wnd_w, 0, 0, Wnd_w, 0, 0, Blue_color, 2); Gl_line (0, 0, -5000, 0, 0,, Green_color, 2);//Sphere Draw_sphere (, White_color);//Ellipsoid Dr Aw_oblong (White_color),//Ring Draw_torus (max, Max, Max, grey_color); Glflush (); void init (void) {Glclearcolor (0.0, 0.0, 0.0, 0.0); Glmatrixmode (gl_projection); glloadidentity (); Glortho (Ortho_left, Ortho_right, Ortho_bottom, Ortho_top, near, far); Glmatrixmode (Gl_modelview); glloadidentity (); Glulookat (LookAt_eyex , Lookat_eyey, Lookat_eyez, Lookat_centerx, Lookat_centery, Lookat_centerz, LOOKAT_UPX, Lookat_upy, LookAt_upz);} void Main (int argc, char** argv) {glutinit (&AMP;ARGC, argv); Glutinitdisplaymode (Glut_rgb | Glutinitwindowposition (Glut_single), Glutinitwindowsize (Wnd_w * 2, Wnd_h * 2), Glviewport (0, 0, Wnd_w, Wnd_H); GlutcReatewindow ("Demo"); init (); Glutdisplayfunc (&display); Glutmainloop ();} 

  

Computer Graphics-code_3

Related Article

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.