Jhy3DView.cpp
资源名称:Jhy3D.rar [点击查看]
上传用户:eehhbb
上传日期:2022-08-03
资源大小:2550k
文件大小:21k
源码类别:
OpenGL
开发平台:
Visual C++
- // Jhy3DView.cpp : implementation of the CJhy3DView class
- //
- #include "stdafx.h"
- #include "Jhy3D.h"
- #include "glglut.h"
- #include "Jhy3DDoc.h"
- #include "Jhy3DView.h"
- #include "3DPolygon.h"
- #include "DlgLightSet.h"
- #include "DlgTeapotSet.h"
- #include "3DBezierSurface.h"
- #include <stdio.h>//对文件进行操作
- #include "Dlg3dBoxSizeSet.h"
- #include "Dlg3dBoxMaterialSet.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CJhy3DView
- IMPLEMENT_DYNCREATE(CJhy3DView, CView)
- BEGIN_MESSAGE_MAP(CJhy3DView, CView)
- //{{AFX_MSG_MAP(CJhy3DView)
- ON_WM_CREATE()
- ON_WM_SIZE()
- ON_WM_DESTROY()
- ON_COMMAND(ID_TRIANGLE, OnTriangle)
- ON_COMMAND(ID_POLYGON, OnPolygon)
- ON_COMMAND(ID_WirePolygon, OnWirePolygon)
- ON_COMMAND(ID_RENDER_WIRE_MODE, OnRenderWireMode)
- ON_COMMAND(ID_WireTeapot, OnWireTeapot)
- ON_WM_LBUTTONDOWN()
- ON_WM_CANCELMODE()
- ON_WM_MOUSEMOVE()
- ON_WM_CAPTURECHANGED()
- ON_WM_KEYDOWN()
- ON_WM_CHAR()
- ON_COMMAND(ID_RENDER_SOLID_MODE, OnRenderSolidMode)
- ON_COMMAND(ID_Pyramid, OnPyramid)
- ON_COMMAND(ID_LIGHTING_SET, OnLightingSet)
- ON_COMMAND(ID_3DBox, On3DBox)
- ON_COMMAND(ID_RENDER_Texture_Mode_Teapot, OnRender3DTextureModeTeapot)
- ON_COMMAND(ID_RenderLightSolid3dBox, OnRenderLightSolid3dBox)
- ON_COMMAND(ID_DRAW_3D_BEZIERSURFACE, OnDraw3dBeziersurface)
- ON_COMMAND(ID_RENDER_Texture_Mode_3dbox, OnRENDERTextureMode3dbox)
- ON_COMMAND(ID_SPHERE_MAP_MODE_TEAPOT, OnSphereMapModeTeapot)
- ON_COMMAND(ID_SPHERE_MAP_MODE_3dbox, OnSPHEREMAPMODE3dbox)
- ON_COMMAND(ID_DRAW_3D_NURBS_SURFACE, OnDraw3dNurbsSurface)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CJhy3DView construction/destruction
- CJhy3DView::CJhy3DView()
- {
- // TODO: add construction code here
- m_nGraph=0;
- m_n3DRenderMode = 20;
- m_n3dRotate_x = m_n3dRotate_z = 0;
- m_n3dRotate_y = 180;
- }
- CJhy3DView::~CJhy3DView()
- {
- }
- BOOL CJhy3DView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CJhy3DView drawing
- void CJhy3DView::OnDraw(CDC* pDC)
- {
- CJhy3DDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- DrawOGLscene();
- }
- /////////////////////////////////////////////////////////////////////////////
- // CJhy3DView printing
- BOOL CJhy3DView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CJhy3DView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CJhy3DView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CJhy3DView diagnostics
- #ifdef _DEBUG
- void CJhy3DView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CJhy3DView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CJhy3DDoc* CJhy3DView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CJhy3DDoc)));
- return (CJhy3DDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CJhy3DView message handlers
- bool CJhy3DView::InitOpenGL()
- {
- static PIXELFORMATDESCRIPTOR pfd=
- {
- sizeof(PIXELFORMATDESCRIPTOR),//pfd的大小
- 1, //结构版本号
- PFD_DRAW_TO_WINDOW| //支持窗口
- PFD_SUPPORT_OPENGL| //支持OPENGL
- PFD_DOUBLEBUFFER, //双缓存
- PFD_TYPE_RGBA, //RGBA颜色
- 24, //24位颜色深度
- 0, 0, 0, 0, 0, 0, //color bits ignored
- 0, //no alpha buffer
- 0, //shift bit ignored
- 0, //不使用累积缓存
- 0, 0, 0, 0, //accum bits ignored
- 32, //32位z缓存
- 0, //不使用模板缓存
- 0, //no auxiliary buffer
- PFD_MAIN_PLANE, //选择主层面
- 0, // 保留
- 0, 0, 0
- };
- //得到客户设备上下文
- m_pDC = new CClientDC(this);
- if(m_pDC == NULL)
- {
- MessageBox("获得设备上下文失败!");
- return false;
- }
- int pixelFormat = ::ChoosePixelFormat(m_pDC -> GetSafeHdc(), &pfd);
- BOOL success = ::SetPixelFormat(m_pDC -> GetSafeHdc(), pixelFormat, &pfd);
- if(!success)
- return FALSE;
- //创建绘图上下文RC
- m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
- if(m_hRC == 0)
- {
- MessageBox("绘图上下文RC创建失败!");
- return FALSE;
- }
- if(::wglMakeCurrent(m_pDC -> GetSafeHdc(), m_hRC) == FALSE)
- {
- MessageBox("关联当前RC失败!");
- return FALSE;
- }
- glShadeModel(GL_SMOOTH); //启用广顺渲染
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//黑色背景
- return TRUE;
- }
- int CJhy3DView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: Add your specialized creation code here
- static PIXELFORMATDESCRIPTOR pfd=
- {
- sizeof(PIXELFORMATDESCRIPTOR),//pfd的大小
- 1, //结构版本号
- PFD_DRAW_TO_WINDOW| //支持窗口
- PFD_SUPPORT_OPENGL| //支持OPENGL
- PFD_DOUBLEBUFFER, //双缓存
- PFD_TYPE_RGBA, //RGBA颜色
- 24, //24位颜色深度
- 0, 0, 0, 0, 0, 0, //color bits ignored
- 0, //no alpha buffer
- 0, //shift bit ignored
- 0, //不使用累积缓存
- 0, 0, 0, 0, //accum bits ignored
- 32, //32位z缓存
- 0, //不使用模板缓存
- 0, //no auxiliary buffer
- PFD_MAIN_PLANE, //选择主层面
- 0, // 保留
- 0, 0, 0
- };
- //得到客户设备上下文
- m_pDC = new CClientDC(this);
- if(m_pDC == NULL)
- {
- MessageBox("获得设备上下文失败!");
- return false;
- }
- int pixelFormat = ::ChoosePixelFormat(m_pDC -> GetSafeHdc(), &pfd);
- BOOL success = ::SetPixelFormat(m_pDC -> GetSafeHdc(), pixelFormat, &pfd);
- if(!success)
- return FALSE;
- //创建绘图上下文RC
- m_hRC = ::wglCreateContext(m_pDC -> GetSafeHdc());
- if(m_hRC == 0)
- {
- MessageBox("绘图上下文RC创建失败!");
- return FALSE;
- }
- if(::wglMakeCurrent(m_pDC -> GetSafeHdc(), m_hRC) == FALSE)
- {
- MessageBox("关联当前RC失败!");
- return FALSE;
- }
- glShadeModel(GL_SMOOTH); //启用广顺渲染
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//黑色背景
- return TRUE;
- return 0;
- }
- void CJhy3DView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
- // TODO: Add your message handler code here
- if(0 >= cx||0 >= cy) return;
- //select the full client area
- //ModelView变换机制
- // gluPerspective(45.0f,2.0,1.0f,1000.0f);
- ::glViewport(0, 0, cx, cy);
- }
- void CJhy3DView::OnDestroy()
- {
- CView::OnDestroy();
- // TODO: Add your message handler code here
- ::wglMakeCurrent(NULL, NULL);
- ::wglDeleteContext(m_hRC);
- if(m_pDC)
- {
- delete m_pDC;
- m_pDC = NULL;
- }
- }
- void CJhy3DView::DrawOGLscene()
- {
- glClearColor(0.4f,0.3f,0.4f,0.5f);
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- gluLookAt(0,0,2,0,0,0,0,1,0);
- glLoadIdentity();
- glPushMatrix();
- glRotatef(m_n3dRotate_y,0.0,1.0,0.0);
- glRotatef(m_n3dRotate_x,1.0,0.0,0.0);
- glRotatef(m_n3dRotate_z,0.0,0.0,1.0);
- //在此处添加场景绘制代码如下
- if(m_nGraph==1)
- {
- //glShadeModel (GL_FLAT);
- glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
- glBegin(GL_TRIANGLES);//每三个顶点组成一个三角形
- glColor3f(1.0, 0.0, 0.0);//设置顶点颜色
- glVertex3f(-1.0, 0.0, 0.0);
- //顶点坐标
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(0.0, 1.0, 0.0);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(1.0, 0.0, 0.0);
- glEnd();
- }
- if(m_nGraph==2)
- {
- DrawPolygon();
- }
- if(m_nGraph==3)
- {
- //glShadeModel (GL_FLAT);
- m_3dpolygon.DrawWirePolygonwithOpenGL();
- }
- if(m_nGraph==4)
- {
- m_3dteapot.DrawWireTeapot();
- }
- if(m_nGraph ==5)
- {
- DrawPyramid();
- }
- if(m_nGraph ==6)
- {
- m_3dbox.Draw3dBox();
- }
- if(m_nGraph ==7)
- {
- if(m_n3DRenderMode == 1)
- {
- m_3dteapot.RenderLightSolidTeapot();
- m_3dbezierSuf.RenderTextureSolidBeizerSurface();
- }
- else if (m_n3DRenderMode == 0)
- {
- m_3dteapot.RenderWireTeapot();
- m_3dnurbsSuf.RenderWireNurbsSurface();
- }
- else if (m_n3DRenderMode == 2)
- {
- m_3dbox.RenderLightSolid3dBox();
- }
- else if (m_n3DRenderMode == 3)//纹理映射茶壶
- {
- m_3dteapot.RenderLightSolidTeapot();
- }
- else if (m_n3DRenderMode == 4)//纹理映射长方体
- {
- m_3dbox.Draw3dBox();
- }
- else if (m_n3DRenderMode == 5)//环境映射茶壶
- {
- glEnable(GL_TEXTURE_GEN_S);
- glEnable(GL_TEXTURE_GEN_T);
- m_3dteapot.RenderLightSolidTeapot();
- }
- else if (m_n3DRenderMode == 6)//环境映射长方体
- {
- glEnable(GL_TEXTURE_GEN_S);
- glEnable(GL_TEXTURE_GEN_T);
- m_3dbox.Draw3dBox();
- }
- }
- glPopMatrix();
- glFinish();
- SwapBuffers(m_pDC -> GetSafeHdc());
- }
- void CJhy3DView::OnTriangle() //菜单响应
- {
- // TODO: Add your command handler code here
- m_nGraph=1;
- Invalidate();
- }
- void CJhy3DView::OnPolygon() //菜单响应
- {
- // TODO: Add your command handler code here
- m_nGraph=2;
- Invalidate();
- }
- //画多边形
- void CJhy3DView::DrawPolygon()
- {
- glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
- glDisable(GL_TEXTURE_2D);
- glBegin(GL_POLYGON);
- glColor3f(0.8,0.0f,0.0f);
- glVertex3f(-1.0,0.0,0.0);
- glColor3f(0.3, 1.0, 0.0);
- glVertex3f(0.0,1.0,0.0);
- glColor3f(0.0, 0.2, 0.2);
- glVertex3f(1.0,0.0,0.0);
- glColor3f(0.0, 0.5, 0.5);
- glVertex3f(0.1,-0.7,0.8);
- // glColor3f(0.2, 0.3, 0.5);
- // glVertex3f(0.1,-0.3,0.5);
- glEnd();
- }
- void CJhy3DView::OnWirePolygon()
- {
- // TODO: Add your command handler code here
- m_nGraph=3;
- Invalidate();
- }
- void CJhy3DView::OnRenderWireMode()
- {
- // TODO: Add your command handler code here
- m_nGraph=7;
- m_n3DRenderMode = 0;
- CloseLight();
- glDisable(GL_TEXTURE_2D);
- Invalidate(FALSE);
- }
- void CJhy3DView::OnWireTeapot()
- {
- // TODO: Add your command handler code here
- CDlgTeapotSet theDlg;
- if(theDlg.DoModal() == IDOK)
- {
- m_3dteapot.m_matTeapot.m_vAmbient[0] = atof(theDlg.m_dlg_Ambient0);
- m_3dteapot.m_matTeapot.m_vAmbient[1] = atof(theDlg.m_dlg_Ambient1);
- m_3dteapot.m_matTeapot.m_vAmbient[2] = atof(theDlg.m_dlg_Ambient2);
- m_3dteapot.m_matTeapot.m_vAmbient[3] = 1.0f;
- m_3dteapot.m_matTeapot.m_vDiffuse[0] = atof(theDlg.m_dlg_Diffuse0);
- m_3dteapot.m_matTeapot.m_vDiffuse[1] = atof(theDlg.m_dlg_Diffuse1);
- m_3dteapot.m_matTeapot.m_vDiffuse[2] = atof(theDlg.m_dlg_Diffuse2);
- m_3dteapot.m_matTeapot.m_vDiffuse[3] = 1.0f;
- m_3dteapot.m_matTeapot.m_vSpecular[0] = atof(theDlg.m_dlg_Specular0);
- m_3dteapot.m_matTeapot.m_vSpecular[1] = atof(theDlg.m_dlg_Specular1);
- m_3dteapot.m_matTeapot.m_vSpecular[2] = atof(theDlg.m_dlg_Specular2);
- m_3dteapot.m_matTeapot.m_vSpecular[3] = 1.0f;
- m_3dteapot.m_matTeapot.m_vEmission[0] = atof(theDlg.m_dlg_Emission0);
- m_3dteapot.m_matTeapot.m_vEmission[1] = atof(theDlg.m_dlg_Emission1);
- m_3dteapot.m_matTeapot.m_vEmission[2] = atof(theDlg.m_dlg_Emission2);
- m_3dteapot.m_matTeapot.m_vEmission[3] = 1.0f;
- m_3dteapot.m_matTeapot.m_vShininess = atof(theDlg.m_dlg_Shininess);
- m_3dteapot.m_position.m_x = theDlg.m_teapot_tx;
- m_3dteapot.m_position.m_y = theDlg.m_teapot_ty;
- m_3dteapot.m_position.m_z = theDlg.m_teapot_tz;
- m_3dteapot.m_fRoundRadius = theDlg.m_teapot_radius;
- }
- m_nGraph=4;
- Invalidate();
- }
- void CJhy3DView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- m_TempMovePos = point;
- CView::OnLButtonDown(nFlags, point);
- }
- void CJhy3DView::OnCancelMode()
- {
- CView::OnCancelMode();
- // TODO: Add your message handler code here
- }
- void CJhy3DView::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- if(nFlags == MK_LBUTTON)
- {
- m_n3dRotate_y+=point.x - m_TempMovePos.x;
- m_n3dRotate_x+=m_TempMovePos.y - point.y;
- m_TempMovePos = point;
- Invalidate(FALSE);
- }
- CView::OnMouseMove(nFlags, point);
- }
- void CJhy3DView::OnCaptureChanged(CWnd *pWnd)
- {
- // TODO: Add your message handler code here
- CView::OnCaptureChanged(pWnd);
- }
- void CJhy3DView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- switch(nChar)
- {
- case 'z':
- case 'Z':
- m_n3dRotate_z+=5.0f;
- Invalidate(FALSE);
- break;
- case 'c':
- case 'C':
- m_n3dRotate_z-=5.0f;
- Invalidate(FALSE);
- }
- CView::OnKeyDown(nChar, nRepCnt, nFlags);
- }
- void CJhy3DView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
- {
- // TODO: Add your message handler code here and/or call default
- CView::OnChar(nChar, nRepCnt, nFlags);
- }
- void CJhy3DView::OnRenderSolidMode()
- {
- // TODO: Add your command handler code here
- m_nGraph=7;
- m_n3DRenderMode = 1;
- OpenLight();
- glDisable(GL_TEXTURE_2D);
- Invalidate(FALSE);
- }
- void CJhy3DView::OpenLight()
- {
- glLightfv(GL_LIGHT0,GL_AMBIENT,m_light.m_vAmbient);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,m_light.m_vDiffuse);
- glLightfv(GL_LIGHT0,GL_SPECULAR,m_light.m_vSpecular);
- glLightfv(GL_LIGHT0,GL_POSITION,m_light.m_vPosition);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- }
- void CJhy3DView::CloseLight()
- {
- glDisable(GL_DEPTH_TEST);
- glDisable(GL_LIGHTING);
- glDisable(GL_LIGHT0);
- }
- void CJhy3DView::OnPyramid()
- {
- // TODO: Add your command handler code here
- m_nGraph=5;
- Invalidate();
- }
- void CJhy3DView::DrawPyramid()
- {
- // glTranslatef(0.0f,0.0f,0.0f);
- glBegin(GL_TRIANGLES);
- glColor3f(0.5f,0.0f,0.0f); // 红色
- glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (前侧面)
- glColor3f(0.0f,0.5f,0.0f); // 绿色
- glVertex3f(-0.5f,-0.5f, 0.5f); // 三角形的左下顶点 (前侧面)
- glColor3f(0.0f,0.0f,0.5f); // 蓝色
- glVertex3f( 0.5f,-0.5f, 0.5f); // 三角形的右下顶点 (前侧面)
- glColor3f(0.5f,0.0f,0.0f); // 红色
- glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (右侧面)
- glColor3f(0.0f,0.0f,0.5f); // 蓝色
- glVertex3f( 0.5f,-0.5f, 0.5f); // 三角形的左下顶点 (右侧面)
- glColor3f(0.0f,0.5f,0.0f); // 绿色
- glVertex3f( 0.5f,-0.5f, -0.5f); // 三角形的右下顶点 (右侧面)
- glColor3f(0.5f,0.0f,0.0f); // 红色
- glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (后侧面)
- glColor3f(0.0f,0.5f,0.0f); // 绿色
- glVertex3f( 0.5f,-0.5f, -0.5f); // 三角形的左下顶点 (后侧面)
- glColor3f(0.0f,0.0f,0.5f); // 蓝色
- glVertex3f(-0.5f,-0.5f, -0.5f); // 三角形的右下顶点 (后侧面)
- glColor3f(0.5f,0.0f,0.0f); // 红色
- glVertex3f( 0.0f, 0.5f, 0.0f); // 三角形的上顶点 (左侧面)
- glColor3f(0.0f,0.0f,0.5f);// 蓝色
- glVertex3f(-0.5f,-0.5f,-0.5f); // 三角形的左下顶点 (左侧面)
- glColor3f(0.0f,0.5f,0.0f); // 绿色
- glVertex3f(-0.5f,-0.5f, 0.5f); // 三角形的右下顶点 (左侧面)
- glEnd(); //金字塔绘制结束
- }
- void CJhy3DView::OnLightingSet()
- {
- // TODO: Add your command handler code here
- CDlgLightSet dlg;
- if (dlg.DoModal() == IDOK)
- {
- m_light.m_vAmbient[0] = atof(dlg.m_dlg_Ambient0);
- m_light.m_vAmbient[1] = atof(dlg.m_dlg_Ambient1);
- m_light.m_vAmbient[2] = atof(dlg.m_dlg_Ambient2);
- m_light.m_vAmbient[3] = 1.0f;
- m_light.m_vDiffuse[0] = atof(dlg.m_dlg_Diffuse0);
- m_light.m_vDiffuse[1] = atof(dlg.m_dlg_Diffuse1);
- m_light.m_vDiffuse[2] = atof(dlg.m_dlg_Diffuse2);
- m_light.m_vDiffuse[3] = 1.0f;
- m_light.m_vSpecular[0] = atof(dlg.m_dlg_Specular0);
- m_light.m_vSpecular[1] = atof(dlg.m_dlg_Specular1);
- m_light.m_vSpecular[2] = atof(dlg.m_dlg_Specular2);
- m_light.m_vSpecular[3] = 1.0f;
- m_light.m_vPosition[0] = atof(dlg.m_dlg_Position0);
- m_light.m_vPosition[1] = atof(dlg.m_dlg_Position1);
- m_light.m_vPosition[2] = atof(dlg.m_dlg_Position2);
- m_light.m_vPosition[3] = 1.0f;
- }
- }
- void CJhy3DView::On3DBox()
- {
- CDlg3dBoxSizeSet dlg;
- if (dlg.DoModal() == IDOK)
- {
- m_3dbox.m_vBox[0].m_x = dlg.m_3dboxx0;
- m_3dbox.m_vBox[0].m_y = dlg.m_3dboxy0;
- m_3dbox.m_vBox[0].m_z = dlg.m_3dboxz0;
- m_3dbox.m_vBox[1].m_x = dlg.m_3dboxx1;
- m_3dbox.m_vBox[1].m_y = dlg.m_3dboxy1;
- m_3dbox.m_vBox[1].m_z = dlg.m_3dboxz1;
- }
- m_nGraph=6;
- Invalidate();
- }
- void CJhy3DView::OnRenderLightSolid3dBox()
- {
- CDlg3dBoxMaterialSet theDlg;
- if(theDlg.DoModal() == IDOK)
- {
- m_3dbox.m_matBox.m_vAmbient[0] = atof(theDlg.m_dlg_ambient0);
- m_3dbox.m_matBox.m_vAmbient[1] = atof(theDlg.m_dlg_ambient1);
- m_3dbox.m_matBox.m_vAmbient[2] = atof(theDlg.m_dlg_ambient2);
- m_3dbox.m_matBox.m_vAmbient[3] = 1.0f;
- m_3dbox.m_matBox.m_vDiffuse[0] = atof(theDlg.m_dlg_diffuse0);
- m_3dbox.m_matBox.m_vDiffuse[1] = atof(theDlg.m_dlg_diffuse1);
- m_3dbox.m_matBox.m_vDiffuse[2] = atof(theDlg.m_dlg_diffuse2);
- m_3dbox.m_matBox.m_vDiffuse[3] = 1.0f;
- m_3dbox.m_matBox.m_vSpecular[0] = atof(theDlg.m_dlg_specular0);
- m_3dbox.m_matBox.m_vSpecular[1] = atof(theDlg.m_dlg_specular1);
- m_3dbox.m_matBox.m_vSpecular[2] = atof(theDlg.m_dlg_specular2);
- m_3dbox.m_matBox.m_vSpecular[3] = 1.0f;
- m_3dbox.m_matBox.m_vEmission[0] = atof(theDlg.m_dlg_emission0);
- m_3dbox.m_matBox.m_vEmission[1] = atof(theDlg.m_dlg_emission1);
- m_3dbox.m_matBox.m_vEmission[2] = atof(theDlg.m_dlg_emission2);
- m_3dbox.m_matBox.m_vEmission[3] = 1.0f;
- m_3dbox.m_matBox.m_vShininess = atof(theDlg.m_dlg_shininess);
- }
- m_nGraph=7;
- m_n3DRenderMode = 2;
- OpenLight();
- glDisable(GL_TEXTURE_2D);
- Invalidate(FALSE);
- }
- AUX_RGBImageRec *CJhy3DView::LoadBMP(char *filename) // 载入位图图象
- {
- FILE *file = NULL;
- if((file = fopen(filename,"r")) != NULL)
- {
- fclose(file);
- return auxDIBImageLoad(filename);
- }
- return NULL;
- }
- int CJhy3DView::LoadGLTextures()
- {
- int status = FALSE;//Status 的变量用来跟踪是否能够载入位图以及能否创建纹理。 Status 缺省设为 FALSE (表示没有载入或创建任何东东)。
- AUX_RGBImageRec *TextureImage[1]; // 创建纹理的存储空间
- memset(TextureImage,0,sizeof(void*)*1);// 将指针设为 NULL
- if(TextureImage[0] = LoadBMP("test.bmp"))
- {
- status = TRUE;
- glGenTextures(1,&m_texture[0]);//生成一个纹理名字
- glBindTexture(GL_TEXTURE_2D,m_texture[0]);//将纹理名字 texture[0] 绑定到纹理目标上...指定内存
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);//滤波方式
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);//滤波方式
- glTexImage2D(GL_TEXTURE_2D,0,3,TextureImage[0]->sizeX,TextureImage[0]->sizeY,0,GL_RGB,GL_UNSIGNED_BYTE,TextureImage[0]->data);
- }
- if(TextureImage[0] && TextureImage[0]->data)// 纹理是否存在 // 纹理图像是否存在
- {
- free(TextureImage[0]->data);// 释放纹理图像占用的内存
- free(TextureImage[0]); // 释放图像结构
- }
- return status;
- }
- void CJhy3DView::OnRender3DTextureModeTeapot()
- {
- m_nGraph=7;
- m_n3DRenderMode = 3;
- if(!glIsTexture(m_texture[0]))
- LoadGLTextures();
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- Invalidate(FALSE);
- }
- void CJhy3DView::OnRENDERTextureMode3dbox()
- {
- m_nGraph=7;
- m_n3DRenderMode = 4;
- AfxMessageBox("请设置长方体的大小:绘图->长方体");
- if(!glIsTexture(m_texture[0]))
- LoadGLTextures();
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- Invalidate(FALSE);
- }
- //茶壶环境映射
- void CJhy3DView::OnSphereMapModeTeapot()
- {
- m_nGraph=7;
- m_n3DRenderMode = 5;
- glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For S To Sphere Mapping (NEW)
- glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For T To Sphere Mapping (NEW)
- if(!glIsTexture(m_texture[0]))
- LoadGLTextures();
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- }
- //长方体环境映射
- void CJhy3DView::OnSPHEREMAPMODE3dbox()
- {
- m_nGraph=7;
- m_n3DRenderMode = 6;
- AfxMessageBox("请设置长方体的大小:绘图->长方体");
- glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For S To Sphere Mapping (NEW)
- glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); // Set The Texture Generation Mode For T To Sphere Mapping (NEW)
- if(!glIsTexture(m_texture[0]))
- LoadGLTextures();
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_TEXTURE_2D);
- }
- void CJhy3DView::OnDraw3dBeziersurface()
- {
- // m_n3DRenderMode = 7;
- m_3dbezierSuf.DefaultBezierSuf();
- Invalidate();
- }
- void CJhy3DView::OnDraw3dNurbsSurface()
- {
- m_3dnurbsSuf.DefaultNurbsSuf();
- Invalidate();
- }