自己写的古剑奇谭3D宣传画(哈哈)

    技术2025-08-18  16

    先上图:

     

     

    也是简单程序,就是加了个背景,然后六个主角的图片作为纹理贴图,覆盖正方体的表面。

     

    貌似旋转起来的时候有部分图案不是很清楚,不过无伤大雅,无伤大雅~~(嘿嘿)

     

     

    用MFC写的,所以贴上view类的代码;

     

    头文件:

    // posterView.h : interface of the CposterView class // #pragma once class CposterView : public CView { protected: // create from serialization only CposterView(); DECLARE_DYNCREATE(CposterView) // Attributes public: CposterDoc* GetDocument() const; // Operations public: // Overrides public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: // Implementation public: virtual ~CposterView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: // Generated message map functions protected: afx_msg void OnFilePrintPreview(); afx_msg void OnRButtonUp(UINT nFlags, CPoint point); afx_msg void OnContextMenu(CWnd* pWnd, CPoint point); DECLARE_MESSAGE_MAP() public: HGLRC m_hRC; CDC* m_pDC; GLuint texture[10]; BOOL InitializeOpenGL(void); BOOL SetupPixelFormat(void); void RenderScene(void); afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnDestroy(); afx_msg BOOL OnEraseBkgnd(CDC* pDC); afx_msg void OnSize(UINT nType, int cx, int cy); BOOL LoadTexture(void); afx_msg void OnTimer(UINT_PTR nIDEvent); int spinX; int spinY; int spinZ; }; #ifndef _DEBUG // debug version in posterView.cpp inline CposterDoc* CposterView::GetDocument() const { return reinterpret_cast<CposterDoc*>(m_pDocument); } #endif  

     

     

    view类的cpp文件

     

     

    // posterView.cpp : implementation of the CposterView class // #include "stdafx.h" // SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail // and search filter handlers and allows sharing of document code with that project. #ifndef SHARED_HANDLERS #include "poster.h" #endif #include "posterDoc.h" #include "posterView.h" #ifdef _DEBUG #define new DEBUG_NEW #endif // CposterView IMPLEMENT_DYNCREATE(CposterView, CView) BEGIN_MESSAGE_MAP(CposterView, CView) ON_WM_CONTEXTMENU() ON_WM_RBUTTONUP() ON_WM_CREATE() ON_WM_DESTROY() ON_WM_ERASEBKGND() ON_WM_SIZE() ON_WM_TIMER() END_MESSAGE_MAP() // CposterView construction/destruction CposterView::CposterView() { // TODO: add construction code here spinX = 0; spinY = 0; spinZ = 0; } CposterView::~CposterView() { } BOOL CposterView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN; return CView::PreCreateWindow(cs); } // CposterView drawing void CposterView::OnDraw(CDC* /*pDC*/) { CposterDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: add draw code for native data here glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); RenderScene(); glFlush(); } void CposterView::OnRButtonUp(UINT /* nFlags */, CPoint point) { ClientToScreen(&point); OnContextMenu(this, point); } void CposterView::OnContextMenu(CWnd* /* pWnd */, CPoint point) { #ifndef SHARED_HANDLERS theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE); #endif } // CposterView diagnostics #ifdef _DEBUG void CposterView::AssertValid() const { CView::AssertValid(); } void CposterView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CposterDoc* CposterView::GetDocument() const // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CposterDoc))); return (CposterDoc*)m_pDocument; } #endif //_DEBUG // CposterView message handlers BOOL CposterView::InitializeOpenGL(void) { m_pDC = new CClientDC(this); if(m_pDC==NULL) { MessageBox(_T("Error Obtaining DC")); return FALSE; } if(!SetupPixelFormat()) { return FALSE; } m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc()); if(m_hRC == 0) { MessageBox(_T("Error Obtaining RC")); return FALSE; } if(::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC)==FALSE) { MessageBox(_T("Error Making RC Current")); return FALSE; } ::glClearColor(1.0,1.0,1.0,0.0); ::glClearDepth(1.0); ::glEnable(GL_DEPTH_TEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return TRUE; } BOOL CposterView::SetupPixelFormat(void) { static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), //size of this pfd 1, //version number PFD_DRAW_TO_WINDOW| //support window PFD_SUPPORT_OPENGL| //support OpenGL PFD_TYPE_RGBA, //RGBA type 24, //24-bit color depth 0,0,0,0,0,0, //color bits ignored 0, //no alpha buffer 0, //shift bit ignored 0, //no accumulation buffer 0,0,0,0, //accum bits ignored 16, //16-bit z-buffer 0, //no stencil buffer 0, //no auxiliary buffer PFD_MAIN_PLANE, //main layer 0, //reserved 0,0,0 //layer masks ignored }; int m_nPixelFormat = ::ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd); if(m_nPixelFormat== 0) { return FALSE; } if(::SetPixelFormat(m_pDC->GetSafeHdc(),m_nPixelFormat,&pfd)==FALSE) { return FALSE; } return TRUE; } void CposterView::RenderScene(void) { glEnable(GL_TEXTURE_2D); glViewport(0,0,840,600); glColor3f(1.0,1.0,1.0); glBindTexture(GL_TEXTURE_2D,texture[0]); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(-20.0,-20.0,-15.0); glTexCoord2f(1.0,0.0); glVertex3f(20.0,-20.0,-15.0); glTexCoord2f(1.0,1.0); glVertex3f(20.0,20.0,-15.0); glTexCoord2f(0.0,1.0); glVertex3f(-20.0,20.0,-15.0); glEnd(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(spinY,0.0,1.0,0.0); glRotatef(spinX,1.0,0.0,0.0); glRotatef(spinZ,0.0,0.0,1.0); // glTexCoord2f(0.0,0.0); // glTexCoord2f(0.0,1.0); // glTexCoord2f(1.0,1.0); // glTexCoord2f(1.0,0.0); glBindTexture(GL_TEXTURE_2D,texture[1]); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(-10.0,-10.0,10.0); glTexCoord2f(0.0,1.0); glVertex3f(-10.0,10.0,10.0); glTexCoord2f(1.0,1.0); glVertex3f(10.0,10.0,10.0); glTexCoord2f(1.0,0.0); glVertex3f(10.0,-10.0,10.0); glEnd(); glBindTexture(GL_TEXTURE_2D,texture[2]); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(10.0,-10.0,10.0); glTexCoord2f(0.0,1.0); glVertex3f(10.0,10.0,10.0); glTexCoord2f(1.0,1.0); glVertex3f(10.0,10.0,-10.0); glTexCoord2f(1.0,0.0); glVertex3f(10.0,-10.0,-10.0); glEnd(); glBindTexture(GL_TEXTURE_2D,texture[3]); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(10.0,-10.0,-10.0); glTexCoord2f(0.0,1.0); glVertex3f(10.0,10.0,-10.0); glTexCoord2f(1.0,1.0); glVertex3f(-10.0,10.0,-10.0); glTexCoord2f(1.0,0.0); glVertex3f(-10.0,-10.0,-10.0); glEnd(); glBindTexture(GL_TEXTURE_2D,texture[4]); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(-10.0,-10.0,-10.0); glTexCoord2f(0.0,1.0); glVertex3f(-10.0,10.0,-10.0); glTexCoord2f(1.0,1.0); glVertex3f(-10.0,10.0,10.0); glTexCoord2f(1.0,0.0); glVertex3f(-10.0,-10.0,10.0); glEnd(); glBindTexture(GL_TEXTURE_2D,texture[5]); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(-10.0,10.0,10.0); glTexCoord2f(0.0,1.0); glVertex3f(-10.0,10.0,-10.0); glTexCoord2f(1.0,1.0); glVertex3f(10.0,10.0,-10.0); glTexCoord2f(1.0,0.0); glVertex3f(10.0,10.0,10.0); glEnd(); glBindTexture(GL_TEXTURE_2D,texture[6]); glBegin(GL_QUADS); glTexCoord2f(0.0,0.0); glVertex3f(-10.0,-10.0,-10.0); glTexCoord2f(0.0,1.0); glVertex3f(10.0,-10.0,-10.0); glTexCoord2f(1.0,1.0); glVertex3f(10.0,-10.0,10.0); glTexCoord2f(1.0,0.0); glVertex3f(-10.0,-10.0,10.0); glEnd(); } int CposterView::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here InitializeOpenGL(); LoadTexture(); SetTimer(1,500,NULL); return 0; } void CposterView::OnDestroy() { CView::OnDestroy(); // TODO: Add your message handler code here CView::OnDestroy(); // TODO: Add your message handler code here if(::wglMakeCurrent(NULL,NULL)==FALSE){ MessageBox(_T("could not make RC non-current")); } if(::wglDeleteContext(m_hRC)==FALSE) { MessageBox(_T("could not delete RC")); } if(m_pDC) { delete m_pDC; } m_pDC = NULL; KillTimer(1); } BOOL CposterView::OnEraseBkgnd(CDC* pDC) { // TODO: Add your message handler code here and/or call default return TRUE; } void CposterView::OnSize(UINT nType, int cx, int cy) { CView::OnSize(nType, cx, cy); // TODO: Add your message handler code here glViewport(0,0,cx,cy); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-20.0,20.0,-20.0,20.0,1.0,25.0); //gluPerspective(100.0f,(GLfloat)cx/(GLfloat)cy,2.0f,100.0f); //glOrtho(-1.0,1.0,-1.0,1.0,1.5,-1.1); //gluPerspective(91.0f,(GLfloat)cx/(GLfloat)cy,1.0f,.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(0.0, 0.0, 15.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } BOOL CposterView::LoadTexture(void) { BOOL flag = FALSE; AUX_RGBImageRec* TextureImage[8]; // Create Storage Space For The Texture glGenTextures(7,texture); // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit if (TextureImage[0]=auxDIBImageLoad(_T("res/bkground.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[0]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); } if (TextureImage[1]=auxDIBImageLoad(_T("res/BaiLiTuSu.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[1]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[2]=auxDIBImageLoad(_T("res/FangLanSheng.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[2]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[2]->sizeX, TextureImage[2]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[2]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[3]=auxDIBImageLoad(_T("res/FengQinXue.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[3]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[3]->sizeX, TextureImage[3]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[3]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[4]=auxDIBImageLoad(_T("res/HongYu.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[4]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[4]->sizeX, TextureImage[4]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[4]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[5]=auxDIBImageLoad(_T("res/XiangLing.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[5]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[5]->sizeX, TextureImage[5]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[5]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } if (TextureImage[6]=auxDIBImageLoad(_T("res/YunQianshang.bmp"))) { // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texture[6]); glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[6]->sizeX, TextureImage[6]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[6]->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); } int i; for(i=0;i<=6;++i) { if (TextureImage[i]) // If Texture Exists { if (TextureImage[i]->data) // If Texture Image Exists { free(TextureImage[i]->data); // Free The Texture Image Memory } free(TextureImage[i]); // Free The Image Structure } } return flag; } void CposterView::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent == 1){ switch (rand()%3){ case 0: spinX += 30; spinX %= 360; break; case 1: spinY += 30; spinY %= 360; break; case 2: spinZ += 30; spinZ %= 360; break; } Invalidate(); } CView::OnTimer(nIDEvent); }  

     

     

    最新回复(0)