OpenGL Study 4

Lessson06

为立方体贴图,使用纹理

bool  LoadGLBitmap(LPTSTR szFileName, GLuint  & texid)                     //  Creates Texture From A Bitmap File
{
    HBITMAP hBMP;                                                        
//  Handle Of The Bitmap
    BITMAP    BMP;                                                         //  Bitmap Structure

    glGenTextures(
1 & texid);                                             //  Create The Texture
    hBMP = (HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP,  0 0 , LR_CREATEDIBSECTION  |  LR_LOADFROMFILE );

    
if  ( ! hBMP)                                                             //  Does The Bitmap Exist?
         return   false ;                                                     //  If Not Return False

    GetObject(hBMP, 
sizeof (BMP),  & BMP);                                     //  Get The Object
                                                                        
//  hBMP:        Handle To Graphics Object
                                                                        
//  sizeof(BMP): Size Of Buffer For Object Information
                                                                        
//  &BMP:        Buffer For Object Information

    glPixelStorei(GL_UNPACK_ALIGNMENT, 
4 );                                 //  Pixel Storage Mode (Word Alignment / 4 Bytes)

    
//  Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texid);                                 //  Bind To The Texture ID
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);     //  Linear Min Filter
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);     //  Linear Mag Filter
    glTexImage2D(GL_TEXTURE_2D,  0 3 , BMP.bmWidth, BMP.bmHeight,  0 , GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);

    DeleteObject(hBMP);                                                    
//  Delete The Object

    
return   true ;                                                         //  Loading Was Successful
}


// Resize and initialize the GL window
GLvoid ReSizeGLScene(GLsizei width,GLsizei height)    
{
    
// Prevent divide by zero
     if (height == 0 )        
    {
        height
= 1 ;
    }

    
// reset the current viewport
    glViewport( 0 , 0 ,width,height);

    
// select the projection matrix
    glMatrixMode(GL_PROJECTION);
    
// reset the projection matrix
    glLoadIdentity();

    gluPerspective(
45.0 ,(GLdouble)width / (GLdouble)height, 0.1 , 100.0 );

    
// select the model view 
    glMatrixMode(GL_MODELVIEW);
    
// reset the model view
    glLoadIdentity();
}

// all setup for OpenGL goes here
bool  InitGL(GLvoid)
{
    
if ( ! LoadGLBitmap(_T( " Data/NeHe.bmp " ),texture[ 0 ]))
        
return   false ;

    glEnable(GL_TEXTURE_2D);
    
// enable smooth shading
    glShadeModel(GL_SMOOTH);
    
    
// black background
    glClearColor( 0.0f , 0.0f , 0.0f , 0.5f );
    
    
// setup the depth buffer
    glClearDepth( 1.0f );
    
// enable depth testing
    glEnable(GL_DEPTH_TEST);
    
// the type of depth test to do
    glDepthFunc(GL_LEQUAL);

    
// really nice perspective calculation
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

    
return   true ;
}

