close
其實這程式碼滿多都是照著課本打而已,xyz的數學式子也都是課本給的。所以只好盜用一下課本的圖了~~~哈哈。
1.更改原本是填滿的Polygon,變成只要線就好。Polygon輸出時是不包含線的,因此如果要線,那就需要重新再以線的模式下去畫。
此作業因為只需要線,所以就直接輸出線就好。用glPolygonMode(GL_FRONT_AND_BACK, GL_LINE),GL_FRONT_AND_BACK讓他前後有區分,GL_LINE畫出線。
2.初步使用Rotatef旋轉坐標軸。
glRotatef(-60,1,0,0) 。第一個參數為旋轉角度,接下來的參數分別為x、y、z軸。要旋轉的軸參數就填上1。
3.設定線的寬度 glLineWidth(2);
線的寬度0.5~10。
結果
程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************** | |
Computer Graghics | |
Author:SHEN ZHI-XUN | |
Time:2016/10/22 | |
********************/ | |
#include <iostream> | |
#include <GL/glut.h> | |
#include <math.h> | |
#define M_PI 3.14159 | |
using namespace std; | |
void display() { | |
glClear(GL_COLOR_BUFFER_BIT); //清除COLOR_BUFFER。 | |
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //設定PolygonMode。 | |
glLineWidth(2); //設定線的寬度。 | |
glRotatef(-60,1,0,0); //向Z軸旋轉-60度。 | |
GLdouble c = M_PI / 180.0; //M_PI=3.14159, 轉degree成radian的係數。 | |
GLdouble x, y, z, thetar; | |
for (double phi = -80.0; phi <= 80;phi+=20.0) { | |
GLdouble phir = c*phi; | |
GLdouble phir20 = c*(phi+20); | |
glBegin(GL_QUAD_STRIP); | |
for (GLdouble theta = -180.0; theta <= 180.0;theta+=20.0) { | |
thetar = c*theta; | |
x = sin(thetar)*cos(phir); | |
y = cos(thetar)*cos(phir); | |
z = sin(phir); | |
glVertex3d(x,y,z); | |
x = sin(thetar)*cos(phir20); | |
y = cos(thetar)*cos(phir20); | |
z = sin(phir20); | |
glVertex3d(x, y, z); | |
} | |
glEnd(); | |
glFlush(); | |
} | |
//north pole | |
glBegin(GL_TRIANGLE_FAN); | |
glVertex3d(0.0,0.0,1.0); | |
GLdouble c80 = c*80.0; | |
z = sin(c80); | |
for (GLdouble theta = -180.0; theta <= 180;theta+=20) { | |
thetar = c*theta; | |
x = sin(thetar)*cos(c80); | |
y = cos(thetar)*cos(c80); | |
glVertex3d(x,y,z); | |
} | |
glEnd(); | |
glFlush(); | |
//south pole | |
glBegin(GL_TRIANGLE_FAN); | |
glVertex3d(0.0,0.0,-1.0); | |
z = -sin(c80); | |
for (GLdouble theta = -180.0; theta <= 180.0;theta+=20.0) { | |
thetar = c*theta; | |
x = sin(thetar)*cos(c80); | |
y = cos(thetar)*cos(c80); | |
glVertex3d(x,y,z); | |
} | |
glEnd(); | |
glFlush(); | |
} | |
void init() {} | |
int main(int argc,char** argv) { | |
glutInit(&argc, argv); | |
glutInitWindowSize(500, 500); | |
glutInitWindowPosition(100,100); | |
glutCreateWindow("Sphere Approximation"); | |
glutDisplayFunc(display); | |
init(); | |
glutMainLoop(); | |
return 0; | |
} |
全站熱搜