#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; double Ortho_bottom = -Wnd_H / 1.5; double Ortho_top = Wnd_H / 1.5; double near = -500.0; double far = 2000.0; double LookAt_eyex = 125; double LookAt_eyey = 105; double LookAt_eyez = -175; double LookAt_centerx = 10; double LookAt_centery = 0; double LookAt_centerz = 0; double LookAt_upx = 0.25; double LookAt_upy = -0.5; double LookAt_upz = -2.5; class Point { //point struct public: 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; this->z = src.z; } return *this; } }; //color table GLfloat 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) {//作直线 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) {//作直线 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); draw_rect(p1, p2, p3, p4, color, width); draw_rect(p5, p6, p2, p1, color, width); draw_rect(p8, p7, p6, p5, color, width); draw_rect(p4, p3, p7, p8, color, width); } inline double transradian(int angle) { return angle*PI / 180;//将角度转换为弧度 } //球体 void draw_sphere(double R, int n, GLfloat* color, int width = 1) { int longitude = 0;//纬度 int latitude = 0;//经度 int incremental = 360/n; glColor4fv(color); glLineWidth(width); for (longitude = 0; longitude <= 180; longitude += incremental) { glBegin(GL_LINE_LOOP); for (latitude = -180; latitude <= 180; latitude += incremental) { glVertex3f( R*sin(transradian(longitude))*cos(transradian(latitude)), R*sin(transradian(longitude))*sin(transradian(latitude)), R*cos(transradian(longitude)) ); } glEnd(); } for (latitude = -180; latitude <= 180; latitude += incremental) { glBegin(GL_LINE_LOOP); for (longitude = 0; longitude <= 180; longitude += incremental) { glVertex3f( R*sin(transradian(longitude))*cos(transradian(latitude)), R*sin(transradian(longitude))*sin(transradian(latitude)), R*cos(transradian(longitude)) ); } glEnd(); } } //椭球 void draw_oblong(double Rx, double Ry, double Rz, int n, GLfloat* color, int width = 1) { int longitude = 0;//纬度 int latitude = 0;//经度 int incremental = 360 / n; glColor4fv(color); glLineWidth(width); for (longitude = 0; longitude <= 180; longitude += incremental) { glBegin(GL_LINE_LOOP); for (latitude = -180; latitude <= 180; latitude += incremental) { glVertex3f( Rx*sin(transradian(longitude))*cos(transradian(latitude)), Ry*sin(transradian(longitude))*sin(transradian(latitude)), Rz*cos(transradian(longitude)) ); } glEnd(); } for (latitude = -180; latitude <= 180; latitude += incremental) { glBegin(GL_LINE_LOOP); for (longitude = 0; longitude <= 180; longitude += incremental) { glVertex3f( Rx*sin(transradian(longitude))*cos(transradian(latitude)), Ry*sin(transradian(longitude))*sin(transradian(latitude)), Rz*cos(transradian(longitude)) ); } glEnd(); } } //环 void draw_torus(double R, double R0, int nlong, int nlat, GLfloat* color, int width = 1) { int longitude = 0;//纬度 int latitude = 0;//经度 int incremental_long = 360 / nlong; int incremental_lat = 360 / nlat; glColor4fv(color); glLineWidth(width); for (longitude = -180; longitude <= 180; longitude += incremental_long) { glBegin(GL_LINE_LOOP); for (latitude = -180; latitude <= 180; latitude += incremental_lat) { glVertex3f( (R0 + R*sin(transradian(longitude)))*cos(transradian(latitude)), (R0 + R*sin(transradian(longitude)))*sin(transradian(latitude)), R*cos(transradian(longitude)) ); } glEnd(); } for (latitude = -180; latitude <= 180; latitude += incremental_lat) { glBegin(GL_LINE_LOOP); for (longitude = -180; longitude <= 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);//坐标轴 GL_Line(-Wnd_W, 0, 0, Wnd_W, 0, 0, blue_color, 2); GL_Line(0, 0, -5000, 0, 0, 5000, green_color, 2); //球体 draw_sphere(150, 30, white_color); //椭球 draw_oblong(75, 75, 150, 40, white_color); //环 draw_torus(40, 200, 30, 60, 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(&argc, argv); glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); glutInitWindowPosition(150, 150); glutInitWindowSize(Wnd_W * 2, Wnd_H * 2); glViewport(0, 0, Wnd_W, Wnd_H); glutCreateWindow("Demo"); init(); glutDisplayFunc(&display); glutMainLoop(); }
时间: 2024-12-23 19:36:07