Timer and OpenGL

I have a Ftastrak and I get x\y\z data from USB port by a timer. The below codes with opengl show a dynamic cube. But id does not run as my wish.

// FastrakSimView.cpp : implementation of the CFastrakSimView class
//

#include "stdafx.h"
#include "FastrakSim.h"
#include "gl\glut.h"
#include "FT\FtApi.h"
#include "FastrakSimDoc.h"
#include "FastrakSimView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFastrakSimView

IMPLEMENT_DYNCREATE(CFastrakSimView, CView)

BEGIN_MESSAGE_MAP(CFastrakSimView, CView)
        //{{AFX_MSG_MAP(CFastrakSimView)
        ON_WM_CREATE()
        ON_WM_DESTROY()
        ON_WM_SIZE()
        ON_WM_TIMER()
        ON_COMMAND(ID_DRAW_CUBE, OnDrawCube)
        //}}AFX_MSG_MAP
        // Standard printing commands
        ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
        ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFastrakSimView construction/destruction

CFastrakSimView::CFastrakSimView()
{
        // TODO: add construction code here

}

CFastrakSimView::~CFastrakSimView()
{
}

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

        
////////////////////////////////////////////////////////////////
//设置窗口类型
        cs.style |=WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
////////////////////////////////////////////////////////////////

        return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CFastrakSimView drawing

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

    int connectionType=FT_IsConnected();

    if (connectionType==FT_NO_CONNECTION)

         strcpy(buf,"no Connection
");
    else
         sprintf(buf,"Connection Established over %s
",FT_IsConnected()==FT_RS232_CONNECTION?"RS 232":"USB");

    pDC->TextOut(10,10,buf);
///////////////////////////显示所取数据
//        pDC->TextOut(10,40,char(pDoc->x));
//        pDC->TextOut(50,40,pDoc->y);
//        pDC->TextOut(100,40,pDoc->z);
//        pDC->TextOut(140,40,pDoc->azimuth);
//        pDC->TextOut(180,40,pDoc->elevation);
//        pDC->TextOut(220,40,pDoc->roll);
        char ascbuf[200];
    FT_GetPnoAsc(ascbuf,pDoc->numStation*47);
        pDC->TextOut(10,30,"    ");
        pDC->TextOut(45,30,"  x  ");
        pDC->TextOut(80,30,"  y  ");
        pDC->TextOut(125,30,"  z  ");
        pDC->TextOut(160,30,"  Az  ");
        pDC->TextOut(195,30,"   El");
        pDC->TextOut(230,30,"    Roll");
    pDC->TextOut(10,50,ascbuf);
        /*
        CRect block;
        CBrush black;
        black.CreateSolidBrush(0);
        block.SetRect(5,5,15,15);
        pDC->FillRect(&block,&black);
        block.SetRect(795,5,805,15);
        pDC->FillRect(&block,&black);
        block.SetRect(5,595,15,605);
        pDC->FillRect(&block,&black);

        pDC->Rectangle(10+pDoc->x - 5,10+pDoc->y - 5,10+pDoc->x + 5,10+pDoc->y + 5);
        pDC->TextOut(10+pDoc->x + 5,10+pDoc->y,"xy");
        pDC->Rectangle(10+pDoc->x - 5,600-(pDoc->z - 5),10+pDoc->x + 5,600-(pDoc->z + 5));
        pDC->TextOut(10+pDoc->x - 5,600-(pDoc->z - 5),"xz");
        pDC->Rectangle(800-(pDoc->y - 5),10+pDoc->z - 5,800-(pDoc->y + 5),10+pDoc->z + 5);
        pDC->TextOut(800-(pDoc->y - 5),10+pDoc->z - 5,"yz");
        */

        // TODO: add draw code for native data here
        //drawcube();
}

/////////////////////////////////////////////////////////////////////////////
// CFastrakSimView printing

BOOL CFastrakSimView::OnPreparePrinting(CPrintInfo* pInfo)
{
        // default preparation
        return DoPreparePrinting(pInfo);
}

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

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

/////////////////////////////////////////////////////////////////////////////
// CFastrakSimView diagnostics

#ifdef _DEBUG
void CFastrakSimView::AssertValid() const
{
        CView::AssertValid();
}

void CFastrakSimView::Dump(CDumpContext& dc) const
{
        CView::Dump(dc);
}

CFastrakSimDoc* CFastrakSimView::GetDocument() // non-debug version is inline
{
        ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFastrakSimDoc)));
        return (CFastrakSimDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CFastrakSimView message handlers

int CFastrakSimView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
        if (CView::OnCreate(lpCreateStruct) == -1)
                return -1;
        
        SetTimer(1,100,NULL);

        // TODO: Add your specialized creation code here
        
        PIXELFORMATDESCRIPTOR pfd = { 
            sizeof(PIXELFORMATDESCRIPTOR),    // pfd结构的大小 
            1,                                // 版本号 
            PFD_DRAW_TO_WINDOW |              // 支持在窗口中绘图 
            PFD_SUPPORT_OPENGL |              // 支持 OpenGL 
            PFD_DOUBLEBUFFER|                 // 双缓存模式 
            PFD_TYPE_RGBA,                    // RGBA 颜色模式 
            24,                               // 24 位颜色深度 
            0, 0, 0, 0, 0, 0,                 // 忽略颜色位 
            0,                                // 没有非透明度缓存 
            0,                                // 忽略移位位 
            0,                                // 无累加缓存 
            0, 0, 0, 0,                       // 忽略累加位 
            32,                               // 32 位深度缓存     
            0,                                // 无模板缓存 
            0,                                // 无辅助缓存 
            PFD_MAIN_PLANE,                   // 主层 
            0,                                // 保留 
            0, 0, 0                           // 忽略层,可见性和损毁掩模 
        }; 

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

        return 0;
}

