OpenGL Programming for Windows MFC


5.Material and Lighting


5.1 InitGL を以下のように変更

GLint InitGL(void)
{
    glClearColor(0.0f, 0.0f, 0.5f, 1.0f) ;	

    glDepthFunc(GL_LEQUAL) ;
    glEnable(GL_DEPTH_TEST) ;

    glEnable(GL_LIGHTING) ;   // Enable lighting
    glEnable(GL_LIGHT0) ;     // Light0 ON

    return (0) ;
}
InitGL is changed as mentioned above.


5.2 OnCreat を以下のように変更

int CXXXView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
    if (CView::OnCreate(lpCreateStruct) == -1)
        return -1;

    m_pDC = new CClientDC(this) ;             // Get device context
    SetDCPixelFormat(m_pDC->m_hDC) ;          // Set OpenGL pixel format
    m_GLRC = wglCreateContext (m_pDC->m_hDC); // Create rendering context
    wglMakeCurrent (m_pDC->m_hDC, m_GLRC);    // Current context set
    InitGL() ;                                // Initalize OpenGL

    m_pRGBImage = auxDIBImageLoad("tiger.bmp"); // DIB Image load
    //m_pRGBImage = auxDIBImageLoad("cow.bmp");

    MatGL() ;                                  // Material setup

                                               // Texture setup
    TexGL(m_pRGBImage->sizeX, m_pRGBImage->sizeY, m_pRGBImage->data) ;	
	
    return 0;
}
OnCreate is changed as mentioned above.


5.3 MatGL

GLint MatGL()
{
    // Material 

    GLfloat mat_shininess[] = {50.0} ;
    GLfloat mat_specular[]  = {1.0, 1.0, 1.0, 1.0} ;
    GLfloat mat_diffuse[]   = {0.2, 0.2, 0.2, 1.0} ;
    GLfloat mat_ambient[]   = {0.0, 0.0, 0.0, 1.0} ;

    glNewList(1,GL_COMPILE) ;       // Object material 
        glMaterialfv(GL_FRONT, GL_SPECULAR , mat_specular ) ;
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess) ;
        glMaterialfv(GL_FRONT, GL_DIFFUSE  , mat_diffuse  ) ;
        glMaterialfv(GL_FRONT, GL_AMBIENT  , mat_ambient  ) ;
    glEndList() ;

    return(0) ;
}

5.4 DrawGL を以下のように変更

GLint DrawGL(GLfloat rAngle)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;

    glMatrixMode(GL_MODELVIEW) ;
    glLoadIdentity() ;
    glTranslated(0.0, 0.0, -3.0) ;

    glRotatef(rAngle, 0.0f, 1.0f, 0.0f) ;

    glCallList(1) ;    // Object material

    glEnable(GL_TEXTURE_2D ) ;
    glCallList(2) ;    // Texture 

    glEnable(GL_NORMALIZE) ;
    glEnable(GL_AUTO_NORMAL) ;
        auxSolidTeapot(1.0) ;
    glDisable(GL_AUTO_NORMAL) ;
    glDisable(GL_NORMALIZE) ;
    glDisable(GL_TEXTURE_2D ) ;

    return(0) ;
}
DrawGL is changed as mentioned above.

5.5 Build and Execute

ビルド・実行すると、Material/Lighting されたTeapotオブジェクトが回転する。


A Material/Lighting Teapot object rotates.


prev next