C++ OpenGL窗口总是很忙

C++ OpenGL窗口总是很忙,c++,opengl,C++,Opengl,我有一个小框架,它在无边界屏幕窗口中显示纹理,但当我启动它时,当我激活OpenGL窗口时,应用程序显示一个忙碌的鼠标图标,我似乎无法理解它来自何处 以下代码是完整的解决方案(VisualStudio2010),应该按原样编译。 不幸的是,我甚至不知道从哪里开始查找,因此我在这里复制了完整的代码。 BasicProject创建一个随机纹理,并将其导入OpenGL窗口 有人能看到为什么代码被困在繁忙的窗口中吗? OpenGL回调从未到达,但我不明白为什么不 BasicProject.cpp(入口点)

我有一个小框架,它在无边界屏幕窗口中显示纹理,但当我启动它时,当我激活OpenGL窗口时,应用程序显示一个忙碌的鼠标图标,我似乎无法理解它来自何处

以下代码是完整的解决方案(VisualStudio2010),应该按原样编译。 不幸的是,我甚至不知道从哪里开始查找,因此我在这里复制了完整的代码。 BasicProject创建一个随机纹理,并将其导入OpenGL窗口

有人能看到为什么代码被困在繁忙的窗口中吗? OpenGL回调从未到达,但我不明白为什么不

BasicProject.cpp(入口点):

/**OpenGL显示功能的示例项目
*
*安东·罗斯,2013年
*/
#包括“stdafx.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括“OpenGL Display.h”
void RunOpenGL(void*pParams);
bool applicationRunning=true;
柄线规;
int _tmain(int argc,_TCHAR*argv[]{
OGD::OpenGLDisplay_Init(6404802000,0,“第一次显示”);
threadOGL=(HANDLE)u beginthread(RunOpenGL,0,NULL);
而(!\u kbhit()){
睡眠(500);
}
applicationRunning=false;
WaitForSingleObject(threadOGL,500);
返回0;
}
无效运行程序(无效*pParams){
矢量纹理;
纹理。调整大小(640*480*3);
标准:srand(时间(0));
while(应用程序运行){
对于(int i=0;i<640*480*3;++i){
纹理.at(i)=标准::兰德()%255;
}
OGD::OpenGLDisplay_包装器(&texture.at(0)、640、480、false、24、0);
int x,y;
OGD::OpenGLDisplay_MousePos(x,y,0);
睡眠(300);
}
}
OpenGL Display.h:

#ifndef __OPENGLDISPLAY
#define __OPENGLDISPLAY
#include <windows.h>        // Header File For Windows
#include <stdio.h>          // Header File For Standard Input/Output
#include <gl\gl.h>          // Header File For The OpenGL32 Library
#include <gl\glu.h>         // Header File For The GLu32 Library
#include <vector>

namespace OGD
{
    class OpenGL_Display
    {
        public:
            OpenGL_Display(int newId);
            ~OpenGL_Display();
            void Init_Display(int width, int height, int posX, int posY, LPCSTR className);
            void DrawNewImage(int width, int height, char* imageData, bool flip, int bpp);
            void GetMouseCords(int& x, int& y);
            int ID;
        protected:
            bool CreateGLWindow(char* title, int bits, int posX, int posY, LPCSTR className);
            GLvoid ReSizeGLScene(GLsizei width, GLsizei height);
            int LoadGLTextures();
            int InitGL(GLvoid);
            void UpdateGLBuffers(int width, int height, char* imageData, int bpp);
            int DrawGLScene(bool flip);
            GLvoid KillGLWindow(GLvoid);
            HDC         hDC;        // Private GDI Device Context
            static LRESULT  CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);   // Declaration For WndProc

            HGLRC       hRC;        // Permanent Rendering Context
            HWND        hWnd;       // Holds Our Window Handle
            HINSTANCE   hInstance;      // Holds The Instance Of The Application

            bool    keys[256];          // Array Used For The Keyboard Routine
            bool    active;     // Window Active Flag Set To TRUE By Default
            bool    fullscreen; // Fullscreen Flag Set To Fullscreen Mode By Default
            bool    g_bExitThread; // end thread
            GLuint  texture;            // Bitmap image buffer
            int     g_iCounter;     // Current buffer being grabbed to
            bool    g_GLdone;   // Counter to make sure no unnecessary updates are made which cause CPU load
            int xPos;
            int yPos;
            int xPos_right;
            int yPos_right;
            int width;
            int height;
    };

    static std::vector<OGD::OpenGL_Display*> oglDisp;

    void OpenGLDisplay_Wrapper(char* image, int width, int height, bool flip, int bpp, int ID);
    void OpenGLDisplay_Init(int width, int height, int posX, int posY, int ID, LPCSTR className);
    void OpenGLDisplay_MousePos(int&x, int&y, int ID);
};

