OpenGL Programming for Windows MFC


7. Addition of Light

7.1 Light1GL


GLint Light1GL()
{
    // Light1
    GLfloat light1_specular[] = {1.0, 0.0, 0.0, 1.0} ; // Red light
    GLfloat light1_diffuse[]  = {1.0, 0.0, 0.0, 1.0} ;
    GLfloat light1_position[] = {1.0, 1.0, 1.0, 1.0} ; // Positional

    glEnable(GL_LIGHT1) ;     // Light0 ON
	
    glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular) ;
    glLightfv(GL_LIGHT1, GL_DIFFUSE , light1_diffuse ) ;

    glNewList(3,GL_COMPILE) ;
        glLightfv(GL_LIGHT1, GL_POSITION, light1_position) ;
    glEndList() ;

    return (0) ;
}


7.2 OnCreateを以下のように変更

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) ;

    Light1GL() ;                               // Light1 Setup

    return 0;
}

OnCreate is changed as mentioned above.


7.3 DrawGLを以下のように変更

GLint DrawGL(GLfloat m_curquat[4])
{
    GLfloat m[4][4] ;

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) ;

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

    build_rotmatrix(m , m_curquat) ;
    glMultMatrixf(&m[0][0]) ;

    glCallList(1) ;    // Object material

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

    glCallList(3) ;    // Light1 position(rotate with object)

    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.

7.4 Build and Execute



prev next