#10 OpenGL

closed
nobody
Widgets (5)
5
2005-12-10
2005-11-19
Anonymous
No

SmartWin++ will have support to OpenGL?

Discussion

  • andrew7
    andrew7
    2005-11-28

    Logged In: YES
    user_id=1157678

    SmartWin++ has no special classes for OpenGL at the
    moment. However, I was able to port a OpenGL sample from
    Ron Fosner's OpenGL Programming for Win95/NT to SmartWin++
    without too much trouble. (See attached file) And Kostas
    has written an OpenGL game too. (Search back through the
    Forums)

    I'll work on OpenGL sample that shows more SmartWin++
    interaction with OpenGL.

    /* Show a little OpenGL
    */

    #include "SmartWin.h"
    using namespace SmartWin;

    #include <GL/gl.h> // OpenGL interface
    #include <GL/glu.h> // OpenGL utility Library interface

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

    class OpenGLWin
    : public WidgetFactory< WidgetWindow, OpenGLWin >
    {

    private:
    HGLRC hRC;
    PAINTSTRUCT ps;
    HDC hDC;
    HWND hWnd;

    public:

    void init( )
    {
    onCreate( &OpenGLWin::whenCreate );
    CreationalStruct cs=
    getDefaultWindowNonChildCreationalStruct();
    cs.style |= CS_HREDRAW | CS_VREDRAW |
    WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
    createWindow( cs );
    // addRemoveStyle( , true );

    setText( _T("OpenGLWin") );

    COLORREF itsColor= RandColor();

    onSized( &OpenGLWin::isResized );
    onPainting( &OpenGLWin::painting );
    onClosing( &OpenGLWin::willClose );
    }

    //----------------------------------------------------------
    --------

    private:

    void whenCreate( const SmartWin::CreationalStruct &
    cs )
    {
    // Select a pixel format and then
    // create a rendering context from it.
    hRC = SetUpOpenGL( this->handle() );

    }

    //----------------------------------------------------------
    --------

    COLORREF RandColor()
    {
    return( RGB( rand()%255, rand()%255, rand()%255 ) );
    }

    //----------------------------------------------------------
    --------

    // Called when the window needs to be redrawn
    //
    void painting( SmartWin::Canvas & c )
    {
    // Get a DC, then make the RC current and
    // associated with this DC
    // hDC = BeginPaint( hWnd, &ps );
    hDC = c.getDc();
    wglMakeCurrent( hDC, hRC );

    DrawOpenGLScene();

    // we're done with the RC, so
    // deselect it
    // (note: This technique is not recommended!)
    wglMakeCurrent( NULL, NULL );

    // EndPaint( hWnd, &ps );
    }

    //----------------------------------------------------------
    ---------------

    // There are new dimensions to the window because
    the user resized the window.
    //
    void isResized( const
    SmartWin::WidgetSizedEventResult & sz )
    {
    // Redefine the viewing volume and viewport
    // when the window size changes.

    // Make the RC current since we're going to
    // make an OpenGL call here...
    hWnd= this->handle();
    hDC = GetDC ( hWnd );

    wglMakeCurrent (hDC, hRC);

    // get the new size of the client window
    // note that we size according to the height,
    // not the smaller of the height or width.
    // glnWidth = (GLsizei) LOWORD (lParam);
    // glnHeight = (GLsizei) HIWORD (lParam);
    GLsizei glnWidth= sz.newSize.x;
    GLsizei glnHeight= sz.newSize.y;

    GLdouble gldAspect = (GLdouble)glnWidth / (GLdouble)
    glnHeight;

    // set up a projection matrix to fill the
    // client window
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    // a perspective-view matrix...
    gluPerspective(
    30.0, // Field-of-view angle
    gldAspect, // Aspect ratio of view volume
    1.0, // Distance to near clipping plane
    10.0 ); // Distance to far clipping plane

    glViewport( 0, 0, glnWidth, glnHeight );
    wglMakeCurrent( NULL, NULL );
    ReleaseDC( hWnd, hDC );

    // updateWidget();
    }

    //----------------------------------------------------------
    ---------------

    bool willClose()
    {
    // Clean up and terminate.
    wglDeleteContext( hRC );
    return true;
    }

    /////////////////////////////////////////////////////////
    // SetUpOpenGL sets the pixel format and a rendering
    // context then returns the RC
    /////////////////////////////////////////////////////////
    HGLRC SetUpOpenGL( HWND hWnd )
    {
    static PIXELFORMATDESCRIPTOR pfd = {
    sizeof (PIXELFORMATDESCRIPTOR), // strcut size
    1, // Version number
    PFD_DRAW_TO_WINDOW | // Flags, draw to a window,
    PFD_SUPPORT_OPENGL, // use OpenGL
    PFD_TYPE_RGBA, // RGBA pixel values
    24, // 24-bit color
    0, 0, 0, // RGB bits & shift sizes.
    0, 0, 0, // Don't care about them
    0, 0, // No alpha buffer info
    0, 0, 0, 0, 0, // No accumulation buffer
    32, // 32-bit depth buffer
    0, // No stencil buffer
    0, // No auxiliary buffers
    PFD_MAIN_PLANE, // Layer type
    0, // Reserved (must be 0)
    0, // No layer mask
    0, // No visible mask
    0 // No damage mask
    };

    int nMyPixelFormatID;
    HDC hDC;
    HGLRC hRC;

    hDC = GetDC( hWnd );
    nMyPixelFormatID = ChoosePixelFormat( hDC, &pfd );

    // catch errors here.
    // If nMyPixelFormat is zero, then there's
    // something wrong... most likely the window's
    // style bits are incorrect (in CreateWindow() )
    // or OpenGl isn't installed on this machine

    SetPixelFormat( hDC, nMyPixelFormatID, &pfd );

    hRC = wglCreateContext( hDC );
    ReleaseDC( hWnd, hDC );

    return hRC;
    }

    //////////////////////////////////////////////////////////
    // DrawScene uses OpenGL commands to draw a cube.
    // This is where the OpenGL drawing commands live
    //////////////////////////////////////////////////////////

    void DrawOpenGLScene( )
    {

    //
    // Enable depth testing and clear the color and depth
    // buffers.
    //
    glEnable( GL_DEPTH_TEST );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    //
    // Define the modelview transformation.
    //
    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // move the viewpoint out to where we can see everything
    glTranslatef( 0.0f, 0.0f, -5.0f );

    // Draw a large triangle out of three smaller triangles
    // sharing common vertex colors

    // Upper left triangle
    glBegin( GL_POLYGON );
    glColor3f( 0.0f, 0.0f, 0.0f ); // black center
    glVertex3f( 0.0f, 0.0f, 0.0f);
    glColor3f( 0.0f, 1.0f, 0.0f ); // left vertex green
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glColor3f( 1.0f, 0.0f, 0.0f ); // upper vertex red
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glEnd();

    // bottom triangle
    glBegin( GL_POLYGON );
    glColor3f( 0.0f, 0.0f, 0.0f ); // black center
    glVertex3f( 0.0f, 0.0f, 0.0f);
    glColor3f( 0.0f, 0.0f, 1.0f ); // right vertex blue
    glVertex3f( 1.0f, -1.0f, 0.0f);
    glColor3f( 0.0f, 1.0f, 0.0f ); // left vertex green
    glVertex3f(-1.0f, -1.0f, 0.0f);
    glEnd();

    // upper right triangle
    glBegin( GL_POLYGON );
    glColor3f( 0.0f, 0.0f, 0.0f ); // black center
    glVertex3f( 0.0f, 0.0f, 0.0f);
    glColor3f( 1.0f, 0.0f, 0.0f ); // upper vertex red
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f( 0.0f, 0.0f, 1.0f ); // bottom right blue
    glVertex3f( 1.0f, -1.0f, 0.0f);
    glEnd();

    // Flush the drawing pipeline since it's single buffered
    glFlush ();

    }

    };

     
  • Thomas Hansen
    Thomas Hansen
    2005-12-10

    Logged In: YES
    user_id=868615

    I've added a similar sample (built from this one) into the
    CVS...

    .t

     
  • Thomas Hansen
    Thomas Hansen
    2005-12-10

    • status: open --> closed