#endif
\ifndef\uu OPENGLDISPLAY
#定义OPENGLDISPLAY
#包含//Windows的头文件
#包含//标准输入/输出的头文件
#包含//OpenGL32库的头文件
#包含//GLu32库的头文件
#包括
名称空间OGD
{
类OpenGL_显示
{
公众:
OpenGL_显示(int-newId);
~OpenGL_Display();
void Init_显示(int-width、int-height、int-posX、int-posY、LPCSTR类名称);
void DrawNewImage(int-width、int-height、char*imageData、bool-flip、int-bpp);
void GetMouseCords(int&x,int&y);
int-ID;
受保护的:
bool CreateGLWindow(char*title,int位,int posX,int posY,LPCSTR类名);
GLvoid ReSizeGLScene(GLsizei宽度、GLsizei高度);
int LoadGLTextures();
int InitGL(GLvoid);
void UpdateGLBuffers(int-width、int-height、char*imageData、int-bpp);
int-DrawGLScene(bool-flip);
GLvoid KillGLWindow(GLvoid);
HDC HDC;//专用GDI设备上下文
静态LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);//WndProc的声明
HGLRC hRC;//永久呈现上下文
HWND HWND;//保存窗口句柄
HINSTANCE HINSTANCE;//保存应用程序的实例
布尔键[256];//用于键盘例程的数组
bool active;//窗口活动标志默认设置为TRUE
bool fullscreen;//全屏标志默认设置为全屏模式
bool g_bExitThread;//结束线程
GLuint纹理;//位图图像缓冲区
int g_i计数器;//当前缓冲区被抓取到
bool g_GLdone;//计数器,以确保不会进行导致CPU负载的不必要更新
int XPO;
int yPos;
int xPos_右;
int yPos_右;
整数宽度;
内部高度;
};
静态std::矢量oglDisp;
void OpenGLDisplay_包装(char*image、int-width、int-height、bool-flip、int-bpp、int-ID);
void OpenGLDisplay_Init(int-width、int-height、int-posX、int-posY、int-ID、LPCSTR-className);
void OpenGLDisplay_鼠标点(int&x、int&y、int-ID);
};
#恩迪夫
OpenGL-Display.cpp:

/** Based on code by NeHe tutorials
    Adapted to display live camera display with OpenGL
*/

#include "OpenGL-Display.h"

using namespace OGD;

OpenGL_Display::OpenGL_Display(int newId)
{
    xPos = -1;
    yPos = -1;
    ID = newId;
    width = 1;
    height = 1;
}

OpenGL_Display::~OpenGL_Display()
{
    KillGLWindow();
}

void OpenGL_Display::Init_Display(int newWidth, int newHeight, int posX, int posY, LPCSTR inputName)
{
    width = newWidth;
    height = newHeight;
    className = inputName;

    if (!CreateGLWindow((char*)className, 24, posX, posY, className))
    {
        throw;
    }   
}

