OpenGL in die MFC einbinden



  • Hi,
    ich bin grade dabei zu versuchen OpenGL kombiniert mit der MFC zu betreiben, doch so richtig klappt das nicht.
    ich habe einen Dialog und nen paar Buttons mit dem einen Button initialisiere ich open gl mit einem weiteren öffne udn shcließe ich das Fenster und mit einem dritten zeiche ich das Fenster neu.
    der code der drei buttons:

    void COpenGL100Dlg::OnButtonInitogl() 
    {
    	m_pOpenGL = new COpenGL();
    	if(m_pOpenGL!=NULL)
    	{
    		m_csInfoText = CString("OpenGL wird initialisiert");
    		UpdateData(FALSE);	
    		if(m_pOpenGL->InitOpenGL())
    		{
    			m_csInfoText = CString("OpenGL ist initialisiert");
    			UpdateData(FALSE);
    		}
    	}
    	else
    	{
    		m_csInfoText = CString("Fehler beim initialisieren von OpenGL");
    		UpdateData(FALSE);
    	}	
    }
    
    void COpenGL100Dlg::OnButtonOnOff() 
    {
    	static BOOL FirstEntry = FALSE;
    	static BOOL OGLWinStatus = FALSE;
    	if(OGLWinStatus==TRUE)
    	{
    		m_pOpenGL->ShowWindow(SW_HIDE);
    		OGLWinStatus=FALSE;
    		m_csInfoText = CString("Das Fenster ist nun unsichtbar");
    	}
    	else
    	{
    		m_pOpenGL->ShowWindow(SW_SHOW);
    		OGLWinStatus = TRUE;
    		m_csInfoText=CString("Fenster ist nun sichbar");
    	}
    
    }
    
    void COpenGL100Dlg::OnButtonRedraw() 
    {
    	m_pOpenGL->HelloOpenGL();
    }
    

    die Methodennamen sprechen für sich 🙂
    so udn hier der code der COPenGL Klasse(nur der *.cpp inhalt)

    BOOL COpenGL::InitOpenGL()
    {
    	if(Create(NULL,"OpenGL-Fenster",WS_OVERLAPPEDWINDOW,CRect(0,0,400,400),NULL,NULL,WS_EX_TOPMOST,NULL))
    	{
    		this->HelloOpenGL();
    		return TRUE;
    	}
    	return FALSE;
    }
    
    BOOL COpenGL::HelloOpenGL()
    {
    	glMatrixMode(GL_MODELVIEW);							
    	glLoadIdentity();
    	glClearColor(1.0f,1.0f,0.0f,1.0f);
    	return TRUE;
    
    }
    

    doc hdas Fenster was zwar erstellt wird bleibt einfach weiß... warum?



  • ich hab schon lang nix mehr mit den MFC gemacht, deshalb weiß ich auch nicht
    was falsch is....

    aber schau mal auf www.codeguru.com und da in dem
    navigationsbalken etwas weiter unten bei "Graphics & Multimedia"
    gibts auch n abschnitt für opengl, da findeste bestimmt was 😉

    mfg
    Plassy



  • Hi

    bin mich auch da in diese Geschichte am Einarbeiten. Aber was mir bei deinem Programm aufgefallen ist das zu gar keinen Rendering Context erstellt hast.

    Das muss aber auf jeden fall gemacht werden den dieser legt z.B. fest ob du RGBA oder nur RGB benutzen willst und vieles mehr. Schau dir mal dazu...

    hmmmmm... wie heißt die Struktr noch mal ?????? hhmmmmmm....

    weiß ich jetzt gerade net. Und nachschauen kann ich das auch net bin nämlich gerade net zu hause. Aber ich kann die mal wenn du willst ein Beispiel schicken.

    Oder am einfachsten du fragst mal Mister Google nach "MFC opengl"
    heheh 😃



  • Einen Rendering-Context sollte man schon erzeugen. 😃
    Ach ja: Seh ich das richtig oder erzeugst du da nochmal ein extra Fenster ?!
    Nur als Tipp: Du kannst jedes beliebige Fenster mit OpenGL initialisieren! Das geht sogar in Buttons 😃



  • de_Bert(GeradeNetGelogt) schrieb:

    ...wie heißt die Struktr noch mal ?????? hhmmmmmm....

    Pixelformat-Descriptor



  • moin

    @Cpp_Junky

    jup so heißt die Struktur. Wollen mal schauen ob ich was finde. So mal schauen ob ich das hin bekomme.

    Also zuerst erstellst du eine Klasse z.B. COpenGLWnd die von CWnd abgeleitet ist. In die OnCreate kommt dann dieser Quellcode rein.

    int COpenGLWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    	if (CWnd::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	// TODO: Add your specialized creation code here
    
    	PIXELFORMATDESCRIPTOR pfd =
    	{ 
    		sizeof(PIXELFORMATDESCRIPTOR),  //  size of this pfd 
    		1,                     // version number 
    		PFD_DRAW_TO_WINDOW |   // support window 
    		PFD_SUPPORT_OPENGL,    // support OpenGL 
    		24,                    // 24-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,            // accum bits ignored 
    		32,                    // 32-bit z-buffer (depth)
    		0,                     // no stencil buffer 
    		0,                     // no auxiliary buffer 
    		PFD_MAIN_PLANE,        // main layer 
    		0,                     // reserved 
    		0, 0, 0                // layer masks ignored 
        }; 
    
    	CClientDC dc(this);
    
    	// Get the best available match of pixel format for the device context
    	// In other words, if this computer doesn't support features that I
    	// asked for, try to get the next best thing.  i.e. 16-bit color mode
    	// instead of 24-bit color mode.
    	int pixelFormat = ChoosePixelFormat(dc.m_hDC, &pfd);
    
    	// Set the pixel format to the best pixel format I can get (see above)
    	// and if that operation fails, bring up a message box that tells the
    	// user the error.
    	if (!SetPixelFormat(dc.m_hDC, pixelFormat, &pfd))
    	{
    		MessageBox("Error: Unable to Set Pixel Format in CGLTemplate1View::OnCreate()",
    			"Application Error", MB_ICONERROR);
    	}
    
    	// Creates an OpenGL rendering context so that OpenGL knows how to draw
    	// to this view. You can't use OpenGL in MFC without using the handle
    	// that this function returns
    	m_hRC = wglCreateContext(dc.m_hDC);
    
    	wglMakeCurrent(dc.m_hDC, m_hRC);
    		// Enable the depth test so that things drawn in front
    		// of other things will cover up the things behind it
    		glEnable(GL_DEPTH_TEST);
    
    		// Set the OpenGL background color to a medium gray
    		glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    
    	wglMakeCurrent(NULL, NULL);
    
    	return 0;
    }
    

    Dann kommt die OnPaint Methode. Da kommen dann halt alle OpenGL Befehle rein.

    void COpenGLWnd::OnPaint() 
    {
    	CPaintDC dc(this); // device context for painting
    
    	// TODO: Add your message handler code here
    
    	// Tell windows that its OpenGL time
    	wglMakeCurrent(dc.m_hDC, m_hRC);
    
    	// Tell OpenGL to clear the screen and redo the
    	// depth calculations
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    	// Reset all of the transformations
    	glLoadIdentity();
    
             // hier kommen die Opengl Befehle rein.
    
    	// Tell windows that I'm done with OpenGL
    	wglMakeCurrent(NULL, NULL);
    }
    

    Und zu aller letzt kommt dann die Membervariable in die Klasse rein

    // The OpenGL Resource context, you need this
    // when you do OpenGL drawing
    HGLRC m_hRC;
    

    so das war die Opengl Klasse. Nun musst du noch das in die OnInitDialog() einfügen.

    // This single line of code puts the OpenGL window into the
    // dialog box. Without it, this program would be really boring
    // and pointless.  Look up the CWnd::Create() function for
    // info on how to use it.
    m_wndOpenGL.Create(NULL, "COpenGLWnd", WS_CAPTION | WS_CHILD | WS_VISIBLE, CRect(10, 10, 310, 310), this, 1234);
    

    so das müsste funzen. Hoffendlich 😃 wenn nicht dann poste mal deine Email adresse und ich schicke dir das komplette Beispiel



  • oder schau mal hier nach da steht alles

    http://pws.prserv.net/mfcogl/ hehe


Anmelden zum Antworten