static GLFWwindow* create_window(GLFWmonitor* monitor) { int width, height; GLFWwindow* window; if (monitor) { const GLFWvidmode* mode = glfwGetVideoMode(monitor); glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); glfwWindowHint(GLFW_RED_BITS, mode->redBits); glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); width = mode->width; height = mode->height; } else { width = 640; height = 480; } window = glfwCreateWindow(width, height, "Iconify", monitor, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); return window; }
int main(int argc, char **argv) { parseArgs(argc, argv); glfwInit(); glfwWindowHint(GLFW_VISIBLE, GL_FALSE); window = glfwCreateWindow(250, 250, argv[0], NULL, NULL); if (!window) { return EXIT_SKIP; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { return EXIT_FAILURE; } Init(); Reshape(); Draw(); glfwDestroyWindow(window); glfwTerminate(); return 0; }
int main() { if (!glfwInit()) { return 1; } GLFWwindow *win = glfwCreateWindow(640, 480, "GLFW test", NULL, NULL); if (!win) { return 1; } int x = 3; // Activate OpenGL context glfwMakeContextCurrent(win); // Load glad gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); while (!glfwWindowShouldClose(win)) { glfwPollEvents(); } glfwDestroyWindow(win); glfwTerminate(); return 0; }
int main(int argc, char *argv[]) { glfwInit(); glfwWindowHint(GLFW_VISIBLE, GL_FALSE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(250, 250, argv[0], NULL, NULL); if (!window) { return EXIT_SKIP; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { return EXIT_FAILURE; } init(); reshape(); draw(); glfwDestroyWindow(window); glfwTerminate(); return 0; }
bool OpenGL::initContext() { if (contextInitialized) return true; if (!gladLoadGLLoader(LOVEGetProcAddress)) return false; initOpenGLFunctions(); initVendor(); initMatrices(); bugs = {}; #if defined(LOVE_WINDOWS) || defined(LOVE_LINUX) // See the comments in OpenGL.h. if (getVendor() == VENDOR_AMD) { bugs.clearRequiresDriverTextureStateUpdate = true; bugs.generateMipmapsRequiresTexture2DEnable = true; } #endif contextInitialized = true; return true; }
int main(void) { GLFWwindow* window; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(400, 400, "English 日本語 русский язык 官話", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); glfwWaitEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); }
static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor) { double base; GLFWwindow* window; base = glfwGetTime(); window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL); if (!window) return NULL; glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetWindowCloseCallback(window, window_close_callback); glfwSetKeyCallback(window, key_callback); if (monitor) { printf("Opening full screen window on monitor %s took %0.3f seconds\n", glfwGetMonitorName(monitor), glfwGetTime() - base); } else { printf("Opening regular window took %0.3f seconds\n", glfwGetTime() - base); } return window; }
void Graphics::init(GLint width, GLint height) { glfwSetErrorCallback(glutil::consoleErrorCallback); if (!glfwInit()) { glutil::fatal_error("Error initializing glfw."); } this->width = width; this->height = height; window = glfwCreateWindow(width, height, "Ray tracing using a Photon Map", NULL, NULL); glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { if (!gladLoadGL()) { glutil::fatal_error("Failed to initialize OpenGL context"); } } // Define the positions for each of the three vertices of the triangle const float positions[] = { 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f }; // Create a handle for the position vertex buffer object GLuint positionBuffer; glGenBuffers(1, &positionBuffer); // Set the newly created buffer as the current one glBindBuffer(GL_ARRAY_BUFFER, positionBuffer); // Send the vertex position data to the current buffer glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW); //******* Connect triangle data with the vertex array object ******* glGenVertexArrays(1, &vao); // Bind the vertex array object // The following calls will affect this vertex array object. glBindVertexArray(vao); // Makes positionBuffer the current array buffer for subsequent calls. glBindBuffer(GL_ARRAY_BUFFER, positionBuffer); // Attaches positionBuffer to vao, in the 0th attribute location glVertexAttribPointer(0, 3, GL_FLOAT, false/*normalized*/, 0/*stride*/, 0/*offset*/); glEnableVertexAttribArray(0); // Enable the vertex position attribute shaderProgram = glutil::loadShader( VERTEX_SHADER_PATH, FRAGMENT_SHADER_PATH ); frontBuffer = std::vector<glm::vec3>(); backBuffer = std::vector<glm::vec3>(); gui.init(window); initialized = true; }
int main(int argc, char** argv) { GLFWwindow* window; glfwSetErrorCallback(on_error); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(800, 600, "show mesh", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, on_key); glfwSetCursorPosCallback(window, on_mouse); glfwSetMouseButtonCallback(window, on_click); glfwSetScrollCallback(window, on_scroll); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); app_init(argc, argv); gui_init(window); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); glfwPollEvents(); app_draw(ratio); gui_logic(); gui_draw(); glfwSwapBuffers(window); } glfwDestroyWindow(window); glfwTerminate(); app_cleanup(); gui_cleanup(); exit(EXIT_SUCCESS); }
bool Window::Create( const std::string& title, unsigned int width, unsigned int height ) { /*****/ /* GLFW Initialization /*****/ glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 ); glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); glfwSetErrorCallback( error_callback ); if( !glfwInit() ) { std::cout << "GLFW could not initialize!\n"; return false; } this->handle = glfwCreateWindow( (int) width, (int) height, title.c_str(), nullptr, nullptr ); int major = glfwGetWindowAttrib( this->handle, GLFW_CONTEXT_VERSION_MAJOR ); int minor = glfwGetWindowAttrib( this->handle, GLFW_CONTEXT_VERSION_MINOR ); int revision = glfwGetWindowAttrib( this->handle, GLFW_CONTEXT_REVISION ); std::cout << "OpenGL Version " << major << "." << minor << "." << revision << std::endl; if( !this->handle ) { std::cout << "Failed to create GLFW window!\n"; glfwTerminate(); return false; } glfwMakeContextCurrent( this->handle ); glfwSwapInterval( 1 ); this->SetCallbacks(); /****** END *******/ /*****/ /* GLAD Initialization /*****/ if( !gladLoadGLLoader( GLADloadproc( glfwGetProcAddress ) ) ) { std::cout << "Failed to initialize OpenGL context" << std::endl; return false; } /****** END *******/ glEnable( GL_DEPTH_TEST ); glViewport( 0, 0, (GLsizei) width, (GLsizei) height ); //glClearColor( 0.2f, 0.3f, 0.3f, 1.0f ); //glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); return true; }
int main(int argc, char** argv) { int ch; GLFWwindow* window; while ((ch = getopt(argc, argv, "h")) != -1) { switch (ch) { case 'h': usage(); exit(EXIT_SUCCESS); default: usage(); exit(EXIT_FAILURE); } } glfwSetErrorCallback(error_callback); if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); exit(EXIT_FAILURE); } window = glfwCreateWindow(200, 200, "Clipboard Test", NULL, NULL); if (!window) { glfwTerminate(); fprintf(stderr, "Failed to open GLFW window\n"); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); glfwSetKeyCallback(window, key_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glClearColor(0.5f, 0.5f, 0.5f, 0); while (!glfwWindowShouldClose(window)) { glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); glfwWaitEvents(); } glfwTerminate(); exit(EXIT_SUCCESS); }
Application::Status Application::initializeComponents() { // Set some window attributes to be used for next window creation glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create the main window m_mainWindow = glfwCreateWindow(DefaultWidth, DefaultHeight, "Vesper", nullptr, nullptr); if (!m_mainWindow) { glfwTerminate(); return Status::Failure; } // This call here is needed for glad GL loader glfwMakeContextCurrent(m_mainWindow); // Load up OpenGL extensions if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize OpenGL context with extensions." << std::endl; return Status::Failure; } const GLubyte* glVersion = glGetString(GL_VERSION); const GLubyte* glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION); std::cout << "OpenGL Version: " << glVersion[0] << "." << glVersion[2] << std::endl; std::cout << "GLSL Version: " << glslVersion[0] << "." << glslVersion[2] << std::endl; // InputMapper is used to respond to basic window event callbacks m_inputMapper = std::make_shared<InputMapper>(m_mainWindow); // Renderer is responsible for getting stuff drawn onto the screen m_renderer = std::make_shared<Renderer>(m_mainWindow); // Ray tracer that loads up the scene and performs the ray tracing task m_rayTracer = std::make_unique<RayTracer>(); // Connect a few events and handlers m_inputMapper->keyPressed.subscribe<Application, &Application::keyEventDebug>(this); m_inputMapper->filesDropped.subscribe<Application, &Application::onFilesDropped>(this); m_inputMapper->keyPressed.subscribe<Renderer, &Renderer::onKeyPressed>(m_renderer.get()); m_rayTracer->sceneInitialized.subscribe<Application, &Application::onSceneInitialized>(this); m_rayTracer->imageUpdated.subscribe<Renderer, &Renderer::onImageUpdated>(m_renderer.get()); // Genetic algorithm for solving TSP problem, fun stuff //m_tsp = std::make_shared<TravellingSalesman>(50, 75); //m_tsp->setShaderProgram(m_renderer->getTechnique("color")); //m_tsp->generatePopulation(); return Status::OK; }
int main(int argc, char** argv) { GLFWwindow* window; struct nk_context* nk; struct nk_font_atlas* atlas; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(400, 400, "Opacity", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS); nk_glfw3_font_stash_begin(&atlas); nk_glfw3_font_stash_end(); while (!glfwWindowShouldClose(window)) { int width, height; struct nk_rect area; glfwGetWindowSize(window, &width, &height); area = nk_rect(0.f, 0.f, (float) width, (float) height); glClear(GL_COLOR_BUFFER_BIT); nk_glfw3_new_frame(); if (nk_begin(nk, "", area, 0)) { float opacity = glfwGetWindowOpacity(window); nk_layout_row_dynamic(nk, 30, 2); if (nk_slider_float(nk, 0.f, &opacity, 1.f, 0.001f)) glfwSetWindowOpacity(window, opacity); nk_labelf(nk, NK_TEXT_LEFT, "%0.3f", opacity); } nk_end(nk); nk_glfw3_render(NK_ANTI_ALIASING_ON); glfwSwapBuffers(window); glfwWaitEventsTimeout(1.0); } nk_glfw3_shutdown(); glfwTerminate(); exit(EXIT_SUCCESS); }
int main( void ) { GLFWwindow* window; /* Init GLFW */ if( !glfwInit() ) exit( EXIT_FAILURE ); window = glfwCreateWindow( 400, 400, "Boing (classic Amiga demo)", NULL, NULL ); if (!window) { glfwTerminate(); exit( EXIT_FAILURE ); } glfwSetWindowAspectRatio(window, 1, 1); glfwSetFramebufferSizeCallback(window, reshape); glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, cursor_position_callback); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval( 1 ); glfwGetFramebufferSize(window, &width, &height); reshape(window, width, height); glfwSetTime( 0.0 ); init(); /* Main loop */ for (;;) { /* Timing */ t = glfwGetTime(); dt = t - t_old; t_old = t; /* Draw one frame */ display(); /* Swap buffers */ glfwSwapBuffers(window); glfwPollEvents(); /* Check if we are still running */ if (glfwWindowShouldClose(window)) break; } glfwTerminate(); exit( EXIT_SUCCESS ); }
int gladLoadGL(void) { int status = 0; if(open_gl()) { status = gladLoadGLLoader(&get_proc); close_gl(); } return status; }
static int lMakeContextCurrent(lua_State *L) { GLFWwindow * window = getwindow(L); glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { return luaL_error(L, "Can't load GL"); } return 0; }
/* program entry */ int main(int argc, char *argv[]) { GLFWwindow* window; int width, height; if( !glfwInit() ) { fprintf( stderr, "Failed to initialize GLFW\n" ); exit( EXIT_FAILURE ); } glfwWindowHint(GLFW_DEPTH_BITS, 16); window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL ); if (!window) { fprintf( stderr, "Failed to open GLFW window\n" ); glfwTerminate(); exit( EXIT_FAILURE ); } // Set callback functions glfwSetFramebufferSizeCallback(window, reshape); glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval( 1 ); glfwGetFramebufferSize(window, &width, &height); reshape(window, width, height); // Parse command-line options init(); // Main loop while( !glfwWindowShouldClose(window) ) { // Draw gears draw(); // Update animation animate(); // Swap buffers glfwSwapBuffers(window); glfwPollEvents(); } // Terminate GLFW glfwTerminate(); // Exit program exit( EXIT_SUCCESS ); }
EmuWindow_SDL2::EmuWindow_SDL2() { keyboard_id = KeyMap::NewDeviceId(); ReloadSetKeymaps(); SDL_SetMainReady(); // Initialize the window if (SDL_Init(SDL_INIT_VIDEO) < 0) { LOG_CRITICAL(Frontend, "Failed to initialize SDL2! Exiting..."); exit(1); } SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0); std::string window_title = Common::StringFromFormat("Citra | %s-%s", Common::g_scm_branch, Common::g_scm_desc); render_window = SDL_CreateWindow( window_title.c_str(), SDL_WINDOWPOS_UNDEFINED, // x position SDL_WINDOWPOS_UNDEFINED, // y position VideoCore::kScreenTopWidth, VideoCore::kScreenTopHeight + VideoCore::kScreenBottomHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); if (render_window == nullptr) { LOG_CRITICAL(Frontend, "Failed to create SDL2 window! Exiting..."); exit(1); } gl_context = SDL_GL_CreateContext(render_window); if (gl_context == nullptr) { LOG_CRITICAL(Frontend, "Failed to create SDL2 GL context! Exiting..."); exit(1); } if (!gladLoadGLLoader(static_cast<GLADloadproc>(SDL_GL_GetProcAddress))) { LOG_CRITICAL(Frontend, "Failed to initialize GL functions! Exiting..."); exit(1); } OnResize(); OnMinimalClientAreaChangeRequest(GetActiveConfig().min_client_area_size); SDL_PumpEvents(); SDL_GL_SetSwapInterval(Settings::values.use_vsync); DoneCurrent(); }
int PCRender::Init(SWindow *win) { GLint vpos_loc, tbuf_loc; m_sWindow = win; if (m_iWidth * m_iHeight > 0) { glfwSetWindowSize(m_sWindow, m_iWidth, m_iHeight); GLFWvidmode *mode = (GLFWvidmode*)glfwGetVideoMode(glfwGetPrimaryMonitor()); glfwSetWindowPos(m_sWindow, (mode->width-m_iWidth)/2, (mode->height-m_iHeight)/2); } glfwMakeContextCurrent(m_sWindow); gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); glGenBuffers(1, &m_vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); m_shaderProgram = MakeShaderProgram(vs, fs); if (m_shaderProgram == 0u) { return -1; } vpos_loc = glGetAttribLocation(m_shaderProgram , "vertexIn"); glEnableVertexAttribArray(vpos_loc); glVertexAttribPointer(vpos_loc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*) 0); tbuf_loc = glGetAttribLocation(m_shaderProgram , "textureIn"); glEnableVertexAttribArray(tbuf_loc); glVertexAttribPointer(tbuf_loc, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*) (sizeof(float) * 2)); //Init Texture glGenTextures(3, m_textures); glBindTexture(GL_TEXTURE_2D, m_textures[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, m_textures[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, m_textures[2]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glfwMakeContextCurrent(NULL); return 0; }
// The MAIN function, from here we start the application and run the game loop int main() { std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl; // Init GLFW glfwInit(); // Set all the required options for GLFW glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create a GLFWwindow object that we can use for GLFW's functions GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL); glfwMakeContextCurrent(window); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } // Set the required callback functions glfwSetKeyCallback(window, key_callback); if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) { std::cout << "Failed to initialize OpenGL context" << std::endl; return -1; } // Define the viewport dimensions glViewport(0, 0, WIDTH, HEIGHT); // Game loop while (!glfwWindowShouldClose(window)) { // Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions glfwPollEvents(); // Render // Clear the colorbuffer glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Swap the screen buffers glfwSwapBuffers(window); } // Terminates GLFW, clearing any resources allocated by GLFW. glfwTerminate(); return 0; }
DuiHWND CWindowWnd::Create(DuiHWND hwndParent, const char* pstrName,int x, int y, int cx , int cy) { m_hWnd = glfwCreateWindow(cx, cy, pstrName, NULL, NULL); glfwMakeContextCurrent(m_hWnd); gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); glfwSetWindowUserPointer(m_hWnd, this); // bind callback for wnd. { glfwSetWindowRefreshCallback(m_hWnd, window_refresh_callback); glfwSetFramebufferSizeCallback(m_hWnd, framebuffer_size_callback); } HandleMessage(UI_WM_CREATE, cx, cy); return m_hWnd; }
/* Nothing to Edit here */ GLFWwindow* initGLFW (int width, int height) { GLFWwindow* window; // window desciptor/handle glfwSetErrorCallback(error_callback); if (!glfwInit()) { exit(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(width, height, "Sample OpenGL 3.3 Application", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval( 1 ); /* --- register callbacks with GLFW --- */ /* Register function to handle window resizes */ /* With Retina display on Mac OS X GLFW's FramebufferSize is different from WindowSize */ glfwSetFramebufferSizeCallback(window, reshapeWindow); glfwSetWindowSizeCallback(window, reshapeWindow); /* Register function to handle window close */ glfwSetWindowCloseCallback(window, quit); /* Register function to handle keyboard input */ glfwSetKeyCallback(window, keyboard); // general keyboard input glfwSetCharCallback(window, keyboardChar); // simpler specific character handling /* Register function to handle mouse click */ glfwSetMouseButtonCallback(window, mouseButton); // mouse button clicks glfwSetScrollCallback(window, scroll_callback); return window; }
int main(void) { GLFWwindow* window; srand((unsigned int) time(NULL)); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(640, 480, "Event Wait Timeout Test", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSetKeyCallback(window, key_callback); while (!glfwWindowShouldClose(window)) { int width, height; float r = nrand(), g = nrand(), b = nrand(); float l = (float) sqrt(r * r + g * g + b * b); glfwGetFramebufferSize(window, &width, &height); glViewport(0, 0, width, height); glClearColor(r / l, g / l, b / l, 1.f); glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); glfwWaitEventsTimeout(1.0); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
void setup_SDL_and_glad(const char* title) { // Setup SDL and OpenGL and a window SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO); // Request OpenGL 3.2 context. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // set double buffer SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // immediate swaps SDL_GL_SetSwapInterval(0); SDL_DisplayMode dm; if (SDL_GetDesktopDisplayMode(0, &dm)) { SDL_Log("SDL_GetDesktopDisplayMode failed: %s", SDL_GetError()); return; } int screen_w = 640; int screen_h = 480; int centered_x = dm.w / 2 - screen_w / 2; int centered_y = dm.h / 2 - screen_h / 2; window = SDL_CreateWindow(title, centered_x, centered_y, screen_w, screen_h, SDL_WINDOW_OPENGL|SDL_WINDOW_ALLOW_HIGHDPI); ctx_gl = SDL_GL_CreateContext(window); gladLoadGLLoader(SDL_GL_GetProcAddress); int major, minor; SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major); SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor); printf("SDL says running on OpenGL ES version %d.%d\n", major, minor); printf("Glad says OpenGL ES version : %d.%d\n", GLVersion.major, GLVersion.minor); printf("OpenGL says : ES %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION)); }
Window::Window(const char* title, int width, int height) : width(width), height(height) { if (!glfwInit()) { printf("glfw init failed!\n"); throw std::exception(); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_SAMPLES, 4); window = glfwCreateWindow(width, height, title, nullptr, nullptr); if (!window) { glfwTerminate(); printf("glfw failed to create a window!\n"); throw std::exception(); } glfwMakeContextCurrent(window); glfwSetWindowSizeCallback(window, onWindowResize); glfwSetScrollCallback(window, onWindowMouseWheel); glfwSetMouseButtonCallback(window, onWindowMouseButton); glfwSetCursorPosCallback(window, onWindowMouseMove); glfwSetKeyCallback(window, onWindowKeyboard); glfwSetWindowFocusCallback(window, onWindowFocus); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { printf("glad faild to init!\n"); throw std::exception(); } instance = this; }
Display::Display(GLuint width, GLuint height, const std::string &title) : Width(width), Height(height), Title(title) { // GLFW initialization if (SDL_Init(SDL_INIT_VIDEO) < 0) std::cout << "It was not possible to initialize SDL2" << std::endl; //this->Window = SDL_CreateWindow(this->Title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->Width, this->Height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); this->Window = SDL_CreateWindow(this->Title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->Width, this->Height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); SDL_ShowCursor(SDL_DISABLE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_CreateContext(this->Window); this->Running = true; // Set initial value for Frame this->Frame = 1; // Setting as 1 to avoid division by 0 this->LastFPSCheck = this->LastTime = (float)SDL_GetTicks()/1000.0f; this->LastFrame = 0; if (!gladLoadGLLoader(SDL_GL_GetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; } // OpenGL Viewport settings glViewport(0, 0, this->Width, this->Height); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set default Clear Color this->ClearColor = glm::vec3(0.0f, 0.0f, 0.0f); // Init debug GUI this->DGUI.Init(this->Window); }
bool OpenGLWindow::Created() { #ifdef DEBUG // cout << "OpenGL Vendor:" << glGetString(GL_VENDOR) << endl; // cout << "OpenGL Renderer: " << glGetString(GL_RENDERER) << endl; // cout << "OpenGL Version: " << glGetString(GL_VERSION) << endl; // cout << "GLSL Version:" << glGetString(GL_SHADING_LANGUAGE_VERSION) << endl; #endif glfwMakeContextCurrent(m_window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return false; } m_renderMgr = new RenderMgr(); m_renderMgr->Init(); return true; }
int main(void) { GLFWwindow* window; GLuint vertex_buffer, vertex_shader, fragment_shader, program; GLint mvp_location, vpos_location, vcol_location; glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetKeyCallback(window, key_callback); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); glfwSwapInterval(1); // NOTE: OpenGL error checks have been omitted for brevity glGenBuffers(1, &vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); vertex_shader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL); glCompileShader(vertex_shader); fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL); glCompileShader(fragment_shader); program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); mvp_location = glGetUniformLocation(program, "MVP"); vpos_location = glGetAttribLocation(program, "vPos"); vcol_location = glGetAttribLocation(program, "vCol"); glEnableVertexAttribArray(vpos_location); glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) 0); glEnableVertexAttribArray(vcol_location); glVertexAttribPointer(vcol_location, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) (sizeof(float) * 2)); while (!glfwWindowShouldClose(window)) { float ratio; int width, height; mat4x4 m, p, mvp; glfwGetFramebufferSize(window, &width, &height); ratio = width / (float) height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT); mat4x4_identity(m); mat4x4_rotate_Z(m, m, (float) glfwGetTime()); mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 1.f, -1.f); mat4x4_mul(mvp, p, m); glUseProgram(program); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(window); glfwPollEvents(); } glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
int main() { // glfw: initialize and configure // ------------------------------ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X // glfw window creation // -------------------- GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL); if (window == NULL) { std::cout << "Failed to create GLFW window" << std::endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetCursorPosCallback(window, mouse_callback); glfwSetScrollCallback(window, scroll_callback); // tell GLFW to capture our mouse glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); // glad: load all OpenGL function pointers // --------------------------------------- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout << "Failed to initialize GLAD" << std::endl; return -1; } // configure global opengl state // ----------------------------- glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); // build and compile shaders // ------------------------- Shader shader("3.2.2.point_shadows.vs", "3.2.2.point_shadows.fs"); Shader simpleDepthShader("3.2.2.point_shadows_depth.vs", "3.2.2.point_shadows_depth.fs", "3.2.2.point_shadows_depth.gs"); // load textures // ------------- unsigned int woodTexture = loadTexture(FileSystem::getPath("resources/textures/wood.png").c_str()); // configure depth map FBO // ----------------------- const unsigned int SHADOW_WIDTH = 1024, SHADOW_HEIGHT = 1024; unsigned int depthMapFBO; glGenFramebuffers(1, &depthMapFBO); // create depth cubemap texture unsigned int depthCubemap; glGenTextures(1, &depthCubemap); glBindTexture(GL_TEXTURE_CUBE_MAP, depthCubemap); for (unsigned int i = 0; i < 6; ++i) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // attach depth texture as FBO's depth buffer glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthCubemap, 0); glDrawBuffer(GL_NONE); glReadBuffer(GL_NONE); glBindFramebuffer(GL_FRAMEBUFFER, 0); // shader configuration // -------------------- shader.use(); shader.setInt("diffuseTexture", 0); shader.setInt("depthMap", 1); // lighting info // ------------- glm::vec3 lightPos(0.0f, 0.0f, 0.0f); // render loop // ----------- while (!glfwWindowShouldClose(window)) { // per-frame time logic // -------------------- float currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; lastFrame = currentFrame; // input // ----- processInput(window); // move light position over time lightPos.z = sin(glfwGetTime() * 0.5) * 3.0; // render // ------ glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 0. create depth cubemap transformation matrices // ----------------------------------------------- float near_plane = 1.0f; float far_plane = 25.0f; glm::mat4 shadowProj = glm::perspective(glm::radians(90.0f), (float)SHADOW_WIDTH / (float)SHADOW_HEIGHT, near_plane, far_plane); std::vector<glm::mat4> shadowTransforms; shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f))); shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f))); shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f))); shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f))); shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f))); shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f))); // 1. render scene to depth cubemap // -------------------------------- glViewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT); glBindFramebuffer(GL_FRAMEBUFFER, depthMapFBO); glClear(GL_DEPTH_BUFFER_BIT); simpleDepthShader.use(); for (unsigned int i = 0; i < 6; ++i) simpleDepthShader.setMat4("shadowMatrices[" + std::to_string(i) + "]", shadowTransforms[i]); simpleDepthShader.setFloat("far_plane", far_plane); simpleDepthShader.setVec3("lightPos", lightPos); renderScene(simpleDepthShader); glBindFramebuffer(GL_FRAMEBUFFER, 0); // 2. render scene as normal // ------------------------- glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader.use(); glm::mat4 projection = glm::perspective(camera.Zoom, (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); glm::mat4 view = camera.GetViewMatrix(); shader.setMat4("projection", projection); shader.setMat4("view", view); // set lighting uniforms shader.setVec3("lightPos", lightPos); shader.setVec3("viewPos", camera.Position); shader.setInt("shadows", shadows); // enable/disable shadows by pressing 'SPACE' shader.setFloat("far_plane", far_plane); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, woodTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_CUBE_MAP, depthCubemap); renderScene(shader); // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // ------------------------------------------------------------------------------- glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; }
bool CrossDelegate::performInit() { if (!initialized_) { if (glfwInit()) { glfwWindowHint(GLFW_SAMPLES, initInfo.windowInfo.aaSamples); glfwWindowHint(GLFW_DEPTH_BITS, initInfo.windowInfo.depthBits); glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // --- int targetWidth; int targetHeight; if (initInfo.windowInfo.size.x * initInfo.windowInfo.size.y == 0) { GLFWmonitor* monitor = glfwGetPrimaryMonitor(); if (monitor) { const GLFWvidmode *mode = glfwGetVideoMode(monitor); targetWidth = mode->width; targetHeight = mode->height; glfwWindowHint(GLFW_RED_BITS, mode->redBits); glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits); glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits); glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate); window = glfwCreateWindow(targetWidth, targetHeight, "", monitor, NULL); } } else { targetWidth = initInfo.windowInfo.width; targetHeight = initInfo.windowInfo.height; window = glfwCreateWindow(targetWidth, targetHeight, "", NULL, NULL); } if (window) { setupInfo.windowInfo = WindowInfo(targetWidth, targetHeight, initInfo.windowInfo.aaSamples, initInfo.windowInfo.depthBits); glfwSetCursorPosCallback(window, cursorPosCallback); glfwSetMouseButtonCallback(window, mouseButtonCallback); glfwSetKeyCallback(window, keyCallback); glfwSetCharCallback(window, characterCallback); glfwSetScrollCallback(window, scrollCallback); glfwSwapInterval(1); glfwMakeContextCurrent(window); gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); // --- intern::instance = this; initialized_ = _init(); } } } return initialized_; }