LRESULT CALLBACK OpenGL_Display::WndProc(   HWND    hWnd,           // Handle For This Window
                            UINT    uMsg,           // Message For This Window
                            WPARAM  wParam,         // Additional Message Information
                            LPARAM  lParam)         // Additional Message Information
{
    switch (uMsg)                                   // Check For Windows Messages
    {
        case WM_SYSCOMMAND:                         // Intercept System Commands
        {
            switch (wParam)                         // Check System Calls
            {
                case SC_SCREENSAVE:                 // Screensaver Trying To Start?
                case SC_MONITORPOWER:               // Monitor Trying To Enter Powersave?
                return 0;                           // Prevent From Happening
            }
            break;                                  // Exit
        }

        case WM_CLOSE:                              // Did We Receive A Close Message?
        {
            PostQuitMessage(0);                     // Send A Quit Message
            return 0;                               // Jump Back
        }

        case WM_SIZE:                               // Resize The OpenGL Window
        {
            oglDisp.at(0)->ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
            return 0;                               // Jump Back
        }
    }

    // Pass All Unhandled Messages To DefWindowProc
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


bool OpenGL_Display::CreateGLWindow(char* title, int bits, int posX, int posY, LPCSTR className)
{
    GLuint      PixelFormat;            // Holds The Results After Searching For A Match
    WNDCLASS    wc;                     // Windows Class Structure
    DWORD       dwExStyle;              // Window Extended Style
    DWORD       dwStyle;                // Window Style
    RECT        WindowRect;             // Grabs Rectangle Upper Left / Lower Right Values
    WindowRect.left=(long)0;            // Set Left Value To 0
    WindowRect.right=(long)width;       // Set Right Value To Requested Width
    WindowRect.top=(long)0;             // Set Top Value To 0
    WindowRect.bottom=(long)height;     // Set Bottom Value To Requested Height

    hInstance           = GetModuleHandle(NULL);                // Grab An Instance For Our Window
    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   // Redraw On Size, And Own DC For Window.
    wc.lpfnWndProc      = WndProc;                              // WndProc Handles Messages
    wc.cbClsExtra       = 0;                                    // No Extra Window Data
    wc.cbWndExtra       = 0;                                    // No Extra Window Data
    wc.hInstance        = hInstance;                            // Set The Instance
    wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);          // Load The Default Icon
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);          // Load The Arrow Pointer
    wc.hbrBackground    = NULL;                                 // No Background Required For GL
    wc.lpszMenuName     = NULL;                                 // We Don't Want A Menu
    wc.lpszClassName    = className;                                // Set The Class Name

    RegisterClass(&wc);                                         // Return FALSE

    dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;           // Window Extended Style
    dwStyle=WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;;                         // Windows Style

    AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size
    //HWND hwndC = GetConsoleWindow();
    // Create The Window
    hWnd=CreateWindowEx(    dwExStyle,                          // Extended Style For The Window
                                className,                          // Class Name
                                title,                              // Window Title
                                //WS_OVERLAPPEDWINDOW,
                                dwStyle |                           // Defined Window Style
                                WS_CLIPSIBLINGS |                   // Required Window Style
                                WS_CLIPCHILDREN,                    // Required Window Style
                                posX, posY,                         // Window Position
                                WindowRect.right-WindowRect.left,   // Calculate Window Width
                                WindowRect.bottom-WindowRect.top,   // Calculate Window Height
                                NULL,                               // No Parent Window
                                NULL,                               // No Menu
                                hInstance,                          // Instance
                                NULL);

    static  PIXELFORMATDESCRIPTOR pfd=              // pfd Tells Windows How We Want Things To Be
    {
        sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
        1,                                          // Version Number
        PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
        PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
        PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
        PFD_TYPE_RGBA,                              // Request An RGBA Format
        bits,                                       // Select Our 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,                                         // 16Bit Z-Buffer (Depth Buffer)  
        0,                                          // No Stencil Buffer
        0,                                          // No Auxiliary Buffer
        PFD_MAIN_PLANE,                             // Main Drawing Layer
        0,                                          // Reserved
        0, 0, 0                                     // Layer Masks Ignored
    };

    hDC=GetDC(hWnd);

    PixelFormat=ChoosePixelFormat(hDC,&pfd);

    SetPixelFormat(hDC,PixelFormat,&pfd);

    hRC=wglCreateContext(hDC);

    wglMakeCurrent(hDC,hRC);

    ShowWindow(hWnd,SW_SHOW);                       // Show The Window
    SetForegroundWindow(hWnd);                      // Slightly Higher Priority
    SetFocus(hWnd);                                 // Sets Keyboard Focus To The Window
    ReSizeGLScene(width, height);                   // Set Up Our Perspective GL Screen

    InitGL();

    return TRUE;                                    // Success
}


int OpenGL_Display::LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
{
    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit

    glGenTextures(1, &texture);                 // Create The Texture
    // Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // Minimizing filter, in case display is smaller image size
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // Magnifying filter, in case display is smaller image size

    return 1;                                       // Return The Status
}

