PDA

View Full Version : Geometric PartView



Digga
01-12-2010, 02:36 PM
Hello,
Im righting an OpenGl partview as a add on to an existing product. I cant seem to figure out how to add features as they are measured to my view:
Right now Im trying to get lines working:
Code:

// CvisualView

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

UINT ogl_timerID;

// CAvalonView

IMPLEMENT_DYNCREATE(CVisualView, CView)

BEGIN_MESSAGE_MAP(CVisualView, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CVisualView::OnFilePrintPreview)
ON_WM_PAINT()
ON_WM_SIZE()
ON_WM_CREATE()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()

// CAvalonView construction/destruction

CVisualView::CVisualView()
{
m_fPosX = 0.0f; // X position of model in camera view
m_fPosY = 0.0f; // Y position of model in camera view
m_fZoom = 10.0f; // Zoom on model in camera view
m_fRotX = 0.0f; // Rotation on model in camera view
m_fRotY = 0.0f; // Rotation on model in camera view
m_bIsMaximized = false;
}

CVisualView::~CVisualView()
{
// KillTimer( ogl_timerID );// Causes GP fault
}

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

return CView::PreCreateWindow(cs);
}

// CAvalonView drawing

void CVisualView::OnDraw(CDC* /*pDC*/)
{

glLoadIdentity();



glTranslatef(0.0f, 0.0f, -m_fZoom);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
glRotatef(m_fRotX, 1.0f, 0.0f, 0.0f);
glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);

// CvisualDoc* pDoc = GetDocument();
// ASSERT_VALID(pDoc);
// if (!pDoc)

// return;

// TODO: add draw code for native data here
}

// CAvalonView printing
void CVisualView::OnFilePrintPreview()
{
AFXPrintPreview(this);
}

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

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

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

void CVisualView::OnRButtonUp(UINT nFlags, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}

void CVisualView::OnContextMenu(CWnd* pWnd, CPoint point)
{
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
}

// CAvalonView diagnostics

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

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

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

void CVisualView::OnPaint()
{
ValidateRect(NULL);
//CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CView::OnPaint() for painting messages
}

void CVisualView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);


if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED) return;

// Map the OpenGL coordinates.
glViewport(0, 0, cx, cy);

// Projection view
glMatrixMode(GL_PROJECTION);

glLoadIdentity();

// Set our current view perspective
gluPerspective(35.0f, (float)cx / (float)cy, 0.01f, 2000.0f);

// Model view
glMatrixMode(GL_MODELVIEW);

switch (nType)
{
// If window resize token is "maximize"
case SIZE_MAXIMIZED:
{
// Get the current window rect
GetWindowRect(m_rect);

// Move the window accordingly
MoveWindow(6, 6, cx - 14, cy - 14);

// Get the new window rect
GetWindowRect(m_rect);

// Store our old window as the new rect
m_oldWindow = m_rect;

break;
}

// If window resize token is "restore"
case SIZE_RESTORED:
{
// If the window is currently maximized
if (m_bIsMaximized)
{
// Get the current window rect
GetWindowRect(m_rect);

// Move the window accordingly (to our stored old window)
MoveWindow(m_oldWindow.left, m_oldWindow.top - 18, m_originalRect.Width() - 4, m_originalRect.Height() - 4);

// Get the new window rect
GetWindowRect(m_rect);

// Store our old window as the new rect
m_oldWindow = m_rect;
}

break;
}
}
}
// TODO: Add your message handler code here


int CVisualView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;

ogl_timerID = SetTimer( 1,0,0);

oglInitialize();



return 0;
// TODO: Add your specialized creation code here
}

void CVisualView::OnTimer(UINT_PTR nIDEvent)
{
{
switch (nIDEvent)
{
case 1:
{
// Clear color and depth buffer bits
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


// Draw OpenGL scene
oglDrawScene();

// Swap buffers
SwapBuffers(hdc);

break;
}

default:
break;
}

CView::OnTimer(nIDEvent);
}
}

