OpenGL and MFC under Visual C++ .NET 2003

HI,
i’m italian so excuse me for my english.
I’ve a problem. I make an MFC application and i have to insert OpenGL code in it.But It doesn’t works.

// OpenGLView.cpp : implementazione della classe COpenGLView
//

#include “stdafx.h”
#include “OpenGL.h”

#include “OpenGLDoc.h”
#include “OpenGLView.h”
#include “.\openglview.h”

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// COpenGLView

IMPLEMENT_DYNCREATE(COpenGLView, CView)

BEGIN_MESSAGE_MAP(COpenGLView, CView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()

// costruzione/eliminazione di COpenGLView

COpenGLView::COpenGLView()
{
// TODO: aggiungere qui il codice di costruzione.
hglrc = NULL;
myhwnd = NULL;
myhdc = NULL;

}

COpenGLView::~COpenGLView()
{
}

BOOL COpenGLView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: modificare la classe o gli stili Window modificando
// CREATESTRUCT cs.
cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC,
::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;

return CView::PreCreateWindow(cs);

}

// disegno di COpenGLView

void COpenGLView::OnDraw(CDC* /pDC/)
{
COpenGLDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: aggiungere qui il codice di disegno per i dati nativi.
wglMakeCurrent(myhdc, hglrc);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

DrawGLScene();

SwapBuffers(myhdc);

}

// diagnostica di COpenGLView

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

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

COpenGLDoc* COpenGLView::GetDocument() const // la versione non debug è inline.
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(COpenGLDoc)));
return (COpenGLDoc*)m_pDocument;
}
#endif //_DEBUG

// gestori di messaggi di COpenGLView

// Setta il PIXELFORMATDESCRIPTOR
bool COpenGLView::myPixelFormat(HDC hdc)
{

PIXELFORMATDESCRIPTOR pfd;

ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
			  PFD_SUPPORT_OPENGL |
			  PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;

int iPixelFormat = ChoosePixelFormat(hdc, &pfd);

if(!iPixelFormat)
	{
	::MessageBox(0,"Can't Find A Suitable PixelFormat.","Error",MB_OK|MB_ICONERROR);
	PostQuitMessage(0);			// This Sends A 'Message' Telling The Program To Quit
	return false ;						// Prevents The Rest Of The Code From Running
	//iPixelFormat = 1;

	//if(!DescribePixelFormat(hdc, iPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd)) return false;
	}

if(SetPixelFormat(hdc, iPixelFormat, &pfd) == false)
	{
	::MessageBox(0,"Can't Set The PixelFormat.","Error",MB_OK|MB_ICONERROR);
	PostQuitMessage(0);
	return false;
	}

return false;
}

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

// TODO:  Aggiungere qui il codice di creazione specifico.
myhwnd = GetSafeHwnd();
myhdc = ::GetDC(myhwnd);

if(!myPixelFormat(myhdc)) return 0;

hglrc = wglCreateContext(myhdc);

if(!hglrc)
	{
	::MessageBox(0,"Can't Create A GL Rendering Context.","Error",MB_OK|MB_ICONERROR);
	PostQuitMessage(0);
	return false;
	}

if(hglrc != NULL)
	wglMakeCurrent(myhdc, hglrc);

if(!wglMakeCurrent(myhdc, hglrc))
	{
	::MessageBox(0,"Can't activate GLRC.","Error",MB_OK|MB_ICONERROR);
	PostQuitMessage(0);
	return false;
	}

return 0;
}

void COpenGLView::OnDestroy()
{
// TODO: aggiungere qui il codice per la gestione dei messaggi.
if(wglGetCurrentContext() != NULL)
{
wglMakeCurrent(NULL, NULL);
}

if(hglrc != NULL)
	{
	wglDeleteContext(hglrc);
	hglrc = NULL;
	}

	CView::OnDestroy();
}

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

// TODO: aggiungere qui il codice per la gestione dei messaggi.

wglMakeCurrent(myhdc, hglrc);

glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(0.0, cx, 0.0, cy, -10.0, 10.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
wglMakeCurrent(NULL, NULL);

}

BOOL COpenGLView::OnEraseBkgnd(CDC* pDC)
{
// TODO: aggiungere qui il codice per la gestione dei messaggi e/o chiamare il codice predefinito.

return CView::OnEraseBkgnd(pDC);
}

void COpenGLView::DrawGLScene(void)
{

glShadeModel(GL_SMOOTH);							// Abilita lo Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Colore di sfondo Nero
glClearDepth(1.0f);									// Setta il Depth Buffer 
glEnable(GL_DEPTH_TEST);							// Abilita il Depth Testing
glDepthFunc(GL_LEQUAL);								// Il tipo di Depth Testing da fare
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Calcoli per la Prospettiva ideali
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Pulisce lo schermo e il Depth Buffer
glLoadIdentity();									// Resetta la Modelview Matrix corrente

glBegin(GL_TRIANGLES);								// Disegna usando i triangoli
	glColor3f(1.0f,0.0f,0.0f);						// Setta il colore Rosso
	glVertex3f( 0.0f, 1.0f, 0.0f);					// Sopra
	glColor3f(0.0f,1.0f,0.0f);						// Setta il colore Verde
	glVertex3f(-1.0f,-1.0f, 0.0f);					// Sotto a sinistra
	glColor3f(0.0f,0.0f,1.0f);						// Setta il colore Blu
	glVertex3f( 1.0f,-1.0f, 0.0f);					// Sotto a destra
glEnd();											// Finito di disegnare il triangolo

glFlush();

}

Where is the error???

help me please!!!

What is this project trying to do? It doesn’t include any of the regular open gl headers like glu.h or gl.h. Try and find another project that does that same thing and that compiles… :stuck_out_tongue:

this is the main file of a Project made with MFC APP WIZARD. It compile with no problem. When i run it, in the view of the window instead of my Opengl code are visualized black and grey rectangles or, if i run a opengl program (game or other) is visualized the last frame of the application.If you want i post you my project.
:eek:

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