GLvoid OpenGL_Display::ReSizeGLScene(GLsizei newWidth, GLsizei newHeight)       // Resize And Initialize The GL Window
{
    width = newWidth;
    height = newHeight;
}

int OpenGL_Display::InitGL(GLvoid)                                      // All Setup For OpenGL Goes Here
{
    if (!LoadGLTextures())                              // Jump To Texture Loading Routine
    {
        return FALSE;                                   // If Texture Didn't Load Return FALSE
    }

    return TRUE;                                        // Initialization Went OK
}

void OpenGL_Display::GetMouseCords(int& x, int& y)      // All Setup For OpenGL Goes Here
{
    x = xPos;
    y = yPos;
}

void OpenGL_Display::UpdateGLBuffers(int width, int height, char* imageData, int bpp) {
    glBindTexture(GL_TEXTURE_2D, texture); 
    switch(bpp) {
    case 8:  glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, imageData); break;
    case 16: glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, GL_RED, GL_UNSIGNED_SHORT, imageData); break;
    case 24: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); break;
    default: return;
    }

    GLenum mError = glGetError();

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
}

int OpenGL_Display::DrawGLScene(bool flip)                                  // Here's Where We Do All The Drawing
{
    if (height==0)                                      // Prevent A Divide By Zero
    {
        height=1;                                       // Making Height Equal One
    }

    glViewport(0,0,width,height);                       // Reset The Current Viewport

    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                   // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(25.0f,1.0f,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
    glLoadIdentity();                                   // Reset The Modelview Matrix

    glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glDisable(GL_DEPTH_TEST);                           // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations

    GLenum mError = glGetError();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glLoadIdentity();                                   // Reset The View
    glTranslatef(0.0f,0.0f,-5.0f);
    mError = glGetError();
    glBindTexture(GL_TEXTURE_2D, texture);
    mError = glGetError();
    glColor4f(1.0, 1.0, 1.0, 1.0);
    mError = glGetError();

    if(flip)
    {
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  0.5f);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  0.5f);
        glEnd();
        mError = glGetError();
    }
    else
    {
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  0.5f);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  0.5f);
        glEnd();
        mError = glGetError();
    }
    mError = glGetError();  
    return TRUE;                                        // Keep Going
}

GLvoid OpenGL_Display::KillGLWindow(GLvoid)                             // Properly Kill The Window
{
    if (fullscreen)                                     // Are We In Fullscreen Mode?
    {
        ChangeDisplaySettings(NULL,0);                  // If So Switch Back To The Desktop
        ShowCursor(TRUE);                               // Show Mouse Pointer
    }

    if (hRC)                                            // Do We Have A Rendering Context?
    {
        wglMakeCurrent(NULL,NULL);
        wglDeleteContext(hRC);
        hRC=NULL;                                       // Set RC To NULL
    }

    ReleaseDC(hWnd,hDC);

    DestroyWindow(hWnd);

    UnregisterClass("OpenGL_Display",hInstance);
}

void OpenGL_Display::DrawNewImage(int width, int height, char* imageData, bool flip, int bpp)
{
    BOOL returnVal = wglMakeCurrent(hDC, hRC);

    UpdateGLBuffers(width, height, imageData, bpp); 
    DrawGLScene(flip);
    SwapBuffers(hDC);
    wglMakeCurrent(NULL, NULL);
}

void OGD::OpenGLDisplay_Init(int width, int height, int posX, int posY, int ID, LPCSTR className)
{
    for(unsigned int i = 0; i < oglDisp.size(); ++i) {
        if(oglDisp.at(i)->ID == ID) {
            return;
        }
    }

    oglDisp.push_back(new OGD::OpenGL_Display(ID));
    oglDisp.at(oglDisp.size()-1)->Init_Display(width, height, posX, posY, className); //by default this ID is latest
    wglMakeCurrent(NULL, NULL);
    GLenum mError = glGetError();
}

void OGD::OpenGLDisplay_Wrapper(char* image, int width, int height, bool flip, int bpp, int ID)
{
    for(unsigned int i = 0; i < oglDisp.size(); ++i) {
        if(oglDisp.at(i)->ID == ID) {
            oglDisp.at(i)->DrawNewImage(width, height, image, flip, bpp);   
            return;
        }
    }   
}
/**基于NeHe教程的代码
适用于使用OpenGL显示实时摄像机显示
*/
#包括“OpenGL Display.h”
使用名称空间OGD;
OpenGL_显示::OpenGL_显示(int newId)
{
xPos=-1;
yPos=-1;
ID=newId;
宽度=1;
高度=1;
}
OpenGL_显示::~OpenGL_显示()
{
KillGLWindow();
}
void OpenGL_Display::Init_Display(int newWidth、int newHeight、int posX、int posY、LPCSTR inputName)
{
宽度=新宽度;
高度=新高度;
className=inputName;
如果(!CreateGLWindow((char*)className,24,posX,posY,className))
{
投掷;
}   
}
LRESULT回调OpenGL_Display::WndProc(HWND HWND,//此窗口的句柄
UINT uMsg,//此窗口的消息
WPARAM WPARAM,//其他消息信息
LPARAM LPARAM)//附加消息信息
{
开关(uMsg)//检查Windows消息
{
案例WM_SYSCOMMAND://拦截系统命令
{
开关(wParam)//检查系统调用
{
案例SC_SCREENSAVE://屏幕保护程序正在尝试启动?
案例SC_MONITORPOWER://监视器试图进入Powersave?
返回0;//防止发生
}
break;//退出
}
案例WM_CLOSE://我们收到关闭消息了吗?
{
PostQuitMessage(0);//发送退出消息
返回0;//Jum
/** Based on code by NeHe tutorials
    Adapted to display live camera display with OpenGL
*/

#include "OpenGL-Display.h"

using namespace OGD;

OpenGL_Display::OpenGL_Display(int newId)
{
    xPos = -1;
    yPos = -1;
    ID = newId;
    width = 1;
    height = 1;
}

OpenGL_Display::~OpenGL_Display()
{
    KillGLWindow();
}

void OpenGL_Display::Init_Display(int newWidth, int newHeight, int posX, int posY, LPCSTR inputName)
{
    width = newWidth;
    height = newHeight;
    className = inputName;

    if (!CreateGLWindow((char*)className, 24, posX, posY, className))
    {
        throw;
    }   
}

LRESULT CALLBACK OpenGL_Display::WndProc(   HWND    hWnd,           // Handle For This Window
                            UINT    uMsg,           // Message For This Window
                            WPARAM  wParam,         // Additional Message Information
                            LPARAM  lParam)         // Additional Message Information
{
    switch (uMsg)                                   // Check For Windows Messages
    {
        case WM_SYSCOMMAND:                         // Intercept System Commands
        {
            switch (wParam)                         // Check System Calls
            {
                case SC_SCREENSAVE:                 // Screensaver Trying To Start?
                case SC_MONITORPOWER:               // Monitor Trying To Enter Powersave?
                return 0;                           // Prevent From Happening
            }
            break;                                  // Exit
        }

        case WM_CLOSE:                              // Did We Receive A Close Message?
        {
            PostQuitMessage(0);                     // Send A Quit Message
            return 0;                               // Jump Back
        }

        case WM_SIZE:                               // Resize The OpenGL Window
        {
            oglDisp.at(0)->ReSizeGLScene(LOWORD(lParam),HIWORD(lParam));  // LoWord=Width, HiWord=Height
            return 0;                               // Jump Back
        }
    }

    // Pass All Unhandled Messages To DefWindowProc
    return DefWindowProc(hWnd,uMsg,wParam,lParam);
}


bool OpenGL_Display::CreateGLWindow(char* title, int bits, int posX, int posY, LPCSTR className)
{
    GLuint      PixelFormat;            // Holds The Results After Searching For A Match
    WNDCLASS    wc;                     // Windows Class Structure
    DWORD       dwExStyle;              // Window Extended Style
    DWORD       dwStyle;                // Window Style
    RECT        WindowRect;             // Grabs Rectangle Upper Left / Lower Right Values
    WindowRect.left=(long)0;            // Set Left Value To 0
    WindowRect.right=(long)width;       // Set Right Value To Requested Width
    WindowRect.top=(long)0;             // Set Top Value To 0
    WindowRect.bottom=(long)height;     // Set Bottom Value To Requested Height

    hInstance           = GetModuleHandle(NULL);                // Grab An Instance For Our Window
    wc.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;   // Redraw On Size, And Own DC For Window.
    wc.lpfnWndProc      = WndProc;                              // WndProc Handles Messages
    wc.cbClsExtra       = 0;                                    // No Extra Window Data
    wc.cbWndExtra       = 0;                                    // No Extra Window Data
    wc.hInstance        = hInstance;                            // Set The Instance
    wc.hIcon            = LoadIcon(NULL, IDI_WINLOGO);          // Load The Default Icon
    wc.hCursor          = LoadCursor(NULL, IDC_ARROW);          // Load The Arrow Pointer
    wc.hbrBackground    = NULL;                                 // No Background Required For GL
    wc.lpszMenuName     = NULL;                                 // We Don't Want A Menu
    wc.lpszClassName    = className;                                // Set The Class Name

    RegisterClass(&wc);                                         // Return FALSE

    dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;           // Window Extended Style
    dwStyle=WS_VISIBLE | WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;;                         // Windows Style

    AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size
    //HWND hwndC = GetConsoleWindow();
    // Create The Window
    hWnd=CreateWindowEx(    dwExStyle,                          // Extended Style For The Window
                                className,                          // Class Name
                                title,                              // Window Title
                                //WS_OVERLAPPEDWINDOW,
                                dwStyle |                           // Defined Window Style
                                WS_CLIPSIBLINGS |                   // Required Window Style
                                WS_CLIPCHILDREN,                    // Required Window Style
                                posX, posY,                         // Window Position
                                WindowRect.right-WindowRect.left,   // Calculate Window Width
                                WindowRect.bottom-WindowRect.top,   // Calculate Window Height
                                NULL,                               // No Parent Window
                                NULL,                               // No Menu
                                hInstance,                          // Instance
                                NULL);

    static  PIXELFORMATDESCRIPTOR pfd=              // pfd Tells Windows How We Want Things To Be
    {
        sizeof(PIXELFORMATDESCRIPTOR),              // Size Of This Pixel Format Descriptor
        1,                                          // Version Number
        PFD_DRAW_TO_WINDOW |                        // Format Must Support Window
        PFD_SUPPORT_OPENGL |                        // Format Must Support OpenGL
        PFD_DOUBLEBUFFER,                           // Must Support Double Buffering
        PFD_TYPE_RGBA,                              // Request An RGBA Format
        bits,                                       // Select Our 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,                                         // 16Bit Z-Buffer (Depth Buffer)  
        0,                                          // No Stencil Buffer
        0,                                          // No Auxiliary Buffer
        PFD_MAIN_PLANE,                             // Main Drawing Layer
        0,                                          // Reserved
        0, 0, 0                                     // Layer Masks Ignored
    };

    hDC=GetDC(hWnd);

    PixelFormat=ChoosePixelFormat(hDC,&pfd);

    SetPixelFormat(hDC,PixelFormat,&pfd);

    hRC=wglCreateContext(hDC);

    wglMakeCurrent(hDC,hRC);

    ShowWindow(hWnd,SW_SHOW);                       // Show The Window
    SetForegroundWindow(hWnd);                      // Slightly Higher Priority
    SetFocus(hWnd);                                 // Sets Keyboard Focus To The Window
    ReSizeGLScene(width, height);                   // Set Up Our Perspective GL Screen

    InitGL();

    return TRUE;                                    // Success
}


int OpenGL_Display::LoadGLTextures()                                    // Load Bitmaps And Convert To Textures
{
    // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit

    glGenTextures(1, &texture);                 // Create The Texture
    // Typical Texture Generation Using Data From The Bitmap
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); // Minimizing filter, in case display is smaller image size
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); // Magnifying filter, in case display is smaller image size

    return 1;                                       // Return The Status
}

GLvoid OpenGL_Display::ReSizeGLScene(GLsizei newWidth, GLsizei newHeight)       // Resize And Initialize The GL Window
{
    width = newWidth;
    height = newHeight;
}

int OpenGL_Display::InitGL(GLvoid)                                      // All Setup For OpenGL Goes Here
{
    if (!LoadGLTextures())                              // Jump To Texture Loading Routine
    {
        return FALSE;                                   // If Texture Didn't Load Return FALSE
    }

    return TRUE;                                        // Initialization Went OK
}

void OpenGL_Display::GetMouseCords(int& x, int& y)      // All Setup For OpenGL Goes Here
{
    x = xPos;
    y = yPos;
}

void OpenGL_Display::UpdateGLBuffers(int width, int height, char* imageData, int bpp) {
    glBindTexture(GL_TEXTURE_2D, texture); 
    switch(bpp) {
    case 8:  glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, imageData); break;
    case 16: glTexImage2D(GL_TEXTURE_2D, 0, GL_INTENSITY, width, height, 0, GL_RED, GL_UNSIGNED_SHORT, imageData); break;
    case 24: glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); break;
    default: return;
    }

    GLenum mError = glGetError();

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
}

