PDA

View Full Version : File-New Problem...Goes Blank!!



03-26-2003, 09:56 AM
Im working in VC++ and i have a simple MDI app working with GL to draw a simple line strip! Ok: when i create a NEW FILE in the menu it opens a new file (expected), but when i close it the previous file open goes blank!! Can You Help ME!!!!

Deiussum
03-26-2003, 10:05 AM
This is just a guess, but I bet your code is wrong somewhere. I'd help more but my mind reading skills seem to be getting a lot of interference today. Maybe if you would post more details it would be helpful.

03-26-2003, 10:20 AM
Ok, I have an MDI project. When i create 2 documents with a GL image, the image on the first document disapears when i close the second. I took an SDI project example from www.devcentral.com! (http://www.devcentral.com!) I would send you some code but I do not know which part! Thankx

Deiussum
03-26-2003, 10:53 AM
Still not a lot of information, but here goes...

Do you do your initialization of OpenGL in the CView derived class?

Do you make sure you use wglMakeCurrent before doing the rendering?

Do you use any static variables that could be getting reset when the one window is closed?

When both documents are open, do both of them display and update correctly when they are hidden/re-shown?

03-27-2003, 06:43 AM
I guess I do all that stuff. I dont use static variables! Is it possible for you to take a look on my View Class?


Originally posted by Deiussum:
Still not a lot of information, but here goes...

Do you do your initialization of OpenGL in the CView derived class?

Do you make sure you use wglMakeCurrent before doing the rendering?

Do you use any static variables that could be getting reset when the one window is closed?

When both documents are open, do both of them display and update correctly when they are hidden/re-shown?

Deiussum
03-27-2003, 07:25 AM
Yeah, I'd be willing to take a look at your code. You didn't answer my fourth question above, though. Does the problem only show up when you close that second window, or does it also show up if you were to just change focus back to the first window. Also try to minimize and restore the app when you have both windows open and make sure both are redrawn correctly.

03-27-2003, 07:40 AM
Ok, about the 4th question:
When i create 2 docs:
Create one and minimize, then create n2;
minimize n2 and maximize n1;
n1 goes blank;
n2 is ok!

Here is the important part of the code!
Now i'm trying do clik the left button and draw a point... THANK YOU!!(Im really a begginer...)

// CCADCorTexView construction/destruction

CCADCorTexView::CCADCorTexView()
{
m_hGLContext = NULL;
m_GLPixelIndex = 0;

}

CCADCorTexView::~CCADCorTexView()
{
}

BOOL CCADCorTexView::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);
}

/////////////////////////////////////////////////////////////////////////////
// CCADCorTexView drawing

void CCADCorTexView::OnDraw(CDC* pDC)
{
CCADCorTexDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CCADCorTexView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CCADCorTexView diagnostics

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

void CCADCorTexView: http://www.opengl.org/discussion_boards/ubb/biggrin.gifump(CDumpContext& dc) const
{
CView: http://www.opengl.org/discussion_boards/ubb/biggrin.gifump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CCADCorTexView message handlers

BOOL CCADCorTexView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);

pixelDesc.nVersion = 1;

pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_DRAW_TO_BITMAP |
PFD_SUPPORT_OPENGL |
PFD_SUPPORT_GDI |
PFD_STEREO_DONTCARE |
PFD_DOUBLEBUFFER;

pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 24;
//pixelDesc.cRedBits = 8;
//pixelDesc.cRedShift = 16;
//pixelDesc.cGreenBits = 8;
//pixelDesc.cGreenShift = 8;
//pixelDesc.cBlueBits = 8;
//pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
//pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
//pixelDesc.cAccumRedBits = 16;
//pixelDesc.cAccumGreenBits = 16;
//pixelDesc.cAccumBlueBits = 16;
//pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;

pixelDesc.iLayerType = PFD_MAIN_PLANE;
//pixelDesc.bReserved = 0;
//pixelDesc.dwLayerMask = 0;
//pixelDesc.dwVisibleMask = 0;
//pixelDesc.dwDamageMask = 0;

m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
if (m_GLPixelIndex==0) // Let's choose a default index.
{
m_GLPixelIndex = 1;
if (DescribePixelFormat(hDC, m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
{
return FALSE;
}
}

if (SetPixelFormat( hDC, m_GLPixelIndex, &pixelDesc)==FALSE)
{
return FALSE;
}

return TRUE;
}

////////////////////////////////////////////////////////////////////////////

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

HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);

if (SetWindowPixelFormat(hDC)==FALSE)
return 0;

if (CreateViewGLContext(hDC)==FALSE)
return 0;


return 0;
}

