close

其實這程式碼滿多都是照著課本打而已,xyz的數學式子也都是課本給的。所以只好盜用一下課本的圖了~~~哈哈。

Hw3.PNG

 

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。

 

結果

hw3.PNG

 

程式碼

/********************
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;
}
view raw main.cpp hosted with ❤ by GitHub

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 a7069810 的頭像
    a7069810

    紀錄自己的程式人生

    a7069810 發表在 痞客邦 留言(0) 人氣()