int OpenGL_Display::DrawGLScene(bool flip)                                  // Here's Where We Do All The Drawing
{
    if (height==0)                                      // Prevent A Divide By Zero
    {
        height=1;                                       // Making Height Equal One
    }

    glViewport(0,0,width,height);                       // Reset The Current Viewport

    glMatrixMode(GL_PROJECTION);                        // Select The Projection Matrix
    glLoadIdentity();                                   // Reset The Projection Matrix

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(25.0f,1.0f,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW);                         // Select The Modelview Matrix
    glLoadIdentity();                                   // Reset The Modelview Matrix

    glEnable(GL_TEXTURE_2D);                            // Enable Texture Mapping
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glDisable(GL_DEPTH_TEST);                           // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Really Nice Perspective Calculations

    GLenum mError = glGetError();
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
    glLoadIdentity();                                   // Reset The View
    glTranslatef(0.0f,0.0f,-5.0f);
    mError = glGetError();
    glBindTexture(GL_TEXTURE_2D, texture);
    mError = glGetError();
    glColor4f(1.0, 1.0, 1.0, 1.0);
    mError = glGetError();

    if(flip)
    {
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  0.5f);
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  0.5f);
        glEnd();
        mError = glGetError();
    }
    else
    {
        glBegin(GL_QUADS);
            // Front Face
            glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  0.5f);
            glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  0.5f);
            glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  0.5f);
        glEnd();
        mError = glGetError();
    }
    mError = glGetError();  
    return TRUE;                                        // Keep Going
}

