//主函数
glutReshapeFunc(ReshapeFunc);//窗口改变时调用
//head.cpp
void ReshapeFunc(int iWidth,int iHeight)
{
glMatrixMode(GL_PROJECTION);//将视口矩阵与投影矩阵设置为当前矩阵
glLoadIdentity();
glViewport(0,0,iWidth,iHeight);//视口矩阵
//glOrtho(-1,1,-1,1,-1,1);//正交投影(2D)
gluPerspective(90,(float)iWidth/(float)iHeight,0.3,1000);//透视投影(3D)
}
//摄像机类
#pragma once
#include "Vector3D.h"
class Camera
{
public:
Vector3D m_vEye;//摄像机位置
Vector3D m_vLookAt;//摄像机看向的点
Vector3D m_vUp;
Vector3D m_vOffset;
Camera();
void Update();
};
#include "Camera.h"
Camera::Camera():m_vEye(0,0,2),m_vLookAt(0,0,0),m_vUp(0,1,0),m_vOffset(0,3,6)
{
}
void Camera::Update()
{
m_vEye = m_vLookAt + m_vOffset;
}
//游戏管理器
#pragma once
#include "Tool.h"
#include "Camera.h"
class CGameManager
{
GLuint TextureID;//纹理编号
Vector3D m_vPos;//平移
float m_fAngle;//旋转角度
Camera m_Camera;//摄像机
BITMAPFILEHEADER* pBmpFileHeader;//指向位图文件的指针
CGameManager();
CGameManager(const CGameManager& that);
public:
static CGameManager& GetInstance();
void Init();
void Darw();
void Run();
void End();
void KeyboardFunc(unsigned char Key,int x,int y);
void SpecialFunc(int Key,int x,int y);
void MouseFunc(int button,int state,int x,int y);
GLuint LoadTexture(const char* pFileName);//加载纹理
BMP LoadBmp(const char* pFileName);//加载位图
};
#include "GameManager.h"
CGameManager::CGameManager()
{
m_fAngle = 0.0f;
}
CGameManager::CGameManager(const CGameManager& that)
{
m_fAngle = that.m_fAngle;
}
CGameManager& CGameManager::GetInstance()
{
static CGameManager gameManager;
return gameManager;
}
void CGameManager::Init()
{
TextureID = LoadTexture("Texture.bmp");
}
void CGameManager::Darw()
{
glBindTexture(GL_TEXTURE_2D,TextureID);//将TextureID所绑定的纹理作为当前绘图纹理
glMatrixMode(GL_MODELVIEW);//将世界变换矩阵与摄像机矩阵设置为当前矩阵
glLoadIdentity();//将当前矩阵设置为单位矩阵
//设置摄像机变换矩阵
gluLookAt(m_Camera.m_vEye.m_fX,m_Camera.m_vEye.m_fY,m_Camera.m_vEye.m_fZ,
m_Camera.m_vLookAt.m_fX,m_Camera.m_vLookAt.m_fY,m_Camera.m_vLookAt.m_fZ,
m_Camera.m_vUp.m_fX,m_Camera.m_vUp.m_fY,m_Camera.m_vUp.m_fZ);
glTranslatef(m_vPos.m_fX,m_vPos.m_fY,m_vPos.m_fZ);//平移
glRotated(m_fAngle,0,1,0);//旋转
glBegin(GL_TRIANGLES);
{
glTexCoord2f(0,1);
glVertex3f(0,5.0f,0);
glTexCoord2f(1,1);
glVertex3f(-5.0f,0.0f,0);
glTexCoord2f(5.0,0);
glVertex3f(0,0.0f,-5.0f);
glTexCoord2f(0,1);
glVertex3f(0,5.0f,0);
glTexCoord2f(1,1);
glVertex3f(-5.0f,0.0f,0);
glTexCoord2f(5.0,0);
glVertex3f(0,0.0f,5.0f);
glTexCoord2f(0,1);
glVertex3f(0,5.0f,0);
glTexCoord2f(1,1);
glVertex3f(5.0f,0.0f,0);
glTexCoord2f(5.0,0);
glVertex3f(0,0.0f,5.0f);
glTexCoord2f(0,1);
glVertex3f(0,5.0f,0);
glTexCoord2f(1,1);
glVertex3f(5.0f,0.0f,0);
glTexCoord2f(5.0,0);
glVertex3f(0,0.0f,-5.0f);
}
glEnd();
}
void CGameManager::Run()
{
m_Camera.m_vLookAt = m_vPos;//让摄像机看向英雄
m_Camera.Update();//更新摄像机位置
}
void CGameManager::End()
{
}
void CGameManager::KeyboardFunc(unsigned char Key,int x,int y)
{
if(Key == 'w' || Key == 'W')
{
m_vPos.m_fZ--;
std::cout<<"向前走"<if(Key == 's' || Key == 'S')
{
m_vPos.m_fZ++;
}
if(Key == 'a' || Key == 'A')
{
m_fAngle++;
}
if(Key == 'd' || Key == 'D')
{
m_fAngle--;
}
}
void CGameManager::SpecialFunc(int Key,int x,int y)
{
if(Key == GLUT_KEY_UP)
{
}
if(Key == GLUT_KEY_DOWN)
{
}
if(Key == GLUT_KEY_LEFT)
{
}
if(Key == GLUT_KEY_RIGHT)
{
}
}
void CGameManager::MouseFunc(int button,int state,int x,int y)
{
if(button == 0)
{
if(state == 0)
{
std::cout<<"鼠标左键按下"<if(state == 1)
{
std::cout<<"鼠标左键抬起"<if(button == 1)
{
if(state == 0)
{
std::cout<<"鼠标中键按下"<if(state == 1)
{
std::cout<<"鼠标中键抬起"<if(button == 2)
{
if(state == 0)
{
std::cout<<"鼠标右键按下"<if(state == 1)
{
std::cout<<"鼠标右键抬起"<const char* pFileName)//加载纹理
{
GLuint TextureID;//
glGenTextures(1,&TextureID);//在显存创建一个空间,并得到空间编号
glBindTexture(GL_TEXTURE_2D,TextureID);//将创建的空间与TextureID号绑定
BMP bmp = LoadBmp(pFileName);//加载位图
glTexParameterf(GL_TEXTURE_2D/*二维纹理*/,GL_TEXTURE_MIN_FILTER/*缩小*/,GL_LINEAR/*线性采样*/);//设置纹理参数
glTexParameterf(GL_TEXTURE_2D/*二维纹理*/,GL_TEXTURE_MAG_FILTER/*放大*/,GL_LINEAR/*线性采样*/);//设置纹理参数
//把图片处理为纹理
glTexImage2D(GL_TEXTURE_2D/*二维纹理*/,0/*表示不创建多级渐进纹理*/,GL_RGB/*RGB模式*/,
bmp.iWidth/*纹理像素宽*/,bmp.iHeight/*纹理像素高*/,0/*不设置纹理边界*/,
GL_BGR_EXT/*表示纹理像素的格式为B、G、R的排列*/,GL_UNSIGNED_BYTE,bmp.pColor/*纹理像素颜色的起始地址*/);
return TextureID;
}
BMP CGameManager::LoadBmp(const char* pFileName)
{
FILE* pFile;//文件指针
fopen_s(&pFile,pFileName,"rb");//打开文件名为pFileName的文件
if(pFile == NULL)
std::cout<<"文件不存在!"<0,SEEK_END);//让文件指针偏向文件尾
int iFileLength = ftell(pFile);//得到文件的大小
rewind(pFile);//让文件指针指向文件头
pBmpFileHeader = (BITMAPFILEHEADER*)new char[iFileLength];
fread(pBmpFileHeader,1,iFileLength,pFile);//将文件读取到pBmpFileHeader指向的内存中
fclose(pFile);//关闭文件
if(pBmpFileHeader->bfType != 'MB')//如果文件的类型的不是位图文件(MB--代表位图)
{
delete [] pBmpFileHeader;//释放内存
return BMP(0,0,0);//返回空位图
}
BMP bmp;//定义一个BMP结构体的对象bmp
bmp.iWidth = ((BITMAPINFOHEADER*)(pBmpFileHeader + 1))->biWidth;
bmp.iHeight = ((BITMAPINFOHEADER*)(pBmpFileHeader + 1))->biHeight;
bmp.pColor = (char*)pBmpFileHeader + pBmpFileHeader->bfOffBits;//颜色的偏移量
return bmp;
}
//代码很多都是重复的,下次我只粘贴部分代码