Hello,
I was trying to create context in opengl.I was able create the window but WGL_ARB_create_context is not supported?
I would like to know why?
#include <Windows.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/wglew.h>
#include <GL/gl.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glew32.lib")
HGLRC hrc;
HDC hdc;
HWND hwnd;
HINSTANCE hInstance;
int running=1;
int windowWidth;
int windowHeight;
void setupScene(void)
{
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
}
void reshapeWindow(int w,int h)
{
windowWidth = w; // Set the window width
windowHeight = h; // Set the window height
}
void renderScene(void) {
glViewport(0, 0, windowWidth, windowHeight); // Set the viewport size to fill the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear required buffers
SwapBuffers(hdc); // Swap buffers so we can see our rendering
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
//WNDCLASS wndclass;
switch(message)
{
case WM_SIZE:
{
reshapeWindow(LOWORD(lparam), HIWORD(lparam)); // Send the new window size to our OpenGLContext
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
}
return DefWindowProc(hwnd,message,wparam,lparam);
}
int CreateGLcontext(HWND hwnd)
{
PIXELFORMATDESCRIPTOR pfd;
int nPixelFormat;
HGLRC tempcontext;
BOOL bResult;
GLenum err;
int attribs[]={WGL_CONTEXT_MAJOR_VERSION_ARB,3,WGL_CONTEXT_MINOR_VERSION_ARB,1,WGL_CONTEXT_FLAGS_ARB,WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,0};
const char*GLversionstring;
int OpenGLVersion[2];
memset(&pfd,0,sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize=sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion=1;
pfd.dwFlags=PFD_DOUBLEBUFFER|PFD_SUPPORT_OPENGL|PFD_DRAW_TO_WINDOW;
pfd.iPixelType=PFD_TYPE_RGBA;
pfd.cColorBits=24;
pfd.cDepthBits=16;
pfd.iLayerType=PFD_MAIN_PLANE;
hdc=GetDC(hwnd);
nPixelFormat=ChoosePixelFormat(hdc,&pfd);
if(nPixelFormat==0)
return 0;
bResult=SetPixelFormat(hdc,nPixelFormat,&pfd);
if(!bResult)
return 0;
tempcontext=wglCreateContext(hdc);
wglMakeCurrent(hdc,tempcontext);
err=glewInit();
if(GLEW_OK!=err)
{
printf("glew is not initialized");
}
if(glewIsSupported("WGL_ARB_create_context")==1)
{
hrc=wglCreateContextAttribsARB(hdc,0,attribs);
wglMakeCurrent(NULL,NULL);
wglDeleteContext(tempcontext);
wglMakeCurrent(hdc,hrc);
}
else
{
hrc=tempcontext;
}
GLversionstring=glGetString(GL_VERSION);
glGetIntegerv(GL_MAJOR_VERSION,&OpenGLVersion[0]);
glGetIntegerv(GL_MINOR_VERSION,&OpenGLVersion[1]);
if(!hrc) return 0;
return 1;
}
int createWindow(LPCWSTR title,int width,int height)
{
WNDCLASS wndclass;
HWND hwnd;
DWORD dwExStyle= WS_EX_APPWINDOW|WS_EX_WINDOWEDGE;
hInstance=GetModuleHandle(NULL);
wndclass.style=CS_OWNDC;
wndclass.lpfnWndProc=(WNDPROC)WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_WINLOGO);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject( BLACK_BRUSH );
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=title;
if(!RegisterClass(&wndclass))
return 0;
hwnd=CreateWindowEx(dwExStyle,title,title,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,width,height,NULL,NULL,hInstance,NULL);
CreateGLcontext(hwnd);
ShowWindow(hwnd,SW_SHOW);
//UpdateWindow(hwnd);
return 1;
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdline,int nCmdShow)
{
MSG msg;
size_t convertedChars=0;
const unsigned int newsize=100;
char wcstring[100];
char *orig="OpenGL Project";
size_t origsize=strlen(orig)+1;
mbstowcs_s(&convertedChars,wcstring,origsize,orig,_TRUNCATE);
createWindow(wcstring,640,400);
//setupscene
setupScene();
while(running)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
{
running=0;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
//render scene
renderScene();
}
}
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hrc);
ReleaseDC(hwnd,hdc);
return (int) msg.wParam;
}