OpenGL入门笔记(十三)

来源:互联网 发布:2017年9月非农数据 编辑:程序博客网 时间:2024/05/17 23:02
int COpenGLDemoView::DrawGLScene()                                   
{
// Here's Where We Do All The Drawing
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    // Clear The Screen And The Depth Buffer
    glLoadIdentity();                                    // Reset The View
    glTranslatef(1.1f*float(cos(rot/16.0f)),0.8f*float(sin(rot/20.0f)),-3.0f);
    glRotatef(rot,
1.0f,0.0f,0.0f);                        // Rotate On The X Axis
    glRotatef(rot*1.2f,0.0f,1.0f,0.0f);                    // Rotate On The Y Axis
    glRotatef(rot*1.4f,0.0f,0.0f,1.0f);                    // Rotate On The Z Axis
    glTranslatef(-0.35f,-0.35f,0.1f);                    // Center On X, Y, Z Axis
    glPrint("N");                                        // Draw A Skull And Crossbones Symbol
    return TRUE;    
}

int COpenGLDemoView::LoadGLTextures()                                    
{
//加载位图并转换为纹理
    int Status=FALSE;                                    // Status Indicator

    AUX_RGBImageRec 
*TextureImage[1];                    // Create Storage Space For The Texture

    memset(TextureImage,
0,sizeof(void *)*1);               // Set The Pointer To NULL

    
// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    if (TextureImage[0]=LoadBMP("Data/Lights.bmp"))
    {
        Status
=TRUE;                                    // Set The Status To TRUE

        glGenTextures(
1&texture[0]);                    // Create The Texture

        glBindTexture(GL_TEXTURE_2D, texture[
0]);
        gluBuild2DMipmaps(GL_TEXTURE_2D, 
3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);

        glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );
        glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP );

        glEnable(GL_TEXTURE_GEN_S);
        glEnable(GL_TEXTURE_GEN_T);
    }

    
if (TextureImage[0])                                    // If Texture Exists
    {
        
if (TextureImage[0]->data)                            // If Texture Image Exists
        {
            free(TextureImage[
0]->data);                    // Free The Texture Image Memory
        }

        free(TextureImage[
0]);                                // Free The Image Structure
    }

    
return Status;                                        // Return The Status
}

GLvoid COpenGLDemoView::BuildFont(GLvoid)
{
    GLYPHMETRICSFLOAT    gmf[
256];                        // Address Buffer For Font Storage
    HFONT    font;                                        // Windows Font ID

    
base = glGenLists(256);                                // Storage For 256 Characters

    font 
= CreateFont(    -12,                            // Height Of Font
                        0,                                // Width Of Font
                        0,                                // Angle Of Escapement
                        0,                                // Orientation Angle
                        FW_BOLD,                        // Font Weight
                        FALSE,                            // Italic
                        FALSE,                            // Underline
                        FALSE,                            // Strikeout
                        SYMBOL_CHARSET,                    // Character Set Identifier
                        OUT_TT_PRECIS,                    // Output Precision
                        CLIP_DEFAULT_PRECIS,            // Clipping Precision
                        ANTIALIASED_QUALITY,            // Output Quality
                        FF_DONTCARE|DEFAULT_PITCH,        // Family And Pitch
                        "Wingdings");                    // Font Name
    

    HDC hDC 
= ::GetDC(this->m_hWnd);

    SelectObject(hDC, font);                            
// Selects The Font We Created

    wglUseFontOutlines(    hDC,                            
// Select The Current DC
                        0,                                // Starting Character
                        255,                            // Number Of Display Lists To Build
                        base,                            // Starting Display Lists
                        0.1f,                            // Deviation From The True Outlines
                        0.2f,                            // Font Thickness In The Z Direction
                        WGL_FONT_POLYGONS,                // Use Polygons, Not Lines
                        gmf);                            // Address Of Buffer To Recieve Data
}


GLvoid COpenGLDemoView::glPrint(
const char *text)                    // Custom GL "Print" Routine
{
 
if (text == NULL)                                        // If There's No Text
    return;                                                // Do Nothing

  glPushAttrib(GL_LIST_BIT);                            
// Pushes The Display List Bits
    glListBase(base);                                    // Sets The Base Character to 32
    glCallLists(strlen(text), GL_UNSIGNED_BYTE, text);    // Draws The Display List Text
  glPopAttrib();                                        // Pops The Display List Bits                                    // Pops The Display List Bits
}

LoadGLTextures中的几行代码将为我们绘制在屏幕上的任何物体自动生成纹理坐标。GL_SGL_T是纹理坐标。默认状态下,glTexGen被设置为提取物体此刻在屏幕上的x坐标和y坐标,并把它们转换为顶点坐标。你会发现到物体在z平面没有纹理,只显示一些斑纹。正面和反面都被赋予了纹理,这些都是由glTexGen函数产生的。(X(GL_S)用于从左到右映射纹理, Y(GL_T)用于从上到下映射纹理。

GL_TEXTURE_GEN_MODE允许我们选择我们想在ST纹理坐标上使用的纹理映射模式。你有3种选择:

GL_EYE_LINEAR - 纹理会固定在屏幕上。它永远不会移动。物体将被赋予处于它通过的地区的那一块纹理。

GL_OBJECT_LINEAR - 这种就是我们使用的模式。纹理被固定于在屏幕上运动的物体上。
GL_SPHERE_MAP -
每个人都喜欢。创建一种有金属质感的物体。

200782702.jpg