GLvoid OpenGL_Display::KillGLWindow(GLvoid)                             // Properly Kill The Window
{
    if (fullscreen)                                     // Are We In Fullscreen Mode?
    {
        ChangeDisplaySettings(NULL,0);                  // If So Switch Back To The Desktop
        ShowCursor(TRUE);                               // Show Mouse Pointer
    }

    if (hRC)                                            // Do We Have A Rendering Context?
    {
        wglMakeCurrent(NULL,NULL);
        wglDeleteContext(hRC);
        hRC=NULL;                                       // Set RC To NULL
    }

    ReleaseDC(hWnd,hDC);

    DestroyWindow(hWnd);

    UnregisterClass("OpenGL_Display",hInstance);
}

void OpenGL_Display::DrawNewImage(int width, int height, char* imageData, bool flip, int bpp)
{
    BOOL returnVal = wglMakeCurrent(hDC, hRC);

    UpdateGLBuffers(width, height, imageData, bpp); 
    DrawGLScene(flip);
    SwapBuffers(hDC);
    wglMakeCurrent(NULL, NULL);
}

void OGD::OpenGLDisplay_Init(int width, int height, int posX, int posY, int ID, LPCSTR className)
{
    for(unsigned int i = 0; i < oglDisp.size(); ++i) {
        if(oglDisp.at(i)->ID == ID) {
            return;
        }
    }

    oglDisp.push_back(new OGD::OpenGL_Display(ID));
    oglDisp.at(oglDisp.size()-1)->Init_Display(width, height, posX, posY, className); //by default this ID is latest
    wglMakeCurrent(NULL, NULL);
    GLenum mError = glGetError();
}

void OGD::OpenGLDisplay_Wrapper(char* image, int width, int height, bool flip, int bpp, int ID)
{
    for(unsigned int i = 0; i < oglDisp.size(); ++i) {
        if(oglDisp.at(i)->ID == ID) {
            oglDisp.at(i)->DrawNewImage(width, height, image, flip, bpp);   
            return;
        }
    }   
}