⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 primitiveview.cpp

📁 C:Documents and SettingsAdministrator桌面VC++多媒体特效制作百例CHAR08Primitive
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PrimitiveView.cpp : implementation of the CPrimitiveView class
//

#include "stdafx.h"
#include "Primitive.h"

#include "PrimitiveDoc.h"
#include "PrimitiveView.h"

//add down
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define PIXEL_CENTER(x) ((long)(x) + 0.5)

#define GAP 10
#define ROWS 3
#define COLS 4

#define OPENGL_WIDTH 48
#define OPENGL_HEIGHT 13
//add up

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPrimitiveView

IMPLEMENT_DYNCREATE(CPrimitiveView, CView)

BEGIN_MESSAGE_MAP(CPrimitiveView, CView)
	//{{AFX_MSG_MAP(CPrimitiveView)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CPrimitiveView construction/destruction

CPrimitiveView::CPrimitiveView()
{
	//add down
	m_pDC = NULL;

    mode1 = GL_TRUE;
    mode2 = GL_TRUE;
	windW = 600;
	windH = 300;
	GLubyte OpenGL_bits[] = {
	   0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 
	   0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01,
	   0x7f, 0xfb, 0xff, 0xff, 0xff, 0x01, 
	   0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
	   0x3e, 0x8f, 0xb7, 0xf9, 0xfc, 0x01, 
	   0x63, 0xdb, 0xb0, 0x8d, 0x0d, 0x00,
	   0x63, 0xdb, 0xb7, 0x8d, 0x0d, 0x00, 
	   0x63, 0xdb, 0xb6, 0x8d, 0x0d, 0x00,
	   0x63, 0x8f, 0xf3, 0xcc, 0x0d, 0x00, 
	   0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0a,
	   0x63, 0x00, 0x00, 0x0c, 0x4c, 0x0e, 
	   0x63, 0x00, 0x00, 0x8c, 0xed, 0x0e,
	   0x3e, 0x00, 0x00, 0xf8, 0x0c, 0x00, 
	};
	//add up
}

CPrimitiveView::~CPrimitiveView()
{
}

BOOL CPrimitiveView::PreCreateWindow(CREATESTRUCT& cs)
{
	//add down
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
	//add up

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPrimitiveView drawing

void CPrimitiveView::OnDraw(CDC* pDC)
{
	CPrimitiveDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	//add down
	DrawScene();
	//add up
}

/////////////////////////////////////////////////////////////////////////////
// CPrimitiveView printing

BOOL CPrimitiveView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPrimitiveView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPrimitiveView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPrimitiveView diagnostics

#ifdef _DEBUG
void CPrimitiveView::AssertValid() const
{
	CView::AssertValid();
}

void CPrimitiveView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CPrimitiveDoc* CPrimitiveView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPrimitiveDoc)));
	return (CPrimitiveDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPrimitiveView message handlers

int CPrimitiveView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	//add down
	Init(); //初始化OpenGL
	//add up
	
	return 0;
}

void CPrimitiveView::OnDestroy() 
{
	//add down
	HGLRC   hrc;

	hrc = ::wglGetCurrentContext();

	::wglMakeCurrent(NULL,  NULL);

	if (hrc)
		::wglDeleteContext(hrc);

	if (m_pDC)
		delete m_pDC;
	//add up

	CView::OnDestroy();
}

BOOL CPrimitiveView::OnEraseBkgnd(CDC* pDC) 
{
	return TRUE;
}

void CPrimitiveView::OnSize(UINT nType, int cx, int cy) 
{
	CView::OnSize(nType, cx, cy);
	
    windW = cx;
    windH = cy;
	
}

//add down
void CPrimitiveView::Init()
{
	PIXELFORMATDESCRIPTOR pfd;
	int         n;
	HGLRC       hrc;

	m_pDC = new CClientDC(this);

	ASSERT(m_pDC != NULL);

	if (!bSetupPixelFormat())
		return;

	n = ::GetPixelFormat(m_pDC->GetSafeHdc());
	::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);

	hrc = wglCreateContext(m_pDC->GetSafeHdc());
	wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);

}

BOOL CPrimitiveView::bSetupPixelFormat()
{
	static PIXELFORMATDESCRIPTOR pfd =
	{
		sizeof(PIXELFORMATDESCRIPTOR),  // size of this pfd
		1,                              // version number
		PFD_DRAW_TO_WINDOW |            // support window
		  PFD_SUPPORT_OPENGL |          // support OpenGL
		  PFD_DOUBLEBUFFER,             // double buffered
		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
		32,                             // 32-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 pixelformat;

	if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
	{
		MessageBox("ChoosePixelFormat failed");
		return FALSE;
	}

	if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
	{
		MessageBox("SetPixelFormat failed");
		return FALSE;
	}

	return TRUE;
}

void CPrimitiveView::DrawScene(void)
{
    glViewport(0, 0, windW, windH);
    glDisable(GL_SCISSOR_TEST);

    glPushAttrib(GL_COLOR_BUFFER_BIT);

    glColorMask(1, 1, 1, 1);
    glIndexMask(~0);

    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);

    glPopAttrib();

    if (mode1) {
	glShadeModel(GL_SMOOTH);
    } else {
	glShadeModel(GL_FLAT);
    }

    if (mode2) {
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    } else {
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    }

    Viewport(0, 0); Point();
    Viewport(0, 1); Lines();
    Viewport(0, 2); LineStrip();
    Viewport(0, 3); LineLoop();

    Viewport(1, 0); Bitmap();

    Viewport(1, 1); TriangleFan();
    Viewport(1, 2); Triangles();
    Viewport(1, 3); TriangleStrip();

    Viewport(2, 0); Rect();
    Viewport(2, 1); Polygons();
    Viewport(2, 2); Quads();
    Viewport(2, 3); QuadStrip();
 
	glFinish();
	SwapBuffers(wglGetCurrentDC());
}

void CPrimitiveView::RotateColorMask(void)
{
    static GLint rotation = 0;
    
    rotation = (rotation + 1) & 0x3;
    switch (rotation) {
      case 0:
	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
	glIndexMask(0xff);
	break;
      case 1:
	glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
	glIndexMask(0xFE);
	break;
      case 2:
	glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
	glIndexMask(0xFD);
	break;
      case 3:
	glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
	glIndexMask(0xFB);
	break;
    }
}

void CPrimitiveView::SetColor(int index)
{

    if (rgb) {
	switch (index) {
	  case 0:
	    glColor3f(0.0, 0.0, 0.0);
	    break;
	  case 1:
	    glColor3f(1.0, 0.0, 0.0);
	    break;
	  case 2:
	    glColor3f(0.0, 1.0, 0.0);
	    break;
	  case 3:
	    glColor3f(1.0, 1.0, 0.0);
	    break;
	  case 4:
	    glColor3f(0.0, 0.0, 1.0);
	    break;
	  case 5:
	    glColor3f(1.0, 0.0, 1.0);
	    break;
	  case 6:
	    glColor3f(0.0, 1.0, 1.0);
	    break;
	  case 7:
	    glColor3f(1.0, 1.0, 1.0);
	    break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -