void main(int argc, char* argv[]) { initGL(argc, argv); InitGLEW(); cord = Model::createModel(sizeof(coodinate) / sizeof(Vertex), coodinate, coordinateIndices, sizeof(coordinateIndices) / sizeof(int), SHADER_TYPE::SHADER_POSITION_COLOR); glm::vec3 pos = glm::vec3(0, 0, 0); Cube *cube = new Cube(pos, 1.0f, "textures/crate.png"); //cube->setRotation(90, glm::vec3(0, 1.0f, 0)); cube->setScale(0.8f); cubes.push_back(cube); renderer = new Renderer(); camera = new Camera3D(glm::vec2(0, 0), winWidth, winHeight); staticCamera = new Camera3D(glm::vec2(0, 0), winWidth, winHeight); glm::mat4 project = glm::perspective(60.0f, (float)winWidth / winHeight, 1.0f, 5.0f); camera->setProject(project); glm::mat4 view = glm::lookAt( glm::vec3(2.0, 2.0, 2.0), glm::vec3(0, 0, 0), glm::vec3(0, 1.0, 0)); camera->setView(view); staticCamera->setProject(project); staticCamera->setView(view); glutMainLoop(); }
bool ccFBOUtils::CheckExtension(const char *extName) { if (!InitGLEW()) return false; return (glewIsSupported(extName)>0); }
bool Engine::InitSystems() { if (!InitSDL() || !InitSDLWindow() || !InitGLContext() || !InitGLEW()) return false; InitOpenGLSettings(); return true; }
int main( int argc, char* argv[] ) { g_InitialCameraPosition = glm::vec3( 0, 0, 10 ); g_Camera.SetPosition( g_InitialCameraPosition ); g_Camera.SetRotation( g_InitialCameraRotation ); InitGL(argc, argv); InitGLEW(); // Load some shaders. GLuint vertexShader = LoadShader( GL_VERTEX_SHADER, "../data/shaders/simpleShader.vert" ); GLuint fragmentShader = LoadShader( GL_FRAGMENT_SHADER, "../data/shaders/simpleShader.frag" ); std::vector<GLuint> shaders; shaders.push_back(vertexShader); shaders.push_back(fragmentShader); // Create the shader program. g_ShaderProgram = CreateShaderProgram( shaders ); assert( g_ShaderProgram != 0 ); GLint positionAtribID = glGetAttribLocation( g_ShaderProgram, "in_position" ); GLint colorAtribID = glGetAttribLocation( g_ShaderProgram, "in_color" ); g_uniformMVP = glGetUniformLocation( g_ShaderProgram, "MVP" ); // Create a VAO for the cube. glGenVertexArrays( 1, &g_vaoCube ); glBindVertexArray( g_vaoCube ); GLuint vertexBuffer, indexBuffer; glGenBuffers( 1, &vertexBuffer ); glGenBuffers( 1, &indexBuffer ); glBindBuffer( GL_ARRAY_BUFFER, vertexBuffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(g_Vertices), g_Vertices, GL_STATIC_DRAW ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, indexBuffer ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(g_Indices), g_Indices, GL_STATIC_DRAW ); glVertexAttribPointer( positionAtribID, 3, GL_FLOAT, false, sizeof(VertexXYZColor), reinterpret_cast<const GLvoid*>(offsetof(VertexXYZColor,m_Pos)) ); glEnableVertexAttribArray( positionAtribID ); glVertexAttribPointer( colorAtribID, 3, GL_FLOAT, false, sizeof(VertexXYZColor), reinterpret_cast<const GLvoid*>(offsetof(VertexXYZColor,m_Color))); glEnableVertexAttribArray( colorAtribID ); // Make sure we disable and unbind everything to prevent rendering issues later. glBindVertexArray( 0 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 ); glDisableVertexAttribArray( positionAtribID ); glDisableVertexAttribArray( colorAtribID ); glutMainLoop(); }
int __stdcall WinMain(__in HINSTANCE hInstance, __in_opt HINSTANCE hPrevInstance, __in_opt LPSTR lpCmdLine, __in int nShowCmd) { BOOL bResult = FALSE; MSG msg = { 0 }; WNDCLASS wc = { 0 }; wc.lpfnWndProc = WndProc; wc.hInstance = hInstance; wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND); wc.lpszClassName = L"Opengl4TemplateWindowClass"; wc.style = CS_OWNDC; if (!RegisterClass(&wc)) return 1; HWND hWnd = CreateWindowW(wc.lpszClassName, L"Opengl 4 Template", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 640, 480, 0, 0, hInstance, 0); ShowWindow(hWnd, nShowCmd); UpdateWindow(hWnd); bResult = CreateOpenGLRenderContext(hWnd); if (bResult == FALSE) { OutputDebugStringA("CreateOpenGLRenderContext failed!\n"); return 1; } InitGLEW(); InitOpenGL(); while (true) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); } else { Update(hWnd); Render(hWnd); } } return 0; }
// Initializes OpenGL rendering context. If succeeds, returns true. // hInstance - application instance // a_hWnd - window to init OpenGL into // a_initScene - pointer to init function // a_renderScene - pointer to render function // a_releaseScene - optional parameter of release function bool COpenGLControl::InitOpenGL(HINSTANCE hInstance, HWND* a_hWnd, void (*a_InitScene)(LPVOID), void (*a_RenderScene)(LPVOID), void(*a_ReleaseScene)(LPVOID), LPVOID lpParam) { if(!InitGLEW(hInstance))return false; hWnd = a_hWnd; hDC = GetDC(*hWnd); bool bError = false; PIXELFORMATDESCRIPTOR pfd; // if we have access to these functions if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format) { const int iPixelFormatAttribList[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, // Enable OpenGL support WGL_DOUBLE_BUFFER_ARB, GL_TRUE, // and double buffer WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, // Depth buffer size WGL_STENCIL_BITS_ARB, 8, 0 // End of attributes list }; int iContextAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3, // OpenGL WGL_CONTEXT_MINOR_VERSION_ARB, 3, // version 3.3 WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 // End of attributes list }; int iPixelFormat, iNumFormats; wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats); // PFD seems to be only redundant parameter now if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; hRC = wglCreateContextAttribsARB(hDC, 0, iContextAttribs); // If everything went OK if(hRC) wglMakeCurrent(hDC, hRC); else bError = true; } else bError = true; if(bError) { // Generate error messages char sErrorMessage[255], sErrorTitle[255]; sprintf(sErrorMessage, "OpenGL 3.3 is not supported! Please download latest GPU drivers!"); sprintf(sErrorTitle, "OpenGL 3.3 Not Supported"); MessageBox(*hWnd, sErrorMessage, sErrorTitle, MB_ICONINFORMATION); return false; } RenderScene = a_RenderScene; InitScene = a_InitScene; ReleaseScene = a_ReleaseScene; if(InitScene != NULL)InitScene(lpParam); return true; }
bool COpenGLControl::InitOpenGL(HINSTANCE hInstance, HWND* a_hWnd, int iMajorVersion, int iMinorVersion, void (*a_ptrInitScene)(LPVOID), void (*a_ptrRenderScene)(LPVOID), void(*a_ptrReleaseScene)(LPVOID), LPVOID lpParam) { if(!InitGLEW(hInstance))return false; hWnd = a_hWnd; hDC = GetDC(*hWnd); bool bError = false; PIXELFORMATDESCRIPTOR pfd; if(iMajorVersion <= 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 = 32; pfd.cDepthBits = 32; pfd.iLayerType = PFD_MAIN_PLANE; int iPixelFormat = ChoosePixelFormat(hDC, &pfd); if (iPixelFormat == 0)return false; if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; // Create the old style context (OpenGL 2.1 and before) hRC = wglCreateContext(hDC); if(hRC)wglMakeCurrent(hDC, hRC); else bError = true; } else if(WGLEW_ARB_create_context && WGLEW_ARB_pixel_format) { const int iPixelFormatAttribList[] = { WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_COLOR_BITS_ARB, 32, WGL_DEPTH_BITS_ARB, 24, WGL_STENCIL_BITS_ARB, 8, 0 // End of attributes list }; int iContextAttribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, iMajorVersion, WGL_CONTEXT_MINOR_VERSION_ARB, iMinorVersion, WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, 0 // End of attributes list }; int iPixelFormat, iNumFormats; wglChoosePixelFormatARB(hDC, iPixelFormatAttribList, NULL, 1, &iPixelFormat, (UINT*)&iNumFormats); // PFD seems to be only redundant parameter now if(!SetPixelFormat(hDC, iPixelFormat, &pfd))return false; hRC = wglCreateContextAttribsARB(hDC, 0, iContextAttribs); // If everything went OK if(hRC) wglMakeCurrent(hDC, hRC); else bError = true; } else bError = true; if(bError) { // Generate error messages char sErrorMessage[255], sErrorTitle[255]; sprintf_s(sErrorMessage, "OpenGL %d.%d is not supported! Please download latest GPU drivers!", iMajorVersion, iMinorVersion); //_s sprintf_s(sErrorTitle, "OpenGL %d.%d Not Supported", iMajorVersion, iMinorVersion); //_s MessageBox(*hWnd, sErrorMessage, sErrorTitle, MB_ICONINFORMATION); return false; } ptrRenderScene = a_ptrRenderScene; ptrInitScene = a_ptrInitScene; ptrReleaseScene = a_ptrReleaseScene; if(ptrInitScene != NULL)ptrInitScene(lpParam); return true; }
bool RenderContextWGL::Create(void* osWnd) { window = static_cast<HWND>(osWnd); int glmajor = 3; int glminor = 3; int colorBits = 32; int depthBits = 24; int stencilBits = 0; int samples = 0; InitGLEW(); int attrs[] = { WGL_SUPPORT_OPENGL_ARB, GL_TRUE, WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB, WGL_DRAW_TO_WINDOW_ARB, GL_TRUE, WGL_DOUBLE_BUFFER_ARB, GL_TRUE, WGL_SAMPLE_BUFFERS_ARB, samples > 0 ? GL_TRUE : GL_FALSE, WGL_SAMPLES_ARB, samples, WGL_COLOR_BITS_ARB, colorBits, WGL_DEPTH_BITS_ARB, depthBits, WGL_STENCIL_BITS_ARB, stencilBits, WGL_ALPHA_BITS_ARB, colorBits == 32 ? 8 : 0, 0 }; int pixelFormat; unsigned numFormats = 0; gdiDc = GetDC(window); int result = wglChoosePixelFormatARB(gdiDc, attrs, nullptr, 1, &pixelFormat, &numFormats); if (result == 0 || numFormats == 0) { Log::E("wglChoosePixelFormatARB failed"); } // info PIXELFORMATDESCRIPTOR pfd; DescribePixelFormat(gdiDc, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd); Log::V() << "Pixel format:\tcColorBits " << (int)pfd.cColorBits << ", cDepthBits " << (int)pfd.cDepthBits << "\n\t\t\tcAlphaBits " << (int)pfd.cAlphaBits << ", cStencilBits " << (int)pfd.cStencilBits; if (!SetPixelFormat(gdiDc, pixelFormat, &pfd)) { Log::E("SetPixelFormat"); } int contextAttrs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, glmajor, // OpenGL 3.3 provides full compatibility with OpenGL ES 3.0. WGL_CONTEXT_MINOR_VERSION_ARB, glminor, WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB, // WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB // WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, // WGL_CONTEXT_DEBUG_BIT_ARB // WGL_CONTEXT_LAYER_PLANE_ARB, 0, 0 }; wglContext = wglCreateContextAttribsARB(gdiDc, 0, contextAttrs); if (!wglContext) { Log::E("wglContext"); } if (!wglMakeCurrent(gdiDc, wglContext)) { Log::E("wglMakeCurrent"); wglDeleteContext(wglContext); ReleaseDC(window, gdiDc); wglContext = nullptr; return false; } RECT rect; GetClientRect(window, &rect); mWidth = rect.right - rect.left; mHeight = rect.bottom - rect.top; return true; }
// From <https://www.opengl.org/wiki/Programming_OpenGL_in_Linux:_GLX_and_Xlib> int main(int argc, char** argv){ Initialised = false; StartTime = GetClock(); double time; Display* dpy; Window root; XVisualInfo* vi; Colormap cmap; XSetWindowAttributes swa; Window win; GLXContext glc; XWindowAttributes gwa; XEvent xev; GLint att[] = {GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None}; dpy = XOpenDisplay(0); if(dpy == 0){ printf("\n\tcannot connect to X server\n\n"); return 1; } root = DefaultRootWindow(dpy); vi = glXChooseVisual(dpy, 0, att); if(vi == 0){ printf("\n\tno appropriate visual found\n\n"); return 1; }else{ // %p creates hexadecimal output like in glxinfo printf("\n\tvisual %p selected\n", (void*)vi->visualid); } cmap = XCreateColormap(dpy, root, vi->visual, AllocNone); swa.colormap = cmap; swa.event_mask = ExposureMask | KeyPressMask; win = XCreateWindow( dpy, root, 0, 0, 640, 480, 0, vi->depth, InputOutput, vi->visual, CWColormap | CWEventMask, &swa ); XMapWindow(dpy, win); XStoreName(dpy, win, "OpenGL Sample"); glc = glXCreateContext(dpy, vi, 0, GL_TRUE); glXMakeCurrent(dpy, win, glc); if(!InitGLEW()){ glXMakeCurrent (dpy, None, 0); glXDestroyContext(dpy, glc); XDestroyWindow (dpy, win); XCloseDisplay (dpy); return 1; } if(!LoadShader ("OpenGL/Texture.vp", "OpenGL/Texture.fp")) return 1; if(!LoadTexture("Pic/greatwall.jpg")) return 1; Initialised = true; bool running = true; while(running){ while(XCheckWindowEvent(dpy, win, 0xFFFFFFFF, &xev)){ switch(xev.type){ case Expose: XGetWindowAttributes(dpy, win, &gwa); Render(gwa.width, gwa.height); glXSwapBuffers(dpy, win); break; case KeyPress: // printf("KeyPress: keycode %u state %u\n", xev.xkey.keycode, xev.xkey.state); if(xev.xkey.keycode == Key_Escape) running = false; else OnKeyDown(xev.xkey.keycode); break; } } XGetWindowAttributes(dpy, win, &gwa); Render(gwa.width, gwa.height); glXSwapBuffers(dpy, win); RenderTime(GetClock()-time); time = GetClock(); usleep(1000); } glXMakeCurrent (dpy, None, 0); glXDestroyContext(dpy, glc); XDestroyWindow (dpy, win); XCloseDisplay (dpy); free(InputBuffer ); free(OutputBuffer); return 0; }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){ Initialised = false; StartTime = GetClock(); double time = StartTime; WNDCLASSEX wcex; HGLRC hRC; RECT rect; MSG msg; // register window class wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_OWNDC; wcex.lpfnWndProc = WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = LoadIcon(0, IDI_APPLICATION); wcex.hCursor = LoadCursor(0, IDC_ARROW); wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wcex.lpszMenuName = 0; wcex.lpszClassName = "GLSample"; wcex.hIconSm = LoadIcon(0, IDI_APPLICATION);; if(!RegisterClassEx(&wcex)) return 0; // create main window hwnd = CreateWindowEx( 0, "GLSample", "OpenGL Sample", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, hInstance, 0 ); ShowWindow(hwnd, nCmdShow); // enable OpenGL for the window PIXELFORMATDESCRIPTOR pfd; int iFormat; // get the device context (DC) hDC = GetDC(hwnd); // set the pixel format for the DC ZeroMemory(&pfd, sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 0; pfd.iLayerType = PFD_MAIN_PLANE; iFormat = ChoosePixelFormat(hDC, &pfd); SetPixelFormat(hDC, iFormat, &pfd); // create and enable the render context (RC) hRC = wglCreateContext(hDC); wglMakeCurrent(hDC, hRC); if(!InitGLEW()){ wglMakeCurrent(0, 0); wglDeleteContext(hRC); ReleaseDC(hwnd, hDC); DestroyWindow(hwnd); return 1; } if(!LoadShader ("..\\OpenGL\\Texture.vp", "..\\OpenGL\\Texture.fp")) return 1; if(!LoadTexture("..\\Pic\\greatwall.jpg")) return 1; Initialised = true; Initialised = true; // program main loop while(true){ while(PeekMessage(&msg, 0, 0, 0, PM_REMOVE)){ if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage (&msg); } if(msg.message == WM_QUIT) break; GetClientRect(hwnd, &rect); Render(rect.right, rect.bottom); SwapBuffers(hDC); RenderTime(GetClock()-time); time = GetClock(); Sleep(1); } wglMakeCurrent(0, 0); wglDeleteContext(hRC); ReleaseDC(hwnd, hDC); DestroyWindow(hwnd); free(InputBuffer ); free(OutputBuffer); return msg.wParam; }