我现在有个NEHE的OPENGL的教程,是个读RAW格式文件数据生成地形的程序,用API写的。现在想把他转换成MFC的单文档程序。把几个主要的函数写成MFC的成员函数,调试总不成功,没有错误,但结果不对。请问各位高手下面这个API程序怎样转换成MFC程序?需要注意哪些问题?我VC学的不好,请各位高手指教!!
下面是主要的函数代码:GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0)// 防止被零除
{
height=1;// 将Height设为1
}
glViewport(0, 0, width, height);// 重置当前的视口(Viewport)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}// Loads The .RAW File And Stores It In pHeightMap
void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
{
FILE *pFile = NULL;
// Open The File In Read / Binary Mode.
pFile = fopen( strName, "rb" );
// Check To See If We Found The File And Could Open It
if ( pFile == NULL )
{
// Display Error Message And Stop The Function
MessageBox(NULL, "Can't Find The Height Map!", "Error", MB_OK);
return;
}
// Here We Load The .RAW File Into Our pHeightMap Data Array
// We Are Only Reading In '1', And The Size Is (Width * Height)
fread( pHeightMap, 1, nSize, pFile );
// After We Read The Data, It's A Good Idea To Check If Everything Read Fine
int result = ferror( pFile );
// Check If We Received An Error
if (result)
{
MessageBox(NULL, "Failed To Get Data!", "Error", MB_OK);
}
// Close The File
fclose(pFile);
}int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup//设置深度缓存
glEnable(GL_DEPTH_TEST); // Enables Depth Testing// 启用深度测试
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do// 所作深度测试的类型
//真正精细的透视修正
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
// Here we read read in the height map from the .raw file and put it in our
// g_HeightMap array. We also pass in the size of the .raw file (1024).
LoadRawFile("Terrain.raw", MAP_SIZE * MAP_SIZE, g_HeightMap); // ( NEW )
return TRUE; // Initialization Went OK
}int Height(BYTE *pHeightMap, int X, int Y) // This Returns The Height From A Height Map Index
{
int x = X % MAP_SIZE; // Error Check Our x value
int y = Y % MAP_SIZE; // Error Check Our y value
if(!pHeightMap) return 0; // Make Sure Our Data Is Valid
return pHeightMap[x + (y * MAP_SIZE)]; // Index Into Our Height Array And Return The Height
}void SetVertexColor(BYTE *pHeightMap, int x, int y) // This Sets The Color value For A Particular Index
{ // Depending On The Height Index
if(!pHeightMap) return; // Make Sure Our Height Data Is Valid
float fColor = -0.15f + (Height(pHeightMap, x, y ) / 256.0f);
// Assign This Blue Shade To The Current Vertex
glColor3f(0.0f, fColor,0.0f);
}void RenderHeightMap(BYTE pHeightMap[]) // This Renders The Height Map As Quads
{
int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability
if(!pHeightMap) return; // Make Sure Our Height Data Is Valid
if(bRender) // What We Want To Render
glBegin( GL_QUADS ); // Render Polygons//??????????????????
//else??????????????????????????????????
//glBegin( GL_LINES ); // Render Lines Instead???????????
for ( X = 0; X < MAP_SIZE; X += STEP_SIZE )
for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
{
// Get The (X, Y, Z) value For The Bottom Left Vertex
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
// Get The (X, Y, Z) value For The Top Left Vertex
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);// Send This Vertex To OpenGL To Be Rendered
// Get The (X, Y, Z) value For The Top Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
// Get The (X, Y, Z) value For The Bottom Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
}
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Reset The Color
}int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The Matrix// 重置当前的模型观察矩阵
// Position View Up Vector
gluLookAt(212, 60, 194, 186, 55, 171, 0, 1, 0); // This Determines The Camera's Position And View
glScalef(scalevalue, scalevalue * HEIGHT_RATIO, scalevalue);
RenderHeightMap(g_HeightMap); // Render The Height Map
return TRUE; // Keep Going// 一切OK
}
下面是主要的函数代码:GLvoid ReSizeGLScene(GLsizei width, GLsizei height) // Resize And Initialize The GL Window
{
if (height==0)// 防止被零除
{
height=1;// 将Height设为1
}
glViewport(0, 0, width, height);// 重置当前的视口(Viewport)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}// Loads The .RAW File And Stores It In pHeightMap
void LoadRawFile(LPSTR strName, int nSize, BYTE *pHeightMap)
{
FILE *pFile = NULL;
// Open The File In Read / Binary Mode.
pFile = fopen( strName, "rb" );
// Check To See If We Found The File And Could Open It
if ( pFile == NULL )
{
// Display Error Message And Stop The Function
MessageBox(NULL, "Can't Find The Height Map!", "Error", MB_OK);
return;
}
// Here We Load The .RAW File Into Our pHeightMap Data Array
// We Are Only Reading In '1', And The Size Is (Width * Height)
fread( pHeightMap, 1, nSize, pFile );
// After We Read The Data, It's A Good Idea To Check If Everything Read Fine
int result = ferror( pFile );
// Check If We Received An Error
if (result)
{
MessageBox(NULL, "Failed To Get Data!", "Error", MB_OK);
}
// Close The File
fclose(pFile);
}int InitGL(GLvoid) // All Setup For OpenGL Goes Here
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup//设置深度缓存
glEnable(GL_DEPTH_TEST); // Enables Depth Testing// 启用深度测试
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do// 所作深度测试的类型
//真正精细的透视修正
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
// Here we read read in the height map from the .raw file and put it in our
// g_HeightMap array. We also pass in the size of the .raw file (1024).
LoadRawFile("Terrain.raw", MAP_SIZE * MAP_SIZE, g_HeightMap); // ( NEW )
return TRUE; // Initialization Went OK
}int Height(BYTE *pHeightMap, int X, int Y) // This Returns The Height From A Height Map Index
{
int x = X % MAP_SIZE; // Error Check Our x value
int y = Y % MAP_SIZE; // Error Check Our y value
if(!pHeightMap) return 0; // Make Sure Our Data Is Valid
return pHeightMap[x + (y * MAP_SIZE)]; // Index Into Our Height Array And Return The Height
}void SetVertexColor(BYTE *pHeightMap, int x, int y) // This Sets The Color value For A Particular Index
{ // Depending On The Height Index
if(!pHeightMap) return; // Make Sure Our Height Data Is Valid
float fColor = -0.15f + (Height(pHeightMap, x, y ) / 256.0f);
// Assign This Blue Shade To The Current Vertex
glColor3f(0.0f, fColor,0.0f);
}void RenderHeightMap(BYTE pHeightMap[]) // This Renders The Height Map As Quads
{
int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability
if(!pHeightMap) return; // Make Sure Our Height Data Is Valid
if(bRender) // What We Want To Render
glBegin( GL_QUADS ); // Render Polygons//??????????????????
//else??????????????????????????????????
//glBegin( GL_LINES ); // Render Lines Instead???????????
for ( X = 0; X < MAP_SIZE; X += STEP_SIZE )
for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
{
// Get The (X, Y, Z) value For The Bottom Left Vertex
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
// Get The (X, Y, Z) value For The Top Left Vertex
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);// Send This Vertex To OpenGL To Be Rendered
// Get The (X, Y, Z) value For The Top Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
// Get The (X, Y, Z) value For The Bottom Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
// Set The Color value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
}
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Reset The Color
}int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The Matrix// 重置当前的模型观察矩阵
// Position View Up Vector
gluLookAt(212, 60, 194, 186, 55, 171, 0, 1, 0); // This Determines The Camera's Position And View
glScalef(scalevalue, scalevalue * HEIGHT_RATIO, scalevalue);
RenderHeightMap(g_HeightMap); // Render The Height Map
return TRUE; // Keep Going// 一切OK
}
BOOL CTerrainMatch20057View::InitializeOpenGL(CDC* pDC)
{
m_pDC = pDC;
SetupPixelFormat();
//生成绘制描述表
m_hRC = ::wglCreateContext(m_pDC->GetSafeHdc());
//置当前绘制描述表
::wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
return TRUE;
}