注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

辫子

这里的东东都转到了:hi.baidu.com/fairzy

 
 
 

日志

 
 

GL学习笔记(3) [转]  

2008-07-31 17:44:48|  分类: OpenGL |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

2007-08-31 18:05

#include "animation.h"

//默认构造函数,初始化数据

Animation::Animation()

{

    r = 0;

    MX = 1024;        //场景x

    MY = 1024;        //场景y

    MZ = 1024;        //场景z

    oEye[0] = 0;    //观察点x

    oEye[1] = 10;    //观察点y

    oEye[2] = 100;    //观察点z

    oLtn[0] = 0;    //视点坐标x

    oLtn[1] = 0;    //视点坐标y

    oLtn[2] = 0;    //视点坐标z

    oAngle = 0;    //视点角度

    oEve    = 0;    //视点距离

   

    CreateTexture("C:\\Images\\FRONT.BMP",    oTexture[0]);

    CreateTexture("C:\\Images\\LEFT.BMP",    oTexture[1]);

    CreateTexture("C:\\Images\\RBACK.BMP",    oTexture[2]);

    CreateTexture("C:\\Images\\RIGHT.BMP",    oTexture[3]);

    CreateTexture("C:\\Images\\TOP.BMP",    oTexture[4]);

    CreateTexture("C:\\Images\\1.bmp",        oTexture[5]);

    CreateTexture("C:\\Images\\kkun.bmp",    oTexture[6]);

    CreateTexture("C:\\Images\\128.bmp",    oTexture[7]);   

    glEnable(GL_TEXTURE_2D);

}

//绘制天空盒

void Animation::DrawSky()

{

    SelectTexture(oTexture[0]);

    glBegin(GL_QUADS);

        //前面

        glTexCoord2f(0,0);        glVertex3f(-MX,0,-MZ);

        glTexCoord2f(1,0);        glVertex3f( MX,0,-MZ);

        glTexCoord2f(1,1);        glVertex3f( MX, MY/2,-MZ);

        glTexCoord2f(0,1);        glVertex3f(-MX, MY/2,-MZ);

    glEnd();

    SelectTexture(oTexture[1]);

    glBegin(GL_QUADS);

        //左面

        glTexCoord2f(0,0);        glVertex3f(-MX,    0, MZ);

        glTexCoord2f(1,0);        glVertex3f(-MX,    0,-MZ);

        glTexCoord2f(1,1);        glVertex3f(-MX,    MY/2, -MZ);

        glTexCoord2f(0,1);        glVertex3f(-MX, MY/2, MZ);

    glEnd();

    SelectTexture(oTexture[2]);

    glBegin(GL_QUADS);

        //后面

        glTexCoord2f(0,0);        glVertex3f( MX,    0, MZ);

        glTexCoord2f(1,0);        glVertex3f(-MX, 0, MZ);

        glTexCoord2f(1,1);        glVertex3f(-MX,    MY/2, MZ);

        glTexCoord2f(0,1);        glVertex3f( MX, MY/2, MZ);

    glEnd();

    SelectTexture(oTexture[3]);

    glBegin(GL_QUADS);

        //右面

        glTexCoord2f(0,0);        glVertex3f( MX,    0, -MZ);

        glTexCoord2f(1,0);        glVertex3f( MX,    0, MZ);

        glTexCoord2f(1,1);        glVertex3f( MX,    MY/2, MZ);

        glTexCoord2f(0,1);        glVertex3f( MX, MY/2, -MZ);

    glEnd();

    SelectTexture(oTexture[4]);

    glBegin(GL_QUADS);

        //顶面

        glTexCoord2f(0,0);        glVertex3f(-MX,    MY/2, -MZ);

        glTexCoord2f(1,0);        glVertex3f(-MX,    MY/2, MZ);

        glTexCoord2f(1,1);        glVertex3f( MX,    MY/2, MZ);

        glTexCoord2f(0,1);        glVertex3f( MX, MY/2, -MZ);

    glEnd();

}

//渲染窗口

void Animation::RenderWindow()

{

    this->DrawSky();

    SelectTextureRepeat(oTexture[5]);

    glBegin(GL_QUADS);           

        //地面       

        glTexCoord2f(0,0);        glVertex3f(-MX/16,0,-MZ/2);

        glTexCoord2f(2,0);        glVertex3f( MX/16,0,-MZ/2);

        glTexCoord2f(2,20);    glVertex3f( MX/16,0, MZ/2);

        glTexCoord2f(0,20);    glVertex3f(-MX/16,0, MZ/2);

    glEnd();

    glTranslatef(0,-1,0);

    SelectTextureRepeat(oTexture[6]);

    glBegin(GL_QUADS);           

        glTexCoord2f(0,0);        glVertex3f(-MX/2,0,-MZ/2);

        glTexCoord2f(10,0);        glVertex3f( MX/2,0,-MZ/2);

        glTexCoord2f(10,10);    glVertex3f( MX/2,0, MZ/2);

        glTexCoord2f(0,10);        glVertex3f(-MX/2,0, MZ/2);

    glEnd();

}

//从文件创建纹理

bool Animation::CreateTexture(char *filename,GLuint &texture)

{

    AUX_RGBImageRec *pImage = NULL;

    //CBitmap oBitmap;

    //oBitmap.LoadBitmap("IDB_BITMAP1");

    pImage = auxDIBImageLoad(filename);

    if(pImage == NULL)        return false;

    glGenTextures(1, &texture);   

    glBindTexture    (GL_TEXTURE_2D,texture);

    gluBuild2DMipmaps(GL_TEXTURE_2D,4, pImage->sizeX,

                    pImage->sizeY,GL_RGB, GL_UNSIGNED_BYTE,pImage->data);

    free(pImage->data);

    free(pImage);   

    return true;

}

//选择纹理

void Animation::SelectTexture(GLuint texture)

{

    glBindTexture (GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

}

//选择纹理重复

void Animation::SelectTextureRepeat(GLuint texture)

{

    glBindTexture (GL_TEXTURE_2D, texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

}

//启用光照

void Animation::SetupLight()

{

    GLfloat light_position[] = {1.0,5.0,1.0,1.0};

    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glEnable(GL_LIGHTING);

    glEnable(GL_LIGHT0);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_COLOR_MATERIAL);

}

//键盘控制

void Animation::InitInput()

{

    float v = 0.5f;

    if(KEY_DOWN(VK_SHIFT)) v = v * 4;        //Shift

    if(KEY_DOWN(65)) oAngle -= v*2;        //A

    if(KEY_DOWN(68)) oAngle += v*2;        //D

    oRad = float(3.14159 * oAngle/180.0f);    //半径

    if(KEY_DOWN(VK_PRIOR)) oEve += v;        //Page Up

    if(KEY_DOWN(VK_NEXT)) oEve -= v;        //Page Down

    if(oEve < -360) oEve = -360;            //边界检查

    if(oEve > 360) oEve = 360;            //边界检查

    if(KEY_DOWN(87))                        //W

    {

        oEye[2] += (float)sin(oRad) * v;

        oEye[0] += (float)cos(oRad) * v;

    }

    if(KEY_DOWN(83))                        //S

    {

        oEye[2] -= (float)sin(oRad) * v;

        oEye[0] -= (float)cos(oRad) * v;

    }

    if(KEY_DOWN(81)) oEye[1] += v;            //Q

    if(KEY_DOWN(69)) oEye[1] -= v;            //E

    oLtn[0] = (float)(oEye[0] + 100*cos(oRad));

    oLtn[2] = (float)(oEye[2] + 100*sin(oRad));

    oLtn[1] = oEye[1] + oEve;

    //设置观察点和视点坐标

    gluLookAt(oEye[0], oEye[1], oEye[2], oLtn[0], oLtn[1], oLtn[2], 0.0, 1.0, 0.0);

}

  评论这张
 
阅读(80)| 评论(1)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018