游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2411|回复: 0

请教问题:关于数据实时获取、及时更新(OpenGL与VC)

[复制链接]

3

主题

9

帖子

17

积分

新手上路

Rank: 1

积分
17
发表于 2004-9-29 08:47:00 | 显示全部楼层 |阅读模式
请教问题:关于数据实时获取、及时更新(OpenGL与VC)

现在在用一Fastrak三维定位设备,它可以实时获取空间某点的三维坐标,提供给在OpenGL场景中使用。

问题是:我设置一个定时器从USB端口每隔100毫秒取数据,用pDC-Textout语句可以实时显示某点有三维坐标(::OnDraw{}),但我直接调用DrawCube()数据却不能及时更新(::OnDraw{}),或用菜单命令也不行。

我想这是定时器与OpenGL之间的接口问题,但又不能解决,请教各位了。
  1. // FastrakSimView.cpp : implementation of the CFastrakSimView class
  2. //

  3. #include "stdafx.h"
  4. #include "FastrakSim.h"
  5. #include "gl\glut.h"
  6. #include "FT\FtApi.h"
  7. #include "FastrakSimDoc.h"
  8. #include "FastrakSimView.h"

  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif

  14. /////////////////////////////////////////////////////////////////////////////
  15. // CFastrakSimView

  16. IMPLEMENT_DYNCREATE(CFastrakSimView, CView)

  17. BEGIN_MESSAGE_MAP(CFastrakSimView, CView)
  18.         //{{AFX_MSG_MAP(CFastrakSimView)
  19.         ON_WM_CREATE()
  20.         ON_WM_DESTROY()
  21.         ON_WM_SIZE()
  22.         ON_WM_TIMER()
  23.         ON_COMMAND(ID_DRAW_CUBE, OnDrawCube)
  24.         //}}AFX_MSG_MAP
  25.         // Standard printing commands
  26.         ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  27.         ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  28.         ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  29. END_MESSAGE_MAP()

  30. /////////////////////////////////////////////////////////////////////////////
  31. // CFastrakSimView construction/destruction

  32. CFastrakSimView::CFastrakSimView()
  33. {
  34.         // TODO: add construction code here

  35. }

  36. CFastrakSimView::~CFastrakSimView()
  37. {
  38. }

  39. BOOL CFastrakSimView::PreCreateWindow(CREATESTRUCT& cs)
  40. {
  41.         // TODO: Modify the Window class or styles here by modifying
  42.         //  the CREATESTRUCT cs

  43.         
  44. ////////////////////////////////////////////////////////////////
  45. //设置窗口类型
  46.         cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
  47. ////////////////////////////////////////////////////////////////

  48.         return CView::PreCreateWindow(cs);
  49. }

  50. /////////////////////////////////////////////////////////////////////////////
  51. // CFastrakSimView drawing

  52. void CFastrakSimView::OnDraw(CDC* pDC)
  53. {
  54.         CFastrakSimDoc* pDoc = GetDocument();
  55.         ASSERT_VALID(pDoc);
  56.         /////////////////////////检查连接类型
  57.         char buf[100];

  58.     int connectionType=FT_IsConnected();

  59.     if (connectionType==FT_NO_CONNECTION)

  60.          strcpy(buf,"no Connection\n");
  61.     else
  62.          sprintf(buf,"Connection Established over %s\n",FT_IsConnected()==FT_RS232_CONNECTION?"RS 232":"USB");

  63.     pDC->TextOut(10,10,buf);
  64. ///////////////////////////显示所取数据
  65. //        pDC->TextOut(10,40,char(pDoc->x));
  66. //        pDC->TextOut(50,40,pDoc->y);
  67. //        pDC->TextOut(100,40,pDoc->z);
  68. //        pDC->TextOut(140,40,pDoc->azimuth);
  69. //        pDC->TextOut(180,40,pDoc->elevation);
  70. //        pDC->TextOut(220,40,pDoc->roll);
  71.         char ascbuf[200];
  72.     FT_GetPnoAsc(ascbuf,pDoc->numStation*47);
  73.         pDC->TextOut(10,30,"    ");
  74.         pDC->TextOut(45,30,"  x  ");
  75.         pDC->TextOut(80,30,"  y  ");
  76.         pDC->TextOut(125,30,"  z  ");
  77.         pDC->TextOut(160,30,"  Az  ");
  78.         pDC->TextOut(195,30,"   El");
  79.         pDC->TextOut(230,30,"    Roll");
  80.     pDC->TextOut(10,50,ascbuf);
  81.         /*
  82.         CRect block;
  83.         CBrush black;
  84.         black.CreateSolidBrush(0);
  85.         block.SetRect(5,5,15,15);
  86.         pDC->FillRect(&block,&black);
  87.         block.SetRect(795,5,805,15);
  88.         pDC->FillRect(&block,&black);
  89.         block.SetRect(5,595,15,605);
  90.         pDC->FillRect(&block,&black);

  91.         pDC->Rectangle(10+pDoc->x - 5,10+pDoc->y - 5,10+pDoc->x + 5,10+pDoc->y + 5);
  92.         pDC->TextOut(10+pDoc->x + 5,10+pDoc->y,"xy");
  93.         pDC->Rectangle(10+pDoc->x - 5,600-(pDoc->z - 5),10+pDoc->x + 5,600-(pDoc->z + 5));
  94.         pDC->TextOut(10+pDoc->x - 5,600-(pDoc->z - 5),"xz");
  95.         pDC->Rectangle(800-(pDoc->y - 5),10+pDoc->z - 5,800-(pDoc->y + 5),10+pDoc->z + 5);
  96.         pDC->TextOut(800-(pDoc->y - 5),10+pDoc->z - 5,"yz");
  97.         */

  98.         // TODO: add draw code for native data here
  99.         //drawcube();
  100. }

  101. /////////////////////////////////////////////////////////////////////////////
  102. // CFastrakSimView printing

  103. BOOL CFastrakSimView::OnPreparePrinting(CPrintInfo* pInfo)
  104. {
  105.         // default preparation
  106.         return DoPreparePrinting(pInfo);
  107. }

  108. void CFastrakSimView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  109. {
  110.         // TODO: add extra initialization before printing
  111. }

  112. void CFastrakSimView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  113. {
  114.         // TODO: add cleanup after printing
  115. }

  116. /////////////////////////////////////////////////////////////////////////////
  117. // CFastrakSimView diagnostics

  118. #ifdef _DEBUG
  119. void CFastrakSimView::AssertValid() const
  120. {
  121.         CView::AssertValid();
  122. }

  123. void CFastrakSimView::Dump(CDumpContext& dc) const
  124. {
  125.         CView::Dump(dc);
  126. }

  127. CFastrakSimDoc* CFastrakSimView::GetDocument() // non-debug version is inline
  128. {
  129.         ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFastrakSimDoc)));
  130.         return (CFastrakSimDoc*)m_pDocument;
  131. }
  132. #endif //_DEBUG

  133. /////////////////////////////////////////////////////////////////////////////
  134. // CFastrakSimView message handlers

  135. int CFastrakSimView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  136. {
  137.         if (CView::OnCreate(lpCreateStruct) == -1)
  138.                 return -1;
  139.         
  140.         SetTimer(1,100,NULL);

  141.         // TODO: Add your specialized creation code here
  142.         
  143.         PIXELFORMATDESCRIPTOR pfd = {
  144.             sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小
  145.             1,                                // 版本号
  146.             PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图
  147.             PFD_SUPPORT_OPENGL |              // 支持 OpenGL
  148.             PFD_DOUBLEBUFFER|                 // 双缓存模式
  149.             PFD_TYPE_RGBA,                    // RGBA 颜色模式
  150.             24,                               // 24 位颜色深度
  151.             0, 0, 0, 0, 0, 0,                 // 忽略颜色位
  152.             0,                                // 没有非透明度缓存
  153.             0,                                // 忽略移位位
  154.             0,                                // 无累加缓存
  155.             0, 0, 0, 0,                       // 忽略累加位
  156.             32,                               // 32 位深度缓存     
  157.             0,                                // 无模板缓存
  158.             0,                                // 无辅助缓存
  159.             PFD_MAIN_PLANE,                   // 主层
  160.             0,                                // 保留
  161.             0, 0, 0                           // 忽略层,可见性和损毁掩模
  162.         };

  163.         CClientDC clientdc(this);
  164.           int pf=ChoosePixelFormat(clientdc.m_hDC,&pfd);
  165.           BOOL rt=SetPixelFormat(clientdc.m_hDC,pf,&pfd);
  166.           hglrc=wglCreateContext(clientdc.m_hDC);

  167.         return 0;
  168. }

  169. void CFastrakSimView::OnDestroy()
  170. {
  171.         CView::OnDestroy();
  172.         
  173.         // TODO: Add your message handler code here
  174.         if(wglGetCurrentContext()!=NULL)
  175.         wglMakeCurrent(NULL,NULL);
  176.         if(hglrc!=NULL)
  177.         {wglDeleteContext(hglrc);
  178.         hglrc=NULL;
  179.         }
  180. }

  181. void CFastrakSimView::OnSize(UINT nType, int cx, int cy)
  182. {
  183.         CView::OnSize(nType, cx, cy);
  184.         
  185.         // TODO: Add your message handler code here
  186.         GLsizei w=cx;
  187.         GLsizei h=cy;
  188.         if(!h)
  189.                 return;
  190.         CClientDC clientDC(this);

  191.         wglMakeCurrent(clientDC.m_hDC,hglrc);
  192.         glViewport(0,0,w,h);
  193.         wglMakeCurrent(NULL,NULL);
  194. }

  195. void CFastrakSimView::OnTimer(UINT nIDEvent)
  196. {
  197.         // TODO: Add your message handler code here and/or call default
  198. //        AfxMessageBox("haha",NULL,NULL);
  199.         CFastrakSimDoc* pDoc = GetDocument();
  200.         pDoc->GetPos();
  201.         Invalidate(0);
  202. //        CView::OnTimer(nIDEvent);
  203. }

  204. void CFastrakSimView::myinit()
  205. {
  206.         glClearColor(1.0,1.0,1.0,1.0);
  207.         glClear(GL_COLOR_BUFFER_BIT);

  208. }

  209. /*
  210. void CFastrakSimView::drawcube()
  211. {
  212.         myinit();
  213.         glMatrixMode(GL_MODELVIEW );
  214.     glLoadIdentity();
  215.         glPushMatrix();
  216.         glBegin(GL_POLYGON);
  217.       glColor3f(0,1,0);
  218.       glVertex3f(0,1,0);
  219.       glColor3f(1,1,0);
  220.       glVertex3f(1,1,0);
  221.       glColor3f(1,0,0);
  222.       glVertex3f(1,0,0);
  223.       glColor3f(0,0,0);
  224.       glVertex3f(0,0,0);
  225.     glEnd();
  226.     glBegin(GL_POLYGON);
  227.       glColor3f(0,0,0);
  228.       glVertex3f(0,0,0);
  229.       glColor3f(0,0,1);
  230.       glVertex3f(0,0,1);   
  231.       glColor3f(0,1,1);
  232.       glVertex3f(0,1,1);   
  233.       glColor3f(0,1,0);
  234.       glVertex3f(0,1,0);   
  235.     glEnd();
  236.     glBegin(GL_POLYGON);
  237.       glColor3f(1,0,0);
  238.       glVertex3f(1,0,0);
  239.       glColor3f(1,1,0);
  240.       glVertex3f(1,1,0);   
  241.       glColor3f(1,1,1);
  242.       glVertex3f(1,1,1);   
  243.       glColor3f(1,0,1);
  244.       glVertex3f(1,0,1);   
  245.     glEnd();
  246.     glBegin(GL_POLYGON);
  247.       glColor3f(1,0,1);
  248.       glVertex3f(1,0,1);   
  249.       glColor3f(1,1,1);
  250.       glVertex3f(1,1,1);   
  251.       glColor3f(0,1,1);
  252.       glVertex3f(0,1,1);   
  253.       glColor3f(0,0,1);
  254.       glVertex3f(0,0,1);
  255.     glEnd();
  256.     glBegin(GL_POLYGON);
  257.       glColor3f(0,0,0);
  258.       glVertex3f(0,0,0);
  259.       glColor3f(1,0,0);
  260.       glVertex3f(1,0,0);   
  261.       glColor3f(1,0,1);
  262.       glVertex3f(1,0,1);   
  263.       glColor3f(0,0,1);
  264.       glVertex3f(0,0,1);   
  265.     glEnd();
  266.     glBegin(GL_POLYGON);
  267.       glColor3f(0,1,1);
  268.       glVertex3f(0,1,1);   
  269.       glColor3f(1,1,1);
  270.       glVertex3f(1,1,1);   
  271.       glColor3f(1,1,0);
  272.       glVertex3f(1,1,0);   
  273.       glColor3f(0,1,0);
  274.       glVertex3f(0,1,0);
  275.       glColor3f(1,1,1);
  276.     glEnd();
  277.         glPopMatrix();
  278.         glFlush();
  279. }*/

  280. void CFastrakSimView::drawcube()
  281. {
  282.     //glutInit(&argc, argv);
  283.         CFastrakSimDoc* pDoc = GetDocument();
  284.         ASSERT_VALID(pDoc);
  285.         
  286.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  287.         glClearColor (0.0, 0.0, 0.0, 0.0);
  288.     glShadeModel (GL_SMOOTH);
  289.         glClear (GL_COLOR_BUFFER_BIT);
  290.     glColor3f (1.0, 0.0, 0.0);
  291.         glLoadIdentity (); /* clear the matrix */
  292.      /* viewing transformation  */
  293.     gluLookAt ((pDoc->x)/100, (pDoc->y)/100, (pDoc->z)/100+5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  294.     glScalef (2.0, 2.0, 2.0);/* modeling transformation */
  295.     glutWireCube (1.5);
  296.         glColor3f (0.0, 0.0, 1.0);
  297.         glScalef ((pDoc->x)/100, (pDoc->y)/100, (pDoc->z)/100);/* modeling transformation */
  298.     glutWireCube (0.1);
  299.     glFlush ();
  300.         //glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  301.     glMatrixMode (GL_PROJECTION);
  302.     glLoadIdentity ();
  303.     glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
  304.     glMatrixMode (GL_MODELVIEW);
  305.         
  306. }

  307. void CFastrakSimView::OnDrawCube()
  308. {
  309.         // TODO: Add your command handler code here
  310.         HWND hWnd=GetSafeHwnd();
  311.         HDC hDC=::GetDC(hWnd);
  312.         wglMakeCurrent(hDC,hglrc);
  313.         myinit();
  314.         drawcube();
  315.         wglMakeCurrent(NULL,NULL);
  316.         SwapBuffers(hDC);
  317. }
复制代码


再问个Glut的问题

利用GLUT可以很 方便的处理窗口、键盘鼠标响应等事件
我的问题是:

      如果我用glutCreateWindow()后,象下列语句能执行吗?

            if (!FT_OpenUSBConnection())
            cout << "Sorry,make sure Fastrak connection is correct!\n";
        else
                cout << "connection is correct!\n";

似乎这时候glut不能够创建窗口 [em20]
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-8-18 19:03

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表