CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2001
    Location
    Pretoria, Gauteng
    Posts
    46

    OpenGL Component for VC++

    Hey there. Anyone know of an OGL component/control for VC++, Something that takes the hassel out of setting up an OGL viewport (and lets one use the viewport on a form with other controls) I have managed to find a great one for Borland's C++ Builder,but nothing for VC++.
    Thanks

    ---
    Matt Benic

    Im not a complete idiot!!
    There are bits missing..

  2. #2
    Join Date
    May 1999
    Location
    UK (South)
    Posts
    93

    Re: OpenGL Component for VC++

    I have used Scenelib. It is Shareware I think you can download from

    http://www.marcus-software.ch/scenelib


  3. #3
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: OpenGL Component for VC++

    Here is a custom CStatic control that I wrote which
    displays openGL() graphics. Below is the source
    code for OpenGLStatic.h and OpenGLstatic.cpp.

    Usage : in the dialog's .h file :


    #include "OpenGLstatic.h"
    //
    //
    //
    // add member variable :
    //
    COpenGLstatic m_oglStatic;
    //




    In the dialog's OnInitDialog :

    //
    m_oglStatic.Create("openGLstatic",WS_VISIBLE,CRect(0,0,100,100),
    this,IDC_OGLSTATIC); // replace IDC_OGLSTATIC with your ID
    //




    The files for the custom class :



    //
    // OpenGLstatic.h
    //


    #if !defined(AFX_OPENGLSTATIC_H__A11A3E7B_C265_4A12_AEFD_C1EC929D17F8__INCLUDED_)
    #define AFX_OPENGLSTATIC_H__A11A3E7B_C265_4A12_AEFD_C1EC929D17F8__INCLUDED_

    #if _MSC_VER >= 1000
    #pragma once
    #endif // _MSC_VER >= 1000
    // OpenGLstatic.h : header file
    //

    /////////////////////////////////////////////////////////////////////////////
    // COpenGLstatic window

    class COpenGLstatic : public CStatic
    {
    // Construction
    public:
    COpenGLstatic();

    // Attributes
    public:

    HGLRC m_hRC; //Rendering Context
    CDC* m_pDC; //Device Context

    // Operations
    public:

    BOOL SetupPixelFormat();

    // Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(COpenGLstatic)
    protected:
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    //}}AFX_VIRTUAL

    // Implementation
    public:
    virtual ~COpenGLstatic();

    // Generated message map functions
    protected:
    //{{AFX_MSG(COpenGLstatic)
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnPaint();
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
    };

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

    //{{AFX_INSERT_LOCATION}}
    // Microsoft Developer Studio will insert additional declarations immediately before the previous line.

    #endif // !defined(AFX_OPENGLSTATIC_H__A11A3E7B_C265_4A12_AEFD_C1EC929D17F8__INCLUDED_)


    //
    // OpenGLstatic.cpp : implementation file
    //

    #include "stdafx.h"
    #include "OpenGLstatic.h"

    #include < GL/gl.h >

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // COpenGLstatic

    COpenGLstatic::COpenGLstatic()
    {
    }

    COpenGLstatic::~COpenGLstatic()
    {
    }


    BEGIN_MESSAGE_MAP(COpenGLstatic, CStatic)
    //{{AFX_MSG_MAP(COpenGLstatic)
    ON_WM_ERASEBKGND()
    ON_WM_CREATE()
    ON_WM_PAINT()
    //}}AFX_MSG_MAP
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // COpenGLstatic message handlers

    BOOL COpenGLstatic::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Add your specialized code here and/or call the base class

    cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC,
    ::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
    cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;


    return CStatic::PreCreateWindow(cs);
    }

    BOOL COpenGLstatic::OnEraseBkgnd(CDC* pDC)
    {
    // TODO: Add your message handler code here and/or call default

    //return CStatic::OnEraseBkgnd(pDC);
    return TRUE; // openGL does this
    }

    int COpenGLstatic::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    if (CStatic::OnCreate(lpCreateStruct) == -1)
    return -1;

    // TODO: Add your specialized creation code here

    //Get a DC for the Client Area
    m_pDC = new CClientDC(this);

    //Failure to Get DC
    if( m_pDC == NULL )
    return FALSE;

    if( !SetupPixelFormat() )
    return FALSE;

    //Create Rendering Context
    m_hRC = ::wglCreateContext( m_pDC->GetSafeHdc() );

    //Failure to Create Rendering Context
    if( m_hRC == 0 )
    return FALSE;

    //Make the RC Current
    if( ::wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC ) == FALSE )
    return FALSE;


    return 0;
    }


    BOOL COpenGLstatic::SetupPixelFormat()
    {
    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
    16, // 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, // accumulation bits ignored
    16, // 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( m_pDC->GetSafeHdc(), &pfd );

    if ( m_nPixelFormat == 0 )
    return FALSE;

    return ::SetPixelFormat( m_pDC->GetSafeHdc(), m_nPixelFormat, &pfd );
    }

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

    // TODO: Add your message handler code here
    //
    // just do something simple for testing
    //
    glClear(GL_COLOR_BUFFER_BIT);
    //
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0f,2.0f,-1.0f,2.0f,-1.0f,1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //
    glColor3f(1.0f,0.0f,0.0f);
    glBegin(GL_POLYGON);
    glVertex3f(0.0f,0.0f,0.0f);
    glVertex3f(1.0f,0.0f,0.0f);
    glVertex3f(1.0f,1.0f,0.0f);
    glEnd();
    glFlush();



    // Do not call CStatic::OnPaint() for painting messages
    }
    //









  4. #4
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: OpenGL Component for VC++

    Hi,

    I have used this code so many years ago and it has most of the time worked fine (and is working fine now on my operating system). However, on some computers of other people, it sometimes shows two types of erratic behaviour:

    1. There is nothing drawn on screen, the static control remains a grey area. Sometimes this happens immediately, sometimes after reopening the control, sometimes not at all
    2. On some computers the text (see below) is not drawn or you can see it flicker briefly

    I use virtually the same code, apart from:

    Code:
    COpenGLstatic::~COpenGLstatic()
    {
    	delete m_pDC;
    }
    Was this forgotten in the original or am I wrong? Isn't also an initialisation in the constructor, m_pDC = NULL; necessary?


    Code:
    void COpenGLstatic::OnPaint()
    {
     CPaintDC dc(this);
    	
     glClearColor(1.0f,1.0f,1.0f,1.0f);
     glClear(GL_COLOR_BUFFER_BIT);
    	
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
    
     glOrtho(-1,1,-1,1,-1,1);
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
    
     glBegin(GL_LINES);
       // Draw some stuff
     glEnd();
    
     SwapBuffers(wglGetCurrentDC());
    
     /* Write out some text */
    	 
     m_pDC->TextOut(2,150,"Test"); // Or should writing out text be BEFORE SwapBuffers?
    }
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: OpenGL Component for VC++

    1) Yes, the two omissions that you mentioned should be there.

    2) In the PIXELFORMATDESCRIPTOR, this line looks strange:

    Code:
    // PFD_DOUBLEBUFFER, // double buffered
    This not only takes away double buffering, but it changes the number
    of values.

    3) I have never mixed openGL graphics with TextOut. I either use native openGL
    text output, or data from Bob Pendleton's polyfonts (not his code, just the font data).

  6. #6
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: OpenGL Component for VC++

    Quote Originally Posted by Philip Nicoletti View Post
    2) In the PIXELFORMATDESCRIPTOR, this line looks strange:

    Code:
    // PFD_DOUBLEBUFFER, // double buffered
    In my code, it's not there:

    Code:
    	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
    		16,                               // 24-bit color depth
    		0, 0, 0, 0, 0, 0,                 // color bits ignored
    		16,                               // no alpha buffer
    		0,                                // shift bit ignored
    		0,                                // no accumulation buffer
    		0, 0, 0, 0,                       // accumulation bits ignored
    		16,                               // 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
    	};
    Maybe I removed it back in the day for a good reason, I don't know. It's over a decade old.

    Quote Originally Posted by Philip Nicoletti View Post
    3) I have never mixed openGL graphics with TextOut. I either use native openGL
    text output, or data from Bob Pendleton's polyfonts (not his code, just the font data).
    Didn't think about it. I did it just out of convenience and because it worked. Maybe I'll indeed use native OpenGL text output to try and avoid the text rendering problem alright. Thanks.
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

  7. #7
    Join Date
    Oct 2001
    Location
    lake of fire and brimstone
    Posts
    1,628

    Re: OpenGL Component for VC++

    Any hints on how to use OpenGL fonts? I'm using the following functions below, BuildFont is called in OnCreate, KillFont in OnDestroy, then e.g.:

    glRasterPos3d(0.5, 0.5, 0.50 );

    PrintString(FontBase,"Test");

    But no text is drawn?



    Code:
    void COpenGLstatic::PrintString(GLint base, char *text, ...)
    {
     char string[256];											// Holds Our String
     va_list ap;												// Pointer To List Of Arguments
    
     if (text==NULL) return;									// If There's No Text Do Nothing
    
     va_start(ap, text);										// Parses The String For Variables
     vsprintf(string, text, ap);								// And Converts Symbols To Actual Numbers
     va_end(ap);												// Results Are Stored In Text
    
     glPushAttrib(GL_LIST_BIT);									// Pushes The Display List Bits
     glListBase(base - 32);										// Sets The Base Character to 32
     glCallLists(strlen(string), GL_UNSIGNED_BYTE, string);		// Draws The Display List Text
     glPopAttrib();												// Pops The Display List Bits
    }
    
    
    
    void COpenGLstatic::BuildFont()								// Build Our Bitmap Font
    {
     HFONT	Font;												// Windows Font ID
     HFONT	OldFont;											// Used For Good House Keeping
    
     FontBase = glGenLists(96);									// Storage For 96 Characters
    
     Font = CreateFont(	-12,									// Height Of Font
    					0,										// Width Of Font
    					0,										// Angle Of Escapement
    					0,										// Orientation Angle
    					FW_BOLD,								// Font Weight
    					FALSE,									// Italic
    					FALSE,									// Underline
    					FALSE,									// Strikeout
    					ANSI_CHARSET,							// Character Set Identifier
    					OUT_TT_PRECIS,							// Output Precision
    					CLIP_DEFAULT_PRECIS,					// Clipping Precision
    					ANTIALIASED_QUALITY,					// Output Quality
    					FF_DONTCARE|DEFAULT_PITCH,				// Family And Pitch
    					"Courier New");							// Font Name
    
     OldFont = (HFONT)SelectObject(wglGetCurrentDC(), Font);	// Selects The Font We Want
     wglUseFontBitmaps(wglGetCurrentDC(), 32, 96, FontBase);	// Builds 96 Characters Starting At Character 32
     SelectObject(wglGetCurrentDC(), OldFont);					// Selects The Font We Want
     DeleteObject(Font);										// Delete The Font
    }
    
    
    
    void COpenGLstatic::KillFont()								// Delete The Font List
    {
     glDeleteLists(FontBase, 96);								// Delete All 96 Characters
    }
    
    void COpenGLstatic::OnDestroy()
    {
    	CStatic::OnDestroy();
    
    	KillFont();
    }
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞
    ۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞۞

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured