📄 textureview.cpp
字号:
// TextureView.cpp : implementation of the CTextureView class
//
#include "stdafx.h"
#include "Texture.h"
#include "TextureDoc.h"
#include "TextureView.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "gl\glaux.h"
#include <stdlib.h>
#include <string.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//add down
/*创建检查板纹理*/
#define checkImageWidth 64
#define checkImageHeight 64
static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
static GLubyte otherImage[checkImageHeight][checkImageWidth][4];
static GLuint texName[2];
void makeCheckImage(void);
/////////////////////////////////////////////////////////////////////////////
// CTextureView
IMPLEMENT_DYNCREATE(CTextureView, CView)
BEGIN_MESSAGE_MAP(CTextureView, CView)
//{{AFX_MSG_MAP(CTextureView)
ON_WM_CREATE()
ON_WM_DESTROY()
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()
/////////////////////////////////////////////////////////////////////////////
// CTextureView construction/destruction
CTextureView::CTextureView()
{
// TODO: add construction code here
}
CTextureView::~CTextureView()
{
}
BOOL CTextureView::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);
}
/////////////////////////////////////////////////////////////////////////////
// CTextureView drawing
void CTextureView::OnDraw(CDC* pDC)
{
CTextureDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
wglMakeCurrent(pDC->m_hDC,hglrc);
drawscene();
wglMakeCurrent(pDC->m_hDC,NULL);
SwapBuffers(pDC->m_hDC);
}
/////////////////////////////////////////////////////////////////////////////
// CTextureView printing
BOOL CTextureView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTextureView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTextureView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTextureView diagnostics
#ifdef _DEBUG
void CTextureView::AssertValid() const
{
CView::AssertValid();
}
void CTextureView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTextureDoc* CTextureView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTextureDoc)));
return (CTextureDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTextureView message handlers
int CTextureView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
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
16, // NOTE: better performance with 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
};
CClientDC clientdc(this);
int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
hglrc=wglCreateContext(clientdc.m_hDC);
return 0;
}
void CTextureView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
wglDeleteContext(hglrc);
}
void CTextureView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLsizei w=cx;
GLsizei h=cy;
//avoid divided by zero
if(!h)return;
CClientDC clientDC(this);
wglMakeCurrent(clientDC.m_hDC,hglrc);
//define the ciewport
glViewport(0,0,w,h);
//prepare for and then
glMatrixMode(GL_PROJECTION);
//define the projection transformation
glLoadIdentity();
gluPerspective(60.0,1.0*(GLfloat)w/(GLfloat)h,1.0,30.0);
//back to modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
wglMakeCurrent(NULL,NULL);
}
void CTextureView::myinit()
{
//Enzble z buffer
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
makeCheckImage();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(2, texName);
glBindTexture(GL_TEXTURE_2D, texName[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
checkImage);
glBindTexture(GL_TEXTURE_2D, texName[1]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
otherImage);
glEnable(GL_TEXTURE_2D);
}
//
void CTextureView::drawscene()
{
//clear the window with current clearing color
glClearColor(0.0,0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//intialize texture
myinit();
//define viewing transformation
glPushMatrix();
glTranslatef(0.0,0.0,-3.6);
//draw squares with texture map
glBegin(GL_QUADS);
//sqiare facing viewer squarely
glTexCoord2f(0.0,0.0);
glVertex3f(-2.0,-1.0,0.0);
glTexCoord2f(0.0,1.0);
glVertex3f(-2.0,1.0,0.0);
glTexCoord2f(1.0,1.0);
glVertex3f(0.0,1.0,0.0);
glTexCoord2f(1.0,0.0);
glVertex3f(0.0,-1.0,0.0);
glEnd();
glPopMatrix();
//Flush drawing commands
glFlush();
}
void CTextureView::makeCheckImage()
{
int i, j, c;
for (i = 0; i < checkImageHeight; i++) {
for (j = 0; j < checkImageWidth; j++) {
c = ((((i&0x8)==0)^((j&0x8))==0))*255;
checkImage[i][j][0] = (GLubyte) c;
checkImage[i][j][1] = (GLubyte) c;
checkImage[i][j][2] = (GLubyte) c;
checkImage[i][j][3] = (GLubyte) 255;
c = ((((i&0x10)==0)^((j&0x10))==0))*255;
otherImage[i][j][0] = (GLubyte) c;
otherImage[i][j][1] = (GLubyte) 0;
otherImage[i][j][2] = (GLubyte) 0;
otherImage[i][j][3] = (GLubyte) 255;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -