PDA

View Full Version : Problem with render, not sure why



dlswimmer
08-05-2003, 02:50 AM
I have done a few opengl programs in mfc like this one and I took the initial data from what I used in them for this, but for some reason, nothing will render. Opengl is initialized fine, but nothing will render. What am I doing wrong?
here is my code---->

BOOL CGameDlg::OnInitDialog()
{
CDialog::OnInitDialog();

if(!InitializeGL())
{
MessageBox("There was an error initializing OpenGL", "Error...", MB_OK | MB_ICONSTOP);
return -1;
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon

// TODO: Add extra initialization here

return TRUE; // return TRUE unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.

void CGameDlg::OnPaint()
{
Render();
CDialog::OnPaint();
}

// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CGameDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}

// Automation servers should not exit when a user closes the UI
// if a controller still holds on to one of its objects. These
// message handlers make sure that if the proxy is still in use,
// then the UI is hidden but the dialog remains around if it
// is dismissed.

void CGameDlg::OnClose()
{
if (CanExit())
CDialog::OnClose();
}

void CGameDlg::OnOK()
{
if (CanExit())
CDialog::OnOK();
}

void CGameDlg::OnCancel()
{
if (CanExit())
CDialog::OnCancel();
}

BOOL CGameDlg::CanExit()
{
// If the proxy object is still around, then the automation
// controller is still holding on to this application. Leave
// the dialog around, but hide its UI.
if (m_pAutoProxy != NULL)
{
ShowWindow(SW_HIDE);
return FALSE;
}

return TRUE;
}

void CGameDlg::OnDestroy()
{
CDialog::OnDestroy();

// TODO: Add your message handler code here

}

BOOL CGameDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
SetCursor(AfxGetApp()->LoadCursor(IDC_POINTER));
return 0;
}

// The pixel format is an extension to the Win32 API that is provided
// for support of OpenGL functionality. To be honest I've never change
// this code every time I use it. If you don't understand pixel format
// then don't worry. Just know that you need this exactly how it is and
// it will most likely never change or would not change much. I will
// go over it in more detail in later tutorials.
// OpenGL code help from NEHE htp://nehe.gamedev.net
int CGameDlg::SetupPixelFormat()
{
// Just like in the Win32 OpenGL tutorials we have to set up the SetupPixelFormat().
// This is the same code from the Win32 tutorials.
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
32, // 32-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accumulation bits ignored
32, // 16-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};

int m_nPixelFormat = ChoosePixelFormat(hDC->GetSafeHdc(), &pfd);
if(m_nPixelFormat == 0) return -1;
return SetPixelFormat(hDC->GetSafeHdc(), m_nPixelFormat, &pfd);
}

void CGameDlg::OnSize(UINT nType, int cx, int cy)
{
// Everytime this window is resized or needs to be resized this function
// is called. This is the same code from the WM_SIZE message from the
// message map in the Win32 version.

// Make sure height or width is not = to zero.
if(cx == 0) cx = 1;
if(cy == 0) cy = 1;
// Set the viewport.
glViewport(0, 0, cx, cy);
// Set the projection.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the windows perspective.
gluPerspective(45.0, (float)cx/(float)cy, 0.1, 1000.0);
// Set the modelview.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

bool CGameDlg::InitializeGL()
{
// This function will first get the display area so we can allow OpenGL
// to attach to it. This is normally done in a way in the Create message entry
// of the Win32 version. We also call SetupPixelFormat() here too.

hDC = new CClientDC(this);
// Error checking.
if(hDC == NULL)
return false;

if(SetupPixelFormat() == -1)
return false;

// Creates the rendering context.
hRC = wglCreateContext(hDC->GetSafeHdc());

// Error checking.
if(hRC == 0)
return false;

// Create the rendering context.
if(wglMakeCurrent(hDC->GetSafeHdc(), hRC) == FALSE)
return false;
BuildFont();

// Lastly lets set up all the OpenGl info we will need (clear the screen to black
// and enable depth testing).
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
//smooth shading
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);

//create a list for the two boxes. This allows them to be used over and over
// while not having to recreate them each time they are displayed.

return true;
}

void CGameDlg::Render()
{
// Clear the screen.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-10.0f);
glColor3f(1.0f,1.0f,1.0f);
auxWireBox(1.0f,1.0f,1.0f);
// Swap buffers.
SwapBuffers(hDC->GetSafeHdc());
}

Thank you for your help!!

Jared@ETC
08-05-2003, 03:28 AM
You aren't calling your render function except in the OnPaint handler...you need to either invalidate the scene (forcing another OnPaint message) or call the render function on a timer.

dlswimmer
08-05-2003, 03:47 AM
I call it as well on a button click, and it still doesn't work.

dlswimmer
08-05-2003, 04:54 AM
have I done anything wrong that anyone has seen, I am not sure why the scene is not rendering. I call Render on Painting and I also call it on a mouse click. The screen comes up fine, but the box that I am creating does not show up. Thanks for your help!

Relic
08-05-2003, 05:23 AM
You're calling the default CDialog::OnPaint message, doesn't that overwrite your drawing done in Render?
Step through the debugger in single steps and look what's drawn when.
You might want to try single buffering using glFinish() instead of swap buffers for that.

Other stuff:
You're missing a PreCreateWindow function which adds some necessary styles to the window class:
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
Request a 24 bit depth buffer.
ChoosePixelFormat is unreliable, check what you got with DescribePixelFormat() afterwards.

[This message has been edited by Relic (edited 08-05-2003).]

dlswimmer
08-05-2003, 06:01 AM
Thank you for your reply, my only concern is that I have done other programs with the same format the I am currently using and they worked fine, I didn't have the precreatewindow function and I used the same style that I have described here... I will try what you said and see if it works, but I am just puzzled as to why it isn't working now.

Jared@ETC
08-05-2003, 06:27 AM
Try overriding OnEraseBkgrnd to always return TRUE or FALSE.

[This message has been edited by Jared@ETC (edited 08-05-2003).]

dlswimmer
08-06-2003, 03:47 AM
I found out what was wrong, the only reason was, that I didn't have the dialog set to overlapped... why does it have to be that? I had it set as a popup, but I guess that doesn't work. Thank you for your help!