OpenGL入门笔记(十三)

简介:
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_S和GL_T是纹理坐标。默认状态下,glTexGen被设置为提取物体此刻在屏幕上的x坐标和y坐标,并把它们转换为顶点坐标。你会发现到物体在z平面没有纹理,只显示一些斑纹。正面和反面都被赋予了纹理,这些都是由glTexGen函数产生的。(X(GL_S)用于从左到右映射纹理, Y(GL_T)用于从上到下映射纹理。

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

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

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

200782702.jpg


本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2007/08/27/871439.html,如需转载请自行联系原作者
目录
相关文章
|
vr&ar Android开发 C++
Android OpenGL入门
Android OpenGL入门
Android OpenGL入门
|
存储 缓存 安全
OpenGL ES 入门:GLKit加载图片
OpenGL ES 入门:GLKit加载图片
149 0
OpenGL ES 入门:GLKit加载图片
|
存储 缓存
案例 02、OpenGL入门--正方形键位控制
OpenGL入门--正方形键位控制
128 0
案例 02、OpenGL入门--正方形键位控制
|
缓存 容器
案例 01、OpenGL入门--绘制三角形
OpenGL中三角形的绘制,就类似于学习编程时的Hello world,是一个入门级的使用,重点在于理解图形是如何绘制的
227 0
案例 01、OpenGL入门--绘制三角形
|
C++ 异构计算 Python
OpenGL渲染入门
## 前言 在开始之前,先来看一段图像解码序列(格式为YUV420)的4个渲染结果,这里我分别截了4张图 ![image.png](https://ata2-img.cn-hangzhou.oss-pub.aliyun-inc.com/dca46d1be7dfee07981a7dea14ae3aa1.png) 其中4个渲染效果分别是 左上:直接渲染视频帧并绘制到窗口上 右上:
1753 0
|
API
OpenGL ES 入门API大全
本文章正确使用姿势:command/Ctrl + f  进行搜索对应的功能代码,找到它的详细解释。 (以下内容如有偏差,欢迎进行指正) 一.CAEAGLLayer 的使用: CAEAGLLayer 官方解释如图CAEAGLLayer,我来简单翻译一下: CAEAGLLayer 是继承于CALayer 的,可以用它在iOS 和tvOS 的设备上使用。
1434 0