FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

MFC and OpenGL

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
flamaros



Joined: 02 Feb 2008
Posts: 33

PostPosted: Sun Feb 03, 2008 1:04 pm    Post subject: MFC and OpenGL Reply with quote

Hi,

I am starting an 3D application and I need to be able to render an OpenGL scene in a MFC Window. I have already create the window and I have also load the OpenGL module with the Derelict.

But my probleme is I don't know how to put the openGL render in a MFC context. I saw some tutorials on the web for c++, but they use the afx extention of MFC.

Is there an other solution to link my openGL render to the MFC window?
I d'ont need to have a control, because the openGL render will get the entiere window.
Back to top
View user's profile Send private message
Uwar



Joined: 10 Feb 2008
Posts: 11
Location: Poland

PostPosted: Mon Feb 11, 2008 1:24 pm    Post subject: Reply with quote

I think will be best if somebody will write something like this for D, please.
Back to top
View user's profile Send private message
jcc7



Joined: 22 Feb 2004
Posts: 657
Location: Muskogee, OK, USA

PostPosted: Mon Feb 11, 2008 2:08 pm    Post subject: Reply with quote

Uwar wrote:
I think will be best if somebody will write something like this for D, please.
D does have OdeFu (subtitle: "My experiences with D, OpenGL and SDL...").
Back to top
View user's profile Send private message AIM Address
odeamus



Joined: 21 Feb 2006
Posts: 78
Location: Helsinki, Finland

PostPosted: Tue Feb 12, 2008 1:59 am    Post subject: Reply with quote

jcc7 wrote:
Uwar wrote:
I think will be best if somebody will write something like this for D, please.
D does have OdeFu (subtitle: "My experiences with D, OpenGL and SDL...").


Yay! =)

Although I've been way too busy with other stuff to update the blog. And anyway I don't think it contains anything helpful for this issue, as I'm using SDL for windowing.
Back to top
View user's profile Send private message
flamaros



Joined: 02 Feb 2008
Posts: 33

PostPosted: Sun Feb 17, 2008 3:08 pm    Post subject: Reply with quote

Uwar wrote:
I think will be best if somebody will write something like this for D, please.


Thx, I think that is the simplest sample that I saw, but I am very busy at this moment to try to port it. I hope to have time a near day.
Back to top
View user's profile Send private message
flamaros



Joined: 02 Feb 2008
Posts: 33

PostPosted: Sun Feb 17, 2008 3:21 pm    Post subject: Reply with quote

jcc7 wrote:
Uwar wrote:
I think will be best if somebody will write something like this for D, please.
D does have OdeFu (subtitle: "My experiences with D, OpenGL and SDL...").


Yep, your rigth, all lesson are downloadable for D. Thx a lot, I don't saw it.

PS : The lesson 01 isn't for me, the portage is written with the SDL lib.
Rolling Eyes
Back to top
View user's profile Send private message
flamaros



Joined: 02 Feb 2008
Posts: 33

PostPosted: Mon Feb 18, 2008 3:31 pm    Post subject: Reply with quote

I corrected some error generated by the D compiler, but I can't remove the lastest :

Code:

C:\D\Projects\HelloWorld\Sources\Main.d(173): Error: undefined identifier PFD_DRAW_TO_WINDOW
C:\D\Projects\HelloWorld\Sources\Main.d(173): Error: undefined identifier PFD_SUPPORT_OPENGL
C:\D\Projects\HelloWorld\Sources\Main.d(173): Error: undefined identifier PFD_DOUBLEBUFFER
C:\D\Projects\HelloWorld\Sources\Main.d(174): Error: undefined identifier PFD_TYPE_RGBA
C:\D\Projects\HelloWorld\Sources\Main.d(177): Error: undefined identifier PFD_MAIN_PLANE


If I comment lines that use those defines it still stay some others link errors :
Code:

Main.obj(Main)
 Error 42: Symbol Undefined _wglMakeCurrent@8
Main.obj(Main)
 Error 42: Symbol Undefined _wglDeleteContext@4
Main.obj(Main)
 Error 42: Symbol Undefined _ZeroMemory@8
Main.obj(Main)
 Error 42: Symbol Undefined _wglCreateContext@4


I don't know how to remove thoses critical errors.

Here it's the full sample code I use:

Code:

import std.c.windows.windows;
import std.c.stdio;
//import c.gl.gl;

import derelict.opengl.gl;

extern(Windows)
int WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   
   switch (message)
   {
      
   case WM_CREATE:
      return 0;
      
   case WM_CLOSE:
      PostQuitMessage( 0 );
      return 0;
      
   case WM_DESTROY:
      return 0;
      
   case WM_KEYDOWN:
      switch ( wParam )
      {
         
      case VK_ESCAPE:
         PostQuitMessage(0);
         return 0;
         
      }
      return 0;
   
   default:
      return DefWindowProcA( hWnd, message, wParam, lParam );
         
   }
}

