www.pudn.com > lgq.rar > lgqView.cpp, change:2006-01-19,size:21693b
// lgqView.cpp : implementation of the CLgqView class
//
#include "stdafx.h"
#include "lgq.h"
#include "math.h"
#include "lgqDoc.h"
#include "lgqView.h"
#define MAX_TEXTURES 1000
// This holds the texture info by an ID
UINT g_Texture[MAX_TEXTURES] = {0};
#define BACK_ID 11 // The texture ID for the back side of the cube
#define FRONT_ID 12 // The texture ID for the front side of the cube
#define BOTTOM_ID 13 // The texture ID for the bottom side of the cube
#define TOP_ID 14 // The texture ID for the top side of the cube
#define LEFT_ID 15 // The texture ID for the left side of the cube
#define RIGHT_ID 16 // The texture ID for the right side of the cube
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLgqView
IMPLEMENT_DYNCREATE(CLgqView, CView)
BEGIN_MESSAGE_MAP(CLgqView, CView)
//{{AFX_MSG_MAP(CLgqView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_TIMER()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_WM_KEYDOWN()
ON_COMMAND(ID_showdem, Onshowdem)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CLgqView construction/destruction
CLgqView::CLgqView()
{
x=-250;
y=-650;
z=-250;
//DEM = false;
m_display=false;
Scale=1;
LH_Rate=10;
m_rotate = false;
m_texture = false;
m_hight=NULL;
// TODO: add construction code here
}
CLgqView::~CLgqView()
{ if (m_hight)
delete [] m_hight;
}
BOOL CLgqView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
/////////设置窗口类型
cs.style|=WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CLgqView drawing
void CLgqView::OnDraw(CDC* pDC)
{
CLgqDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CRect rect;
GetClientRect(&rect);
int Win_Height=rect.bottom-rect.top;
int Win_Width=rect.right-rect.left;
// TODO: add draw code for native data here
if (m_display)
{
RenderScene(); /////////渲染场景
}
}
/////////////////////////////////////////////////////////////////////////////
// CLgqView printing
BOOL CLgqView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CLgqView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CLgqView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CLgqView diagnostics
#ifdef _DEBUG
void CLgqView::AssertValid() const
{
CView::AssertValid();
}
void CLgqView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CLgqDoc* CLgqView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CLgqDoc)));
return (CLgqDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLgqView message handlers
int CLgqView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
//初始化GL和设置定时器
m_pDC=new CClientDC(this);
SetTimer(1,20,NULL);
InitializeOpenGL(m_pDC);
return 0;
}
void CLgqView::OnDestroy()
{
CView::OnDestroy();
// TODO: Add your message handler code here
////////////////////删除调色板和渲染上下文、定时器
::wglMakeCurrent(0,0);
::wglDeleteContext(m_hRC);
if(m_hPalette)
DeleteObject(m_hPalette);
if(m_pDC)
{
delete m_pDC;
}
KillTimer(1);
}
void CLgqView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
//////////添加窗口缩放时的图形变换函数
if(cy == 0)
{
cy = 1;
}
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(GLdouble)cx/(GLdouble)cy,1,10000.0);
glMatrixMode(GL_MODELVIEW);
}
void CLgqView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
Invalidate(false);
CView::OnTimer(nIDEvent);
}
void CLgqView::SetLogicalPalette(void)
{
struct
{
WORD Version;
WORD NumberOfEntries;
PALETTEENTRY aEntries[256];
}
logicalPalette={0x300,256};
BYTE reds[]={0,36,72,109,145,182,218,255};
BYTE greens[]={0,36,72,109,145,182,218,255};
BYTE blues[]={0,85,170,255};
for(int colorNum=0;colorNum<256;++colorNum)
{
logicalPalette.aEntries[colorNum].peRed=
reds[colorNum & 0x07];
logicalPalette.aEntries[colorNum].peGreen=
greens[(colorNum>>0x03) & 0x07];
logicalPalette.aEntries[colorNum].peBlue=
blues[(colorNum>>0x06) & 0x07];
logicalPalette.aEntries[colorNum].peFlags=0;
}
m_hPalette=CreatePalette((LOGPALETTE*)&logicalPalette);
}
BOOL CLgqView::InitializeOpenGL(CDC* pDC)
{
m_pDC=pDC;
SetupPixelFormat();
m_hRC=::wglCreateContext(m_pDC->GetSafeHdc());
::wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC);
CreateTexture(g_Texture[0], "texture.bmp"); // Load the terrain texture
CreateTexture(g_Texture[BACK_ID], "Back.bmp"); // Load the Sky box Back texture
CreateTexture(g_Texture[FRONT_ID], "Front.bmp"); // Load the Sky box Front texture
CreateTexture(g_Texture[BOTTOM_ID], "Bottom.bmp"); // Load the Sky box Bottom texture
CreateTexture(g_Texture[TOP_ID], "Top.bmp"); // Load the Sky box Top texture
CreateTexture(g_Texture[LEFT_ID], "Left.bmp"); // Load the Sky box Left texture
CreateTexture(g_Texture[RIGHT_ID], "Right.bmp"); // Load the Sky box Right texture
return TRUE;
}
BOOL CLgqView::SetupPixelFormat(void)
{
PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0,
0,
0,
0,
0,0,0,0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0,0,0
};
int pixelformat;
pixelformat=::ChoosePixelFormat(m_pDC->GetSafeHdc(),&pfd);
::SetPixelFormat(m_pDC->GetSafeHdc(),pixelformat,&pfd);
if(pfd.dwFlags&PFD_NEED_PALETTE)
SetLogicalPalette();
return TRUE;
}
BOOL CLgqView::RenderScene()
{
wglMakeCurrent(m_pDC->GetSafeHdc(),m_hRC);
glClearColor(0.2f,0.2f,0.5f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glDepthFunc(GL_LESS);
gluLookAt(4000, 1300, 8000, 10000, 1300, 0.0, 0.0f, 1.0f, 0.0f);
glTranslatef(x, y, z);
//::SwapBuffers(m_pDC->GetSafeHdc());
glPushMatrix();
glColor3f(0.0f,1.0f,0.0f);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
DrawDem();
glPopMatrix();
::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
return TRUE;
}
void CLgqView::OnFileOpen()
{
// TODO: Add your command handler code here
CString File;
CFileDialog dlg(TRUE,NULL,NULL,OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY|OFN_NONETWORKBUTTON,
"矢量数据文件(*.grd,*.dat)|*.grd;*.dat|txt文件(*.txt)|*.txt|dat文件(*.dat)|*.dat|");
if(dlg.DoModal()==IDOK)
{
File = dlg.GetPathName();
Read(File);
}
}
BOOL CLgqView::Read(CString filename)
{
FILE *fp;
fp = fopen(filename, "r");
// 判断文件是否存在
if ( fp == NULL )
{
return 0 ;
}
fscanf(fp,"%f",&m_dX0);
fscanf(fp,"%f",&m_dY0);
fscanf(fp,"%f",&m_dDX);
fscanf(fp,"%f",&m_dDY);
fscanf(fp,"%d",&m_dRow);
fscanf(fp,"%d",&m_dCol);
m_hight=new float [m_dRow*m_dCol];
for( int i=0;i<m_dRow*m_dCol;i++)
{
fscanf(fp,"%f",&m_hight[i]);
}
fclose(fp);
return TRUE;
}
void CLgqView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
switch(nChar)
{
case VK_UP:
z=z+100;
break;
case VK_DOWN:
z=z-100;
break;
case VK_RIGHT:
x=x-100;
break;
case VK_LEFT:
x=x+100;
break;
case VK_F4:
y=y+100;
break;
case VK_F5:
y=y-100;
break;
default :
break;
}
// TODO: Add your message handler code here and/or call default
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CLgqView::DrawDem()
{
m_dDX=m_dDX*6378140*3.1415926/180;
m_dDY=m_dDY*6378140*3.1415926/180;
int DX=m_dRow*m_dDX;
int DY=m_dCol*m_dDY;
float X1,X2,X3,X4,Y1,Y2,Y3,Y4,Z1,Z2,Z3,Z4;
float p1[3], p2[3], p3[3], p4[3];
float r[4];
float s[4];
glBegin(GL_TRIANGLES);
for(int j=0;j<m_dRow-1;j++)
{
for(int i=0;i<m_dCol-1;i++)
{
p1[0]=i*DX;
p1[1]=m_hight[j*(m_dCol)+i];
p1[2]=j*DY;
p2[0]=(i+1)*DX;
p2[1]=m_hight[j*(m_dCol)+i+1];
p2[2]=j*DY;
p3[0]=(i+1)*DX;
p3[1]=m_hight[(j+1)*(m_dCol)+i+1];
p3[2]=(j+1)*DY;
p4[0]=i*DX;
p4[1]=m_hight[(j+1)*(m_dCol)+i];
p4[2]=(j+1)*DY;
//glTexCoord2f(r[0],s[0]);
glVertex3f(p1[0],p1[1],p1[2]);
//glTexCoord2f(r[1],s[1]);
glVertex3f(p2[0],p2[1],p2[2]);
//glTexCoord2f(r[2],s[2]);
glVertex3f(p3[0],p3[1],p3[2]);
//glTexCoord2f(r[0],s[0]);
glVertex3f(p1[0],p1[1],p1[2]);
//glTexCoord2f(r[2],s[2]);
glVertex3f(p3[0],p3[1],p3[2]);
//glTexCoord2f(r[3],s[3]);
glVertex3f(p4[0],p4[1],p4[2]);
}
}
glEnd();
}
void CLgqView::Onshowdem()
{
// TODO: Add your command handler code here
m_display=true;
}
bool CLgqView::CreateTexture(UINT &texture, LPSTR strFileName)
{
AUX_RGBImageRec *pImage = NULL;
FILE *pFile = NULL;
if(!strFileName)
return false;
// Open a file pointer to the BMP file and check if it was found and opened
if((pFile = fopen(strFileName, "rb")) == NULL)
{
// Display an error message saying the file was not found, then return NULL
//MessageBox(g_hWnd, "Unable to load BMP File!", "Error", MB_OK);
return NULL;
}
// Load the bitmap using the aux function stored in glaux.lib
pImage = auxDIBImageLoad(strFileName);
// Make sure valid image data was given to pImage, otherwise return false
if(pImage == NULL)
return false;
// Generate a texture with the associative texture ID stored in the array
glGenTextures(1, &texture);
// This sets the alignment requirements for the start of each pixel row in memory.
glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
// Bind the texture to the texture arrays index and init the texture
glBindTexture(GL_TEXTURE_2D, texture);
// Build Mipmaps (builds different versions of the picture for distances - looks better)
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->sizeX,
pImage->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);
/* 控制滤波 */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
/* 说明映射方式*/
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//Assign the mip map levels and texture info
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
// Now we need to free the image data that we loaded since openGL stored it as a texture
if (pImage) // If we loaded the image
{
if (pImage->data) // If there is texture data
{
free(pImage->data); // Free the texture data, we don't need it anymore
}
free(pImage); // Free the image structure
}
// Return a success
return true;
}
///////////////////////////////// CREATE SKY BOX \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
/////
///// This creates a sky box centered around X Y Z with a width, height and length
/////
///////////////////////////////// CREATE SKY BOX \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*
void CLgqView::CreateSkyBox(float x, float y, float z, float width, float height, float length)
{
// glEnable(GL_TEXTURE_2D); // Turn on texture mapping if it's not already
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
// This centers the sky box around (x, y, z)
x = x - width / 2;
y = y - height / 2;
z = z - length / 2;
// Bind the BACK texture of the sky map to the BACK side of the cube
glBindTexture(GL_TEXTURE_2D, g_Texture[BACK_ID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Start drawing the side as a QUAD
glBegin(GL_QUADS);
/*
// Assign the texture coordinates and vertices for the BACK Side
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y, z+ length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y, z+ length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
*/
// Assign the texture coordinates and vertices for the BACK Side
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, z, y);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, z+ length, y);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, z+ length, y);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, z, y);
glEnd();
// Bind the FRONT texture of the sky map to the FRONT side of the box
glBindTexture(GL_TEXTURE_2D, g_Texture[FRONT_ID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Start drawing the side as a QUAD
glBegin(GL_QUADS);
/* // Assign the texture coordinates and vertices for the FRONT Side
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y + height, z );
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z);
*/
// Assign the texture coordinates and vertices for the FRONT Side
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, z , y+ height );
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, z + length, y+ height );
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, z + length, y+ height );
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, z , y+ height);
glEnd();
// Bind the BOTTOM texture of the sky map to the BOTTOM side of the box
glBindTexture(GL_TEXTURE_2D, g_Texture[BOTTOM_ID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Start drawing the side as a QUAD
glBegin(GL_QUADS);
/*
// Assign the texture coordinates and vertices for the BOTTOM Side
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z);
*/
// Assign the texture coordinates and vertices for the BOTTOM Side
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, z, y);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, z , y+ height);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, z , y+ height);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, z, y);
glEnd();
// Bind the TOP texture of the sky map to the TOP side of the box
glBindTexture(GL_TEXTURE_2D, g_Texture[TOP_ID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Start drawing the side as a QUAD
glBegin(GL_QUADS);
/*
// Assign the texture coordinates and vertices for the TOP Side
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z + length);
*/
// Assign the texture coordinates and vertices for the TOP Side
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, z + length, y);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, z + length, y+ height );
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, z+ length , y+ height );
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, z+ length, y );
glEnd();
// Bind the LEFT texture of the sky map to the LEFT side of the box
glBindTexture(GL_TEXTURE_2D, g_Texture[LEFT_ID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Start drawing the side as a QUAD
glBegin(GL_QUADS);
/*
// Assign the texture coordinates and vertices for the LEFT Side
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z + length);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y + height, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y, z);
*/
// Assign the texture coordinates and vertices for the LEFT Side
glTexCoord2f(1.0f, 1.0f); glVertex3f(x, z+ length, y );
glTexCoord2f(0.0f, 1.0f); glVertex3f(x, z + length, y + height);
glTexCoord2f(0.0f, 0.0f); glVertex3f(x, z, y + height);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x, z, y);
glEnd();
// Bind the RIGHT texture of the sky map to the RIGHT side of the box
glBindTexture(GL_TEXTURE_2D, g_Texture[RIGHT_ID]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
// Start drawing the side as a QUAD
glBegin(GL_QUADS);
/*
// Assign the texture coordinates and vertices for the RIGHT Side
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y, z);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y + height, z);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z + length);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y, z + length);
*/
// Assign the texture coordinates and vertices for the RIGHT Side
glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, z, y);
glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, z , y+ height);
glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, z+ length , y + height);
glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, z + length, y);
glEnd();
}
BOOL CLgqView::LoadTexture2(LPCTSTR fileName)
{
// Load the bitmap.
HBITMAP hBmp = (HBITMAP) ::LoadImage (NULL,
(LPCTSTR) fileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE |
LR_CREATEDIBSECTION);
if (hBmp == NULL)
{
AfxMessageBox("Error: unable to load bitmap file");
return FALSE;
}
// Get bitmap info.
BITMAP BM;
::GetObject (hBmp, sizeof (BM), &BM);
// Must be 24 bit
if (BM.bmBitsPixel != 24)
{
AfxMessageBox("Error: bitmap must be 24 bit");
return FALSE;
}
// Make the rendering context current
// (must do b/f any OpenGL calls)
// ::wglMakeCurrent(m_hDC,m_hRC);
// Generate name for and bind texture.
if (!m_texName)
{
glGenTextures(1, &m_texName);
}
glBindTexture(GL_TEXTURE_2D, m_texName);
// Set up all the the texture mapping params.
// glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
// glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
// glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
// glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
/* glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Build the texture map
// Could use glTexImage2D instead but then we would have to scale
// the image to make it a power of 2.
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, BM.bmWidth, BM.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE,
BM.bmBits );
glBindTexture(GL_TEXTURE_2D, 0);
//RebuildSphereDisplayList();
// wglMakeCurrent(m_hDC,NULL);
*/
/* 定义纹理 */
glTexImage2D(GL_TEXTURE_2D, 0, 3, BM.bmWidth,
BM.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BM.bmBits);
/* 控制滤波 */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
/* 说明映射方式*/
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
/* 启动纹理映射 */
// glEnable(GL_TEXTURE_2D);
return TRUE;
}