void CVisualView::OnMouseMove(UINT nFlags, CPoint point)
{
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;

// Left mouse button
if (nFlags & MK_LBUTTON)
{
m_fRotX += (float)0.5f * diffY;

if ((m_fRotX > 360.0f) || (m_fRotX < -360.0f))
{
m_fRotX = 0.0f;
}

m_fRotY += (float)0.5f * diffX;

if ((m_fRotY > 360.0f) || (m_fRotY < -360.0f))
{
m_fRotY = 0.0f;
}
}

// Right mouse button
else if (nFlags & MK_RBUTTON)
{
m_fZoom -= (float)0.1f * diffY;
}

// Middle mouse button
else if (nFlags & MK_MBUTTON)
{
m_fPosX += (float)0.05f * diffX;
m_fPosY -= (float)0.05f * diffY;
}

OnDraw(NULL);


// TODO: Add your message handler code here and/or call default

CView::OnMouseMove(nFlags, point);
}

void CVisualView::oglInitialize(void)
{
// Initial Setup:
//
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // bit depth
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
16, // z-buffer depth
0, 0, 0, 0, 0, 0, 0,
};


// Get device context only once.
hdc = GetDC()->m_hDC;

// Pixel format.
m_nPixelFormat = ChoosePixelFormat(hdc, &amp;pfd);
SetPixelFormat(hdc, m_nPixelFormat, &amp;pfd);

// Create the OpenGL Rendering Context.
hrc = wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);

// Basic Setup:
//
// Set color to use when clearing the background.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);// black color

glClearDepth(1.0f);

// Turn on backface culling
glFrontFace(GL_CCW);
glCullFace(GL_BACK);

// Turn on depth testing
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

// Send draw request
OnDraw(NULL);
}



void CVisualView::DrawBackGround(void)
{
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glBegin(GL_QUADS); // Start Drawing A Quad
glColor4f(0.2f,0.2f,0.6f,1.0f);
glVertex3f(-1.0f, 1.0f, -2.0f); // Top Left Of The Quad
glVertex3f( 1.0f, 1.0f, -2.0f); // Top Right Of The Quad
glColor4f(0.9f,0.9f,1.0f,1.0f);
glVertex3f( 1.0f,-1.0f, -2.0f); // Bottom Right Of The Quad
glVertex3f(-1.0f,-1.0f, -2.0f); // Bottom Left Of The Quad
glEnd();

}

void CVisualView::oglDrawScene(void)
{


glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);

// Then as before
//glMatrixMode(GL_MODELVIEW); // maybe not needed
glPushMatrix();
glLoadIdentity();
DrawBackGround();
glPopMatrix();

// And reenable those
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);


// Wireframe Mode

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glBegin(GL_QUADS);
glColor3f(0.f,0.f,1.f);
// Front Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);

// Back Side
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);

// Top Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);

// Bottom Side
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

// Right Side
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);

// Left Side
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();

// draw carthesian axes
glBegin(GL_LINES);
// red x axis
glColor3f(1.f,0.f,0.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,0.1f,0.0f);
glVertex3f(1.0f,0.0f,0.0f);
glVertex3f(0.9f,-0.1f,0.0f);
// green y axis
glColor3f(0.f,1.f,0.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(0.1f,0.9f,0.0f);
glVertex3f(0.0f,1.0f,0.0f);
glVertex3f(-0.1f,0.9f,0.0f);
// blue z axis
glColor3f(0.f,0.f,1.f);
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,0.1f,0.9f);
glVertex3f(0.0f,0.0f,1.0f);
glVertex3f(0.0f,-0.1f,0.9f);
glEnd();

}

void CVisualView::DrawOglLine( float x1, float y1, float x2, float y2 )
{

float zval = 1.0f;
glPushMatrix();
glColor3f(1.f,0.f,0.f);
glBegin(GL_LINES);
glVertex3f(x1,y2,zval);
glVertex3f(x2,y2,zval);
glEnd();
glPopMatrix();



}Any help would be great
Thanks
Derrek

Digga
01-13-2010, 07:52 AM
Anyone?
Is there a tutorial I should look at?

Derrek

ZbuffeR
01-13-2010, 08:17 AM
Sorry but I do not understand your problem. What do you want to do, and what does not work ?

Digga
01-13-2010, 08:45 AM
Hello,
I have a MFC view that has a gradient background. What Im trying to do is add geometry as its measured. For example I start measuring in my software and measure a circle.In the view a circle would be drawn. Then I measure a line and a line is added to the view and so on.
The measure computations and view prep are done in other portions of the code. Sp what I need is to be able to call for features to be drawn a single feature at a time.

Basically When I measure a line the view doesnt get redrawn with the line visible, however If I comment out GLEnd I see the line but the whole view flashes and doesnt redraw properly

Thanks
Derrek