/**********************************************************/

/* Note the similarity of this code to the console D startup
 * code in \dmd\src\phobos\dmain2.d
 * You'll also need a .def file with at least the following in it:
 *   EXETYPE NT
 *   SUBSYSTEM WINDOWS
 */

extern (C) void gc_init();
extern (C) void gc_term();
extern (C) void _minit();
extern (C) void _moduleCtor();
extern (C) void _moduleUnitTests();

extern(Windows)
{
    void DestroyWindow(HANDLE);
   BOOL SwapBuffers(HDC);
   void ZeroMemory(PVOID, size_t);
   int ChoosePixelFormat(HDC, PIXELFORMATDESCRIPTOR*);
   HGLRC wglCreateContext(HDC);
   BOOL wglMakeCurrent(HDC, HGLRC  hglrc);
   BOOL wglDeleteContext(HGLRC);
}


void main()
{
   DerelictGL.load();
    gc_init();         // initialize garbage collector
    _minit();         // initialize module constructor table

   WNDCLASS wc;
   HWND hWnd;
   HDC hDC;
   HGLRC hRC;
   MSG msg;
   BOOL quit = FALSE;
   float theta = 0.0f;
   HINSTANCE hInstance = GetModuleHandleA(null);
   
   // register window class
   wc.style = CS_OWNDC;
   wc.lpfnWndProc = &WndProc;
   wc.cbClsExtra = 0;
   wc.cbWndExtra = 0;
   wc.hInstance = hInstance;
   wc.hIcon = LoadIconA( null, IDI_APPLICATION );
   wc.hCursor = LoadCursorA( null, IDC_ARROW );
//   wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
   wc.lpszMenuName = null;
   wc.lpszClassName = "GLSample";
   RegisterClassA( &wc );
   
   // create main window
   hWnd = CreateWindowA(
      "GLSample", "OpenGL Sample",
      WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
      0, 0, 256, 256,
      null, null, hInstance, null );
   
   // enable OpenGL for the window
   EnableOpenGL( hWnd, &hDC, &hRC );
   
   // program main loop
   while ( !quit )
   {
      
      // check for messages
      if ( PeekMessageA( &msg, null, 0, 0, PM_REMOVE )  )
      {
         
         // handle or dispatch messages
         if ( msg.message == WM_QUIT )
         {
            quit = TRUE;
         }
         else
         {
            TranslateMessage( &msg );
            DispatchMessageA( &msg );
         }
         
      }
      else
      {
         
         // OpenGL animation code goes here
         
         glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
         glClear( GL_COLOR_BUFFER_BIT );
         
         glPushMatrix();
         glRotatef( theta, 0.0f, 0.0f, 1.0f );
         glBegin( GL_TRIANGLES );
         glColor3f( 1.0f, 0.0f, 0.0f ); glVertex2f( 0.0f, 1.0f );
         glColor3f( 0.0f, 1.0f, 0.0f ); glVertex2f( 0.87f, -0.5f );
         glColor3f( 0.0f, 0.0f, 1.0f ); glVertex2f( -0.87f, -0.5f );
         glEnd();
         glPopMatrix();
         
         SwapBuffers( hDC );
         
         theta += 1.0f;
         
      }
      
   }
   
   // shutdown OpenGL
   DisableOpenGL( hWnd, hDC, hRC );
   
   // destroy the window explicitly
   DestroyWindow( hWnd );
   return msg.wParam;
}

// Enable OpenGL

void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
   PIXELFORMATDESCRIPTOR pfd;
   int format;
   
   // get the device context (DC)
   *hDC = GetDC( hWnd );
   
   // set the pixel format for the DC
   ZeroMemory( &pfd, pfd.sizeof );
   pfd.nSize = pfd.sizeof;
   pfd.nVersion = 1;
//   pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
//   pfd.iPixelType = PFD_TYPE_RGBA;
   pfd.cColorBits = 24;
   pfd.cDepthBits = 16;
//   pfd.iLayerType = PFD_MAIN_PLANE;
   format = ChoosePixelFormat( *hDC, &pfd );
   SetPixelFormat( *hDC, format, &pfd );
   
   // create and enable the render context (RC)
   *hRC = wglCreateContext( *hDC );
   wglMakeCurrent( *hDC, *hRC );
   
}

// Disable OpenGL

void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
   wglMakeCurrent( null, null );
   wglDeleteContext( hRC );
   ReleaseDC( hWnd, hDC );
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group