本人菜鸟,今天把红宝书上的几个opengl的例子练习了一下,关于雾的,搞来搞去,中间有些小问题,不过都解决了,当然我是在MFC下编程编写的。
首先,要先感谢洞庭散人(Phinecos),如何在MFC框架下进行OpenGL编程,我就是从他的博客里边学到的。《基于MFC的OpenGL编程》系列文章,有兴趣可以去看看,比较容易上手。
像往常一样,我开始联系opengl编程的话,首先打开“《基于MFC的OpenGL编程》Part 2 Setting up OpenGL on Windows”,把这里面的基本步骤先弄完。然后,拿着红宝书就开始敲代码。其实嘛,SGI网站上是有源代码的,不过初学者还是自己敲一下,记忆深刻点。
一切准备妥当,就可以开始了。
第一个例子,就是画五个球那个程序。(下面贴的主要代码,至于增加的变量,贴在最后,或者本人太懒的话就不贴了)
1.新创建一个函数void COpenGLDemo20100319View::LightAndFog(void)
设置光线和雾
void COpenGLDemo20100319View::LightAndFog(void)
{
GLfloat position[] = {0.5f,0.5f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
GLfloat mat[3] = {0.1745f,0.01175f,0.01175f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat);
mat[0] =0.61424f;
mat[1] =0.04136f;
mat[2] =0.04136f;
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat);
mat[0] =0.727811f;
mat[1] =0.626959f;
mat[2] =0.626959f;
glMaterialfv(GL_FRONT,GL_SPECULAR,mat);
glMaterialf(GL_FRONT,GL_SHININESS,0.6*128.0);
}
glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.5f,0.5f,0.5f,1.0f};
fogMode = GL_EXP;
glFogi(GL_FOG_MODE,fogMode);
glFogfv(GL_FOG_COLOR,fogColor);
glFogf(GL_FOG_DENSITY,0.35);
glHint(GL_FOG_HINT,GL_DONT_CARE);
glFogf(GL_FOG_START,1.0f);
glFogf(GL_FOG_END,5.0f);
}
}
{
GLfloat position[] = {0.5f,0.5f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
{
GLfloat mat[3] = {0.1745f,0.01175f,0.01175f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat);
mat[0] =0.61424f;
mat[1] =0.04136f;
mat[2] =0.04136f;
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat);
mat[0] =0.727811f;
mat[1] =0.626959f;
mat[2] =0.626959f;
glMaterialfv(GL_FRONT,GL_SPECULAR,mat);
glMaterialf(GL_FRONT,GL_SHININESS,0.6*128.0);
}
glEnable(GL_FOG);
{
GLfloat fogColor[4] = {0.5f,0.5f,0.5f,1.0f};
fogMode = GL_EXP;
glFogi(GL_FOG_MODE,fogMode);
glFogfv(GL_FOG_COLOR,fogColor);
glFogf(GL_FOG_DENSITY,0.35);
glHint(GL_FOG_HINT,GL_DONT_CARE);
glFogf(GL_FOG_START,1.0f);
glFogf(GL_FOG_END,5.0f);
}
}
2.在OnCreate()函数内调用LightAndFog()进行初始化
代码
int COpenGLDemo20100319View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) ==-1)
return-1;
// TODO: Add your specialized creation code here
InitializeOpenGL();
LightAndFog();
Create2DTextLists();//在屏幕上显示雾的MODE,剽窃了下洞庭散人博客里的代码,再次感谢
return0;
}
{
if (CView::OnCreate(lpCreateStruct) ==-1)
return-1;
// TODO: Add your specialized creation code here
InitializeOpenGL();
LightAndFog();
Create2DTextLists();//在屏幕上显示雾的MODE,剽窃了下洞庭散人博客里的代码,再次感谢
return0;
}
3.贴上函数Create2DTextLists()的代码先
void COpenGLDemo20100319View::Create2DTextLists(void)
{
CFont m_font;
m_font.CreateFont( -24, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_NORMAL, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
DEFAULT_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Algerian");
CFont* m_pOldFont = m_pDC->SelectObject(&m_font);
// create display lists for glyphs 0 through 255 with 0.1 extrusion
// and default deviation. The display list numbering starts at 1000
// (it could be any number)
m_2DTextList = glGenLists(256);
wglUseFontBitmaps(m_pDC->GetSafeHdc(), 0, 255, m_2DTextList);
m_pDC->SelectObject(m_pOldFont);
}
{
CFont m_font;
m_font.CreateFont( -24, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_NORMAL, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
DEFAULT_QUALITY, // Output Quality
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
"Algerian");
CFont* m_pOldFont = m_pDC->SelectObject(&m_font);
// create display lists for glyphs 0 through 255 with 0.1 extrusion
// and default deviation. The display list numbering starts at 1000
// (it could be any number)
m_2DTextList = glGenLists(256);
wglUseFontBitmaps(m_pDC->GetSafeHdc(), 0, 255, m_2DTextList);
m_pDC->SelectObject(m_pOldFont);
}
4.进入RenderScene()进行编写
void COpenGLDemo20100319View::RenderScene(void)
{
glPushMatrix();
glPushMatrix();
// Position The Text On The Screen
glDisable(GL_LIGHTING);
glTranslatef(-1.0,0.0,-2.0f);
glColor3f(1.0f,1.0f,0.0f);
glRasterPos2f(0,0);
glListBase(m_2DTextList);
glCallLists(fogModeString.GetLength(), GL_UNSIGNED_BYTE ,fogModeString);
glEnable(GL_LIGHTING);
glPopMatrix();
glTranslatef(1.0f,0.0f,-2.0f);
RenderSphere(-2.0f,-0.5f,-1.0f);//就是个画球的小函数,下面贴上代码
RenderSphere(-1.0f,-0.5f,-2.0f);
RenderSphere(0.0f,-0.5f,-3.0f);
RenderSphere(1.0f,-0.5f,-4.0f);
RenderSphere(2.0f,-0.5f,-5.0f);
glPopMatrix();
}
{
glPushMatrix();
glPushMatrix();
// Position The Text On The Screen
glDisable(GL_LIGHTING);
glTranslatef(-1.0,0.0,-2.0f);
glColor3f(1.0f,1.0f,0.0f);
glRasterPos2f(0,0);
glListBase(m_2DTextList);
glCallLists(fogModeString.GetLength(), GL_UNSIGNED_BYTE ,fogModeString);
glEnable(GL_LIGHTING);
glPopMatrix();
glTranslatef(1.0f,0.0f,-2.0f);
RenderSphere(-2.0f,-0.5f,-1.0f);//就是个画球的小函数,下面贴上代码
RenderSphere(-1.0f,-0.5f,-2.0f);
RenderSphere(0.0f,-0.5f,-3.0f);
RenderSphere(1.0f,-0.5f,-4.0f);
RenderSphere(2.0f,-0.5f,-5.0f);
glPopMatrix();
}
5.画球
void COpenGLDemo20100319View::RenderSphere(GLfloat x, GLfloat y, GLfloat z)
{
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere(0.4f,16,16);
glPopMatrix();
}
{
glPushMatrix();
glTranslatef(x,y,z);
glutSolidSphere(0.4f,16,16);
glPopMatrix();
}
6.咱来鼠标切换切换模式吧
void COpenGLDemo20100319View::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case'F':
case'f':
if(fogMode == GL_EXP)
{
fogMode = GL_EXP2;
fogModeString.Format("Fog mode is GL_EXP2.");
}
elseif(fogMode == GL_EXP2)
{
fogMode = GL_LINEAR;
fogModeString.Format("Fog mode is GL_LINEAR.");
}
elseif(fogMode == GL_LINEAR)
{
fogMode = GL_EXP;
fogModeString.Format("Fog mode is GL_EXP.");
}
glFogi(GL_FOG_MODE,fogMode);
InvalidateRect(NULL,FALSE);
break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
{
// TODO: Add your message handler code here and/or call default
switch(nChar)
{
case'F':
case'f':
if(fogMode == GL_EXP)
{
fogMode = GL_EXP2;
fogModeString.Format("Fog mode is GL_EXP2.");
}
elseif(fogMode == GL_EXP2)
{
fogMode = GL_LINEAR;
fogModeString.Format("Fog mode is GL_LINEAR.");
}
elseif(fogMode == GL_LINEAR)
{
fogMode = GL_EXP;
fogModeString.Format("Fog mode is GL_EXP.");
}
glFogi(GL_FOG_MODE,fogMode);
InvalidateRect(NULL,FALSE);
break;
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
7.增加的变量
public:
GLint fogMode;
GLuint m_2DTextList;
CString fogModeString;
GLint fogMode;
GLuint m_2DTextList;
CString fogModeString;
F5,我用的是VS2005,一切OK,正常运行。途中没遇到什么阻力,正常运行了。上述是凭我的记忆写的步骤,呵呵,没有去再次验证过。
效果图也来几张吧。