VISUAL C++/MFC Lecture 2? Update Visual C ++/MFC Graphic Library OpenGL? Frame OpenGL 3D Graphic library coding CLecture1View? OpenGL MFC coding Visual C++ Project Settings Link Tap Opengl32lib, Glu32lib, glauxlib header file member data lecture1viewh
void winglresize(cx, cy) wingldrawscene() public: HGLRC m_hrc; HDC m_hdc; void winglresize(cx, cy); void wingldrawscene();? ^^ Window Style Pixel Format Rendering Context OnCreate() OnCreate() WM_CREATE, lecture1viewh OnCreate()? Class Wizard(View- ClassWizard, or Ctrl+w) Message WM_CREATE Add Function Member function OnCreate() OK
lecture1viewcpp OnCreate() int CLecture1View::OnCreate(LPCREATESTRUCT lpcreatestruct) if (CView::OnCreate(lpCreateStruct) == -1) return -1; int npixelformat; m_hdc = ::GetDC(m_hWnd); 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 ; npixelformat = ChoosePixelFormat(m_hDC, &pfd);
VERIFY(SetPixelFormat(m_hDC, npixelformat, &pfd)); m_hrc = wglcreatecontext(m_hdc); return 0;? WM_DESTROY ClassWizard Message box WM_DESTROY Add function Ok lecture1viewcpp OnDestroy code void Clecutre1View::OnDestroy() CView::OnDestroy(); wgldeletecontext(m_hrc); ::ReleaseDC(m_hWnd, m_hdc); WM_SIZE Message ClassWizard WM_SIZE function OnSize() void CLecture1View::OnSize(UINT ntype, int cx, int cy ) CView::OnSize(nType, cx, cy); x=0; y=10; wglmakecurrent(m_hdc, m_hrc); winglresize(600, 600, x, y); wglmakecurrent(null, NULL); OnDraw CLecture1Viewcpp void CLecture1View::OnDraw(CDC* pdc) CLecture1Doc* pdoc = GetDocument(); ASSERT_VALID(pDoc); wglmakecurrent(m_hdc, m_hrc); wingldrawscene(); SwapBuffers(m_hDC); wglmakecurrent(m_hdc, NULL);
MFC OpenGL OpenGL cider can rendering Rotation, Translation lingting, shading, texture mapping ^^ // lecture1viewh /////////////////////////////////////////////////////////// class CLecture1View : public CView /////////////////////////////////////////////////////////// // privat: double angle; unsigned int texname[4]; GLUquadricObj *CiderObj; float light_position[3]; enum CIDER, EARTH ; int view_mode; public: HGLRC m_hrc; HDC m_hdc; void winglresize(cx, cy); void wingldrawscene(); void winglresize(int w, int h); void winglinitrc(); /////////////////////////////////////////////////////////// lecture1viewcpp void CLecuture1View::winGLInitRC() glclearcolor (00, 00, 00, 10); glcleardepth( 10f); glshademodel (GL_SMOOTH); glenable(gl_depth_test); // //texture mapping //cider can object // // // // // //OpenGL // // Texture setting
// Bitmap image AUX_RGBImageRec * prgbimage; // 4 texture glgentextures (4, texname); ////////////////////////// First texture ///////////////////////////////////// glbindtexture (GL_TEXTURE_2D, texname[0]); // Bitmap (cider_tbmp) prgbimage = auxdibimageloada ( "cider_tbmp" ); // S gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // T gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // Mag gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Min gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Texture glteximage2d (GL_TEXTURE_2D, 0, 3, prgbimage->sizex, prgbimage->sizey, 0, GL_RGB, GL_UNSIGNED_BYTE, prgbimage->data); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// Second texture //////////////////////////////////// // texture glbindtexture (GL_TEXTURE_2D, texname[1]); // Bitmap (cider_bbmp) prgbimage = auxdibimageloada ( "cider_bbmp" ); // S gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // T gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // Mag gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Min gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Texture glteximage2d (GL_TEXTURE_2D, 0, 3, prgbimage->sizex, prgbimage->sizey, 0, GL_RGB, GL_UNSIGNED_BYTE, prgbimage->data); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// Third texture ///////////////////////////////////// // texture glbindtexture (GL_TEXTURE_2D, texname[2]);
// Bitmap (cider_sbmp) prgbimage = auxdibimageloada ( "cider_sbmp" ); // S gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // T gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // Mag gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Min gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Texture glteximage2d (GL_TEXTURE_2D, 0, 3, prgbimage->sizex, prgbimage->sizey, 0, GL_RGB, GL_UNSIGNED_BYTE, prgbimage->data); ////////////////////////////////////////////////////////////////////////////// ////////////////////////// Fourth texture ///////////////////////////////////// // texture glbindtexture (GL_TEXTURE_2D, texname[3]); // Bitmap (earthbmp) prgbimage = auxdibimageloada ( "earthbmp" ); // S gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); // T gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); // Mag gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Min gltexparameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Texture glteximage2d (GL_TEXTURE_2D, 0, 3, prgbimage->sizex, prgbimage->sizey, 0, GL_RGB, GL_UNSIGNED_BYTE, prgbimage->data); ////////////////////////////////////////////////////////////////////////////// // GL_MODULATE gltexenvi (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // CiderObj Quadric object CiderObj = glunewquadric(); CiderObj = glunewquadric (); // Quadric object texture gluquadrictexture (CiderObj, GL_TRUE); // Quadric object normal gluquadricnormals (CiderObj, GLU_SMOOTH); glenable(gl_texture_2d); glenable(gl_lighting); glenable(gl_light0);
void CLecture1View::winGLResize(int w, int h) glviewport (0, 0, (GLsizei) w, (GLsizei) h); glmatrixmode (GL_PROJECTION); glloadidentity (); gluperspective( 500f, (GLdouble) w/h, 10f, 1500f ); glmatrixmode (GL_MODELVIEW); glloadidentity(); glulookat( 50, 50, 50, 0, 0, 0, 0, 0, 1); WM_KEYDOWN WM_TIMER ClassWizard CLecture1Viewcpp void CLecture1View::OnTimer(UINT nidevent) // TODO: Add your message handler code here and/or call default Invalidate(FALSE); // // CView::OnTimer(nIDEvent); //
CLecture1View::OnCreate() SetTimer() 50msec wglmakecurrent(m_hdc, m_hrc); winglinitrc(); wglmakecurrent(null,null); SetTimer(1, 50, NULL); // return 0; ClassWizard? WM_KEYDOWN keyboard m view_mode void CLecture1View::OnKeyDown(UINT nchar, UINT nrepcnt, UINT nflags) // TODO: Add your message handler code here and/or call default if(nchar = 'm') if(view_mode == CIDER) view_mode = EARTH; // else if(view_mode == EARTH) view_mode = CIDER; // // CView::OnKeyDown(nChar, nrepcnt, nflags);? Compile
?, file \Lecture1\ directory gks directory \debug\ Lecture1exe file \debug\ keyboard m
? C++ soruce CLecture1View CcideView, CLecture1Doc CCideDoc Good Bye!! ^^// Reference :, Visual C++ 2,, 1998,, OpenGL, 1999 : ( )