#include "OpenGL.h"
#include <time.h>
#ifdef _UNICODE || UNICODE
#define GL_FONT_SIZE GL_UNSIGNED_SHORT
#else
#define GL_FONT_SIZE GL_UNSIGNED_BYTE
#endif
//构造函数
OpenGL::OpenGL(HWND hwnd)
{
if (hwnd == NULL)
return;
this->hwnd = hwnd;
this->hdc = GetDC(this->hwnd);
}
//初始化OpenGL
void OpenGL::initOpenGL(void)
{
if (hwnd == NULL || hdc == NULL)
return;
DEVMODE dmScreenSettings = { {0},
0,
0,
sizeof(DEVMODE),
0,
DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT|DM_DISPLAYFREQUENCY };
RECT rect;
GetWindowRect(this->hwnd,&rect);
ChangeDisplaySettings(NULL, CDS_FULLSCREEN);
static PIXELFORMATDESCRIPTOR pfd ={
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
32,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
SetPixelFormat(this->hdc, ChoosePixelFormat(this->hdc, &pfd), &pfd);
this->hrc = wglCreateContext(this->hdc);
wglMakeCurrent(this->hdc,this->hrc);
setViewPort(rect);
glEnable(GL_BLEND);//开启混合
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glShadeModel(GL_SMOOTH);//开启平滑
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
initLight();
buildFont();
//补充列
for(int i=0;i<MAXNUM;i++)
{
if(list[i] == NULL)
{
list[i] = new CharList(fovy,aspect,zNear,zFar,20,10);
}
}
}
//设置视口
void OpenGL::setfovy(GLdouble fovy)
{
this->fovy = fovy;
}
void OpenGL::setZFar(GLdouble zFar)
{
this->zFar = zFar;
}
void OpenGL::setZNear(GLdouble zNear){
this->zNear = zNear;
}
void OpenGL::setViewPort(RECT rect)
{
setViewPort(rect.bottom-rect.top,rect.right-rect.left);
}
void OpenGL::setViewPort(GLsizei width,GLsizei height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspect = (GLfloat)width / (GLfloat)height;
gluPerspective(fovy,aspect , zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//执行渲染
void OpenGL::render(void){
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->DrawScene();
SwapBuffers(this->hdc);
}
//析构函数
OpenGL::~OpenGL(void)
{
killFont();
ReleaseDC(this->hwnd,this->hdc);
this->hdc = NULL;
this->hwnd = NULL;
for(int i=0;i<MAXNUM;i++)
{
if(list[i])
{
delete list[i];
}
}
}
//创建字体
void OpenGL::buildFont(){//创建字体
HFONT font; // 字体句柄
HFONT oldfont; // 旧的字体句柄
font = CreateFont( -12, // 字体高度
0, // 字体宽度
0, // 字体的旋转角度 Angle Of Escapement
0, // 字体底线的旋转角度Orientation Angle
FW_BOLD, // 字体的重量
FALSE, // 是否使用斜体
FALSE, // 是否使用下划线
FALSE, // 是否使用删除线
ANSI_CHARSET, // 设置字符集
OUT_TT_PRECIS, // 输出精度
CLIP_DEFAULT_PRECIS, // 裁剪精度
ANTIALIASED_QUALITY, // 输出质量
FF_DONTCARE|DEFAULT_PITCH, // Family And Pitch
TEXT("Courier New") ); // 字体名称
oldfont = (HFONT)SelectObject(hdc, font); // 选择我们需要的字体
base = glGenLists(256); // 创建256个显示列表
wglUseFontOutlines(hdc, 0, 256, base ,0.0f, // 字体的光滑度,越小越光滑,0.0为最光滑的状态
0.0f, // 在z方向突出的距离
WGL_FONT_POLYGONS, // 使用多边形来生成字符,每个顶点具有独 立的法线
gmf
); //创建256个ASCII字符
SelectObject(hdc, oldfont); // 选择原来的字体
DeleteObject(font); // 删除字体
}//创建字体结束
//删除字体
void OpenGL::killFont(){//删除字体
glDeleteLists(base, 256);
}
//初始化灯光
void OpenGL::initLight(){
return;
GLfloat LightSpecular[]= { 1.0f, 1.0f, 1.0f, 0.2f }; // 环境光参数
GLfloat LightAmbientDiffuse[]= { 0.0f, 1.0f, 0.2f, 0.2f }; // 漫射光参数
GLfloat LightPosition[]= { 0.0f, 0.0f, 0.0f, 1.0f }; // 光源位置
//光照
glLightfv(GL_LIGHT1, GL_AMBIENT, LightSpecular); // 设置环境光
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightAmbientDiffuse); // 设置漫射光
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // 设置光源位置
//glLightfv(GL_LIGHT1, GL_SPECULAR,LightSpecular); //设置镜面反射
glEnable(GL_LIGHT1); // 启用一号光源
glEnable(GL_LIGHTING); // 启用光源
}
//显示文字
void OpenGL::puts(TCHAR ch)
{
glPushAttrib(GL_LIST_BIT);
glListBase(base);
glPushMatrix();
glCallLists(1, GL_FONT_SIZE, &ch); // 调用显示列表绘制字符
glPopMatrix();
glPopAttrib();
}
void OpenGL::puts(TCHAR *text)
{
glPushAttrib(GL_LIST_BIT);
glListBase(base);
glPushMatrix();
glCallLists(_tcslen(text), GL_FONT_SIZE, text); // 调用显示列表绘制字符串
glPopMatrix();
glPopAttrib();
}
//渲染循环
void OpenGL::DrawScene(){
t =clock() - t;
//static TCHAR list[]=TEXT("ILoveYou ");
static CharList * temp = NULL;
PCHARCELL tp;
//冒泡排序
for(int i=0;i<MAXNUM-1;i++)
{
for(int j=i;j<MAXNUM;j++)
{
if(list[i]->z < list[j]->z)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
//循环渲染
for(int i=0;i<MAXNUM;i++)
{
glPushMatrix();
glTranslatef(list[i]->x,list[i]->y,list[i]->z);
list[i]->moveFirst();
float alpha = 1.0f/list[i]->length;
int total = list[i]->length;
while ( tp=list[i]->moveNext() )
{
if(total == list[i]->length)
glColor4f(0.8f,0.8f,0.8f,alpha*total--);
else
glColor4f(0,1,0.5f,alpha*total--);
puts(tp->ch);
glTranslatef(0,1.0f,0);
}
glPopMatrix();
list[i]->fallDown(t);
if(list[i]->outOfScreen())
{
delete list[i];
list[i]=new CharList(fovy,aspect,zNear,zFar,20,10);;
}
}
t = clock();
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Matrix3D.rar (78个子文件)
Matrix3D
Release
msvcr100.dll 756KB
Matrix3D.scr 31KB
Matrix3D.pdb 411KB
Matrix3D.suo 19KB
Matrix3D.sdf 15.44MB
Matrix3D.sln 891B
Matrix3D
OpenGL.h 922B
Release
vc100.idb 515KB
CL.write.1.tlog 2KB
CL.read.1.tlog 43KB
mt.read.1.tlog 626B
rc.write.1.tlog 210B
rc.read.1.tlog 3KB
Matrix3D.Build.CppClean.log 1KB
mt.command.1.tlog 436B
Matrix3D.exe.intermediate.manifest 381B
cl.command.1.tlog 2KB
link-cvtres.read.1.tlog 2B
Matrix3D.lastbuildstate 54B
link.write.1.tlog 786B
Matrix3D.res 13KB
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
rc.command.1.tlog 488B
link.read.1.tlog 4KB
Matrix3D.obj 97KB
OpenGL.obj 104KB
mt.write.1.tlog 254B
Matrix3D.vcxprojResolveAssemblyReference.cache 713B
Matrix3D.log 3KB
Matrix3D.write.1.tlog 0B
CharList.obj 95KB
vc100.pdb 132KB
Matrix3D.cpp 2KB
Matrix3D.aps 36KB
Matrix3D.vcxproj.user 603B
icon1.ico 13KB
Matrix3DSCR.cpp 1KB
Matrix3D.vcxproj 4KB
OpenGL.cpp 6KB
resource.h 2KB
Matrix3D.vcxproj.filters 2KB
CharList.h 1KB
CharList.cpp 3KB
Debug
vc100.idb 515KB
link.5228.read.1.tlog 2B
link.5228-cvtres.read.1.tlog 2B
CL.write.1.tlog 1KB
CL.read.1.tlog 43KB
mt.read.1.tlog 294B
Matrix3D_manifest.rc 206B
rc.write.1.tlog 478B
rc.read.1.tlog 3KB
Matrix3D.Build.CppClean.log 2KB
Matrix3D.exe.embed.manifest.res 472B
link.5228-cvtres.write.1.tlog 2B
mt.command.1.tlog 376B
Matrix3D.exe.intermediate.manifest 381B
Matrix3D.exe.embed.manifest 406B
cl.command.1.tlog 2KB
link-cvtres.read.1.tlog 2B
Matrix3D.lastbuildstate 52B
link.write.1.tlog 992B
Matrix3D.res 13KB
link-cvtres.write.1.tlog 2B
link.command.1.tlog 2KB
rc.command.1.tlog 988B
link.read.1.tlog 4KB
Matrix3D.obj 31KB
OpenGL.obj 47KB
mt.write.1.tlog 294B
link.5228.write.1.tlog 2B
Matrix3D.log 2KB
Matrix3D.write.1.tlog 0B
CharList.obj 35KB
vc100.pdb 132KB
Matrix3D.rc 5KB
Matrix3D.v11.suo 32KB
共 78 条
- 1
资源评论
- monday_l2013-03-313.9M很大了,我下下来但不知怎么搞定,不能运行,再调一段时间试试看吧,希望不错!
- hu834209162013-11-10不知道什么情况,不太好用,没用起来- -
- IronCrusade2014-03-28资源不错 很有帮助
- byandong2013-05-20只是有点像而已,没有电影里做的好
寒冬飞雪
- 粉丝: 6
- 资源: 5
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功