OpenGL.cpp
上传用户:jsylhbnbhn
上传日期:2013-11-03
资源大小:119k
文件大小:6k
- #include "stdafx.h"
- #include "OpenGL.h"
- //////////////////////////////////////////////////////////////////////
- extern HWND hWnd;
- const float pi = 3.1415926f;
- GLUquadricObj* quadric=NULL;
- //////////////////////////////////////////////////////////////////////
- OpenGL::OpenGL()
- {
- x_arc = 0;
- y_arc = 0;
- z_arc = 0;
- }
- OpenGL::~OpenGL()
- { CleanUp();
- }
- BOOL OpenGL::SetupPixelFormat(HDC hDC0)//检测安装OpenGL
- { int nPixelFormat; // 象素点格式
- hDC=hDC0;
- PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // pfd结构的大小
- 1, // 版本号
- PFD_DRAW_TO_WINDOW | // 支持在窗口中绘图
- PFD_SUPPORT_OPENGL | // 支持 OpenGL
- PFD_DOUBLEBUFFER, // 双缓存模式
- PFD_TYPE_RGBA, // RGBA 颜色模式
- 16, // 24 位颜色深度
- 0, 0, 0, 0, 0, 0, // 忽略颜色位
- 0, // 没有非透明度缓存
- 0, // 忽略移位位
- 0, // 无累加缓存
- 0, 0, 0, 0, // 忽略累加位
- 16, // 32 位深度缓存
- 0, // 无模板缓存
- 0, // 无辅助缓存
- PFD_MAIN_PLANE, // 主层
- 0, // 保留
- 0, 0, 0 // 忽略层,可见性和损毁掩模
- };
- if (!(nPixelFormat = ChoosePixelFormat(hDC, &pfd)))
- { MessageBox(NULL,"没找到合适的显示模式","Error",MB_OK|MB_ICONEXCLAMATION);
- return FALSE;
- }
- SetPixelFormat(hDC,nPixelFormat,&pfd);//设置当前设备的像素点格式
- hRC = wglCreateContext(hDC); //获取渲染描述句柄
- wglMakeCurrent(hDC, hRC); //激活渲染描述句柄
- return TRUE;
- }
- void OpenGL::init(int Width, int Height)
- { glViewport(0,0,Width,Height); // 设置OpenGL视口大小。
- glMatrixMode(GL_PROJECTION); // 设置当前矩阵为投影矩阵。
- glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵
- /*
- gluPerspective // 设置透视图
- ( 54.0f, // 透视角设置为 45 度
- (GLfloat)Width/(GLfloat)Height, // 窗口的宽与高比
- 0.1f, // 视野透视深度:近点1.0f
- 3000.0f // 视野透视深度:始点0.1f远点1000.0f
- );
- */
- glOrtho(-3.0, 3.0, -3.0, 3.0,-3.0,3.0);
- // 这和照象机很类似,第一个参数设置镜头广角度,第二个参数是长宽比,后面是远近剪切。
- glMatrixMode(GL_MODELVIEW); // 设置当前矩阵为模型视图矩阵
- glLoadIdentity(); // 重置当前指定的矩阵为单位矩阵
- //====================================================
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glDepthFunc(GL_LESS);
- glEnable(GL_DEPTH_TEST);
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
- glEnable(GL_COLOR_MATERIAL);
- }
- void OpenGL::Render()//OpenGL图形处理
- {
- glClearColor(0.0f, 0.0f, 0.3f, 1.0f); // 设置刷新背景色
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);// 刷新背景
- glLoadIdentity(); // 重置当前的模型观察矩阵
- Vector3f vVector1, vVector2;
- vVector1.x = 200;
- vVector1.y = 200;
- vVector1.z = 200;
- vVector2.x = 0;
- vVector2.y = 0;
- vVector2.z = 0;
- draw2point(vVector1,vVector2);
- vVector2.x = 440;
- vVector2.y = 440;
- vVector2.z = 440;
- draw2point(vVector1,vVector2);
- glFlush(); // 更新窗口
- SwapBuffers(hDC); // 切换缓冲区
- }
- void OpenGL::CleanUp()//清除OpenGL
- {
- wglMakeCurrent(hDC, NULL); //清除OpenGL
- wglDeleteContext(hRC); //清除OpenGL
- }
- int OpenGL::Init()
- {return 0;}
- void OpenGL::display()
- {
- glFlush();
- SwapBuffers(hDC); // 切换缓冲区
- }
- void OpenGL::draw2point(Vector3f vVector1, Vector3f vVector2)
- {
- float Zoom = 100;
- //printf("before unproject: %f,%f,%f n",vVector1.x,vVector1.y,vVector1.z);
- //printf("before unproject: %f,%f,%f n",vVector2.x,vVector2.y,vVector2.z);
- //printf("after unproject: %f,%f,%f nn",xx,yy,zz);
-
- //需要考虑float 越界问题
- Vector3f *vt1 = new Vector3f(vVector1.x/Zoom,vVector1.y/Zoom,vVector1.z/Zoom);
- Vector3f *vt2 = new Vector3f(vVector2.x/Zoom,vVector2.y/Zoom,vVector2.z/Zoom);
- Vector3f vt4 = *vt2-*vt1;
- Vector3f *vty = new Vector3f(0,0,1);
-
- double arc12 = AngleBetweenVectors(*vty,vt4);
- double rarc12 = 180 + 180*arc12/pi;
- float len= Distance(*vt1,*vt2);
- Vector3f vt3 = Cross(*vty,vt4);
- glPushMatrix();
- glTranslatef (vt1->x,vt1->y,vt1->z);
- glColor3f(1.0f,0.5f,0.7f);
- auxSolidSphere(0.09);
- glTranslatef (vt4.x,vt4.y,vt4.z);
- auxSolidSphere(0.09);
- glColor3f(1.0f,1.0f,0.7f);
- glRotatef ((float)rarc12,vt3.x,vt3.y,vt3.z);
- gluCylinder(quadric,0.06,0.06,len,35,35);
- glPopMatrix();
- delete vt1;
- delete vt2;
- delete vty;
- }
- void OpenGL::clear()
- {
- glClearColor(0.0f, 0.0f, 0.3f, 1.0f); // 设置刷新背景色
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);// 刷新背景
- glLoadIdentity();
- if(!quadric)
- {
- quadric=gluNewQuadric();
- gluQuadricNormals(quadric,GLU_SMOOTH);
- gluQuadricTexture(quadric,GL_TRUE);
- }
- //gluCylinder(quadric,0.06,0.06,0.9,35,35);
- glRotatef (25.0,1.0,1.0,0.0);
- glRotatef ((float)x_arc,1.0,0.0,0.0);
- glRotatef ((float)y_arc,0.0,1.0,0.0);
- glRotatef ((float)z_arc,0.0,0.0,1.0);
- draw_ground();
-
- }
- //将骨架旋转到y轴上来
- int OpenGL::Correct_3D_Layout(Vector3f vVector1, Vector3f vVector2)
- {
- Vector3f vt4 = vVector2-vVector1;
- Vector3f *vty = new Vector3f(0,1,0);
- double arc12 = AngleBetweenVectors(*vty,vt4);
- double rarc12 = 180*arc12/pi;
- Vector3f vt3 = Cross(*vty,vt4);
- glRotatef ((float)rarc12,vt3.x,vt3.y,vt3.z);
- delete vty;
- return 0;
- }
- void OpenGL::draw_ground()
- {
- glPushAttrib(GL_CURRENT_BIT);
- glEnable(GL_BLEND);
- glPushMatrix();
- glColor3f(0.5f, 0.7f, 1.0f);
- glTranslatef(0,0.0f,0);
- float size0=2;
- glBegin(GL_LINES);
- for (float x = -size0; x < size0;x+=0.5f)
- {glVertex3f(x, -1.5, -size0); glVertex3f(x, -1.5, size0);}
- for (float z = -size0; z < size0;z+=0.3f)
- {glVertex3f(-size0, -1.5, z); glVertex3f( size0, -1.5, z);}
- glEnd();
- glPopMatrix();
- glDisable(GL_BLEND);
- glPopAttrib();
- }