算法源码贴出来,希望牛人多多指教
nt CMyOpenglView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24, //24-bit color
0,0,0,0,0,0,
0,0,0,0,0,0,0,
16, //32 bit depth buffer
0,0,
PFD_MAIN_PLANE, //Main layer type
0,
0,0,0
};
CClientDC clientDC(this);
int pixelFormat =ChoosePixelFormat(clientDC.m_hDC,&pfd);//得到相应的像素格式
BOOL success = SetPixelFormat(clientDC.m_hDC,pixelFormat,&pfd);//指定相应dc的像素格式
m_hRC=wglCreateContext(clientDC.m_hDC);
InitGL();
return 0;
}void CMyOpenglView::OnDestroy()
{
CView::OnDestroy();
wglDeleteContext(m_hRC);
// TODO: Add your message handler code here
}void CMyOpenglView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLfloat nRange=500.0f;
if(cy==0)
cx=1;
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// if(cx<=cy)
// glOrtho(-nRange,nRange,-nRange*cy/cx,nRange*cy/cx,-nRange,nRange);
// else
// glOrtho(-nRange*cx/cy,nRange*cx/cy,-nRange,nRange,-nRange,nRange);
gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,500.0f); glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/****************************************************************/
void CMyOpenglView::LoadRawFile(LPSTR strName,int nSize,BYTE *pHeightMap)
{
FILE *pFile=NULL;
pFile=fopen(strName,"rb");
if (pFile==NULL)
{
MessageBox("Can't find the height map!","ERROR",MB_OK);
return;
}
fread(pHeightMap,1,nSize,pFile);
int result =ferror(pFile);
if (result)
{
MessageBox("Failed to get data!","ERROR",MB_OK);
}
fclose(pFile);
}
/*****************************************************************/
int CMyOpenglView::InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.5f);
glClearDepth(1.0f); //设置深度缓存
glEnable(GL_DEPTH_TEST); //测试深度缓存
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
LoadRawFile("Debug/Terrain.raw",MAP_SIZE*MAP_SIZE,g_HeightMap);
return TRUE;
}
/****************************************************************/
int CMyOpenglView::Height(BYTE *pHeightMap,int X,int Y)
{
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)]; }
/***************************************************************/
void CMyOpenglView::SetVertexColor(BYTE *pHeightMap,int x,int y)
{
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(fColor, 0, 0 );}
/*****************************************************************/
void CMyOpenglView::RenderHeightMap(BYTE pHeightMap[])
{
int X = 0, Y = 0;
int x, y, z;
if(!pHeightMap) return;
if(bRender)
glBegin( GL_QUADS );
else
glBegin( GL_LINES );
for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
{
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
}
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void CMyOpenglView::DrawWithOpengl()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(212, 60, 194, 186, 55, 171, 0, 1, 0); // This Determines Where The Camera's Position And View Is
glScalef(scaleValue, scaleValue * HEIGHT_RATIO, scaleValue);
RenderHeightMap(g_HeightMap); // Render The Height Map
}
nt CMyOpenglView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
PIXELFORMATDESCRIPTOR pfd=
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|
PFD_SUPPORT_OPENGL|
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24, //24-bit color
0,0,0,0,0,0,
0,0,0,0,0,0,0,
16, //32 bit depth buffer
0,0,
PFD_MAIN_PLANE, //Main layer type
0,
0,0,0
};
CClientDC clientDC(this);
int pixelFormat =ChoosePixelFormat(clientDC.m_hDC,&pfd);//得到相应的像素格式
BOOL success = SetPixelFormat(clientDC.m_hDC,pixelFormat,&pfd);//指定相应dc的像素格式
m_hRC=wglCreateContext(clientDC.m_hDC);
InitGL();
return 0;
}void CMyOpenglView::OnDestroy()
{
CView::OnDestroy();
wglDeleteContext(m_hRC);
// TODO: Add your message handler code here
}void CMyOpenglView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
GLfloat nRange=500.0f;
if(cy==0)
cx=1;
glViewport(0,0,cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// if(cx<=cy)
// glOrtho(-nRange,nRange,-nRange*cy/cx,nRange*cy/cx,-nRange,nRange);
// else
// glOrtho(-nRange*cx/cy,nRange*cx/cy,-nRange,nRange,-nRange,nRange);
gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.1f,500.0f); glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/****************************************************************/
void CMyOpenglView::LoadRawFile(LPSTR strName,int nSize,BYTE *pHeightMap)
{
FILE *pFile=NULL;
pFile=fopen(strName,"rb");
if (pFile==NULL)
{
MessageBox("Can't find the height map!","ERROR",MB_OK);
return;
}
fread(pHeightMap,1,nSize,pFile);
int result =ferror(pFile);
if (result)
{
MessageBox("Failed to get data!","ERROR",MB_OK);
}
fclose(pFile);
}
/*****************************************************************/
int CMyOpenglView::InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.5f);
glClearDepth(1.0f); //设置深度缓存
glEnable(GL_DEPTH_TEST); //测试深度缓存
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
LoadRawFile("Debug/Terrain.raw",MAP_SIZE*MAP_SIZE,g_HeightMap);
return TRUE;
}
/****************************************************************/
int CMyOpenglView::Height(BYTE *pHeightMap,int X,int Y)
{
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)]; }
/***************************************************************/
void CMyOpenglView::SetVertexColor(BYTE *pHeightMap,int x,int y)
{
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(fColor, 0, 0 );}
/*****************************************************************/
void CMyOpenglView::RenderHeightMap(BYTE pHeightMap[])
{
int X = 0, Y = 0;
int x, y, z;
if(!pHeightMap) return;
if(bRender)
glBegin( GL_QUADS );
else
glBegin( GL_LINES );
for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
{
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
SetVertexColor(pHeightMap, x, z);
glVertex3i(x, y, z);
}
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void CMyOpenglView::DrawWithOpengl()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(212, 60, 194, 186, 55, 171, 0, 1, 0); // This Determines Where The Camera's Position And View Is
glScalef(scaleValue, scaleValue * HEIGHT_RATIO, scaleValue);
RenderHeightMap(g_HeightMap); // Render The Height Map
}
解决方案 »
- 句柄表是谁的组成部分?
- 仿资源管理器 树图标问题
- 关于StringTable的问题
- CPropertySheet 删除 不需要按钮
- 如何屏蔽调CListCtrl上的自动鼠标响应?
- 求POP3协议的用纯 C++ 实现的封装类
- 如何实现让ClistCtrl支持拖放和接受文件?
- 请熟悉输入法的高人帮帮忙:截获了WM_IME_COMPOSITION消息,如何得到CompositionString?
- 请问大家,学vc难还是学Java难?有一点java基础学vc会很困难吗?
- 新手请教接口聚合!!!!!!!!!
- 问个弱弱的VB问题
- 本人材料专业高级工程师+高程(2005年考过)+系分(2006年考过),现想搞软件开发,没有资金,没有客户。哪里能找到投资渠道呢?哪里能找到客户呢?如何起步呢?
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(212, 60, 194, 186, 55, 171, 0, 1, 0); // This Determines Where The Camera's Position And View Is glScalef(scaleValue, scaleValue * HEIGHT_RATIO, scaleValue); RenderHeightMap(g_HeightMap); // Render The Height Map
} 我想主要是这段代码的问题吧.
首先,你应该在最后加上 SwapBuffers( ); 如果用glut的话就用glutSwapBuffers( );反正要交换缓存不然他就会一直绘制第一帧,从而看不见东西
第2种黑屏的情况有可能是你的相机没有对准你所要渲染的对象,或者对象离开了glPrespective( )所确定的视图体.
判断是否是相机未对准你可以使用glOrtho来设置视图体来看能不能绘制出来,如果能绘制出来就证明是相机的设置问题即
gluLookAt设置的参数问题了.如果也不能绘制出来你就要检查一下是你的对象是否在视图体之外了.或者你所绘制的物体没有设置颜色.
.
我觉得glScalef()这种变换最好用glPushMatrix( )和glPopMatrix()弄一下吧