예제 #1
0
int main(void)
{
    GLFWwindow* window;
    int width, height;

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    window = glfwCreateWindow(window_width, window_height, "", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetCursorPosCallback(window, cursor_position_callback);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetKeyCallback(window, key_callback);

    glfwMakeContextCurrent(window);

    glfwGetFramebufferSize(window, &width, &height);
    framebuffer_size_callback(window, width, height);

    set_swap_interval(window, swap_interval);

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_LINES);
        glVertex2f(0.f, (GLfloat) window_height - cursor_y);
        glVertex2f((GLfloat) window_width, (GLfloat) window_height - cursor_y);
        glVertex2f((GLfloat) cursor_x, 0.f);
        glVertex2f((GLfloat) cursor_x, (GLfloat) window_height);
        glEnd();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    exit(EXIT_SUCCESS);
}
예제 #2
0
void OpenGLContext::init() {
    glfwSetErrorCallback(glfw_error_callback);

    // Initialise GLFW
    if (!glfwInit()) {
        exit(EXIT_FAILURE);
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_SAMPLES, 4);

    // Open a window and create its OpenGL context
    window = glfwCreateWindow(windowWidth, windowHeight, windowTitle.c_str(), NULL, NULL);
    if (!window) {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }
    glfwMakeContextCurrent(window);

    // Initialize GLEW
    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        exit(EXIT_FAILURE);
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

    glfwSetKeyCallback(window, key_callback);
    glfwSetCharCallback(window, (GLFWcharfun) TwEventCharGLFW);

    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    glfwSetMouseButtonCallback(window, mouse_buttons_callback);
    glfwSetCursorPosCallback(window, mouse_position_callback);
    glfwSetScrollCallback(window, mouse_scroll_callback);

    framebuffer_size_callback(window, windowWidth, windowHeight);
}
예제 #3
0
파일: main.cpp 프로젝트: aaalexandrov/Alex
//------------------------------------------------------------------------------
int main(int argc, char** argv) {
//GRAPHICS SETUP
    glfwSetErrorCallback(error_callback);

    if(!glfwInit()) {
        std::cerr << "ERROR - glfwInit" << std::endl;
        exit(EXIT_FAILURE);
    }

    GLFWwindow* window = glfwCreateWindow(800, 600,
                                          "Ray 1!", NULL, NULL);
    if (!window) {
        std::cerr << "ERROR - glfwCreateWindow" << std::endl;
        glfwTerminate();
        exit(EXIT_FAILURE);
    }


    glfwSetKeyCallback(window, key_callback);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    glfwMakeContextCurrent(window);

    gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);

    std::cout << "OpenGL version: " << glGetString(GL_VERSION) << std::endl;
    std::cout << "GLSL version: "
              << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
    std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;


//GEOMETRY
    //geometry: textured quad
    float quad[] = {-1.0f,  1.0f, 0.0f, 1.0f,
                    -1.0f, -1.0f, 0.0f, 1.0f,
                     1.0f, -1.0f, 0.0f, 1.0f,
                     1.0f, -1.0f, 0.0f, 1.0f,
                     1.0f,  1.0f, 0.0f, 1.0f,
                    -1.0f,  1.0f, 0.0f, 1.0f};

    float  texcoord[] = {0.0f, 1.0f,
                         0.0f, 0.0f,
                         1.0f, 0.0f,
                         1.0f, 0.0f,
                         1.0f, 1.0f,
                         0.0f, 1.0f};
    //OpenGL >= 3.3 core requires a vertex array object containing multiple attribute
    //buffers
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    //geometry buffer
    GLuint quadvbo;
    glGenBuffers(1, &quadvbo);
    glBindBuffer(GL_ARRAY_BUFFER, quadvbo);
    glBufferData(GL_ARRAY_BUFFER, 6 * 4 * sizeof(float),
                 &quad[0], GL_STATIC_DRAW);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    //texture coordinate buffer
    GLuint texbo;
    glGenBuffers(1, &texbo);

    glBindBuffer(GL_ARRAY_BUFFER, texbo);
    glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(float),
                 &texcoord[0], GL_STATIC_DRAW);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);

//OPENGL RENDERING SHADERS

    GLuint glprogram = create_program(vertexShaderSrc, fragmentShaderSrc);

    //enable gl program
    glUseProgram(glprogram);

    //extract ids of shader variables
    GLint mvpID = glGetUniformLocation(glprogram, "MVP");
    assert(mvpID>=0);

    GLint textureID = glGetUniformLocation(glprogram, "cltexture");
    assert(textureID>=0);

    //beckground color
    glClearColor(0.0f, 0.0f, 0.4f, 1.0f);

    g_Backbuffer = std::make_shared<Backbuffer>();
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    framebuffer_size_callback(window, width, height);

//RENDER LOOP
    //rendering & simulation loop
    while (!glfwWindowShouldClose(window)) {
        // Clear the screen
        glClear(GL_COLOR_BUFFER_BIT);

        //setup OpenGL matrices
        const Eigen::Matrix4f modelView = Eigen::Matrix4f::Identity();
        const Eigen::Matrix4f MVP        = g_Backbuffer->m_Projection * modelView;

        glUniformMatrix4fv(mvpID, 1, GL_FALSE, MVP.data());

        //only need texture unit 0
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, g_Backbuffer->m_Texture);
        glUniform1i(textureID, 0);

        //select geometry to render
        glBindVertexArray(vao);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
        //draw
        glDrawArrays(GL_TRIANGLES, 0, 6);
        //unbind
        glBindVertexArray(0);
        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glBindTexture(GL_TEXTURE_2D, 0);
        //put pixels on screen
        glfwSwapBuffers(window);
        //query events
        glfwPollEvents();
    }

