COpenGLDC,此类和CDC类相似,暂时添加画点函数和画线函数,也可以添加其它函数
class COpenGLDC : public CObject
{
public:
COpenGLDC(HWND hWnd);
virtual ~COpenGLDC();
private:
HWND m_hWnd;
HGLRC m_hRC;
HDC m_hDC;
public:
GCamera m_Camera;
CVector3D m_vecLight;
COLORREF m_mat_clr;
COLORREF m_bk_clr;
public:
//initialize
BOOL InitDC();
void GLResize(int cx,int cy);
void GLSetupRC();
void Ready();//开始取景
void Finish();//强制执行且交换缓存
void Lighting(bool bLighting);
BOOL IsLighting();
void SetLightDirection(float *l_direction);
//指定背景色及物体颜色
void SetMaterialColor(COLORREF clr);
void SetBkColor(COLORREF clr);
void GetBkColor(COLORREF &clr);
void ClearBkground();
void SetColor(COLORREF clr);
void DrawLine(const CPoint3D &p_Begin, const CPoint3D &p_End );//画线
void DrawPoint(const CPoint3D &p);//画点
void DrawElement(ELEMENT *ele);//画八面体单元
};
实现如下
COpenGLDC::COpenGLDC(HWND hWnd):m_hWnd(hWnd)
{
}
COpenGLDC::~COpenGLDC()
{
}
BOOL COpenGLDC::InitDC()
{
if (m_hWnd == NULL) return FALSE;
m_Camera.init();
m_hDC = ::GetDC(m_hWnd); // Get the Device context
PIXELFORMATDESCRIPTOR pfdWnd =
{
sizeof(PIXELFORMATDESCRIPTOR), // Structure size.
1, // Structure version number.
PFD_DRAW_TO_WINDOW | // Property flags.
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24, // 24-bit color.
0, 0, 0, 0, 0, 0, // Not concerned with these.
0, 0, 0, 0, 0, 0, 0, // No alpha or accum buffer.
32, // 32-bit depth buffer.
0, 0, // No stencil or aux buffer.
PFD_MAIN_PLANE, // Main layer type.
0, // Reserved.
0, 0, 0 // Unsupported.
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_hDC, &pfdWnd)) == 0 )
{
AfxMessageBox("ChoosePixelFormat to wnd failed");
return FALSE;
}
if (SetPixelFormat(m_hDC, pixelformat, &pfdWnd) == FALSE)
AfxMessageBox("SetPixelFormat failed");
m_hRC=wglCreateContext(m_hDC);
VERIFY(wglMakeCurrent(m_hDC,m_hRC));
GLSetupRC();
wglMakeCurrent(NULL,NULL);
return m_hRC!=0;
}
void COpenGLDC::GLResize(int w,int h)
{
wglMakeCurrent(m_hDC,m_hRC);
// Prevent a divide by zero
if(h == 0) h = 1;
if(w == 0) w = 1;
m_Camera.set_screen(w,h);
wglMakeCurrent(NULL,NULL);
}
void COpenGLDC::GLSetupRC()
{
glEnable(GL_DEPTH_TEST); // Hidden surface removal
glEnable(GL_COLOR_MATERIAL);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); //background color
// default color
GLfloat lightpos[] = {0.0, 0.0, 1000.0, 1.0};
GLfloat specular[] = {0.4, 0.6, 0.8, 1.0};
GLfloat matspecular[] = {1.0, 1.0, 1.0, 1.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glMaterialfv(GL_FRONT, GL_SPECULAR, matspecular);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
glColor3ub(0, 0, 255);
/* glPolygonMode(GL_FRONT,GL_LINE);
glPolygonMode(GL_BACK,GL_LINE);
glShadeModel(GL_FLAT);
glEnable(GL_NORMALIZE);
// 设置光照及材质的属性
GLfloat ambientProperties[] = {0.f, 0.f, 0.f, 1.0f};
GLfloat diffuseProperties[] = {0.f, 0.f, 0.f, 1.0f};
GLfloat specularProperties[] = {0.f, 0.f, 0.f, 1.0f};
glClearDepth( 1.0f );
glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
glLightfv( GL_LIGHT0, GL_SPECULAR, specularProperties);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 1.0f);
// 缺省情况下加光照
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
*/
}
void COpenGLDC::Ready()
{
wglMakeCurrent(m_hDC,m_hRC);
m_Camera.projection();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void COpenGLDC::Finish()
{
glFlush();
SwapBuffers(m_hDC);
wglMakeCurrent(NULL,NULL);
}
void COpenGLDC::Lighting(bool bLighting)//控制光照
{
if (bLighting)
{
glEnable(GL_LIGHTING);
}
else
{
glDisable(GL_LIGHTING);
}
}
BOOL COpenGLDC::IsLighting()//判断光照状态
{
GLboolean bLighting ;
glGetBooleanv(GL_LIGHTING, &bLighting);
return bLighting;
}
void SetLightDirection(float *l_direction)//四个值,齐次坐标
{
// CVector3D m_vecLight = CVector3D(l_direction);//自动调用构造函数
// glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, l_direction);
}
void COpenGLDC::SetMaterialColor(COLORREF clr)
{
m_mat_clr = clr;
GLclampf r, g, b;
r = GetRValue(clr);
g = GetGValue(clr);
b = GetBValue(clr);
GLfloat mat_a_d[] = {(GLfloat)r/255, (GLfloat)g/255, (GLfloat)b/255};
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mat_a_d);
}
void COpenGLDC::SetBkColor(COLORREF clr)
{
m_bk_clr = clr;
}
void COpenGLDC::GetBkColor(COLORREF &clr)
{
clr = m_bk_clr;
}
void COpenGLDC::ClearBkground()
{
GLclampf r, g, b;
r = (GLclampf)GetRValue(m_bk_clr)/255;
g = (GLclampf)GetGValue(m_bk_clr)/255;
b = (GLclampf)GetBValue(m_bk_clr)/255;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(r, g, b, 1.0);
}
void COpenGLDC::SetColor(COLORREF clr)
{
glColor3ub(GetRValue(clr), GetGValue(clr), GetBValue(clr));
}
void COpenGLDC::DrawLine(const CPoint3D &p_Begin, const CPoint3D &p_End )
{
glBegin(GL_LINES);
glVertex3f(p_Begin.x, p_Begin.y, p_Begin.z);
glVertex3f(p_End.x, p_End.y, p_End.z);
glEnd();
}
void COpenGLDC::DrawPoint(const CPoint3D &p)
{
glBegin(GL_POINTS);
glVertex3f(p.x, p.y, p.z);
glEnd();
}
void COpenGLDC::DrawElement(ELEMENT *ele)
{
glBegin(GL_QUADS);
glVertex3f(ele->Num[1], ele->Num[2], ele->Num[3]);
glVertex3f(ele->Num[4], ele->Num[5], ele->Num[6]);
glVertex3f(ele->Num[7], ele->Num[8], ele->Num[9]);
glVertex3f(ele->Num[10], ele->Num[11], ele->Num[12]);
glEnd();
glBegin(GL_QUADS);
glVertex3f(ele->Num[13], ele->Num[14], ele->Num[15]);
glVertex3f(ele->Num[16], ele->Num[17], ele->Num[18]);
glVertex3f(ele->Num[19], ele->Num[20], ele->Num[21]);
glVertex3f(ele->Num[22], ele->Num[23], ele->Num[24]);
glEnd();
glBegin(GL_QUADS);
glVertex3f(ele->Num[13], ele->Num[14], ele->Num[15]);
glVertex3f(ele->Num[16], ele->Num[17], ele->Num[18]);
glVertex3f(ele->Num[4], ele->Num[5], ele->Num[6]);
glVertex3f(ele->Num[1], ele->Num[2], ele->Num[3]);
glEnd();
glBegin(GL_QUADS);
glVertex3f(ele->Num[13], ele->Num[14], ele->Num[15]);
glVertex3f(ele->Num[1], ele->Num[2], ele->Num[3]);
glVertex3f(ele->Num[10], ele->Num[11], ele->Num[12]);
glVertex3f(ele->Num[22], ele->Num[23], ele->Num[24]);
glEnd();
glBegin(GL_QUADS);
glVertex3f(ele->Num[22], ele->Num[23], ele->Num[24]);
glVertex3f(ele->Num[19], ele->Num[20], ele->Num[21]);
glVertex3f(ele->Num[7], ele->Num[8], ele->Num[9]);
glVertex3f(ele->Num[10], ele->Num[11], ele->Num[12]);
glEnd();
glBegin(GL_QUADS);
glVertex3f(ele->Num[16], ele->Num[17], ele->Num[18]);
glVertex3f(ele->Num[4], ele->Num[5], ele->Num[6]);
glVertex3f(ele->Num[7], ele->Num[8], ele->Num[9]);
glVertex3f(ele->Num[19], ele->Num[20], ele->Num[21]);
glEnd();
}