BOOL CCADCorTexView::CreateViewGLContext(HDC hDC)
{
//Cria o contexto de rendering
m_hGLContext = wglCreateContext(hDC);
if (m_hGLContext == NULL)
{
return FALSE;
}

//Torna o contexto corrente
if (wglMakeCurrent(hDC, m_hGLContext)==FALSE)
{
return FALSE;
}

return TRUE;

}

void CCADCorTexView::OnDestroy()
{
if(wglGetCurrentContext()!=NULL)
{
// make the rendering context not current
wglMakeCurrent(NULL, NULL) ;
}

if (m_hGLContext!=NULL)
{
wglDeleteContext(m_hGLContext);
m_hGLContext = NULL;
}
// Now the associated DC can be released.
CView::OnDestroy();

}

void CCADCorTexView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
GLsizei width, height;
GLdouble aspect;

width = cx;
height = cy;
TRACE("cx: %d ; cy: %d\n",cx,cy);
if (cy==0)
aspect = (GLdouble)width;
else
aspect = (GLdouble)width/(GLdouble)height;

glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//gluOrtho2D(0.0, 500.0*aspect, 0.0, 500.0);
gluOrtho2D(0.0, cx, 0.0, cy);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Invalidate(TRUE);

}

void CCADCorTexView::OnPaint()
{
CPaintDC dc(this); // device context for painting

glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_POINTS);

glVertex2f(20,20);
glVertex2f(100,300);
glVertex2f(200,300);
glVertex2f(300,150);
glEnd();

glFlush();

}


void CCADCorTexView::OnLButtonDown(UINT nFlags, CPoint point)
{

glBegin(GL_POINTS);
glVertex2f((float)point.x, (float)point.y);
glEnd();

glFlush();

TRACE("x:%d ",point.x);
TRACE("y:%d \n",point.y);

CView::OnLButtonDown(nFlags, point);

}

Deiussum
03-27-2003, 10:00 AM
The first thing I notice is that you aren't setting the CS_OWNDC in your PreCreateWindow. You should have a line of code there that looks like so:

// You can also pass in the cursor, background brush, and icon if you wish
cs.lpszClass = AfxRegisterWindClass(CS_VREDRAW | CS_HREDRAW | CS_OWNDC);

The other thing I see is that in your OnPaint, you are NOT using wglMakeCurrent! (See my second question above...)

This is necessary because you have 2 OpenGL contexts created and you need to specify which one to use when you do your drawing.

Likewise, you should be using wglMakeCurrent in your OnSize method as well.

So far as the way you add points, it would be much better to store a list of the points that are drawn in the OnPaint. Then in OnLButtonDown, add a point to that list and refresh the display with something like the Invalidate method.

03-28-2003, 03:58 AM
THANK YOU VERY VERY VERY MUCH!!!!!!
HEY, GUESS WHAT?! IT WORKS!!!!
Sorry to bother you so much!!!
:-D

About the points, eventualy i will make a vector of accumulated points and print them on the screen!
Thank you again!

Deiussum
03-28-2003, 05:32 AM
No problem. Glad I could help. http://www.opengl.org/discussion_boards/ubb/smile.gif