labwindows三維opengl編程代碼_第1頁
labwindows三維opengl編程代碼_第2頁
labwindows三維opengl編程代碼_第3頁
labwindows三維opengl編程代碼_第4頁
labwindows三維opengl編程代碼_第5頁
已閱讀5頁,還剩1頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、/ File Name: OpenGLwithCVI.c/ Date: 2001-8-20/ Content:在CVI下使用OpenGL繪制三維物體,并作運(yùn)動(dòng)。/ Notes:在項(xiàng)目文件中需要加入兩個(gè)靜態(tài)庫: glu32.lib 和/ opengl32.lib ,它們位于目錄下,/ 還需要添加一個(gè)文件 cviogl.fp ,它位于目錄下。/ 頭文件/#include <windows.h>#include <GLgl.h>#include <GLglu.h>#include <ansi_c.h>#include <cvirte.h>#

2、include <userint.h>#include "cviogl.h"/ 定義默認(rèn)值/#define DFLT_VIEW_LATITUDE 45.0#define DFLT_LIGHT_LATITUDE 45.0#define DFLT_VIEW_LONGITUDE 45.0#define DFLT_VIEWPOINT_X 0.0#define DFLT_VIEWPOINT_Y 0.0#define DFLT_VIEWPOINT_Z 0.0#define DFLT_VIEW_DISTANCE 10.0/ 全局變量/int mainPanel;int pic

3、Ctrl;int quitBtn;int OpenglCtrl;int timerCtrl; unsigned long Time=0; GLUquadricObj *object;/ 偏轉(zhuǎn)角度typedef structdouble RotatX;double RotatY; angle;angle MyAngle;/ 函數(shù)原型/int CVICALLBACK Quit( int panelHandle, int controlID,int event, void *callbackData, int eventData1,int eventData2);int CVICALLBACK ti

4、merPlot (int panel, int control, int event, void *callbackData, int eventData1, int eventData2);int CVICALLBACK OGLCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);void InitOGLControl(void);void DrawImage(int fastFlag);void RenderImage(int fastFlag);/ 主

5、程序入口點(diǎn)/int main (int argc, char *argv)if (InitCVIRTE (0, argv, 0) = 0) return -1; /* out of memory */ 創(chuàng)建面板mainPanel = NewPanel (0, "OpenGL 演示", VAL_AUTO_CENTER, VAL_AUTO_CENTER, 500, 400);/ 添加圖形控件picCtrl = NewCtrl (mainPanel, CTRL_PICTURE, "", 8, 8); SetCtrlAttribute (mainPanel, p

6、icCtrl, ATTR_HEIGHT, 450); SetCtrlAttribute (mainPanel, picCtrl, ATTR_WIDTH, 384);/將圖形控件轉(zhuǎn)換為 Ope nGL控件,并添加處理函數(shù)OpenglCtrl = OGLConvertCtrl (mainPanel, picCtrl); InstallCtrlCallback (mainPanel, OpenglCtrl, OGLCallback, 0);/ 初始化OpenGL控件InitOGLControl();OGLRefreshGraph (mainPanel, OpenglCtrl);/ 添加退出按鈕,并安

