opengl初探部分代码.doc_第1页
opengl初探部分代码.doc_第2页
opengl初探部分代码.doc_第3页
opengl初探部分代码.doc_第4页
opengl初探部分代码.doc_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

将Onsize代码替换成如下的代码,使前述的平行投影变成透视投影void CMyOpenGLView:OnSize(UINT nType, int cx, int cy)glViewport(0,0,cx,cy);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluPerspective(45,(float)cx/(float)cy,0.1,15);glMatrixMode(GL_MODELVIEW);glLoadIdentity();以下是修改Draw方法的代码,以实现不同的功能/2D图形绘制glClearColor(0,0,0,1);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glShadeModel(GL_SMOOTH);glTranslatef(-1.5f,1.0f,-7.0f);glColor3f(1.0f,0.0f,0.0f);glBegin(GL_TRIANGLES);glVertex3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,0.0f);glVertex3f(1.0f,-1.0f,0.0f);glEnd();glTranslatef(3.0f,0.0f,0.0f);glColor3f(0.0f,0.0f,1.0f);glBegin(GL_QUADS);glVertex3f(-1.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,0.0f);glVertex3f(1.0f,-1.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glEnd();glTranslatef(-1.5f,-2.5f,0.0f);glColor3f(1.0f,1.0f,0.0f);glLineWidth(2.0f);glBegin(GL_LINE_LOOP);for(int i=0;i200;i+)glVertex3f(1.0*float(sin(2*3.14*i/200),1.0*float(cos(2*3.14*i/200),0.0);glEnd();SwapBuffers(m_hDC);glFlush();/阴暗处理glClearColor(0,0,0,1);glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glLoadIdentity();glShadeModel(GL_SMOOTH);/设置平滑插值glTranslatef(-1.5f,1.0f,-7.0f);glColor3f(1.0f,0.0f,0.0f);glBegin(GL_TRIANGLES);glColor3ub(255,0,0);glVertex3f(0.0f,1.0f,0.0f);glColor3ub(0,255,0);glVertex3f(-1.0f,-1.0f,0.0f);glColor3ub(255,255,0);glVertex3f(1.0f,-1.0f,0.0f);glEnd();glTranslatef(3.0f,0.0f,0.0f);glColor3ub(0,255,255);glBegin(GL_QUADS);glVertex3f(-1.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,0.0f);glVertex3f(1.0f,-1.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glEnd();glTranslatef(-1.5f,-2.5f,0.0f);glColor3f(1.0f,1.0f,0.0f);glLineWidth(2.0f);glBegin(GL_LINE_LOOP);for(int i=0;i360.0)angle_Left-=360.0;glBegin(GL_TRIANGLES);/正面 glColor3ub(217,167,22);glVertex3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,1.0f);glVertex3f(1.0f,-1.0f,1.0f);/背面glColor3ub(200,100,22);glVertex3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,-1.0f);glVertex3f(-1.0f,-1.0f,-1.0f);/右侧面glColor3ub(217,122,0);glVertex3f(0.0f,1.0f,0.0f);glVertex3f(1.0f,-1.0f,1.0f);glVertex3f(1.0f,-1.0f,-1.0f);/左侧面glVertex3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f,-1.0f);glVertex3f(-1.0f,-1.0f,1.0f);glEnd();/底面glColor3ub(217,122,100);glBegin(GL_QUADS);glVertex3f(-1.0f,-1.0f,-1.0f);glVertex3f(1.0f,-1.0f,-1.0f);glVertex3f(1.0f,-1.0f,1.0f);glVertex3f(-1.0f,-1.0f,1.0f);glEnd();glPopMatrix();/矩阵出栈glTranslatef(4.0f,0.0f,0.0f);glRotatef(angle_X,1.0f,0.0f,0.0f);glRotatef(angle_Y,0.0f,1.0f,0.0f);DrawCube(2.0f,0.2f,0.2f);DrawCube(0.2f,2.0f,0.2f);SwapBuffers(m_hDC);glFlush();除此之外,我们添加一个Timer函数为消息中WM_TIMER添加响应OnTimer方法,代码如下void CMyOpenGLView:OnTimer(UINT nIDEvent) / TODO: Add your message handler code here and/or call defaultDraw();CView:OnTimer(nIDEvent);则SetTimer(1,100,NULL)是触发Timer消息,而KillTimer(1);是使得响应消亡。为实现能在键盘控制下旋转上述的3D图形为消息WM_KEYUP添加响应OnKeyUpvoid CMyOpenGLView:OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) / TODO: Add your message handler code here and/or call defaultif (nChar = VK_F2)KillTimer(1);if (nChar = VK_F1)SetTimer(1,100,NULL);if (nC

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论