//CLEANUP
    glDeleteBuffers(1, &quadvbo);
    glDeleteBuffers(1, &texbo);
    glDeleteVertexArrays(1, &vao);

    g_Backbuffer.reset();

    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
    return 0;
}
예제 #4
0
int main(int argc, char* argv[])
{
    GLFWwindow* window;
    double t, dt_total, t_old;
    int width, height;

    glfwSetErrorCallback(error_callback);

    if (!glfwInit())
        exit(EXIT_FAILURE);

    window = glfwCreateWindow(640, 480, "Wave Simulation", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetKeyCallback(window, key_callback);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    glfwSetMouseButtonCallback(window, mouse_button_callback);
    glfwSetCursorPosCallback(window, cursor_position_callback);
    glfwSetScrollCallback(window, scroll_callback);

    glfwMakeContextCurrent(window);
    gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
    glfwSwapInterval(1);

    glfwGetFramebufferSize(window, &width, &height);
    framebuffer_size_callback(window, width, height);

    // Initialize OpenGL
    init_opengl();

    // Initialize simulation
    init_vertices();
    init_grid();
    adjust_grid();

    // Initialize timer
    t_old = glfwGetTime() - 0.01;

    while (!glfwWindowShouldClose(window))
    {
        t = glfwGetTime();
        dt_total = t - t_old;
        t_old = t;

        // Safety - iterate if dt_total is too large
        while (dt_total > 0.f)
        {
            // Select iteration time step
            dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total;
            dt_total -= dt;

            // Calculate wave propagation
            calc_grid();
        }

        // Compute height of each vertex
        adjust_grid();

        // Draw wave grid to OpenGL display
        draw_scene(window);

        glfwPollEvents();
    }

    exit(EXIT_SUCCESS);
}
예제 #5
0
std::unique_ptr<psi_serv::IWindowService> psi_serv::start_gl_window_service(GLWindowServiceArgs args) {
	// -- START GLFW --
	if (glfwInit() == 0)
		throw std::runtime_error("Failed to initialize GLFW.");

	glfwSetErrorCallback(
		[] (GLint code, char const* desc) {
			psi_log::error("GLFW") << "(" << code << "): " << desc << "\n";
		}
	);

	psi_log::info("GLWindowService") << "Initialized GLFW " << glfwGetVersionString() << "\n";

	// -- START GLFW WINDOW --
	// GL version 4.4
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
	// No backwards compatibility
	glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
	glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
	// Resizability of this window
	glfwWindowHint(GLFW_RESIZABLE, args.resizable);
	// Whether the window is always on top
	glfwWindowHint(GLFW_FLOATING, args.always_on_top);
	// How many samples to take for anti-aliasing (MSAA)
	glfwWindowHint(GLFW_SAMPLES, args.samples);
	// Whether it's a debug context
	glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, args.debug);
	// sets the window to be sRGB capable, meaning it can convert linear output from shader to sRGB in framebuffer
	glfwWindowHint(GLFW_SRGB_CAPABLE, true);

	auto window = glfwCreateWindow(args.width, args.height, args.title, nullptr, nullptr);
	if (window == nullptr)
		throw std::runtime_error("Failed to create a GLFW window.");

	// key presses are remembered until polled
	glfwSetInputMode(window, GLFW_STICKY_KEYS, true);

	// create the service and map its address to the user pointer in GLFW
	auto service = new GLWindowService(window);
	glfwSetWindowUserPointer(window, service);

	glfwSetFramebufferSizeCallback(window,
		[] (GLFWwindow* win, int width, int height) {
			if (width <= 1 || height <= 1)
				return;

			static_cast<GLWindowService*>(glfwGetWindowUserPointer(win))->framebuffer_size_callback(width, height);
		}
	);
	service->framebuffer_size_callback(args.width, args.height);

	glfwSetKeyCallback(window,
		[] (GLFWwindow* win, int key, int scancode, int action, int mods) {
			static_cast<GLWindowService*>(glfwGetWindowUserPointer(win))->key_pressed_callback(key, scancode, action, mods);
		}
	);

	glfwSetMouseButtonCallback(window,
		[] (GLFWwindow* win, int button, int action, int mods) {
			static_cast<GLWindowService*>(glfwGetWindowUserPointer(win))->mouse_pressed_callback(button, action, mods);
		}
	);


	glfwSetCursorPosCallback(window,
		[] (GLFWwindow* win, double x, double y) {
			static_cast<GLWindowService*>(glfwGetWindowUserPointer(win))->cursor_moved_callback(x, y);
		}
	);

	glfwMakeContextCurrent(window);

	// -- INITIALIZE OPENGL --
	auto load_status = gl::sys::LoadFunctions();
	if (!load_status)
		throw std::runtime_error(std::string("OpenGL Core ") + std::to_string(gl::sys::GetMajorVersion()) + "." + std::to_string(gl::sys::GetMinorVersion()) + " initialization failed.\n"
		+ "Number of functions that failed to load: " + std::to_string(load_status.GetNumMissing()));

	gl::Enable(gl::DEBUG_OUTPUT_SYNCHRONOUS);
	gl::DebugMessageCallback(gl_error_callback, nullptr);

	psi_log::info("GLWindowService") << "Initialized OpenGL Core " << gl::sys::GetMajorVersion() << "." << gl::sys::GetMinorVersion() << "\n";

	// -- RETURN SERVICE --
	return std::unique_ptr<IWindowService>(service);
}