7、裝其回調(diào)函數(shù)quitBtn = NewCtrl (mainPanel, CTRL_SQUARE_COMMAND_BUTTON, " 退出", 466, 8); InstallCtrlCallback (mainPanel, quitBtn, Quit, 0);/ 添加定時(shí)器控件,并安裝其回調(diào)函數(shù)timerCtrl = NewCtrl (mainPanel, CTRL_TIMER, "", 466, 8);SetCtrlAttribute (mainPanel, timerCtrl, ATTR_ENABLED, 1);SetCtrlAttribute (m

8、ainPanel, timerCtrl, ATTR_INTERVAL, 0.05);InstallCtrlCallback (mainPanel, timerCtrl, timerPlot, 0);/ 顯示主面板DisplayPanel (mainPanel);/ 進(jìn)入 UI 循環(huán)RunUserInterface ();/ 退出的清理工作OGLDiscardCtrl (mainPanel, OpenglCtrl);DiscardPanel (mainPanel);return 0;/ Quit() :退出函數(shù)/int CVICALLBACK Quit( int panelHandle, int

9、 controlID,int event, void *callbackData, int eventData1,int eventData2)switch (event)case EVENT_COMMIT:QuitUserInterface (0);break;return 0;/InitOGLControl():初始化 OpenGL控件/void InitOGLControl(void)/ 設(shè)置系統(tǒng)光照屬性 OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_LIGHTING_ENABLE, 1);OGLSetCtrlAttribute(mai

10、nPanel,OpenglCtrl,OGLATTR_LIGHT_SELECT, 1);OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_LIGHT_ENABLE, 1);OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_LIGHT_DISTANCE, 2.0);OGLSetCtrlAttribute (mainPanel, OpenglCtrl,OGLATTR_LIGHT_LATITUDE, DFLT_LIGHT_LATITUDE);/ 設(shè)置視點(diǎn)位置(mainPanel,OpenglCtrl,OGL

11、ATTR_PROJECTION_TYPE,OGLSetCtrlAttributeOGLVAL_ORTHOGRAPHIC);OGLSetCtrlAttribute (mainPanel,OpenglCtrl,OGLATTR_ENABLE_PAN_ZOOM_ROTATE, 1);OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_VIEW_DIRECTION,OGLVAL_USER_DEFINED);OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_VIEW_LATITUDE,DFLT_VIEW_LATI

12、TUDE);OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_VIEW_LONGITUDE,DFLT_VIEW_LONGITUDE);OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_VIEW_CENTERX,DFLT_VIEWPOINT_X);OGLSetCtrlAttribute (mainPanel,OpenglCtrl,OGLATTR_VIEW_CENTERY,DFLT_VIEWPOINT_Y); OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATT

13、R_VIEW_CENTERZ,DFLT_VIEWPOINT_Z); OGLSetCtrlAttribute(mainPanel,OpenglCtrl,OGLATTR_VIEW_DISTANCE,DFLT_VIEW_DISTANCE);/ 屏蔽 cviogl.fp 的 3D 繪圖屬性O(shè)GLSetCtrlAttribute (mainPanel, OpenglCtrl, OGLATTR_PLOTTING_ENABLE, OGLVAL_FALSE);OGLSetCtrlAttribute (mainPanel, OpenglCtrl, OGLATTR_ENABLE_PROPERTY_POPUP, O

14、GLVAL_FALSE);/ RenderImage():紋理設(shè)置/void RenderImage(int fastFlag)GLfloat specularLight0 =1.0f, 1.0f, 1.0f, 1.0f; glPushAttrib(GL_ALL_ATTRIB_BITS); glMatrixMode(GL_PROJECTION);glPushMatrix();glMatrixMode(GL_MODELVIEW);glPushMatrix();glEnable(GL_DEPTH_TEST);glShadeModel(GL_SMOOTH);glEnable(GL_COLOR_MAT

15、ERIAL);glEnable(GL_BLEND);glEnable(GL_LINE_SMOOTH); glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specularLight0); glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 128);DrawImage(fastFlag);glPopMatrix();glMatrixMode(GL_PROJECTION);glPopMatrix();g

16、lPopAttrib();glFlush();/ DrawImage():繪圖/void DrawImage(int fastFlag)object = gluNewQuadric();glPushMatrix();glRotatef(MyAngle.RotatX,0.0,0.0,1.0); / 物體旋轉(zhuǎn) glColor3f(0.5f,0.6f,0.7f); / 物體著色 glRotatef(MyAngle.RotatY,0.0,1.0,0.0);gluCylinder(object,1.0,1.0,0.01,30,10); /繪圓柱體glColor3f(0.4f,0.5f,0.6f);glu

17、Disk(object,0.0,1.0,20,20); / 繪圓柱體上下表面 glTranslatef(0.0f,0.0f,0.01f);glColor3f(0.6f,0.7f,0.8f);gluDisk(object,0.0,1.0,20,20);glColor3f(0.9f,0.9f,0.0f);繪球體gluSphere(object,0.3,30,10); / glPopMatrix();gluDeleteQuadric(object);return;/ timerPlot(): 定時(shí)處理函數(shù)/int CVICALLBACK timerPlot (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) switch (event)case EVENT_TIMER_TICK:MyAngle.RotatX = 180/3.14*sin(3.14/720*(Time*50+1024); MyAngle.RotatY = 180/3.14*cos(3.14/720*(Time*50+1024); Time = Time+1;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論