|
楼主 |
发表于 2010-5-3 22:19:08
|
显示全部楼层
2# array
以下是我源码的主要部分,望各位指点一下到底是什么地方出问题了。。。
OSG初始化,加载模型函数(用sceneView)
-
- void COSGFrame::InitSceneView(std::string filename)
- {
- m_root = new osg::Group;
- fileName = filename;
- sceneView->setDefaults();
- sceneView->setComputeNearFarMode(osgUtil::CullVisitor::DO_NOT_COMPUTE_NEAR_FAR);
- m_Model = osgDB::readNodeFile(fileName);
- osgUtil::Optimizer optimizer;
- optimizer.optimize(m_Model.get());
- optimizer.reset();
- osg::PositionAttitudeTransform* newModelPAT = new osg::PositionAttitudeTransform;
- newModelPAT->addChild(m_Model.get());
- newModelPAT->setPosition(osg::Vec3(0,-100,-10));
- newModelPAT->setScale(osg::Vec3(1.0,1.0,1.0));
- m_root->addChild(newModelPAT);
- osg::ref_ptr<osg::Node> node = osgDB::readNodeFile("01.ive");
- osg::PositionAttitudeTransform* pat = new osg::PositionAttitudeTransform;
- pat->setPosition(osg::Vec3(-350.035,123.94,-10.000136811));
- pat->setScale(osg::Vec3(0.5,0.5,0.5));
- pat->addChild(node.get());
- m_root->addChild(pat);
- osg::PositionAttitudeTransform* pat1 = new osg::PositionAttitudeTransform;
- pat1->setPosition(osg::Vec3(127.492,-59.3833,-10));
- pat1->setScale(osg::Vec3(0.3,0.3,0.3));
- pat1->addChild(m_Model.get());
- m_root->addChild(pat1);
- precipitationEffect = new osgParticle::PrecipitationEffect;
- precipitationEffect->snow(0.5);
- precipitationEffect->setParticleColor(osg::Vec4(1,1,1,1));
- precipitationEffect->setWind(osg::Vec3(2,0,0));
- //m_root->addChild(precipitationEffect.get());
- sceneView->setSceneData(m_root.get());
- viewMatrix.makeLookAt(eyePoint,center,osg::Vec3(0.0f,0.0f,1.0f));
- }
-
- void COSGFrame::OSG_Render_Frame()
- {
- //PreOSGRenderFrame(x,y,z);
- sceneView->getCamera()->setClearMask(GL_DEPTH_BUFFER_BIT);
- sceneView->setViewport(0,0,getWindowWidth(),getWindowHeight());
- viewMatrix.makeLookAt(eyePoint,center,upDirection);
- sceneView->setViewMatrix(viewMatrix);
- sceneView->update();
- sceneView->cull();
- sceneView->draw();
- //PostOSGRenderFrame();
- //SwapBuffers(::GetDC(_hwnd));
- }
复制代码
自定义的相机操作,主要是计算场景漫游时的视点位置、观察点位置的变化
只贴了旋转部分的
-
- void CSelfCamera::rotateView(float angle, float X,float Y,float Z)
- {
- Vector3 newView;
-
- Vector3 view = m_View - m_Position;
- float cosTheta = (float)cos(angle);
- float sinTheta = (float)sin(angle);
- newView.x = (cosTheta + (1 - cosTheta) * X * X) * view.x;
- newView.x += ((1 - cosTheta) * X * Y - Z * sinTheta) * view.y;
- newView.x += ((1 - cosTheta) * X * Z + Y * sinTheta) * view.z;
- newView.y = ((1 - cosTheta) * X * Y + Z * sinTheta) * view.x;
- newView.y += (cosTheta + (1 - cosTheta) * Y * Y) * view.y;
- newView.y += ((1 - cosTheta) * Y * Z - X * sinTheta) * view.z;
- newView.z = ((1 - cosTheta) * X * Z - Y * sinTheta) * view.x;
- newView.z += ((1 - cosTheta) * Y * Z + X * sinTheta) * view.y;
- newView.z += (cosTheta + (1 - cosTheta) * Z * Z) * view.z;
- m_View = m_Position + newView;
- }
- void CSelfCamera::setViewByMouse(CPoint oldPot,CPoint currPot)
- {
- CRect rect;
- GetClientRect(m_windowHandle,rect);
- CPoint mousePos;
- int middleX = rect.Width()/2;
- int middleY = rect.Height()/2;
- float angleY = 0.0f;
- float angleZ = 0.0f;
- static float currentRotX = 0.0f;
- if(currPot == oldPot)
- return;
- angleY = (float)(( currPot.x - oldPot.x)) / 1000.0f;
- angleZ = (float)(( currPot.y - oldPot.y)) / 1000.0f;
- static float lastRotX = 0.0f;
- lastRotX = currentRotX;
- currentRotX += angleZ;
- if (currentRotX > 1.0f)
- {
- currentRotX = 1.0f;
- if (lastRotX != 1.0f)
- {
- Vector3 vAxis = m_View - m_Position;
- vAxis = vAxis.crossProduct(m_upVector);
- vAxis = vAxis.normalize();
- rotateView(1.0f - lastRotX,vAxis.x,vAxis.y,vAxis.z);
- }
- }
- else if (currentRotX < -1.0f)
- {
- currentRotX = -1.0f;
- if (lastRotX != -1.0f)
- {
- Vector3 vAxis = m_View - m_Position;
- vAxis = vAxis.crossProduct(m_upVector);
- vAxis = vAxis.normalize();
- rotateView(-1.0f - lastRotX,vAxis.x,vAxis.y,vAxis.z);
- }
- }
- else
- {
- Vector3 vAxis = m_View - m_Position;
- vAxis = vAxis.crossProduct(m_upVector);
- vAxis = vAxis.normalize();
- rotateView(angleZ,vAxis.x,vAxis.y,vAxis.z);
- }
- rotateView(angleY,0,1,0);
- }
复制代码
以下是MFC程序的view类
-
- int CosgSceneViewTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
- {
- if (CView::OnCreate(lpCreateStruct) == -1)
- return -1;
- // TODO: 在此添加您专用的创建代码
- Init();
- m_osgFrame = new COSGFrame(m_hWnd);
- m_selfCamera = new CSelfCamera(m_hWnd);
- m_selfCamera->setCamera(0.0f,0.5f, -100.0f, 0.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f);
- m_osgFrame->eyePoint = OpenglToOsg(-(m_selfCamera->m_Position));
- m_osgFrame->center = OpenglToOsg(m_selfCamera->m_View);
- return 0;
- }
- void CosgSceneViewTestView::OnDestroy()
- {
- if (wglGetCurrentContext() != NULL)
- {
- wglMakeCurrent(NULL,NULL);
- }
- if (m_hGLContext != NULL)
- {
- wglDeleteContext(m_hGLContext);
- m_hGLContext = NULL;
- }
- CView::OnDestroy();
- // TODO: 在此处添加消息处理程序代码
- }
- void CosgSceneViewTestView::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
- // TODO: 在此处添加消息处理程序代码
- // 不为绘图消息调用 CView::OnPaint()
- DrawGroundTexture();
- DrawScene();
- //SwapBuffers(dc.m_ps.hdc);
- }
- BOOL CosgSceneViewTestView::OnEraseBkgnd(CDC* pDC)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- //return CView::OnEraseBkgnd(pDC);
- return TRUE;
- }
- void CosgSceneViewTestView::OnSize(UINT nType, int cx, int cy)
- {
- CView::OnSize(nType, cx, cy);
- CSize size(cx,cy);
- double aspect;
- aspect = (cy == 0) ? (double)size.cx : (double)size.cx/(double)size.cy;
- glViewport(0, 0, (GLsizei) cx, (GLsizei) cy);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0, (GLfloat) cx/(GLfloat) cy, 1.0f, 500.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- m_selfCamera->setLook();
- // TODO: 在此处添加消息处理程序代码
- }
- void CosgSceneViewTestView::OnInitialUpdate()
- {
- CView::OnInitialUpdate();
- }
- void CosgSceneViewTestView::InitViewPort()
- {
- CRect rect;
- GetClientRect(rect);
- if ((rect.Width() == 0) || (rect.Height() == 0))
- {
- return;
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //gluPerspective(90.0,rect.Width()/rect.Height(),1.0,10000000000);
- gluPerspective(50.0f,1.4f,1.0f,10000.0f);
- glViewport(0,0,rect.Width(),rect.Height());
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
-
- //Lights,Meterial Properties
- GLfloat ambientProperties[] = {0.5f, 0.5f, 0.5f, 1.0f};
- GLfloat diffuseProperties[] = {1.0f, 1.0f, 1.0f, 1.0f};
- GLfloat positionProperties[] = {0.0f, 0.0f, 2.0f, 1.0f};
- GLfloat specularProperties[] = {1.0f, 0.0f, 0.0f, 1.0f};
- glLightfv( GL_LIGHT0, GL_AMBIENT, ambientProperties);
- glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseProperties);
- glLightfv(GL_LIGHT0,GL_SPECULAR,specularProperties);
- glLightfv( GL_LIGHT0, GL_POSITION, positionProperties);
- //Default: lighting
- glEnable(GL_LIGHT0);
- // //Modelate: texture lighting
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glEnable(GL_TEXTURE_2D);
- glShadeModel(GL_SMOOTH);
- glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,diffuseProperties);
- glEnable(GL_DEPTH_TEST);
-
- glDrawBuffer(GL_BACK);
- }
- void CosgSceneViewTestView::ChangeScene()
- {
- m_selfCamera->setLook();
- }
-
- void CosgSceneViewTestView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- IsLeftButtonDown = true;
- SetCapture();
- m_lptorigin = m_lptold = point;
- CView::OnLButtonDown(nFlags, point);
- }
- void CosgSceneViewTestView::OnRButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- IsRightButtonDown = true;
- SetCapture();
- m_rptorigin = m_rptold = point;
- CView::OnRButtonDown(nFlags, point);
- }
-
- void CosgSceneViewTestView::ClearUserScreen()
- {
- glLoadIdentity();
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
- }
- void CosgSceneViewTestView::OnReadconfigfile()
- {
- // TODO: 在此添加命令处理程序代码
- CFileDialog fileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,_T("配置文件(*.txt)|*.txt||"),NULL);
- if (fileDlg.DoModal() == IDOK)
- {
- char file[100];
- /*strcpy(file,(char*)fileDlg.GetFileName().GetBuffer());*/
- strcpy(file,(char*)(LPCTSTR(fileDlg.GetFileName())));
- m_showlist.InitFromFile(file);
- AfxMessageBox(_T("ok"));
- }
- }
- void CosgSceneViewTestView::OnLoadscene()
- {
-
- }
- void CosgSceneViewTestView::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- CRect rect;
- GetClientRect(rect);
- if (IsLeftButtonDown)
- {
- if (m_lptold == point)
- {
- return;
- }
- m_selfCamera->setViewByMouse(m_lptold,point);
- m_osgFrame->eyePoint = OpenglToOsg(-m_selfCamera->m_Position);
- m_osgFrame->center = OpenglToOsg(m_selfCamera->m_View);
- DrawScene();
- }
- if (IsRightButtonDown)
- {
- if (m_rptold == point)
- {
- return;
- }
- float DifLength=sqrt(pow((float)(point.x-m_rptold.x),(float)2)+pow((float)(point.y-m_rptold.y),(float)2));
- if(point.y<m_rptold.y) DifLength=-DifLength;
- float DifRatio=DifLength/(sqrt(pow((float)rect.Width(),(float)2)+pow((float)rect.Height(),(float)2)))*5;
- m_selfCamera->zoomCamera(1-DifRatio);
- m_osgFrame->eyePoint = OpenglToOsg(-m_selfCamera->m_Position);
- m_osgFrame->center = OpenglToOsg(m_selfCamera->m_View);
- //TRACE("%f,%f,%f,%f %f %f",m_selfCamera->m_Position.x,m_selfCamera->m_Position.y,m_selfCamera->m_Position.z,m_osgFrame->eyePoint.x(),m_osgFrame->eyePoint.y(),m_osgFrame->eyePoint.z());
- DrawScene();
- }
-
- if (IsMiddleButtonDown)
- {
- if (m_mptold == point)
- {
- return;
- }
- m_selfCamera->yawCamera(20*m_selfCamera->getSpeed()*(m_mptold.x - point.x));
- m_osgFrame->eyePoint = OpenglToOsg(-m_selfCamera->m_Position);
- m_osgFrame->center = OpenglToOsg(m_selfCamera->m_View);
- DrawScene();
- }
-
- if(IsLeftButtonDown) m_lptold=point;
- if(IsRightButtonDown) m_rptold=point;
- if (IsMiddleButtonDown) m_mptold = point;
- m_ptold=point;
- CView::OnMouseMove(nFlags, point);
- }
- void CosgSceneViewTestView::OnMButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- IsMiddleButtonDown = true;
- SetCapture();
- m_mptorigin = m_mptold = point;
- CView::OnMButtonDown(nFlags, point);
- }
- void CosgSceneViewTestView::OnOpenModel()
- {
- // TODO: 在此添加命令处理程序代码
- CFileDialog fileDlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT, _T("All Files (*.*)|*.*||"));
- if (fileDlg.DoModal() == IDOK)
- {
- CString filename = fileDlg.GetFileName();
- std::string std_Name(filename);
- m_osgFrame->InitSceneView(std_Name);
- DrawScene();
- }
- }
- void CosgSceneViewTestView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- IsLeftButtonDown = false;
- ReleaseCapture();
- CView::OnLButtonUp(nFlags, point);
- }
- void CosgSceneViewTestView::OnRButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- IsRightButtonDown = false;
- ReleaseCapture();
- CView::OnRButtonUp(nFlags, point);
- }
- void CosgSceneViewTestView::OnMButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: 在此添加消息处理程序代码和/或调用默认值
- IsMiddleButtonDown = false;
- ReleaseCapture();
- CView::OnMButtonUp(nFlags, point);
- }
- //////////////////////////////////////////////////////////////////////////
- /* opengl坐标转换为osg坐标 */
- osg::Vec3 CosgSceneViewTestView::OpenglToOsg(Vector3 opengl)
- {
- osg::Vec3 des;
- float Xosg = opengl.x;
- float Yosg = opengl.z;
- float Zosg = -opengl.y;
- des.set(Xosg,Yosg,Zosg);
- return des;
- }
- //////////////////////////////////////////////////////////////////////////
- void CosgSceneViewTestView::DrawScene()
- {
- InitViewPort();
- ClearUserScreen();
- DrawOpenGLModel();
- m_osgFrame->OSG_Render_Frame();
- SwapBuffers(wglGetCurrentDC());
- }
- ///////////////////////////////////////////////////////////////////////////
- void CosgSceneViewTestView::DrawOpenGLModel()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- ChangeScene();
- DrawGroundTexture();
- glPopMatrix();
- }
- //////////////////////////////////////////////////////////////////////////
- void CosgSceneViewTestView::DrawGroundTexture()
- {
- glColor4f(1.0f,1.0f,1.0f,1.0f);
- glBindTexture(GL_TEXTURE_2D,TextureArray[0]);
- glBegin(GL_QUADS);
- glNormal3f(0.0f,1.0f,0.0f);
- glTexCoord2f(0.0f,0.0f);glVertex3f(500.0f,-10.0f,-500.f);
- glTexCoord2f(1.0f,0.0f);glVertex3f(500.0f,-10.0f,500.0f);
- glTexCoord2f(1.0f,1.0f);glVertex3f(-500.0f,-10.0f,500.0f);
- glTexCoord2f(0.0f,1.0f);glVertex3f(-500.0f,-10.0f,-500.0f);
- glEnd();
- glFlush();
- glDisable(GL_TEXTURE_2D);
- }
- //////////////////////////////////////////////////////////////////////////
- /* 加载jpeg地形纹理*/
- void CosgSceneViewTestView::JPEG_Texture(UINT textureArray[], LPSTR strFileName, int ID)
- {
- if(!strFileName) return;
- tImageJPG *pBitMap = Load_JPEG(strFileName);
- if(pBitMap == NULL) return;
- glGenTextures(1,&textureArray[ID]);
- glBindTexture(GL_TEXTURE_2D,textureArray[ID]);
- gluBuild2DMipmaps(GL_TEXTURE_2D,3,pBitMap->sizeX,pBitMap->sizeY,GL_RGB,GL_UNSIGNED_BYTE,pBitMap->data);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_NEAREST);
- if (pBitMap)
- {
- if (pBitMap->data)
- {
- free(pBitMap->data);
- }
- free(pBitMap);
- }
- }
- tImageJPG* CosgSceneViewTestView::Load_JPEG(char *strfilename)
- {
- struct jpeg_decompress_struct cInfo;
- tImageJPG* pImgData = NULL;
- FILE *pFile;
- if ((pFile = fopen(strfilename,"rb")) == NULL)
- {
- AfxMessageBox("Error loading jpg file.");
- return NULL;
- }
- jpeg_error_mgr jerr;
- cInfo.err = jpeg_std_error(&jerr);
- jpeg_create_decompress(&cInfo);
- jpeg_stdio_src(&cInfo,pFile);
- pImgData = (tImageJPG*)malloc(sizeof(tImageJPG));
- Decompress_JPEG(&cInfo,pImgData);
- jpeg_destroy_decompress(&cInfo);
- fclose(pFile);
- return pImgData;
- }
- void CosgSceneViewTestView::Decompress_JPEG(jpeg_decompress_struct* cInfo, tImageJPG *pImgData)
- {
- jpeg_read_header(cInfo,TRUE);
- jpeg_start_decompress(cInfo);
- pImgData->rowSpan = cInfo->image_width * cInfo->num_components;
- pImgData->sizeX = cInfo->image_width;
- pImgData->sizeY = cInfo->image_height;
- pImgData->data = new unsigned char[pImgData->rowSpan * pImgData->sizeY];
- unsigned char** rowPtr = new unsigned char*[pImgData->sizeY];
- for (int i = 0;i<pImgData->sizeY;i++)
- {
- rowPtr[i] = &(pImgData->data[i*pImgData->rowSpan]);
- }
- int rowsRead = cInfo->output_height - 1;
- while (cInfo->output_scanline < cInfo->output_height)
- {
- rowsRead -= jpeg_read_scanlines(cInfo,&rowPtr[rowsRead],cInfo->output_height - rowsRead);
- }
- delete []rowPtr;
- jpeg_finish_decompress(cInfo);
- }
复制代码 |
|