OpenGL手动绘制线框球体和棱锥

内容

此代码还不完善,画球体和正棱锥封装不完善,封装成任一点为中心画更好。

实现了三个要求:

  1. 将绘制圆的过程做成一个显示列表,并调用显示列表显示一个圆。
  2. 绘制一个正棱锥。(本题不能用GLUT中的对象来绘制)
  3. 绘制一个球。(同上)

其中第一个要求在实现下面功能是并没有用,只是为了实现要求,因为虽然显示列表保存后能提高性能,但保存后不能修改,所以下面没用到。

画棱锥了球时半径的变化用相似算的。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <bits/stdc++.h>
using namespace std;
#include <stdlib.h>
const double PI = acos(-1.0);

static GLsizei iMode = 1;
static GLfloat xRot = 0;
static GLfloat yRot = 0;

GLint n = 5;
GLint Circle_List = 0;

void Initial(){
    glClearColor(1, 1, 1, 1);
    glColor3f(0, 0, 0);
}

void ChangeSize(int w, int h){
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-1.5, 1.5, -1.5, 1.5);
}

void Draw_Circle(int n, double r, double z){
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    glBegin(GL_POLYGON);
        for(int i=0; i<n; i++){
            glVertex3f(r*cos(2*i*PI/n), r*sin(2*i*PI/n), z);
        }
    glEnd();
}

GLint GenCircleList(){
    GLint lid = glGenLists(1);
    glNewList(lid, GL_COMPILE);
        Draw_Circle(360, 0.5, 0);
    glEndList();
    return lid;
}
void Draw_C(){
    glPushMatrix();
        glCallList(Circle_List);
    glPopMatrix();
}
void Draw_Cylinder(int n=5, double r=0.5, double h=1, int layer = 10){
    glPushMatrix();
        for(int i=0; i<n; i++){ // 棱
            glBegin(GL_LINES);
                glVertex3f(0, 0, h);
                glVertex3f(r*cos(2*i*PI/n), r*sin(2*i*PI/n), 0);
            glEnd();
        }

        for(double i=0; i<h; i+=h/layer){ // 多边形框
            double new_r = r*(h-i)/h;
            Draw_Circle(n, new_r, i);
        }
    glPopMatrix();
}
void Draw_Line_Sphere(int n=360, double r=1, int layer=5, int line=10){
    glPushMatrix();
        for(double i=0; i<=r; i+=r/layer){ // 圆框
            double new_r = sqrt(r*r-i*i);
            Draw_Circle(n, new_r, i);
            Draw_Circle(n, new_r, -i);
        }
        glRotatef(90, 0, 1, 0);
        for(double i=0; i<360; i+=360.0/line){
            glRotated(360.0/line, 1, 0, 0);
            Draw_Circle(n, r, 0);
        }
    glPopMatrix();
}
void Display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glRotatef(xRot, 1, 0, 0);
    glRotatef(yRot, 0, 1, 0);

    switch(iMode){
    case 1: Draw_C();  break;
    case 2: Draw_Cylinder(n); break;
    case 3: Draw_Line_Sphere(); break;
    }

    glFlush();
}

void SpecialKeys(int key, int x, int y){
    if(key == GLUT_KEY_UP) xRot -= 5;
    if(key == GLUT_KEY_DOWN) xRot += 5;
    if(key == GLUT_KEY_LEFT) yRot -= 5;
    if(key == GLUT_KEY_RIGHT) yRot += 5;
    if(xRot > 356) xRot = 0;
    if(xRot < -1) xRot = 355;
    if(yRot > 356) yRot = 0;
    if(yRot < -1) yRot = 355;
    glutPostRedisplay();
}
static void key(unsigned char key, int x, int y){
    switch(key){
        case '=': n=min(99, n+1); break;
        case '-': n=max(3, n-1); break;
    }
    glutPostRedisplay();
}

void ProcessMenu(int value){
    iMode = value;
    glutPostRedisplay();
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Hello");

    int nMainMenu = glutCreateMenu(ProcessMenu);
    glutAddMenuEntry("圆", 1);
    glutAddMenuEntry("线框多棱锥", 2);
    glutAddMenuEntry("线框球体", 3);
    glutAttachMenu(GLUT_RIGHT_BUTTON);


    glutDisplayFunc(Display);
    glutReshapeFunc(ChangeSize);
    glutSpecialFunc(SpecialKeys);
    glutKeyboardFunc(key);
    Initial();

    Circle_List = GenCircleList();

    glutMainLoop();
}

# 使用单$作为行内数学公式分界符