// here we do all the drawings
bool  DrawGLScene(GLvoid)
{
    
// clear the screen and the depth buffer
    glClear(GL_COLOR_BUFFER_BIT  |  GL_DEPTH_BUFFER_BIT);
    
// reset the current modelview matrix
    glLoadIdentity();
    glTranslatef(
0.0f , 0.0f , - 5.0f );

    glRotatef(xrot,
1.0f , 0.0f , 0.0f );
    glRotatef(yrot,
0.0f , 1.0f , 0.0f );
    glRotatef(zrot,
0.0f , 0.0f , 1.0f );

    glBindTexture(GL_TEXTURE_2D,texture[
0 ]);

    glBegin(GL_QUADS);
        
//  Front Face
        glTexCoord2f( 0.0f 0.0f ); glVertex3f( - 1.0f - 1.0f ,   1.0f );     //  Bottom Left Of The Texture and Quad
        glTexCoord2f( 1.0f 0.0f ); glVertex3f(  1.0f - 1.0f ,   1.0f );     //  Bottom Right Of The Texture and Quad
        glTexCoord2f( 1.0f 1.0f ); glVertex3f(  1.0f ,   1.0f ,   1.0f );     //  Top Right Of The Texture and Quad
        glTexCoord2f( 0.0f 1.0f ); glVertex3f( - 1.0f ,   1.0f ,   1.0f );     //  Top Left Of The Texture and Quad
        
//  Back Face
        glTexCoord2f( 1.0f 0.0f ); glVertex3f( - 1.0f - 1.0f - 1.0f );     //  Bottom Right Of The Texture and Quad
        glTexCoord2f( 1.0f 1.0f ); glVertex3f( - 1.0f ,   1.0f - 1.0f );     //  Top Right Of The Texture and Quad
        glTexCoord2f( 0.0f 1.0f ); glVertex3f(  1.0f ,   1.0f - 1.0f );     //  Top Left Of The Texture and Quad
        glTexCoord2f( 0.0f 0.0f ); glVertex3f(  1.0f - 1.0f - 1.0f );     //  Bottom Left Of The Texture and Quad
        
//  Top Face
        glTexCoord2f( 0.0f 1.0f ); glVertex3f( - 1.0f ,   1.0f - 1.0f );     //  Top Left Of The Texture and Quad
        glTexCoord2f( 0.0f 0.0f ); glVertex3f( - 1.0f ,   1.0f ,   1.0f );     //  Bottom Left Of The Texture and Quad
        glTexCoord2f( 1.0f 0.0f ); glVertex3f(  1.0f ,   1.0f ,   1.0f );     //  Bottom Right Of The Texture and Quad
        glTexCoord2f( 1.0f 1.0f ); glVertex3f(  1.0f ,   1.0f - 1.0f );     //  Top Right Of The Texture and Quad
        
//  Bottom Face
        glTexCoord2f( 1.0f 1.0f ); glVertex3f( - 1.0f - 1.0f - 1.0f );     //  Top Right Of The Texture and Quad
        glTexCoord2f( 0.0f 1.0f ); glVertex3f(  1.0f - 1.0f - 1.0f );     //  Top Left Of The Texture and Quad
        glTexCoord2f( 0.0f 0.0f ); glVertex3f(  1.0f - 1.0f ,   1.0f );     //  Bottom Left Of The Texture and Quad
        glTexCoord2f( 1.0f 0.0f ); glVertex3f( - 1.0f - 1.0f ,   1.0f );     //  Bottom Right Of The Texture and Quad
        
//  Right face
        glTexCoord2f( 1.0f 0.0f ); glVertex3f(  1.0f - 1.0f - 1.0f );     //  Bottom Right Of The Texture and Quad
        glTexCoord2f( 1.0f 1.0f ); glVertex3f(  1.0f ,   1.0f - 1.0f );     //  Top Right Of The Texture and Quad
        glTexCoord2f( 0.0f 1.0f ); glVertex3f(  1.0f ,   1.0f ,   1.0f );     //  Top Left Of The Texture and Quad
        glTexCoord2f( 0.0f 0.0f ); glVertex3f(  1.0f - 1.0f ,   1.0f );     //  Bottom Left Of The Texture and Quad
        
//  Left Face
        glTexCoord2f( 0.0f 0.0f ); glVertex3f( - 1.0f - 1.0f - 1.0f );     //  Bottom Left Of The Texture and Quad
        glTexCoord2f( 1.0f 0.0f ); glVertex3f( - 1.0f - 1.0f ,   1.0f );     //  Bottom Right Of The Texture and Quad
        glTexCoord2f( 1.0f 1.0f ); glVertex3f( - 1.0f ,   1.0f ,   1.0f );     //  Top Right Of The Texture and Quad
        glTexCoord2f( 0.0f 1.0f ); glVertex3f( - 1.0f ,   1.0f - 1.0f );     //  Top Left Of The Texture and Quad
    glEnd();

    xrot
+= 0.3f ;
    yrot
+= 0.2f ;
    zrot
+= 0.4f ;

    
return   true ;
}

 使用windows中LoadImage()函数读取bmp文件,并且用于纹理的bmp文件的width,height都必须是2的幂,最小64象素,最大256象素

使用纹理时,现要使用glGenTexture()产生纹理名,然后用glBindTexture()将纹理绑定,用glTexImage2D()将读取的bmp数据绑定到纹理,最后使用glTexParameteri()处理当纹理太大或太小的情况

应用纹理时,先glBindTexture()绑定纹理,对于每个面上的顶点用glTexCoord2f()设定纹理坐标

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值