Road to OpenGL

  1. Setting up OpenGL with Visual C++

    1. Download the OpenGL library, unzip it, then
    2. put "glut32.dll" to "C:\Windows\System"
    3. put "glut.h" to "\Microsoft Visual Studio\VC98\Include\GL"
    4. put "glut32.lib" to "\Microsoft Visual Studio\VC98\Lib"
  2. Test in a console application

    (Copy and paste the code to your project, run it, you will see a teapot. Download here simple.zip | simple.exe )
    #include          // Header
    
    void display(void)
    {
    	// clear window
    	glClear(GL_COLOR_BUFFER_BIT); 
    
    	//	draw something
    	glutWireTeapot(0.5);
    
    	// flush GL buffers 
    	glFlush(); 
    }
    
    void init()
    {
    	//  set background color (R,G,B,A)
    	glClearColor (0.0, 0.0, 0.2, 0.0);
    
    	// set drawing color (R,G,B) 
    	glColor3f(1.0, 1.0, 0.);
    
    	// set up standard orthogonal view with clipping 
    	// box as cube of side 2 centered at origin
    	// This is default view and these statement could be removed
    
    	glMatrixMode (GL_PROJECTION);
    	glLoadIdentity ();
    }
    
    void main(int argc, char** argv)
    {
    	glutInit(&argc,argv); 
    	glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
    	glutInitWindowSize(300,300);	// Size of the window
    	glutInitWindowPosition(0,0);	// Position to the screen
    	glutCreateWindow("Simple");		// Name of the windows
    	glutDisplayFunc(display);
    	init();
    	glutMainLoop();
    }
    
  3. Test in MFC application

    Here is a single-document interface with a normal menu, a toolbar and an indicator. To openGL, you have to initialize first. (Download here vcgl.zip | vcgl.exe)

    int CVCGLView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    	if (CView::OnCreate(lpCreateStruct) == -1)
    		return -1;
    	
    	if(IniOpenGL(this) == -1)
    		return -1;
    
    	DrawScene();
    
    	return 0;
    }
    
    int GLApp::IniOpenGL(CView* pView)
    {
    	PIXELFORMATDESCRIPTOR pfd =	// structure describes the pixel format of a drawing surface.
    	{
    		sizeof(PIXELFORMATDESCRIPTOR),
    		1,			// Version
    		PFD_DRAW_TO_WINDOW |	// Supports GDI
    		PFD_SUPPORT_OPENGL |	// Supports OpenGL
    		PFD_DOUBLEBUFFER,	// Use double buffering (more efficient drawing)
    		PFD_TYPE_RGBA,		// No pallettes
    		24, 			// Number of color planes in each color buffer
    		24,	0,		// for Red-component
    		24,	0,		// for Green-component
    		24,	0,		// for Blue-component
    		24,	0,		// for Alpha-component
    		0,			// Number of planes of Accumulation buffer
    		0,			// for Red-component
    		0,			// for Green-component
    		0,			// for Blue-component
    		0,			// for Alpha-component
    		32, 		// Depth of Z-buffer
    		0,			// Depth of Stencil-buffer
    		0,			// Depth of Auxiliary-buffer
    		0,			// Now is ignored
    		0,			// Number of planes
    		0,			// Now is ignored
    		0,			// Color of transparent mask
    		0			// Now is ignored
    	};
    
    	// Get current Windows context
    	m_hdc = ::GetDC(pView->GetSafeHwnd());
    
    	// Ask to find the nearest compatible pixel-format
    	int iD = ChoosePixelFormat(m_hdc, &pfd);
    	if ( !iD )
    	{
    		AfxMessageBox("ChoosePixelFormat::Error");
    		return -1;
    	}
    
    	// Try to set this format
    	if ( !SetPixelFormat (m_hdc, iD, &pfd) )
    	{
    		AfxMessageBox("SetPixelFormat::Error");
    		return -1;
    	}
    
    	//====== Try to create the OpenGL rendering context
    	if ( !(m_hRC = wglCreateContext (m_hdc)))
    	{
    		AfxMessageBox("wglCreateContext::Error");
    		return -1;
    	}
    
    	//====== Try to put it in action
    	if ( !wglMakeCurrent (m_hdc, m_hRC))
    	{
    		AfxMessageBox("wglMakeCurrent::Error");
    		return -1;
    	}
    	return 0;
    }
    
  4. Trackball makes rotation easily

    When you look at a 3D object, you may want to rotate it so that you can see it from any specific angle. Drag it!

    "Drag" means press and hold the left mouse button as you move the mouse across the screen. Response to the drag, the code starts from here.
    (Click to download exe-file to play the trackball)

    • Step 1: Update mouse whenever you press the right button.
      void CVCGLView::OnLButtonDown(UINT nFlags, CPoint point) 
      {
      	m_currMouse = point;
      	m_lastMouse = point;	// Update
      	
      	CView::OnLButtonDown(nFlags, point);
      }
      
    • Step 2: Make a continue action.
      void CVCGLView::OnMouseMove(UINT nFlags, CPoint point) 
      {
      	if ( (nFlags & MK_LBUTTON) == MK_LBUTTON )
      	{
      		m_lastMouse = m_currMouse;
      		m_currMouse = point;
      
      		lookAtScene();
      		RenderScene();
      	}
      
      	COpenGLView::OnMouseMove(nFlags, point);
      }
      
    • Step 3: Trackball processing.
      void GLApp::RenderScene()
      {	
      //------------------  trackball  ----------------------------
      	float x1 = 2. * m_lastMouse.x / m_clientWindow.cx - 1.; 
      	float y1 = 1. - 2. * m_lastMouse.y / m_clientWindow.cy; 
      	float x2 = 2. * m_currMouse.x / m_clientWindow.cx - 1.; 
      	float y2 = 1. - 2. * m_currMouse.y / m_clientWindow.cy; 
      
      	TrackBall trackball(x1, y1, x2, y2);
      	m_trackBall.add_quats(trackball.return_quats());
      //-----------------------------------------------------------
      	::glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      
      	::glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
      
      	glPushMatrix();
      		glMultMatrixf(m_trackBall.return_matrix());
      		drawTeapot();
      	glPopMatrix();
      
      	::glFlush();
      	SwapBuffers(m_hdc);
      }
      
  5. Lights and Materials

    • Step 1: Light source must be enabled individually. After that color assigned by glColor are no longer used.
      	if (lightingEnabled) 
      	{
      		glEnable(GL_LIGHTING);
      		glEnable(GL_LIGHT0);
      	} else 
      	{
      		glDisable(GL_LIGHTING);
      		glDisable(GL_LIGHT0);
      	}
      
    • Step 2:
  6. View

    • glOrtho(left, right, bottom, top, zNear, zFar);
    • glFrustum(left, right, bottom, top, zNear, zFar);
    • gluPerspective(fovy, aspect, zNear, zFar)