資源簡介
在太陽系程序的基礎上添加光照效果
- 宇宙中加一盞泛光
- 太陽自發光
- 太陽系中加一艘飛碟,飛碟沿著橢圓軌道繞地球或太陽運行,其上有一個spot light始終照耀著地球
程序為博主個人獨自編寫,切勿用于商用
代碼片段和文件信息
#include
#include?
#include?
#include?
/*變量設置*/
const?GLfloat?windowWidth?=?700.0f;//初始窗口寬
const?GLfloat?windowHeight?=?500.0f;//初始窗口高
const?GLfloat?pi?=?3.1415926;
GLfloat??distance?=?-3.6f;
GLfloat?sunTheta?=?0.0f;//太陽旋轉角度
GLfloat?earthTheta?=?0.0f;//地球旋轉角度
GLfloat?monthTheta?=?0.0f;//月球旋轉角度
GLfloat?dSunToEarth?=?2.5f;//太陽到地球
GLfloat?dEarthToMonth?=?0.35f;//地球到月亮
GLfloat?sunR?=?0.6f;//太陽半徑
GLfloat?earthR?=?0.15f;//地球半徑
GLfloat?monthR?=?0.08f;//月亮半徑
GLsizei?n?=?7200;//線段繪制次數
GLfloat?sunEarthFai?=?10.0f;//控制日地軌道傾斜
GLfloat?earthMonthFai?=?-20.0f;//控制地月軌道傾斜
GLfloat?a?=?1.0f;//橢圓長半軸
GLfloat?b?=?0.35f;//橢圓短半軸
GLfloat?t?=?0.0f;//衛星旋轉角度
GLfloat?sateX?=?a?*?cos(t?*?pi);
GLfloat?sateZ?=?b?*?sin(t?*?pi);
GLfloat?sateR?=?0.05f;//衛星半徑
//視點位置
GLfloat?x?=?0;
GLfloat?y?=?0.3;
GLfloat?z?=?2.0;
//方向光照參數
GLfloat?vLit0Position[4]?=?{?0100.0f?};//位置
GLfloat?vLit0Ambient[4]?=?{?0.2f0.2f0.2f1.0f?};//環境光
GLfloat?vLit0Diffuse[4]?=?{?0.8f0.8f0.8f1.0f?};//漫射光
GLfloat?vLit0Specluar[4]?=?{?0.5f0.5f0.5f1.0f?};//強光
//聚光燈光照參數
GLfloat?vSpotPosition[4]?=?{?sateX?0.0fsateZ1.0f?};//位置——始終為衛星的位置
GLfloat?vSpotDirection[3]?=?{-sateX?0.0f?-sateZ?};//方向——始終從衛星射向地球
GLfloat?spotExp?=?1.0f;//衰減系數
GLfloat?spotCutoff?=?15.0f;//聚光燈的光錐的發散角度
GLfloat?vEmission[4]?=?{?0.80.50.01.0?};?//材質的發射光顏色
GLfloat?vEmission2[4]?=?{?0.00.00.11.0?};?//材質的發射光顏色2
/*用戶函數*/
void?myInit();//初始化函數
/*回調函數*/
void?display(void);//繪制函數
void?myReshape(GLsizei?wGLsizei?h);
void?myTimeFunc(int?value);//定時器回調函數
void?processNormalKeys(unsigned?char?key?int?x?int?y);//響應鍵盤輸入,從而設定物體移近移遠以及旋轉的回調函數
int?main(int?argcchar?*?argv[])
{
glutInit(&argc?argv);
/*1-設置顯示模式*/
glutInitDisplayMode(GLUT_DOUBLE?|?GLUT_RGB);
/*2-初始化窗口*/
glutInitWindowSize(windowWidthwindowHeight);
glutInitWindowPosition(100100);
/*3-創建窗口*/
glutCreateWindow(“LightSunEarthMonth“);
/*4-設置一系列回調函數*/
myInit();
glutKeyboardFunc(processNormalKeys);//鍵盤響應
glutTimerFunc(10myTimeFunc0);//使物體運動
glutReshapeFunc(myReshape);//窗口變化響應
glutDisplayFunc(display);
glutIdleFunc(display);
/*5-啟動主循環*/
glutMainLoop();
return?0;
}
void?display()
{
//設置清除屏幕的顏色,并清除屏幕和深度緩沖
glClearColor(0.0f?0.0f?0.0f?0.0f);
glClear(GL_COLOR_BUFFER_BIT?|?GL_DEPTH_BUFFER_BIT);
gluLookAt(x?y?z?0?0?0?0?1.0?0);//改變視點,便于觀察
//設置成模型矩陣模式
glMatrixMode(GL_MODELVIEW);
/***************繪圖*******************/
//載入單位化矩陣
glLoadIdentity();
glTranslatef(0?0?distance);//后推distance距離,方便顯示
/*太陽*/
glRotatef(sunEarthFai?0?0?1);//日地軌道傾斜
/*日地軌道線——軌道線要先畫,如果在太陽自轉之后畫,則會讓軌道線也發生旋轉,光照打在旋轉的軌道線上,會發生變色,另外可使用Disable關閉光照對其的效果*/
glDisable(GL_LIGHTING);//軌道線不受光照影響
glBegin(GL_LINE_LOOP);
glColor4f(0.0f?1.0f?1.0f?0.0f);
for?(int?i?=?0;?i? {
glVertex3f(dSunToEarth?*?cos(2?*?pi?*?i?/?n)?0.0f?dSunToEarth?*?sin(2?*?pi?*?i?/?n));//定義頂點
}
glEnd();
glFlush();
glEnable(GL_LIGHTING);//開啟
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-05-12?19:44??作業5-光照太陽系\
?????文件?????2056581??2020-03-24?16:21??作業5-光照太陽系\LightSunEarthMonth.gif
?????文件??????????81??2020-03-24?16:32??作業5-光照太陽系\README.md
?????文件??????491659??2020-03-24?16:34??作業5-光照太陽系\作業5-光照太陽系.pdf
?????目錄???????????0??2020-03-24?16:31??作業5-光照太陽系\作業5-程序\
?????文件????????7018??2020-03-24?16:14??作業5-光照太陽系\作業5-程序\main.cpp
?????文件???????68608??2020-03-24?16:14??作業5-光照太陽系\作業5-程序\MyTask05-LightSunEarthMonth.exe
?????文件????????1473??2020-03-23?15:47??作業5-光照太陽系\作業5-程序\MyTask05-LightSunEarthMonth.sln
?????文件????????6676??2020-03-23?16:02??作業5-光照太陽系\作業5-程序\MyTask05-LightSunEarthMonth.vcxproj
?????文件?????????949??2020-03-23?16:02??作業5-光照太陽系\作業5-程序\MyTask05-LightSunEarthMonth.vcxproj.filters
?????文件?????????168??2020-03-23?15:47??作業5-光照太陽系\作業5-程序\MyTask05-LightSunEarthMonth.vcxproj.user
評論
共有 條評論