void CFastrakSimView::OnDestroy() 
{
        CView::OnDestroy();
        
        // TODO: Add your message handler code here
        if(wglGetCurrentContext()!=NULL)
        wglMakeCurrent(NULL,NULL);
        if(hglrc!=NULL)
        {wglDeleteContext(hglrc);
        hglrc=NULL;
        }
}

void CFastrakSimView::OnSize(UINT nType, int cx, int cy) 
{
        CView::OnSize(nType, cx, cy);
        
        // TODO: Add your message handler code here
        GLsizei w=cx;
        GLsizei h=cy;
        if(!h)
                return;
        CClientDC clientDC(this);

        wglMakeCurrent(clientDC.m_hDC,hglrc);
        glViewport(0,0,w,h);
        wglMakeCurrent(NULL,NULL);
}

void CFastrakSimView::OnTimer(UINT nIDEvent) 
{
        // TODO: Add your message handler code here and/or call default
//        AfxMessageBox("haha",NULL,NULL);
        CFastrakSimDoc* pDoc = GetDocument();
        pDoc->GetPos();
        Invalidate(0);
//        CView::OnTimer(nIDEvent);
}

void CFastrakSimView::myinit()
{
        glClearColor(1.0,1.0,1.0,1.0);
        glClear(GL_COLOR_BUFFER_BIT);

}

/*
void CFastrakSimView::drawcube()
{
        myinit();
        glMatrixMode(GL_MODELVIEW );
    glLoadIdentity();
        glPushMatrix();
        glBegin(GL_POLYGON);
      glColor3f(0,1,0);
      glVertex3f(0,1,0);
      glColor3f(1,1,0);
      glVertex3f(1,1,0);
      glColor3f(1,0,0);
      glVertex3f(1,0,0);
      glColor3f(0,0,0);
      glVertex3f(0,0,0);
    glEnd();
    glBegin(GL_POLYGON);
      glColor3f(0,0,0);
      glVertex3f(0,0,0);
      glColor3f(0,0,1);
      glVertex3f(0,0,1);    
      glColor3f(0,1,1);
      glVertex3f(0,1,1);    
      glColor3f(0,1,0);
      glVertex3f(0,1,0);    
    glEnd();
    glBegin(GL_POLYGON);
      glColor3f(1,0,0);
      glVertex3f(1,0,0);
      glColor3f(1,1,0);
      glVertex3f(1,1,0);    
      glColor3f(1,1,1);
      glVertex3f(1,1,1);    
      glColor3f(1,0,1);
      glVertex3f(1,0,1);    
    glEnd();
    glBegin(GL_POLYGON);
      glColor3f(1,0,1);
      glVertex3f(1,0,1);    
      glColor3f(1,1,1);
      glVertex3f(1,1,1);    
      glColor3f(0,1,1);
      glVertex3f(0,1,1);    
      glColor3f(0,0,1);
      glVertex3f(0,0,1);
    glEnd();
    glBegin(GL_POLYGON);
      glColor3f(0,0,0);
      glVertex3f(0,0,0);
      glColor3f(1,0,0);
      glVertex3f(1,0,0);    
      glColor3f(1,0,1);
      glVertex3f(1,0,1);    
      glColor3f(0,0,1);
      glVertex3f(0,0,1);    
    glEnd();
    glBegin(GL_POLYGON);
      glColor3f(0,1,1);
      glVertex3f(0,1,1);    
      glColor3f(1,1,1);
      glVertex3f(1,1,1);    
      glColor3f(1,1,0);
      glVertex3f(1,1,0);    
      glColor3f(0,1,0);
      glVertex3f(0,1,0);
      glColor3f(1,1,1);
    glEnd();
        glPopMatrix();
        glFlush();
}*/

void CFastrakSimView::drawcube()
{
    //glutInit(&argc, argv);
        CFastrakSimDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
        glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_SMOOTH);
        glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 0.0, 0.0);
        glLoadIdentity (); /* clear the matrix */
     /* viewing transformation  */
    gluLookAt ((pDoc->x)/100, (pDoc->y)/100, (pDoc->z)/100+5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    glScalef (2.0, 2.0, 2.0);/* modeling transformation */ 
    glutWireCube (1.5);
        glColor3f (0.0, 0.0, 1.0);
        glScalef ((pDoc->x)/100, (pDoc->y)/100, (pDoc->z)/100);/* modeling transformation */ 
    glutWireCube (0.1);
    glFlush ();
        //glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
    glMatrixMode (GL_MODELVIEW);
        
}

void CFastrakSimView::OnDrawCube() 
{
        // TODO: Add your command handler code here
        HWND hWnd=GetSafeHwnd();
        HDC hDC=::GetDC(hWnd);
        wglMakeCurrent(hDC,hglrc);
        myinit();
        drawcube();
        wglMakeCurrent(NULL,NULL);
        SwapBuffers(hDC);
}  

I need your help.
maczone@163.com
zone_mac@hotmail.com

It would be nice to know, what your wish is…
(or where the problem is)

Marc

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.