📄 fogcolorindexview.cpp
字号:
// FogColorIndexView.cpp : implementation of the CFogColorIndexView class
//
#include "stdafx.h"
#include "FogColorIndex.h"
#include "FogColorIndexDoc.h"
#include "FogColorIndexView.h"
//add down
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
//add up
//add down定义常量
#define NUMCOLORS 32
#define RAMPSTART 16
//add up定义常量
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CFogColorIndexView
IMPLEMENT_DYNCREATE(CFogColorIndexView, CView)
BEGIN_MESSAGE_MAP(CFogColorIndexView, CView)
//{{AFX_MSG_MAP(CFogColorIndexView)
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()
/////////////////////////////////////////////////////////////////////////////
// CFogColorIndexView construction/destruction
CFogColorIndexView::CFogColorIndexView()
{
//add down
m_pDC = NULL;
//add up
}
CFogColorIndexView::~CFogColorIndexView()
{
}
BOOL CFogColorIndexView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
//add down
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
//add up
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CFogColorIndexView drawing
void CFogColorIndexView::OnDraw(CDC* pDC)
{
CFogColorIndexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//add down
DrawScene();
//add up
}
/////////////////////////////////////////////////////////////////////////////
// CFogColorIndexView printing
BOOL CFogColorIndexView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CFogColorIndexView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CFogColorIndexView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CFogColorIndexView diagnostics
#ifdef _DEBUG
void CFogColorIndexView::AssertValid() const
{
CView::AssertValid();
}
void CFogColorIndexView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CFogColorIndexDoc* CFogColorIndexView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFogColorIndexDoc)));
return (CFogColorIndexDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CFogColorIndexView message handlers
int CFogColorIndexView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CFogColorIndexView::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 CFogColorIndexView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CFogColorIndexView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
//add down
int w=cx;
int h=cy;
//避免除数为0
if(h==0) h=1;
//设置视口与窗口匹配
glViewport(0,0,w,h);
//重新设置坐标系统
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//建立正交投影
if(w<=(h*3))
glOrtho(-2.0,2.0,-2.0*(GLfloat)h/(GLfloat)w,
2.0*(GLfloat)h/(GLfloat)w,0.0,10.0);
else
glOrtho(-2.0*(GLfloat)w/(GLfloat)h,
2.0*(GLfloat)w/(GLfloat)h,
-2.0,2.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//add up
}
//add down
void CFogColorIndexView::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);
//进行深度测试
int i;
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
for(i=0;i<NUMCOLORS;i++)
{
GLfloat shade;
shade=(GLfloat)(NUMCOLORS-i)/(GLfloat)NUMCOLORS;
auxSetOneColor(16+i,shade,shade,shade);
}
//产生雾的效果
//雾化的步骤:
//1. 使用glEnable(GL_FOG)启用雾;
//2. 控制雾。调用函数glFog*()来选择控制浓度和颜色的雾方程;
//3. 若有必要,可用函数glHint(GL_FOG_HINT)提供一个值。
glEnable(GL_FOG);
{
glFogi(GL_FOG_MODE,GL_LINEAR);
glFogi(GL_FOG_INDEX,NUMCOLORS);
glFogf(GL_FOG_START,0.0);
glFogf(GL_FOG_END,4.0);
glHint(GL_FOG_HINT,GL_NICEST);
glClearIndex((GLfloat)(NUMCOLORS+RAMPSTART-1));
}
}
BOOL CFogColorIndexView::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 CFogColorIndexView::DrawScene(void)
{
//清除颜色缓冲区和深度缓冲区
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//在(-3.0,-1.5,-2.0)绘制一个茶壶
glPushMatrix();
glTranslatef(-1.0,-0.0,-1.0);
glIndexi(RAMPSTART);
auxSolidTeapot(0.3);
glPopMatrix();
//在(-0.5,-0.5,-7.0)绘制一个茶壶
glPushMatrix();
glTranslatef(0.0,-0.0,-2.25);
glIndexi(RAMPSTART);
auxSolidTeapot(0.3);
glPopMatrix();
//在(2.0,0.8,-10.0)绘制一个茶壶
glPushMatrix();
glTranslatef(1.0,-0.0,-3.5);
glIndexi(RAMPSTART);
auxSolidTeapot(0.3);
glPopMatrix();
//刷新屏幕,绘制图形
glFinish();
SwapBuffers(wglGetCurrentDC());
}
//add up
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -