資源簡介
一、實驗目的:
掌握幾何變換的原理,尤其是復合變換
二、實驗內容:
1、利用OpenGL函數畫一個三維物體;
2、運用齊次坐標,采用矩陣相乘的方式自己編程實現幾何變換,不能直接調用OpenGL幾何變換函數;
3、利用鼠標或鍵盤控制三維物體在屏幕上移動、旋轉和放縮;
三、實現效果及步驟(或流程)
(1) 以數組wcPt3D verts[8]存儲立方體八個頂點,然后利用glBegin(GL_QUADS); glEnd();函數繪制立方體。在繪制物體之前設置函數glPolygonMode(GL_FRONT_AND_BACK ,GL_LINE ); 讓多邊形以線框形式顯示。
(2) 編寫矩陣初始化方法MatrixInit,將矩陣初始化為單位矩陣。編寫矩陣乘法函數MatrixMultiply,以便求兩個矩陣相乘的結果。方法transformVerts3D把原始立方體頂點坐標轉化為經過復合矩陣變換的三維坐標。
(3) 構造平移矩陣,具體方法實現如下:
void translatePolygon(GLfloat tx, GLfloat ty, GLfloat tz) {//平移
Matrix ml;
MatrixInit(ml);
ml[0][3] = tx;
ml[1][3] = ty;
ml[2][3] = tz;
MatrixMultiply(ml, matComposite);
}
(4) 構造旋轉矩陣,具體方法實現如下:
void RotateLeftRight(wcPt3D pivotPt, GLfloat theta)//繞Z軸旋轉
{
Matrix matRot;
MatrixInit(matRot);
matRot[0][0] = cos(theta);
matRot[0][1] = -sin(theta);
matRot[1][0] = sin(theta);
matRot[1][1] = cos(theta);
MatrixMultiply(matRot, matComposite);
}
void RotateUpDown(wcPt3D pivotPt, GLfloat theta)//繞X軸旋轉
{
Matrix matRot;
MatrixInit(matRot);
matRot[1][1] = cos(theta);
matRot[1][2] = -sin(theta);
matRot[2][1] = sin(theta);
matRot[2][2] = cos(theta);
MatrixMultiply(matRot, matComposite);
}
(5) 構造放縮矩陣,具體方法實現如下:
void Scale3D(GLfloat sx, GLfloat sy, GLfloat sz, wcPt3D fixedPt) //放縮大小
{
Matrix matScale;
MatrixInit(matScale);
matScale[0][0] = sx;
matScale[0][3] = (1 - sx) * fixedPt.x;
matScale[1][1] = sy;
matScale[1][3] = (1 - sy) * fixedPt.y;
matScale[2][2] = sz;
matScale[2][3] = (1 - sz) * fixedPt.z;
MatrixMultiply(matScale, matComposite);
}
(6) 當觸發相應鼠標鍵盤事件時則構建相應的矩陣,通過矩陣變換求得變換后的坐標然后重新繪制立方體。
鍵盤上下左右實現平移:
鍵盤awsd實現旋轉:
鼠標滾輪實現放縮:
代碼片段和文件信息
#include?“stdafx.h“
#include???
#include???
#include?
using?namespace?std;
//獲取屏幕的寬度??
GLint?SCREEN_WIDTH?=?0;
GLint?SCREEN_HEIGHT?=?0;
//設置程序的窗口大小??
GLint?windowWidth?=?600;
GLint?windowHeight?=?600;
//繞x軸旋轉角度??
GLfloat?xRotAngle?=?0.0f;
//繞z軸旋轉角度??
GLfloat?zRotAngle?=?0.0f;
//受支持的點大小范圍??
GLfloat?sizes[2];
//受支持的點大小增量??
GLfloat?step;
int?firstTime?=?1;
#define??GLUT_WHEEL_UP?3???????????//定義滾輪操作??
#define??GLUT_WHEEL_DOWN?4??
void?init(void)
{
glClearColor(0.9?0.4?1.0?0.0);??//?Set?display-window?color?to?white.
glMatrixMode(GL_PROJECTION);???????//?Set?projection?parameters.
gluOrtho2D(0.0?600.0?0.0?600.0);??//設置窗口坐標范圍
}
class?wcPt3D?{
public:
GLfloat?x?y?z;
};
typedef?GLfloat?Matrix[4][4];
Matrix?mat
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
????..A..H.?????26624??2017-04-27?18:32??ex4\.vs\ex4\v14\.suo
?????文件??????51200??2017-04-27?14:45??ex4\Debug\ex4.exe
?????文件?????413756??2017-04-27?14:45??ex4\Debug\ex4.ilk
?????文件?????995328??2017-04-27?14:45??ex4\Debug\ex4.pdb
?????文件?????218624??2015-10-14?09:58??ex4\Debug\freeglut.dll
?????文件?????337408??2015-10-14?09:58??ex4\Debug\glew32.dll
?????文件?????350720??2015-10-14?09:58??ex4\Debug\glewinfo.exe
?????文件??????43520??2015-10-14?09:58??ex4\Debug\glfw3.dll
?????文件?????222720??2015-10-14?09:58??ex4\Debug\visualinfo.exe
?????文件???????2194??2017-04-27?14:45??ex4\ex4\Debug\ex4.log
?????文件??????75534??2017-04-27?14:45??ex4\ex4\Debug\ex4.obj
?????文件????3407872??2017-04-26?15:03??ex4\ex4\Debug\ex4.pch
?????文件???????1750??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\CL.command.1.tlog
?????文件??????27350??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\CL.read.1.tlog
?????文件????????838??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\CL.write.1.tlog
?????文件????????215??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\ex4.lastbuildstate
?????文件???????1852??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\li
?????文件???????3990??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\li
?????文件????????444??2017-04-27?14:45??ex4\ex4\Debug\ex4.tlog\li
?????文件??????12248??2017-04-26?15:03??ex4\ex4\Debug\stdafx.obj
?????文件?????609280??2017-04-27?14:45??ex4\ex4\Debug\vc140.idb
?????文件?????438272??2017-04-27?14:45??ex4\ex4\Debug\vc140.pdb
?????文件??????11648??2017-04-27?15:10??ex4\ex4\ex4.cpp
?????文件???????9363??2017-04-26?15:03??ex4\ex4\ex4.vcxproj
?????文件???????1381??2017-04-26?15:00??ex4\ex4\ex4.vcxproj.filters
?????文件????????225??2017-04-26?15:00??ex4\ex4\packages.config
?????文件???????1482??2017-04-26?14:56??ex4\ex4\ReadMe.txt
?????文件????????207??2017-04-26?14:56??ex4\ex4\stdafx.cpp
?????文件????????234??2017-04-26?14:56??ex4\ex4\stdafx.h
?????文件????????240??2017-04-26?14:56??ex4\ex4\targetver.h
............此處省略71個文件信息
- 上一篇:QQ登錄界面MFC
- 下一篇:掌握利用OpenGL函數進行鼠標、鍵盤操作,創建菜單等
評論
共有 條評論